1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.geronimo.mail.handlers;
17
18 import javax.activation.ActivationDataFlavor;
19 import javax.activation.DataContentHandler;
20 import javax.activation.DataSource;
21 import javax.mail.internet.ContentType;
22 import javax.mail.Message;
23 import javax.mail.MessageAware;
24 import javax.mail.MessageContext;
25 import javax.mail.MessagingException;
26 import javax.mail.internet.MimeMessage;
27 import javax.mail.internet.MimeUtility;
28 import javax.mail.internet.ParseException;
29 import java.awt.datatransfer.DataFlavor;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.OutputStream;
33 import java.io.OutputStreamWriter;
34 import java.io.StringWriter;
35 import java.io.UnsupportedEncodingException;
36
37 public class MessageHandler implements DataContentHandler {
38 /**
39 * Field dataFlavor
40 */
41 ActivationDataFlavor dataFlavor;
42
43 public MessageHandler(){
44 dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text");
45 }
46
47
48 /**
49 * Method getDF
50 *
51 * @return dataflavor
52 */
53 protected ActivationDataFlavor getDF() {
54 return dataFlavor;
55 }
56
57 /**
58 * Method getTransferDataFlavors
59 *
60 * @return dataflavors
61 */
62 public DataFlavor[] getTransferDataFlavors() {
63 return (new DataFlavor[]{dataFlavor});
64 }
65
66 /**
67 * Method getTransferData
68 *
69 * @param dataflavor
70 * @param datasource
71 * @return
72 * @throws IOException
73 */
74 public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
75 throws IOException {
76 if (getDF().equals(dataflavor)) {
77 return getContent(datasource);
78 }
79 return null;
80 }
81
82 /**
83 * Method getContent
84 *
85 * @param datasource
86 * @return
87 * @throws IOException
88 */
89 public Object getContent(DataSource datasource) throws IOException {
90
91 try {
92
93
94 if (datasource instanceof MessageAware) {
95 MessageContext context = ((MessageAware)datasource).getMessageContext();
96
97
98 return new MimeMessage(context.getSession(), datasource.getInputStream());
99 }
100 } catch (MessagingException e) {
101
102 throw new IOException("Exception writing MimeMultipart: " + e.toString());
103 }
104 return null;
105 }
106
107 /**
108 * Method writeTo
109 *
110 * @param object
111 * @param s
112 * @param outputstream
113 * @throws IOException
114 */
115 public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
116
117 if (object instanceof Message) {
118 try {
119 ((Message)object).writeTo(outputstream);
120 } catch (MessagingException e) {
121 throw new IOException("Error parsing message: " + e.toString());
122 }
123 }
124 }
125 }
126