Drupal: vyhození zbytečností.
3 * see AUTHORS for the list of contributors
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.
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.
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/>.
18 package org.sonews.storage;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.Enumeration;
28 import javax.mail.Header;
29 import javax.mail.MessagingException;
30 import javax.mail.Multipart;
31 import javax.mail.Session;
32 import javax.mail.internet.InternetAddress;
33 import javax.mail.internet.MimeBodyPart;
34 import javax.mail.internet.MimeMessage;
35 import javax.mail.internet.MimeMultipart;
38 * This is MimeMessage which enables custom Message-ID header
39 * (this header will not be overwritten by the default one like in MimeMessage).
41 * Also add header and body separate serialization.
43 * And can be deserialized from SQL ResultSet
45 * @author František Kučera (frantovo.cz)
47 public class DrupalMessage extends MimeMessage {
49 private static final String MESSAGE_ID_HEADER = "Message-ID";
50 private static final String CRLF = "\r\n";
51 public static final String CHARSET = "UTF-8";
52 private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET;
53 private String messageID;
56 * Constructs MIME message from SQL result.
57 * @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read.
58 * @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy).
60 public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException {
61 super(Session.getDefaultInstance(System.getProperties()));
63 addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain));
64 addHeader("Newsgroups", rs.getString("group_name"));
65 setFrom(new InternetAddress("anonym@example.com", rs.getString("sender_name")));
66 setSubject(rs.getString("subject"));
67 setSentDate(new Date(rs.getLong("created")));
69 Integer parentID = rs.getInt("parent_id");
70 if (parentID != null && parentID > 0) {
71 String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
72 addHeader("In-Reply-To", parentMessageID);
73 addHeader("References", parentMessageID);
77 Multipart multipart = new MimeMultipart("alternative");
78 setContent(multipart);
80 /** TODO: Plain text part */
81 MimeBodyPart textPart = new MimeBodyPart();
82 multipart.addBodyPart(textPart);
83 textPart.setText(readPlainText(rs));
85 /** TODO: XHTML part */
86 MimeBodyPart htmlPart = new MimeBodyPart();
87 multipart.addBodyPart(htmlPart);
88 htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE);
94 private String readPlainText(ResultSet rs) {
96 * TODO: převést na prostý text
98 return "TODO: obyčejný text";
101 private String readXhtmlText(ResultSet rs) {
103 * TODO: převést na XHTML
105 return "<html><body>TODO: tady bude nějaký <strong>(X)HTML</strong></body></html>";
108 private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {
109 StringBuilder sb = new StringBuilder();
111 sb.append(articleID);
115 sb.append(groupName);
117 sb.append(domainName);
119 return sb.toString();
123 public void setHeader(String name, String value) throws MessagingException {
124 super.setHeader(name, value);
126 if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
132 public final void addHeader(String name, String value) throws MessagingException {
133 super.addHeader(name, value);
135 if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
141 public void removeHeader(String name) throws MessagingException {
142 super.removeHeader(name);
144 if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
149 public void setMessageID(String messageID) {
150 this.messageID = messageID;
154 protected void updateMessageID() throws MessagingException {
155 if (messageID == null) {
156 super.updateMessageID();
158 setHeader(MESSAGE_ID_HEADER, messageID);
163 * Call {@link #saveChanges()} before this method, if you want all headers including such ones like:
165 * <pre>MIME-Version: 1.0
166 *Content-Type: multipart/alternative;</pre>
168 * @return serialized headers
169 * @throws MessagingException if getAllHeaders() fails
171 public String getHeaders() throws MessagingException {
172 StringBuilder sb = new StringBuilder();
173 for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) {
174 sb.append(eh.nextElement());
177 return sb.toString();
180 public byte[] getBody() throws IOException, MessagingException {
183 ArrayList<String> skipHeaders = new ArrayList<String>();
184 for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) {
185 Header h = (Header) eh.nextElement();
186 skipHeaders.add(h.getName());
189 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
190 writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()]));
191 return baos.toByteArray();