KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > mail > handlers > message_disposition_notification


1 /***********************************************************************
2  * Copyright (c) 2003-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17 package org.apache.james.util.mail.handlers;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.BufferedWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import javax.activation.ActivationDataFlavor JavaDoc;
31 import javax.activation.DataSource JavaDoc;
32 import javax.mail.MessagingException JavaDoc;
33 import javax.mail.internet.ContentType JavaDoc;
34 import javax.mail.internet.MimeUtility JavaDoc;
35 import javax.mail.internet.ParseException JavaDoc;
36
37 /**
38  * <p>Data Content Handler for...</p>
39  * <dl>
40  * <dt>MIME type name</dt><dd>message</dd>
41  * <dt>MIME subtype name</dt><dd>disposition-notification</dd>
42  * </dl>
43  */

44 public class message_disposition_notification
45         extends
46             AbstractDataContentHandler
47 {
48
49     /**
50      * Default Constructor.
51      */

52     public message_disposition_notification()
53     {
54         super();
55     }
56
57     /**
58      * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeDataFlavor()
59      */

60     protected ActivationDataFlavor JavaDoc computeDataFlavor()
61     {
62         return new ActivationDataFlavor JavaDoc(String JavaDoc.class,
63                 "message/disposition-notification", "Message String");
64     }
65
66     /**
67      * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeContent(javax.activation.DataSource)
68      */

69     protected Object JavaDoc computeContent(DataSource JavaDoc aDataSource)
70             throws MessagingException JavaDoc
71     {
72         String JavaDoc encoding = getCharacterSet(aDataSource.getContentType());
73         Reader JavaDoc reader = null;
74         Writer JavaDoc writer = new StringWriter JavaDoc(2048);
75         String JavaDoc content = null;
76         try
77         {
78             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(aDataSource
79                     .getInputStream(), encoding), 2048);
80             while (reader.ready())
81                 writer.write(reader.read());
82             writer.flush();
83             content = writer.toString();
84         }
85         catch (IllegalArgumentException JavaDoc e)
86         {
87             throw new MessagingException JavaDoc("Encoding = \"" + encoding + "\"", e);
88         }
89         catch (IOException JavaDoc e)
90         {
91             throw new MessagingException JavaDoc(
92                     "Exception obtaining content from DataSource", e);
93         }
94         finally
95         {
96             try
97             {
98                 writer.close();
99             }
100             catch (IOException JavaDoc e1)
101             {
102                 // No-op
103
}
104         }
105         return content;
106     }
107
108     /**
109      * @see javax.activation.DataContentHandler#writeTo(java.lang.Object,
110      * java.lang.String, java.io.OutputStream)
111      */

112     public void writeTo(Object JavaDoc aPart, String JavaDoc aMimeType, OutputStream JavaDoc aStream)
113             throws IOException JavaDoc
114     {
115         if (!(aPart instanceof String JavaDoc))
116             throw new IOException JavaDoc("Type \"" + aPart.getClass().getName()
117                     + "\" is not supported.");
118
119         String JavaDoc encoding = getCharacterSet(getDataFlavor().getMimeType());
120         Writer JavaDoc writer = null;
121         try
122         {
123             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(aStream,
124                     encoding), 2048);
125         }
126         catch (IllegalArgumentException JavaDoc e)
127         {
128             throw new UnsupportedEncodingException JavaDoc(encoding);
129         }
130         writer.write((String JavaDoc) aPart);
131         writer.flush();
132     }
133
134     protected String JavaDoc getCharacterSet(String JavaDoc aType)
135     {
136         String JavaDoc characterSet = null;
137         try
138         {
139             characterSet = new ContentType JavaDoc(aType).getParameter("charset");
140         }
141         catch (ParseException JavaDoc e)
142         {
143             // no-op
144
}
145         finally
146         {
147             if (null == characterSet)
148                 characterSet = "us-ascii";
149         }
150         return MimeUtility.javaCharset(characterSet);
151     }
152
153 }
154
Popular Tags