View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.james.util.mail.handlers;
20  
21  import java.io.BufferedReader;
22  import java.io.BufferedWriter;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.OutputStream;
26  import java.io.OutputStreamWriter;
27  import java.io.Reader;
28  import java.io.StringWriter;
29  import java.io.UnsupportedEncodingException;
30  import java.io.Writer;
31  
32  import javax.activation.ActivationDataFlavor;
33  import javax.activation.DataSource;
34  import javax.mail.MessagingException;
35  import javax.mail.internet.ContentType;
36  import javax.mail.internet.MimeUtility;
37  import javax.mail.internet.ParseException;
38  
39  /***
40   * <p>Data Content Handler for...</p>
41   * <dl>
42   * <dt>MIME type name</dt><dd>message</dd>
43   * <dt>MIME subtype name</dt><dd>disposition-notification</dd>
44   * </dl>
45   */
46  public class message_disposition_notification
47          extends
48              AbstractDataContentHandler
49  {
50  
51      /***
52       * Default Constructor.
53       */
54      public message_disposition_notification()
55      {
56          super();
57      }
58  
59      /***
60       * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeDataFlavor()
61       */
62      protected ActivationDataFlavor computeDataFlavor()
63      {
64          return new ActivationDataFlavor(String.class,
65                  "message/disposition-notification", "Message String");
66      }
67  
68      /***
69       * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeContent(javax.activation.DataSource)
70       */
71      protected Object computeContent(DataSource aDataSource)
72              throws MessagingException
73      {
74          String encoding = getCharacterSet(aDataSource.getContentType());
75          Reader reader = null;
76          Writer writer = new StringWriter(2048);
77          String content = null;
78          try
79          {
80              reader = new BufferedReader(new InputStreamReader(aDataSource
81                      .getInputStream(), encoding), 2048);
82              while (reader.ready())
83                  writer.write(reader.read());
84              writer.flush();
85              content = writer.toString();
86          }
87          catch (IllegalArgumentException e)
88          {
89              throw new MessagingException("Encoding = \"" + encoding + "\"", e);
90          }
91          catch (IOException e)
92          {
93              throw new MessagingException(
94                      "Exception obtaining content from DataSource", e);
95          }
96          finally
97          {
98              try
99              {
100                 writer.close();
101             }
102             catch (IOException e1)
103             {
104                 // No-op
105             }
106         }
107         return content;
108     }
109 
110     /***
111      * @see javax.activation.DataContentHandler#writeTo(java.lang.Object,
112      *      java.lang.String, java.io.OutputStream)
113      */
114     public void writeTo(Object aPart, String aMimeType, OutputStream aStream)
115             throws IOException
116     {
117         if (!(aPart instanceof String))
118             throw new IOException("Type \"" + aPart.getClass().getName()
119                     + "\" is not supported.");
120 
121         String encoding = getCharacterSet(getDataFlavor().getMimeType());
122         Writer writer = null;
123         try
124         {
125             writer = new BufferedWriter(new OutputStreamWriter(aStream,
126                     encoding), 2048);
127         }
128         catch (IllegalArgumentException e)
129         {
130             throw new UnsupportedEncodingException(encoding);
131         }
132         writer.write((String) aPart);
133         writer.flush();
134     }
135 
136     protected String getCharacterSet(String aType)
137     {
138         String characterSet = null;
139         try
140         {
141             characterSet = new ContentType(aType).getParameter("charset");
142         }
143         catch (ParseException e)
144         {
145             // no-op
146         }
147         finally
148         {
149             if (null == characterSet)
150                 characterSet = "us-ascii";
151         }
152         return MimeUtility.javaCharset(characterSet);
153     }
154 
155 }