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