diff -r beb11d70f0eb -r aae4b4688700 src/org/sonews/storage/DrupalMessage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/sonews/storage/DrupalMessage.java Wed Oct 12 00:11:25 2011 +0200 @@ -0,0 +1,193 @@ +/* + * SONEWS News Server + * see AUTHORS for the list of contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.sonews.storage; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Date; +import java.util.Enumeration; +import javax.mail.Header; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +/** + * This is MimeMessage which enables custom Message-ID header + * (this header will not be overwritten by the default one like in MimeMessage). + * + * Also add header and body separate serialization. + * + * And can be deserialized from SQL ResultSet + * + * @author František Kučera (frantovo.cz) + */ +public class DrupalMessage extends MimeMessage { + + private static final String MESSAGE_ID_HEADER = "Message-ID"; + private static final String CRLF = "\r\n"; + public static final String CHARSET = "UTF-8"; + private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET; + private String messageID; + + /** + * Constructs MIME message from SQL result. + * @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read. + * @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy). + */ + public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException { + super(Session.getDefaultInstance(System.getProperties())); + + addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain)); + addHeader("Newsgroups", rs.getString("group_name")); + setFrom(new InternetAddress("anonym@example.com", rs.getString("sender_name"))); + setSubject(rs.getString("subject")); + setSentDate(new Date(rs.getLong("created"))); + + Integer parentID = rs.getInt("parent_id"); + if (parentID != null && parentID > 0) { + String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain); + addHeader("In-Reply-To", parentMessageID); + addHeader("References", parentMessageID); + } + + if (constructBody) { + Multipart multipart = new MimeMultipart("alternative"); + setContent(multipart); + + /** TODO: Plain text part */ + MimeBodyPart textPart = new MimeBodyPart(); + multipart.addBodyPart(textPart); + textPart.setText(readPlainText(rs)); + + /** TODO: XHTML part */ + MimeBodyPart htmlPart = new MimeBodyPart(); + multipart.addBodyPart(htmlPart); + htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE); + } else { + setText(""); + } + } + + private String readPlainText(ResultSet rs) { + /** + * TODO: převést na prostý text + */ + return "TODO: obyčejný text"; + } + + private String readXhtmlText(ResultSet rs) { + /** + * TODO: převést na XHTML + */ + return "TODO: tady bude nějaký (X)HTML"; + } + + private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) { + StringBuilder sb = new StringBuilder(); + sb.append("<"); + sb.append(articleID); + sb.append("-"); + sb.append(groupID); + sb.append("-"); + sb.append(groupName); + sb.append("@"); + sb.append(domainName); + sb.append(">"); + return sb.toString(); + } + + @Override + public void setHeader(String name, String value) throws MessagingException { + super.setHeader(name, value); + + if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { + messageID = value; + } + } + + @Override + public final void addHeader(String name, String value) throws MessagingException { + super.addHeader(name, value); + + if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { + messageID = value; + } + } + + @Override + public void removeHeader(String name) throws MessagingException { + super.removeHeader(name); + + if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { + messageID = null; + } + } + + public void setMessageID(String messageID) { + this.messageID = messageID; + } + + @Override + protected void updateMessageID() throws MessagingException { + if (messageID == null) { + super.updateMessageID(); + } else { + setHeader(MESSAGE_ID_HEADER, messageID); + } + } + + /** + * Call {@link #saveChanges()} before this method, if you want all headers including such ones like: + * + *
MIME-Version: 1.0
+	 *Content-Type: multipart/alternative;
+ * + * @return serialized headers + * @throws MessagingException if getAllHeaders() fails + */ + public String getHeaders() throws MessagingException { + StringBuilder sb = new StringBuilder(); + for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) { + sb.append(eh.nextElement()); + sb.append(CRLF); + } + return sb.toString(); + } + + public byte[] getBody() throws IOException, MessagingException { + saveChanges(); + + ArrayList skipHeaders = new ArrayList(); + for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) { + Header h = (Header) eh.nextElement(); + skipHeaders.add(h.getName()); + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); + writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()])); + return baos.toByteArray(); + } +}