1.1 --- a/src/org/sonews/storage/DrupalMessage.java Wed Oct 12 00:25:14 2011 +0200
1.2 +++ b/src/org/sonews/storage/DrupalMessage.java Wed Oct 12 23:10:31 2011 +0200
1.3 @@ -19,12 +19,16 @@
1.4
1.5 import java.io.ByteArrayOutputStream;
1.6 import java.io.IOException;
1.7 +import java.io.StringReader;
1.8 +import java.io.StringWriter;
1.9 import java.io.UnsupportedEncodingException;
1.10 import java.sql.ResultSet;
1.11 import java.sql.SQLException;
1.12 import java.util.ArrayList;
1.13 import java.util.Date;
1.14 import java.util.Enumeration;
1.15 +import java.util.logging.Level;
1.16 +import java.util.logging.Logger;
1.17 import javax.mail.Header;
1.18 import javax.mail.MessagingException;
1.19 import javax.mail.Multipart;
1.20 @@ -33,6 +37,11 @@
1.21 import javax.mail.internet.MimeBodyPart;
1.22 import javax.mail.internet.MimeMessage;
1.23 import javax.mail.internet.MimeMultipart;
1.24 +import javax.xml.transform.Transformer;
1.25 +import javax.xml.transform.TransformerFactory;
1.26 +import javax.xml.transform.stream.StreamResult;
1.27 +import javax.xml.transform.stream.StreamSource;
1.28 +import org.sonews.util.io.Resource;
1.29
1.30 /**
1.31 * This is MimeMessage which enables custom Message-ID header
1.32 @@ -46,6 +55,7 @@
1.33 */
1.34 public class DrupalMessage extends MimeMessage {
1.35
1.36 + private static final Logger log = Logger.getLogger(DrupalMessage.class.getName());
1.37 private static final String MESSAGE_ID_HEADER = "Message-ID";
1.38 private static final String CRLF = "\r\n";
1.39 public static final String CHARSET = "UTF-8";
1.40 @@ -62,12 +72,12 @@
1.41
1.42 addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain));
1.43 addHeader("Newsgroups", rs.getString("group_name"));
1.44 - setFrom(new InternetAddress("anonym@example.com", rs.getString("sender_name")));
1.45 + setFrom(new InternetAddress(rs.getString("sender_email"), rs.getString("sender_name")));
1.46 setSubject(rs.getString("subject"));
1.47 setSentDate(new Date(rs.getLong("created")));
1.48 -
1.49 - Integer parentID = rs.getInt("parent_id");
1.50 - if (parentID != null && parentID > 0) {
1.51 +
1.52 + int parentID = rs.getInt("parent_id");
1.53 + if (parentID > 0) {
1.54 String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
1.55 addHeader("In-Reply-To", parentMessageID);
1.56 addHeader("References", parentMessageID);
1.57 @@ -77,12 +87,12 @@
1.58 Multipart multipart = new MimeMultipart("alternative");
1.59 setContent(multipart);
1.60
1.61 - /** TODO: Plain text part */
1.62 + /** Plain text part */
1.63 MimeBodyPart textPart = new MimeBodyPart();
1.64 multipart.addBodyPart(textPart);
1.65 textPart.setText(readPlainText(rs));
1.66
1.67 - /** TODO: XHTML part */
1.68 + /** XHTML part */
1.69 MimeBodyPart htmlPart = new MimeBodyPart();
1.70 multipart.addBodyPart(htmlPart);
1.71 htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE);
1.72 @@ -100,9 +110,29 @@
1.73
1.74 private String readXhtmlText(ResultSet rs) {
1.75 /**
1.76 - * TODO: převést na XHTML
1.77 + * TODO: znovupoužívat XSL transformér
1.78 */
1.79 - return "<html><body>TODO: tady bude nějaký <strong>(X)HTML</strong></body></html>";
1.80 + try {
1.81 + String originalText = rs.getString("text");
1.82 + StringReader input = new StringReader("<body>" + originalText + "</body>");
1.83 + StringWriter output = new StringWriter();
1.84 + TransformerFactory tf = TransformerFactory.newInstance();
1.85 + Transformer t = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart.xsl")));
1.86 + t.setParameter("isRoot", (rs.getInt("parent_id") == 0));
1.87 + t.setParameter("title", rs.getString("subject"));
1.88 + t.setParameter("urlBase", rs.getString("urlBase"));
1.89 + t.setParameter("wwwRead", rs.getString("wwwRead"));
1.90 + t.setParameter("wwwPost", rs.getString("wwwPost"));
1.91 + t.transform(new StreamSource(input), new StreamResult(output));
1.92 +
1.93 + return output.toString();
1.94 + } catch (Exception e) {
1.95 + /**
1.96 + * TODO: lepší ošetření chyby
1.97 + */
1.98 + log.log(Level.WARNING, "Error while transforming article to XHTML", e);
1.99 + return "<html><body><p>Při transformaci příspěvku bohužel došlo k chybě.</p></body></html>";
1.100 + }
1.101 }
1.102
1.103 private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {