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 * Field dataFlavor
36 */
37 ActivationDataFlavor dataFlavor;
38
39 public TextHandler(){
40 dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
41 }
42
43 /**
44 * Constructor TextHandler
45 *
46 * @param dataFlavor
47 */
48 public TextHandler(ActivationDataFlavor dataFlavor) {
49 this.dataFlavor = dataFlavor;
50 }
51
52 /**
53 * Method getDF
54 *
55 * @return dataflavor
56 */
57 protected ActivationDataFlavor getDF() {
58 return dataFlavor;
59 }
60
61 /**
62 * Method getTransferDataFlavors
63 *
64 * @return dataflavors
65 */
66 public DataFlavor[] getTransferDataFlavors() {
67 return (new DataFlavor[]{dataFlavor});
68 }
69
70 /**
71 * Method getTransferData
72 *
73 * @param dataflavor
74 * @param datasource
75 * @return
76 * @throws IOException
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 * Method getContent
88 *
89 * @param datasource
90 * @return
91 * @throws IOException
92 */
93 public Object getContent(DataSource datasource) throws IOException {
94 InputStreamReader reader;
95 try {
96 String s = getCharSet(datasource.getContentType());
97 reader = new InputStreamReader(datasource.getInputStream(), s);
98 } catch (Exception ex) {
99 throw new UnsupportedEncodingException(ex.toString());
100 }
101 StringWriter writer = new StringWriter();
102 int ch;
103 while ((ch = reader.read()) != -1) {
104 writer.write(ch);
105 }
106 return writer.getBuffer().toString();
107 }
108
109 /**
110 * Method writeTo
111 *
112 * @param object
113 * @param s
114 * @param outputstream
115 * @throws IOException
116 */
117 public void writeTo(Object object, String s, OutputStream outputstream)
118 throws IOException {
119 OutputStreamWriter os;
120 try {
121 String charset = getCharSet(s);
122 os = new OutputStreamWriter(outputstream, charset);
123 } catch (Exception ex) {
124 throw new UnsupportedEncodingException(ex.toString());
125 }
126 String content = (String) object;
127 os.write(content, 0, content.length());
128 os.flush();
129 }
130
131 /**
132 * get the character set from content type
133 * @param contentType
134 * @return
135 * @throws ParseException
136 */
137 protected String getCharSet(String contentType) throws ParseException {
138 ContentType type = new ContentType(contentType);
139 String charset = type.getParameter("charset");
140 if (charset == null) {
141 charset = "us-ascii";
142 }
143 return MimeUtility.javaCharset(charset);
144 }
145 }