1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mailet.base.mail;
21
22 import java.io.BufferedReader;
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Reader;
29 import java.io.StringWriter;
30 import java.io.UnsupportedEncodingException;
31 import java.io.Writer;
32
33 import javax.activation.ActivationDataFlavor;
34 import javax.activation.DataSource;
35 import javax.mail.MessagingException;
36 import javax.mail.internet.ContentType;
37 import javax.mail.internet.MimeUtility;
38 import javax.mail.internet.ParseException;
39
40
41
42
43
44
45
46
47 public class message_disposition_notification
48 extends
49 AbstractDataContentHandler
50 {
51
52
53
54
55 public message_disposition_notification()
56 {
57 super();
58 }
59
60
61
62
63 protected ActivationDataFlavor computeDataFlavor()
64 {
65 return new ActivationDataFlavor(String.class,
66 "message/disposition-notification", "Message String");
67 }
68
69
70
71
72 protected Object computeContent(DataSource aDataSource)
73 throws MessagingException
74 {
75 String encoding = getCharacterSet(aDataSource.getContentType());
76 Reader reader = null;
77 Writer writer = new StringWriter(2048);
78 String content = null;
79 try
80 {
81 reader = new BufferedReader(new InputStreamReader(aDataSource
82 .getInputStream(), encoding), 2048);
83 while (reader.ready())
84 writer.write(reader.read());
85 writer.flush();
86 content = writer.toString();
87 }
88 catch (IllegalArgumentException e)
89 {
90 throw new MessagingException("Encoding = \"" + encoding + "\"", e);
91 }
92 catch (IOException e)
93 {
94 throw new MessagingException(
95 "Exception obtaining content from DataSource", e);
96 }
97 finally
98 {
99 try
100 {
101 writer.close();
102 }
103 catch (IOException e1)
104 {
105
106 }
107 }
108 return content;
109 }
110
111
112
113
114
115 public void writeTo(Object aPart, String aMimeType, OutputStream aStream)
116 throws IOException
117 {
118 if (!(aPart instanceof String))
119 throw new IOException("Type \"" + aPart.getClass().getName()
120 + "\" is not supported.");
121
122 String encoding = getCharacterSet(getDataFlavor().getMimeType());
123 Writer writer = null;
124 try
125 {
126 writer = new BufferedWriter(new OutputStreamWriter(aStream,
127 encoding), 2048);
128 }
129 catch (IllegalArgumentException e)
130 {
131 throw new UnsupportedEncodingException(encoding);
132 }
133 writer.write((String) aPart);
134 writer.flush();
135 }
136
137 protected String getCharacterSet(String aType)
138 {
139 String characterSet = null;
140 try
141 {
142 characterSet = new ContentType(aType).getParameter("charset");
143 }
144 catch (ParseException e)
145 {
146
147 }
148 finally
149 {
150 if (null == characterSet)
151 characterSet = "us-ascii";
152 }
153 return MimeUtility.javaCharset(characterSet);
154 }
155
156 }