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@72
|
20 |
import java.io.ByteArrayOutputStream;
|
franta-hg@72
|
21 |
import java.io.IOException;
|
franta-hg@74
|
22 |
import java.io.StringReader;
|
franta-hg@74
|
23 |
import java.io.StringWriter;
|
franta-hg@72
|
24 |
import java.io.UnsupportedEncodingException;
|
franta-hg@72
|
25 |
import java.sql.ResultSet;
|
franta-hg@72
|
26 |
import java.sql.SQLException;
|
franta-hg@72
|
27 |
import java.util.ArrayList;
|
franta-hg@72
|
28 |
import java.util.Date;
|
franta-hg@72
|
29 |
import java.util.Enumeration;
|
franta-hg@74
|
30 |
import java.util.logging.Level;
|
franta-hg@74
|
31 |
import java.util.logging.Logger;
|
franta-hg@72
|
32 |
import javax.mail.Header;
|
franta-hg@72
|
33 |
import javax.mail.MessagingException;
|
franta-hg@72
|
34 |
import javax.mail.Multipart;
|
franta-hg@72
|
35 |
import javax.mail.Session;
|
franta-hg@72
|
36 |
import javax.mail.internet.InternetAddress;
|
franta-hg@72
|
37 |
import javax.mail.internet.MimeBodyPart;
|
franta-hg@72
|
38 |
import javax.mail.internet.MimeMessage;
|
franta-hg@72
|
39 |
import javax.mail.internet.MimeMultipart;
|
franta-hg@74
|
40 |
import javax.xml.transform.Transformer;
|
franta-hg@74
|
41 |
import javax.xml.transform.TransformerFactory;
|
franta-hg@74
|
42 |
import javax.xml.transform.stream.StreamResult;
|
franta-hg@74
|
43 |
import javax.xml.transform.stream.StreamSource;
|
franta-hg@74
|
44 |
import org.sonews.util.io.Resource;
|
franta-hg@72
|
45 |
|
franta-hg@72
|
46 |
/**
|
franta-hg@72
|
47 |
* This is MimeMessage which enables custom Message-ID header
|
franta-hg@72
|
48 |
* (this header will not be overwritten by the default one like in MimeMessage).
|
franta-hg@72
|
49 |
*
|
franta-hg@72
|
50 |
* Also add header and body separate serialization.
|
franta-hg@72
|
51 |
*
|
franta-hg@72
|
52 |
* And can be deserialized from SQL ResultSet
|
franta-hg@72
|
53 |
*
|
franta-hg@72
|
54 |
* @author František Kučera (frantovo.cz)
|
franta-hg@72
|
55 |
*/
|
franta-hg@72
|
56 |
public class DrupalMessage extends MimeMessage {
|
franta-hg@72
|
57 |
|
franta-hg@74
|
58 |
private static final Logger log = Logger.getLogger(DrupalMessage.class.getName());
|
franta-hg@72
|
59 |
private static final String MESSAGE_ID_HEADER = "Message-ID";
|
franta-hg@72
|
60 |
private static final String CRLF = "\r\n";
|
franta-hg@72
|
61 |
public static final String CHARSET = "UTF-8";
|
franta-hg@72
|
62 |
private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET;
|
franta-hg@72
|
63 |
private String messageID;
|
franta-hg@72
|
64 |
|
franta-hg@72
|
65 |
/**
|
franta-hg@72
|
66 |
* Constructs MIME message from SQL result.
|
franta-hg@72
|
67 |
* @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read.
|
franta-hg@72
|
68 |
* @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy).
|
franta-hg@72
|
69 |
*/
|
franta-hg@72
|
70 |
public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException {
|
franta-hg@72
|
71 |
super(Session.getDefaultInstance(System.getProperties()));
|
franta-hg@72
|
72 |
|
franta-hg@72
|
73 |
addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain));
|
franta-hg@72
|
74 |
addHeader("Newsgroups", rs.getString("group_name"));
|
franta-hg@74
|
75 |
setFrom(new InternetAddress(rs.getString("sender_email"), rs.getString("sender_name")));
|
franta-hg@72
|
76 |
setSubject(rs.getString("subject"));
|
franta-hg@72
|
77 |
setSentDate(new Date(rs.getLong("created")));
|
franta-hg@74
|
78 |
|
franta-hg@74
|
79 |
int parentID = rs.getInt("parent_id");
|
franta-hg@74
|
80 |
if (parentID > 0) {
|
franta-hg@72
|
81 |
String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
|
franta-hg@72
|
82 |
addHeader("In-Reply-To", parentMessageID);
|
franta-hg@72
|
83 |
addHeader("References", parentMessageID);
|
franta-hg@72
|
84 |
}
|
franta-hg@72
|
85 |
|
franta-hg@72
|
86 |
if (constructBody) {
|
franta-hg@72
|
87 |
Multipart multipart = new MimeMultipart("alternative");
|
franta-hg@72
|
88 |
setContent(multipart);
|
franta-hg@72
|
89 |
|
franta-hg@74
|
90 |
/** Plain text part */
|
franta-hg@72
|
91 |
MimeBodyPart textPart = new MimeBodyPart();
|
franta-hg@72
|
92 |
multipart.addBodyPart(textPart);
|
franta-hg@72
|
93 |
textPart.setText(readPlainText(rs));
|
franta-hg@72
|
94 |
|
franta-hg@74
|
95 |
/** XHTML part */
|
franta-hg@72
|
96 |
MimeBodyPart htmlPart = new MimeBodyPart();
|
franta-hg@72
|
97 |
multipart.addBodyPart(htmlPart);
|
franta-hg@72
|
98 |
htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE);
|
franta-hg@72
|
99 |
} else {
|
franta-hg@72
|
100 |
setText("");
|
franta-hg@72
|
101 |
}
|
franta-hg@72
|
102 |
}
|
franta-hg@72
|
103 |
|
franta-hg@72
|
104 |
private String readPlainText(ResultSet rs) {
|
franta-hg@72
|
105 |
/**
|
franta-hg@72
|
106 |
* TODO: převést na prostý text
|
franta-hg@72
|
107 |
*/
|
franta-hg@72
|
108 |
return "TODO: obyčejný text";
|
franta-hg@72
|
109 |
}
|
franta-hg@72
|
110 |
|
franta-hg@72
|
111 |
private String readXhtmlText(ResultSet rs) {
|
franta-hg@72
|
112 |
/**
|
franta-hg@74
|
113 |
* TODO: znovupoužívat XSL transformér
|
franta-hg@72
|
114 |
*/
|
franta-hg@74
|
115 |
try {
|
franta-hg@74
|
116 |
String originalText = rs.getString("text");
|
franta-hg@74
|
117 |
StringReader input = new StringReader("<body>" + originalText + "</body>");
|
franta-hg@74
|
118 |
StringWriter output = new StringWriter();
|
franta-hg@74
|
119 |
TransformerFactory tf = TransformerFactory.newInstance();
|
franta-hg@74
|
120 |
Transformer t = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart.xsl")));
|
franta-hg@74
|
121 |
t.setParameter("isRoot", (rs.getInt("parent_id") == 0));
|
franta-hg@74
|
122 |
t.setParameter("title", rs.getString("subject"));
|
franta-hg@74
|
123 |
t.setParameter("urlBase", rs.getString("urlBase"));
|
franta-hg@74
|
124 |
t.setParameter("wwwRead", rs.getString("wwwRead"));
|
franta-hg@74
|
125 |
t.setParameter("wwwPost", rs.getString("wwwPost"));
|
franta-hg@74
|
126 |
t.transform(new StreamSource(input), new StreamResult(output));
|
franta-hg@74
|
127 |
|
franta-hg@74
|
128 |
return output.toString();
|
franta-hg@74
|
129 |
} catch (Exception e) {
|
franta-hg@74
|
130 |
/**
|
franta-hg@74
|
131 |
* TODO: lepší ošetření chyby
|
franta-hg@74
|
132 |
*/
|
franta-hg@74
|
133 |
log.log(Level.WARNING, "Error while transforming article to XHTML", e);
|
franta-hg@74
|
134 |
return "<html><body><p>Při transformaci příspěvku bohužel došlo k chybě.</p></body></html>";
|
franta-hg@74
|
135 |
}
|
franta-hg@72
|
136 |
}
|
franta-hg@72
|
137 |
|
franta-hg@72
|
138 |
private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {
|
franta-hg@72
|
139 |
StringBuilder sb = new StringBuilder();
|
franta-hg@72
|
140 |
sb.append("<");
|
franta-hg@72
|
141 |
sb.append(articleID);
|
franta-hg@72
|
142 |
sb.append("-");
|
franta-hg@72
|
143 |
sb.append(groupID);
|
franta-hg@72
|
144 |
sb.append("-");
|
franta-hg@72
|
145 |
sb.append(groupName);
|
franta-hg@72
|
146 |
sb.append("@");
|
franta-hg@72
|
147 |
sb.append(domainName);
|
franta-hg@72
|
148 |
sb.append(">");
|
franta-hg@72
|
149 |
return sb.toString();
|
franta-hg@72
|
150 |
}
|
franta-hg@72
|
151 |
|
franta-hg@72
|
152 |
@Override
|
franta-hg@72
|
153 |
public void setHeader(String name, String value) throws MessagingException {
|
franta-hg@72
|
154 |
super.setHeader(name, value);
|
franta-hg@72
|
155 |
|
franta-hg@72
|
156 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
157 |
messageID = value;
|
franta-hg@72
|
158 |
}
|
franta-hg@72
|
159 |
}
|
franta-hg@72
|
160 |
|
franta-hg@72
|
161 |
@Override
|
franta-hg@72
|
162 |
public final void addHeader(String name, String value) throws MessagingException {
|
franta-hg@72
|
163 |
super.addHeader(name, value);
|
franta-hg@72
|
164 |
|
franta-hg@72
|
165 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
166 |
messageID = value;
|
franta-hg@72
|
167 |
}
|
franta-hg@72
|
168 |
}
|
franta-hg@72
|
169 |
|
franta-hg@72
|
170 |
@Override
|
franta-hg@72
|
171 |
public void removeHeader(String name) throws MessagingException {
|
franta-hg@72
|
172 |
super.removeHeader(name);
|
franta-hg@72
|
173 |
|
franta-hg@72
|
174 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
175 |
messageID = null;
|
franta-hg@72
|
176 |
}
|
franta-hg@72
|
177 |
}
|
franta-hg@72
|
178 |
|
franta-hg@72
|
179 |
public void setMessageID(String messageID) {
|
franta-hg@72
|
180 |
this.messageID = messageID;
|
franta-hg@72
|
181 |
}
|
franta-hg@72
|
182 |
|
franta-hg@72
|
183 |
@Override
|
franta-hg@72
|
184 |
protected void updateMessageID() throws MessagingException {
|
franta-hg@72
|
185 |
if (messageID == null) {
|
franta-hg@72
|
186 |
super.updateMessageID();
|
franta-hg@72
|
187 |
} else {
|
franta-hg@72
|
188 |
setHeader(MESSAGE_ID_HEADER, messageID);
|
franta-hg@72
|
189 |
}
|
franta-hg@72
|
190 |
}
|
franta-hg@72
|
191 |
|
franta-hg@72
|
192 |
/**
|
franta-hg@72
|
193 |
* Call {@link #saveChanges()} before this method, if you want all headers including such ones like:
|
franta-hg@72
|
194 |
*
|
franta-hg@72
|
195 |
* <pre>MIME-Version: 1.0
|
franta-hg@72
|
196 |
*Content-Type: multipart/alternative;</pre>
|
franta-hg@72
|
197 |
*
|
franta-hg@72
|
198 |
* @return serialized headers
|
franta-hg@72
|
199 |
* @throws MessagingException if getAllHeaders() fails
|
franta-hg@72
|
200 |
*/
|
franta-hg@72
|
201 |
public String getHeaders() throws MessagingException {
|
franta-hg@72
|
202 |
StringBuilder sb = new StringBuilder();
|
franta-hg@72
|
203 |
for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) {
|
franta-hg@72
|
204 |
sb.append(eh.nextElement());
|
franta-hg@72
|
205 |
sb.append(CRLF);
|
franta-hg@72
|
206 |
}
|
franta-hg@72
|
207 |
return sb.toString();
|
franta-hg@72
|
208 |
}
|
franta-hg@72
|
209 |
|
franta-hg@72
|
210 |
public byte[] getBody() throws IOException, MessagingException {
|
franta-hg@72
|
211 |
saveChanges();
|
franta-hg@72
|
212 |
|
franta-hg@72
|
213 |
ArrayList<String> skipHeaders = new ArrayList<String>();
|
franta-hg@72
|
214 |
for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) {
|
franta-hg@72
|
215 |
Header h = (Header) eh.nextElement();
|
franta-hg@72
|
216 |
skipHeaders.add(h.getName());
|
franta-hg@72
|
217 |
}
|
franta-hg@72
|
218 |
|
franta-hg@72
|
219 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
franta-hg@72
|
220 |
writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()]));
|
franta-hg@72
|
221 |
return baos.toByteArray();
|
franta-hg@72
|
222 |
}
|
franta-hg@72
|
223 |
}
|