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@72
|
22 |
import java.io.UnsupportedEncodingException;
|
franta-hg@72
|
23 |
import java.sql.ResultSet;
|
franta-hg@72
|
24 |
import java.sql.SQLException;
|
franta-hg@72
|
25 |
import java.util.ArrayList;
|
franta-hg@72
|
26 |
import java.util.Date;
|
franta-hg@72
|
27 |
import java.util.Enumeration;
|
franta-hg@72
|
28 |
import javax.mail.Header;
|
franta-hg@72
|
29 |
import javax.mail.MessagingException;
|
franta-hg@72
|
30 |
import javax.mail.Multipart;
|
franta-hg@72
|
31 |
import javax.mail.Session;
|
franta-hg@72
|
32 |
import javax.mail.internet.InternetAddress;
|
franta-hg@72
|
33 |
import javax.mail.internet.MimeBodyPart;
|
franta-hg@72
|
34 |
import javax.mail.internet.MimeMessage;
|
franta-hg@72
|
35 |
import javax.mail.internet.MimeMultipart;
|
franta-hg@72
|
36 |
|
franta-hg@72
|
37 |
/**
|
franta-hg@72
|
38 |
* This is MimeMessage which enables custom Message-ID header
|
franta-hg@72
|
39 |
* (this header will not be overwritten by the default one like in MimeMessage).
|
franta-hg@72
|
40 |
*
|
franta-hg@72
|
41 |
* Also add header and body separate serialization.
|
franta-hg@72
|
42 |
*
|
franta-hg@72
|
43 |
* And can be deserialized from SQL ResultSet
|
franta-hg@72
|
44 |
*
|
franta-hg@72
|
45 |
* @author František Kučera (frantovo.cz)
|
franta-hg@72
|
46 |
*/
|
franta-hg@72
|
47 |
public class DrupalMessage extends MimeMessage {
|
franta-hg@72
|
48 |
|
franta-hg@72
|
49 |
private static final String MESSAGE_ID_HEADER = "Message-ID";
|
franta-hg@72
|
50 |
private static final String CRLF = "\r\n";
|
franta-hg@72
|
51 |
public static final String CHARSET = "UTF-8";
|
franta-hg@72
|
52 |
private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET;
|
franta-hg@72
|
53 |
private String messageID;
|
franta-hg@72
|
54 |
|
franta-hg@72
|
55 |
/**
|
franta-hg@72
|
56 |
* Constructs MIME message from SQL result.
|
franta-hg@72
|
57 |
* @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read.
|
franta-hg@72
|
58 |
* @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy).
|
franta-hg@72
|
59 |
*/
|
franta-hg@72
|
60 |
public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException {
|
franta-hg@72
|
61 |
super(Session.getDefaultInstance(System.getProperties()));
|
franta-hg@72
|
62 |
|
franta-hg@72
|
63 |
addHeader("Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain));
|
franta-hg@72
|
64 |
addHeader("Newsgroups", rs.getString("group_name"));
|
franta-hg@72
|
65 |
setFrom(new InternetAddress("anonym@example.com", rs.getString("sender_name")));
|
franta-hg@72
|
66 |
setSubject(rs.getString("subject"));
|
franta-hg@72
|
67 |
setSentDate(new Date(rs.getLong("created")));
|
franta-hg@72
|
68 |
|
franta-hg@72
|
69 |
Integer parentID = rs.getInt("parent_id");
|
franta-hg@72
|
70 |
if (parentID != null && parentID > 0) {
|
franta-hg@72
|
71 |
String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
|
franta-hg@72
|
72 |
addHeader("In-Reply-To", parentMessageID);
|
franta-hg@72
|
73 |
addHeader("References", parentMessageID);
|
franta-hg@72
|
74 |
}
|
franta-hg@72
|
75 |
|
franta-hg@72
|
76 |
if (constructBody) {
|
franta-hg@72
|
77 |
Multipart multipart = new MimeMultipart("alternative");
|
franta-hg@72
|
78 |
setContent(multipart);
|
franta-hg@72
|
79 |
|
franta-hg@72
|
80 |
/** TODO: Plain text part */
|
franta-hg@72
|
81 |
MimeBodyPart textPart = new MimeBodyPart();
|
franta-hg@72
|
82 |
multipart.addBodyPart(textPart);
|
franta-hg@72
|
83 |
textPart.setText(readPlainText(rs));
|
franta-hg@72
|
84 |
|
franta-hg@72
|
85 |
/** TODO: XHTML part */
|
franta-hg@72
|
86 |
MimeBodyPart htmlPart = new MimeBodyPart();
|
franta-hg@72
|
87 |
multipart.addBodyPart(htmlPart);
|
franta-hg@72
|
88 |
htmlPart.setContent(readXhtmlText(rs), XHTML_CONTENT_TYPE);
|
franta-hg@72
|
89 |
} else {
|
franta-hg@72
|
90 |
setText("");
|
franta-hg@72
|
91 |
}
|
franta-hg@72
|
92 |
}
|
franta-hg@72
|
93 |
|
franta-hg@72
|
94 |
private String readPlainText(ResultSet rs) {
|
franta-hg@72
|
95 |
/**
|
franta-hg@72
|
96 |
* TODO: převést na prostý text
|
franta-hg@72
|
97 |
*/
|
franta-hg@72
|
98 |
return "TODO: obyčejný text";
|
franta-hg@72
|
99 |
}
|
franta-hg@72
|
100 |
|
franta-hg@72
|
101 |
private String readXhtmlText(ResultSet rs) {
|
franta-hg@72
|
102 |
/**
|
franta-hg@72
|
103 |
* TODO: převést na XHTML
|
franta-hg@72
|
104 |
*/
|
franta-hg@72
|
105 |
return "<html><body>TODO: tady bude nějaký <strong>(X)HTML</strong></body></html>";
|
franta-hg@72
|
106 |
}
|
franta-hg@72
|
107 |
|
franta-hg@72
|
108 |
private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {
|
franta-hg@72
|
109 |
StringBuilder sb = new StringBuilder();
|
franta-hg@72
|
110 |
sb.append("<");
|
franta-hg@72
|
111 |
sb.append(articleID);
|
franta-hg@72
|
112 |
sb.append("-");
|
franta-hg@72
|
113 |
sb.append(groupID);
|
franta-hg@72
|
114 |
sb.append("-");
|
franta-hg@72
|
115 |
sb.append(groupName);
|
franta-hg@72
|
116 |
sb.append("@");
|
franta-hg@72
|
117 |
sb.append(domainName);
|
franta-hg@72
|
118 |
sb.append(">");
|
franta-hg@72
|
119 |
return sb.toString();
|
franta-hg@72
|
120 |
}
|
franta-hg@72
|
121 |
|
franta-hg@72
|
122 |
@Override
|
franta-hg@72
|
123 |
public void setHeader(String name, String value) throws MessagingException {
|
franta-hg@72
|
124 |
super.setHeader(name, value);
|
franta-hg@72
|
125 |
|
franta-hg@72
|
126 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
127 |
messageID = value;
|
franta-hg@72
|
128 |
}
|
franta-hg@72
|
129 |
}
|
franta-hg@72
|
130 |
|
franta-hg@72
|
131 |
@Override
|
franta-hg@72
|
132 |
public final void addHeader(String name, String value) throws MessagingException {
|
franta-hg@72
|
133 |
super.addHeader(name, value);
|
franta-hg@72
|
134 |
|
franta-hg@72
|
135 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
136 |
messageID = value;
|
franta-hg@72
|
137 |
}
|
franta-hg@72
|
138 |
}
|
franta-hg@72
|
139 |
|
franta-hg@72
|
140 |
@Override
|
franta-hg@72
|
141 |
public void removeHeader(String name) throws MessagingException {
|
franta-hg@72
|
142 |
super.removeHeader(name);
|
franta-hg@72
|
143 |
|
franta-hg@72
|
144 |
if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) {
|
franta-hg@72
|
145 |
messageID = null;
|
franta-hg@72
|
146 |
}
|
franta-hg@72
|
147 |
}
|
franta-hg@72
|
148 |
|
franta-hg@72
|
149 |
public void setMessageID(String messageID) {
|
franta-hg@72
|
150 |
this.messageID = messageID;
|
franta-hg@72
|
151 |
}
|
franta-hg@72
|
152 |
|
franta-hg@72
|
153 |
@Override
|
franta-hg@72
|
154 |
protected void updateMessageID() throws MessagingException {
|
franta-hg@72
|
155 |
if (messageID == null) {
|
franta-hg@72
|
156 |
super.updateMessageID();
|
franta-hg@72
|
157 |
} else {
|
franta-hg@72
|
158 |
setHeader(MESSAGE_ID_HEADER, messageID);
|
franta-hg@72
|
159 |
}
|
franta-hg@72
|
160 |
}
|
franta-hg@72
|
161 |
|
franta-hg@72
|
162 |
/**
|
franta-hg@72
|
163 |
* Call {@link #saveChanges()} before this method, if you want all headers including such ones like:
|
franta-hg@72
|
164 |
*
|
franta-hg@72
|
165 |
* <pre>MIME-Version: 1.0
|
franta-hg@72
|
166 |
*Content-Type: multipart/alternative;</pre>
|
franta-hg@72
|
167 |
*
|
franta-hg@72
|
168 |
* @return serialized headers
|
franta-hg@72
|
169 |
* @throws MessagingException if getAllHeaders() fails
|
franta-hg@72
|
170 |
*/
|
franta-hg@72
|
171 |
public String getHeaders() throws MessagingException {
|
franta-hg@72
|
172 |
StringBuilder sb = new StringBuilder();
|
franta-hg@72
|
173 |
for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) {
|
franta-hg@72
|
174 |
sb.append(eh.nextElement());
|
franta-hg@72
|
175 |
sb.append(CRLF);
|
franta-hg@72
|
176 |
}
|
franta-hg@72
|
177 |
return sb.toString();
|
franta-hg@72
|
178 |
}
|
franta-hg@72
|
179 |
|
franta-hg@72
|
180 |
public byte[] getBody() throws IOException, MessagingException {
|
franta-hg@72
|
181 |
saveChanges();
|
franta-hg@72
|
182 |
|
franta-hg@72
|
183 |
ArrayList<String> skipHeaders = new ArrayList<String>();
|
franta-hg@72
|
184 |
for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) {
|
franta-hg@72
|
185 |
Header h = (Header) eh.nextElement();
|
franta-hg@72
|
186 |
skipHeaders.add(h.getName());
|
franta-hg@72
|
187 |
}
|
franta-hg@72
|
188 |
|
franta-hg@72
|
189 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
franta-hg@72
|
190 |
writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()]));
|
franta-hg@72
|
191 |
return baos.toByteArray();
|
franta-hg@72
|
192 |
}
|
franta-hg@72
|
193 |
}
|