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  
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   * <p>Data Content Handler for...</p>
42   * <dl>
43   * <dt>MIME type name</dt><dd>message</dd>
44   * <dt>MIME subtype name</dt><dd>disposition-notification</dd>
45   * </dl>
46   */
47  public class message_disposition_notification
48          extends
49              AbstractDataContentHandler
50  {
51  
52      /**
53       * Default Constructor.
54       */
55      public message_disposition_notification()
56      {
57          super();
58      }
59  
60      /**
61       * @see org.apache.mailet.base.mail.AbstractDataContentHandler#computeDataFlavor()
62       */
63      protected ActivationDataFlavor computeDataFlavor()
64      {
65          return new ActivationDataFlavor(String.class,
66                  "message/disposition-notification", "Message String");
67      }
68  
69      /**
70       * @see org.apache.mailet.base.mail.AbstractDataContentHandler#computeContent(javax.activation.DataSource)
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                 // No-op
106             }
107         }
108         return content;
109     }
110 
111     /**
112      * @see javax.activation.DataContentHandler#writeTo(java.lang.Object,
113      *      java.lang.String, java.io.OutputStream)
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             // no-op
147         }
148         finally
149         {
150             if (null == characterSet)
151                 characterSet = "us-ascii";
152         }
153         return MimeUtility.javaCharset(characterSet);
154     }
155 
156 }