src/org/sonews/storage/DrupalMessage.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Oct 2011 21:06:24 +0200
changeset 92 a865136070a0
parent 89 c60625d58158
child 100 08c9fb6fb017
permissions -rw-r--r--
Drupal: text/plain – h2 bude podtřžené = místo # + podpora značky cite.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package org.sonews.storage;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.ByteArrayOutputStream;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.InputStreamReader;
    25 import java.io.PrintStream;
    26 import java.io.StringReader;
    27 import java.io.StringWriter;
    28 import java.io.UnsupportedEncodingException;
    29 import java.sql.ResultSet;
    30 import java.sql.SQLException;
    31 import java.util.ArrayList;
    32 import java.util.Date;
    33 import java.util.Enumeration;
    34 import java.util.logging.Level;
    35 import java.util.logging.Logger;
    36 import javax.mail.Header;
    37 import javax.mail.MessagingException;
    38 import javax.mail.Multipart;
    39 import javax.mail.Session;
    40 import javax.mail.internet.InternetAddress;
    41 import javax.mail.internet.MimeBodyPart;
    42 import javax.mail.internet.MimeMessage;
    43 import javax.mail.internet.MimeMultipart;
    44 import javax.xml.transform.Transformer;
    45 import javax.xml.transform.TransformerFactory;
    46 import javax.xml.transform.stream.StreamResult;
    47 import javax.xml.transform.stream.StreamSource;
    48 import org.sonews.util.io.Resource;
    49 
    50 /**
    51  * This is MimeMessage which enables custom Message-ID header
    52  * (this header will not be overwritten by the default one like in MimeMessage).
    53  * 
    54  * Also add header and body separate serialization.
    55  * 
    56  * And can be deserialized from SQL ResultSet
    57  * 
    58  * @author František Kučera (frantovo.cz)
    59  */
    60 public class DrupalMessage extends MimeMessage {
    61 
    62 	private static final Logger log = Logger.getLogger(DrupalMessage.class.getName());
    63 	private static final String MESSAGE_ID_HEADER = "Message-ID";
    64 	private static final String CRLF = "\r\n";
    65 	public static final String CHARSET = "UTF-8";
    66 	private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET;
    67 	private String messageID;
    68 
    69 	/**
    70 	 * Constructs MIME message from SQL result.
    71 	 * @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read.
    72 	 * @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy).
    73 	 */
    74 	public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException {
    75 		super(Session.getDefaultInstance(System.getProperties()));
    76 
    77 		addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain));
    78 		addHeader("Newsgroups", rs.getString("group_name"));
    79 		setFrom(new InternetAddress(rs.getString("sender_email"), rs.getString("sender_name")));
    80 		setSubject(rs.getString("subject"));
    81 		setSentDate(new Date(rs.getLong("created")));
    82 
    83 		int parentID = rs.getInt("parent_id");
    84 		if (parentID > 0) {
    85 			String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
    86 			addHeader("In-Reply-To", parentMessageID);
    87 			addHeader("References", parentMessageID);
    88 		}
    89 
    90 		if (constructBody) {
    91 			Multipart multipart = new MimeMultipart("alternative");
    92 			setContent(multipart);
    93 
    94 			/** XHTML part */
    95 			MimeBodyPart htmlPart = new MimeBodyPart();
    96 			String xhtmlText = readXhtmlText(rs);
    97 			htmlPart.setContent(xhtmlText, XHTML_CONTENT_TYPE);
    98 
    99 			/** Plain text part */
   100 			MimeBodyPart textPart = new MimeBodyPart();
   101 			String plainText = readPlainText(rs, xhtmlText);
   102 			textPart.setText(plainText);
   103 			//addHeader("Lines", String.valueOf(plainText.split("\n").length));
   104 
   105 			/**
   106 			 * Thunderbirdu záleží, v jakém pořadí části jsou 
   107 			 * (když je prostý text druhý, html se nezobrazí),
   108 			 * KNode zobrazuje HTML správně, i když je na prvním místě.
   109 			 */
   110 			multipart.addBodyPart(textPart);
   111 			multipart.addBodyPart(htmlPart);
   112 		} else {
   113 			/** empty body, just headers */
   114 			setText("");
   115 		}
   116 	}
   117 
   118 	private String readPlainText(ResultSet rs, String xhtmlText) {
   119 		try {
   120 			TransformerFactory tf = TransformerFactory.newInstance();
   121 			Transformer textTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeTextPart.xsl")));
   122 
   123 			StringReader input = new StringReader(xhtmlText);
   124 			StringWriter output = new StringWriter(xhtmlText.length());
   125 			textTransformer.transform(new StreamSource(input), new StreamResult(output));
   126 
   127 			return output.toString();
   128 		} catch (Exception e) {
   129 			/**
   130 			 * TODO: lepší ošetření chyby
   131 			 */
   132 			log.log(Level.WARNING, "Error while transforming article to plain text", e);
   133 			return makeSimpleXHTML("Při transformaci příspěvku bohužel došlo k chybě.");
   134 		}
   135 	}
   136 
   137 	private String readXhtmlText(ResultSet rs) {
   138 		/**
   139 		 * TODO: 
   140 		 *		- znovupoužívat XSL transformér
   141 		 *		- používat cache, ukládat si vygenerované články
   142 		 */
   143 		try {
   144 			String inputText = makeSimpleXHTML(rs.getString("text"));
   145 
   146 			TransformerFactory tf = TransformerFactory.newInstance();
   147 			Transformer paragraphTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart-make-paragraphs.xsl")));
   148 
   149 			String paragraphedText;
   150 			boolean tidyWasUsed = false;
   151 			try {
   152 				StringReader input = new StringReader(inputText);
   153 				StringWriter output = new StringWriter(2 * inputText.length());
   154 				paragraphTransformer.transform(new StreamSource(input), new StreamResult(output));
   155 				paragraphedText = output.toString();
   156 			} catch (Exception e) {
   157 				log.log(Level.FINER, "HTML input was shitty – Tidy had to be called.", e);
   158 				StringReader input = new StringReader(tidyXhtml(inputText));
   159 				StringWriter output = new StringWriter(2 * inputText.length());
   160 				paragraphTransformer.transform(new StreamSource(input), new StreamResult(output));
   161 				paragraphedText = output.toString();
   162 				tidyWasUsed = true;
   163 			}
   164 
   165 			Transformer xhtmlTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart.xsl")));
   166 			xhtmlTransformer.setParameter("isRoot", (rs.getInt("parent_id") == 0));
   167 			xhtmlTransformer.setParameter("title", rs.getString("subject"));
   168 			xhtmlTransformer.setParameter("urlBase", rs.getString("urlBase"));
   169 			xhtmlTransformer.setParameter("wwwRead", rs.getString("wwwRead"));
   170 			xhtmlTransformer.setParameter("wwwPost", rs.getString("wwwPost"));
   171 			xhtmlTransformer.setParameter("headComment", String.format("Drupal-NNTP bridge. Transformed: %1$tc. Tidy had to be used: %2$b", new Date(), tidyWasUsed));
   172 			StringReader input = new StringReader(paragraphedText);
   173 			StringWriter output = new StringWriter(2 * paragraphedText.length());
   174 			xhtmlTransformer.transform(new StreamSource(input), new StreamResult(output));
   175 
   176 			return output.toString();
   177 		} catch (Exception e) {
   178 			/**
   179 			 * TODO: lepší ošetření chyby
   180 			 */
   181 			log.log(Level.WARNING, "Error while transforming article to XHTML", e);
   182 			return makeSimpleXHTML("<p>Při transformaci příspěvku bohužel došlo k chybě.</p>");
   183 		}
   184 	}
   185 
   186 	private static String makeSimpleXHTML(String body) {
   187 		return "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>" + body + "</body></html>";
   188 	}
   189 
   190 	/**
   191 	 * TODO: refaktorovat, přesunout
   192 	 */
   193 	private static String tidyXhtml(String inputText) throws IOException {
   194 		/*
   195 		 * Viz https://sourceforge.net/tracker/index.php?func=detail&aid=3424437&group_id=27659&atid=390966
   196 		 *
   197 		 * TODO:
   198 		 *		- použít delší zástupný řetězec, ne jen jeden znak
   199 		 *		- umísťovat ho jen tam, kde už nějaký text je (ne mezi >\s*<)
   200 		 */
   201 		inputText = inputText.replaceAll("\\n", "◆\n");
   202 
   203 		Runtime r = Runtime.getRuntime();
   204 		Process p = r.exec(new String[]{"tidy", // http://tidy.sourceforge.net
   205 					"-asxml", // well formed XHTML
   206 					"-numeric", // číselné entity
   207 					"-utf8", // kódování
   208 					"--show-warnings", "false", // žádná varování nás nezajímají
   209 					"--show-errors", "0", // ani chyby
   210 					"--doctype", "omit", // doctype nepotřebujeme (doplníme si případně vlastní v XSLT)
   211 					"--logical-emphasis", "true", // em a strong místo i a b
   212 					"--literal-attributes", "true", // zachovat mezery a konce řádků v atributech
   213 					"--force-output", "true" // neznámé značky zahodíme, vložíme jen jejich obsah
   214 				});
   215 
   216 		PrintStream vstupProcesu = new PrintStream(p.getOutputStream());
   217 		vstupProcesu.print(inputText);
   218 		vstupProcesu.close();
   219 
   220 		String outputText = streamToString(p.getInputStream());
   221 
   222 		outputText = outputText.replaceAll("◆\\n", "\n");
   223 		outputText = outputText.replaceAll("◆", "\n");
   224 
   225 		return outputText;
   226 	}
   227 
   228 	/**
   229 	 * TODO: refaktorovat, přesunout
   230 	 */
   231 	private static String streamToString(InputStream proud) throws IOException {
   232 		StringBuilder výsledek = new StringBuilder();
   233 		BufferedReader buf = new BufferedReader(new InputStreamReader(proud));
   234 		while (true) {
   235 			String radek = buf.readLine();
   236 			if (radek == null) {
   237 				break;
   238 			} else {
   239 				výsledek.append(radek);
   240 				výsledek.append("\n");
   241 			}
   242 		}
   243 		return výsledek.toString();
   244 	}
   245 
   246 	private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {
   247 		StringBuilder sb = new StringBuilder();
   248 		sb.append("<");
   249 		sb.append(articleID);
   250 		sb.append("-");
   251 		sb.append(groupID);
   252 		sb.append("-");
   253 		sb.append(groupName);
   254 		sb.append("@");
   255 		sb.append(domainName);
   256 		sb.append(">");
   257 		return sb.toString();
   258 	}
   259 
   260 	@Override
   261 	public void setHeader(String name, String value) throws MessagingException {
   262 		super.setHeader(name, value);
   263 
   264 		if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
   265 			messageID = value;
   266 		}
   267 	}
   268 
   269 	@Override
   270 	public final void addHeader(String name, String value) throws MessagingException {
   271 		super.addHeader(name, value);
   272 
   273 		if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
   274 			messageID = value;
   275 		}
   276 	}
   277 
   278 	@Override
   279 	public void removeHeader(String name) throws MessagingException {
   280 		super.removeHeader(name);
   281 
   282 		if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
   283 			messageID = null;
   284 		}
   285 	}
   286 
   287 	public void setMessageID(String messageID) {
   288 		this.messageID = messageID;
   289 	}
   290 
   291 	@Override
   292 	protected void updateMessageID() throws MessagingException {
   293 		if (messageID == null) {
   294 			super.updateMessageID();
   295 		} else {
   296 			setHeader(MESSAGE_ID_HEADER, messageID);
   297 		}
   298 	}
   299 
   300 	/**
   301 	 * Call {@link #saveChanges()} before this method, if you want all headers including such ones like:
   302 	 * 
   303 	 * <pre>MIME-Version: 1.0
   304 	 *Content-Type: multipart/alternative;</pre>
   305 	 * 
   306 	 * @return serialized headers
   307 	 * @throws MessagingException if getAllHeaders() fails
   308 	 */
   309 	public String getHeaders() throws MessagingException {
   310 		StringBuilder sb = new StringBuilder();
   311 		for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) {
   312 			sb.append(eh.nextElement());
   313 			sb.append(CRLF);
   314 		}
   315 		return sb.toString();
   316 	}
   317 
   318 	public byte[] getBody() throws IOException, MessagingException {
   319 		saveChanges();
   320 
   321 		ArrayList<String> skipHeaders = new ArrayList<String>();
   322 		for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) {
   323 			Header h = (Header) eh.nextElement();
   324 			skipHeaders.add(h.getName());
   325 		}
   326 
   327 		ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
   328 		writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()]));
   329 		return baos.toByteArray();
   330 	}
   331 }