01: package dalma.endpoints.email;
02:
03: import javax.activation.DataHandler;
04: import javax.mail.Message;
05: import javax.mail.MessagingException;
06: import javax.mail.Session;
07: import javax.mail.internet.MimeMessage;
08: import java.io.ByteArrayInputStream;
09: import java.io.ByteArrayOutputStream;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.Serializable;
13:
14: /**
15: * A wrapper around {@link MimeMessage} to make it serializable.
16: *
17: * @author Kohsuke Kawaguchi
18: */
19: public class MimeMessageEx extends MimeMessage implements Serializable {
20: public MimeMessageEx(MimeMessage source) throws MessagingException {
21: super (source);
22: }
23:
24: public MimeMessageEx(Session session, InputStream is)
25: throws MessagingException {
26: super (session, is);
27: }
28:
29: public MimeMessageEx(Session session) {
30: super (session);
31: }
32:
33: protected void updateHeaders() throws MessagingException {
34: // JavaMail clears the message-id with its own, so restore the one
35: // we set
36: String[] mid = getHeader("Message-ID");
37: super .updateHeaders();
38: if (mid != null) {
39: removeHeader("Message-ID");
40: for (String h : mid)
41: addHeader("Message-ID", h);
42: }
43: }
44:
45: public Message reply(boolean replyToAll) throws MessagingException {
46: MimeMessage msg = (MimeMessage) super .reply(replyToAll);
47:
48: // set References header
49: String msgId = getHeader("Message-Id", null);
50:
51: String header = getHeader("References", " ");
52: if (header == null)
53: header = "";
54: header += ' ' + msgId.trim();
55:
56: msg.setHeader("References", header);
57: msg.setText(""); // set the dummy body otherwise the following method fails
58:
59: return new MimeMessageEx(msg);
60: }
61:
62: /**
63: * Working around another bug in JavaMail.
64: */
65: public synchronized void setDataHandler(DataHandler dh)
66: throws MessagingException {
67: super .setDataHandler(dh);
68: // content = null;
69: // contentStream = null;
70: saveChanges();
71: }
72:
73: private Object writeReplace() throws IOException,
74: MessagingException {
75: ByteArrayOutputStream baos = new ByteArrayOutputStream();
76: writeTo(baos);
77: return new Moniker(baos.toByteArray());
78: }
79:
80: private static final class Moniker implements Serializable {
81: private final byte[] data;
82:
83: public Moniker(byte[] data) {
84: this .data = data;
85: }
86:
87: private Object readResolve() throws MessagingException {
88: return new MimeMessageEx(Session.getInstance(System
89: .getProperties()), new ByteArrayInputStream(data));
90: }
91:
92: private static final long serialVersionUID = 1L;
93: }
94:
95: private static final long serialVersionUID = 1L;
96: }
|