|
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 |
| |
|
40 |
| |
|
41 |
| ActivationDataFlavor dataFlavor; |
|
42 |
| |
|
43 |
0
| public MessageHandler(){
|
|
44 |
0
| dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text");
|
|
45 |
| } |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
0
| protected ActivationDataFlavor getDF() {
|
|
54 |
0
| return dataFlavor;
|
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
0
| public DataFlavor[] getTransferDataFlavors() {
|
|
63 |
0
| return (new DataFlavor[]{dataFlavor});
|
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
0
| public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
|
|
75 |
| throws IOException { |
|
76 |
0
| if (getDF().equals(dataflavor)) {
|
|
77 |
0
| return getContent(datasource);
|
|
78 |
| } |
|
79 |
0
| return null;
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
0
| public Object getContent(DataSource datasource) throws IOException {
|
|
90 |
| |
|
91 |
0
| try {
|
|
92 |
| |
|
93 |
| |
|
94 |
0
| if (datasource instanceof MessageAware) {
|
|
95 |
0
| MessageContext context = ((MessageAware)datasource).getMessageContext();
|
|
96 |
| |
|
97 |
| |
|
98 |
0
| return new MimeMessage(context.getSession(), datasource.getInputStream());
|
|
99 |
| } |
|
100 |
| } catch (MessagingException e) { |
|
101 |
| |
|
102 |
0
| throw new IOException("Exception writing MimeMultipart: " + e.toString());
|
|
103 |
| } |
|
104 |
0
| return null;
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
0
| public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
|
|
116 |
| |
|
117 |
0
| if (object instanceof Message) {
|
|
118 |
0
| try {
|
|
119 |
0
| ((Message)object).writeTo(outputstream);
|
|
120 |
| } catch (MessagingException e) { |
|
121 |
0
| throw new IOException("Error parsing message: " + e.toString());
|
|
122 |
| } |
|
123 |
| } |
|
124 |
| } |
|
125 |
| } |
|
126 |
| |