View Javadoc

1   /************************************************************************
2    * Copyright (c) 2003-2006 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;
20  import java.io.BufferedWriter;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.OutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Reader;
26  import java.io.StringWriter;
27  import java.io.UnsupportedEncodingException;
28  import java.io.Writer;
29  
30  import javax.activation.ActivationDataFlavor;
31  import javax.activation.DataSource;
32  import javax.mail.MessagingException;
33  import javax.mail.internet.ContentType;
34  import javax.mail.internet.MimeUtility;
35  import javax.mail.internet.ParseException;
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 computeDataFlavor()
61      {
62          return new ActivationDataFlavor(String.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 computeContent(DataSource aDataSource)
70              throws MessagingException
71      {
72          String encoding = getCharacterSet(aDataSource.getContentType());
73          Reader reader = null;
74          Writer writer = new StringWriter(2048);
75          String content = null;
76          try
77          {
78              reader = new BufferedReader(new InputStreamReader(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 e)
86          {
87              throw new MessagingException("Encoding = \"" + encoding + "\"", e);
88          }
89          catch (IOException e)
90          {
91              throw new MessagingException(
92                      "Exception obtaining content from DataSource", e);
93          }
94          finally
95          {
96              try
97              {
98                  writer.close();
99              }
100             catch (IOException 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 aPart, String aMimeType, OutputStream aStream)
113             throws IOException
114     {
115         if (!(aPart instanceof String))
116             throw new IOException("Type \"" + aPart.getClass().getName()
117                     + "\" is not supported.");
118 
119         String encoding = getCharacterSet(getDataFlavor().getMimeType());
120         Writer writer = null;
121         try
122         {
123             writer = new BufferedWriter(new OutputStreamWriter(aStream,
124                     encoding), 2048);
125         }
126         catch (IllegalArgumentException e)
127         {
128             throw new UnsupportedEncodingException(encoding);
129         }
130         writer.write((String) aPart);
131         writer.flush();
132     }
133 
134     protected String getCharacterSet(String aType)
135     {
136         String characterSet = null;
137         try
138         {
139             characterSet = new ContentType(aType).getParameter("charset");
140         }
141         catch (ParseException 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 }