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