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