|
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 java.awt.datatransfer.DataFlavor; |
|
19 |
| import java.io.IOException; |
|
20 |
| import java.io.InputStreamReader; |
|
21 |
| import java.io.OutputStream; |
|
22 |
| import java.io.OutputStreamWriter; |
|
23 |
| import java.io.StringWriter; |
|
24 |
| import java.io.UnsupportedEncodingException; |
|
25 |
| |
|
26 |
| import javax.activation.ActivationDataFlavor; |
|
27 |
| import javax.activation.DataContentHandler; |
|
28 |
| import javax.activation.DataSource; |
|
29 |
| import javax.mail.internet.ContentType; |
|
30 |
| import javax.mail.internet.MimeUtility; |
|
31 |
| import javax.mail.internet.ParseException; |
|
32 |
| |
|
33 |
| public class TextHandler implements DataContentHandler { |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| ActivationDataFlavor dataFlavor; |
|
38 |
| |
|
39 |
0
| public TextHandler(){
|
|
40 |
0
| dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
|
|
41 |
| } |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
0
| public TextHandler(ActivationDataFlavor dataFlavor) {
|
|
49 |
0
| this.dataFlavor = dataFlavor;
|
|
50 |
| } |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
0
| protected ActivationDataFlavor getDF() {
|
|
58 |
0
| return dataFlavor;
|
|
59 |
| } |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
0
| public DataFlavor[] getTransferDataFlavors() {
|
|
67 |
0
| return (new DataFlavor[]{dataFlavor});
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
0
| public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
|
|
79 |
| throws IOException { |
|
80 |
0
| if (getDF().equals(dataflavor)) {
|
|
81 |
0
| return getContent(datasource);
|
|
82 |
| } |
|
83 |
0
| return null;
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
0
| public Object getContent(DataSource datasource) throws IOException {
|
|
94 |
0
| InputStreamReader reader;
|
|
95 |
0
| try {
|
|
96 |
0
| String s = getCharSet(datasource.getContentType());
|
|
97 |
0
| reader = new InputStreamReader(datasource.getInputStream(), s);
|
|
98 |
| } catch (Exception ex) { |
|
99 |
0
| throw new UnsupportedEncodingException(ex.toString());
|
|
100 |
| } |
|
101 |
0
| StringWriter writer = new StringWriter();
|
|
102 |
0
| int ch;
|
|
103 |
0
| while ((ch = reader.read()) != -1) {
|
|
104 |
0
| writer.write(ch);
|
|
105 |
| } |
|
106 |
0
| return writer.getBuffer().toString();
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
0
| public void writeTo(Object object, String s, OutputStream outputstream)
|
|
118 |
| throws IOException { |
|
119 |
0
| OutputStreamWriter os;
|
|
120 |
0
| try {
|
|
121 |
0
| String charset = getCharSet(s);
|
|
122 |
0
| os = new OutputStreamWriter(outputstream, charset);
|
|
123 |
| } catch (Exception ex) { |
|
124 |
0
| throw new UnsupportedEncodingException(ex.toString());
|
|
125 |
| } |
|
126 |
0
| String content = (String) object;
|
|
127 |
0
| os.write(content, 0, content.length());
|
|
128 |
0
| os.flush();
|
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
0
| protected String getCharSet(String contentType) throws ParseException {
|
|
138 |
0
| ContentType type = new ContentType(contentType);
|
|
139 |
0
| String charset = type.getParameter("charset");
|
|
140 |
0
| if (charset == null) {
|
|
141 |
0
| charset = "us-ascii";
|
|
142 |
| } |
|
143 |
0
| return MimeUtility.javaCharset(charset);
|
|
144 |
| } |
|
145 |
| } |