View Javadoc

1   /**
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You 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 implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.javamail.handlers;
18  
19  import java.awt.datatransfer.DataFlavor;
20  import java.awt.datatransfer.UnsupportedFlavorException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.Reader;
27  import java.io.UnsupportedEncodingException;
28  
29  import javax.activation.DataContentHandler;
30  import javax.activation.DataSource;
31  
32  import javax.mail.internet.ContentType; 
33  import javax.mail.internet.MimeUtility;
34  
35  /**
36   * @version $Rev: 669902 $ $Date: 2008-06-20 10:04:41 -0400 (Fri, 20 Jun 2008) $
37   */
38  public class AbstractTextHandler implements DataContentHandler {
39      private final DataFlavor flavour;
40  
41      public AbstractTextHandler(DataFlavor flavour) {
42          this.flavour = flavour;
43      }
44  
45      public DataFlavor[] getTransferDataFlavors() {
46          return new DataFlavor[] {flavour};
47      }
48  
49      public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
50          return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
51      }
52  
53      /**
54       * Read the content from the DataSource and transform 
55       * it into a text object (String). 
56       * 
57       * @param ds     The source DataSource.
58       * 
59       * @return The content object. 
60       * @exception IOException
61       */
62      public Object getContent(DataSource ds) throws IOException {
63          InputStream is = ds.getInputStream(); 
64          InputStreamReader reader;
65          // process any encoding to make sure the chars get transformed into the 
66          // correct byte types. 
67          try {
68              String charset = getCharSet(ds.getContentType());
69              reader = new InputStreamReader(is, charset);
70          } catch (Exception ex) {
71              throw new UnsupportedEncodingException(ex.toString());
72          }
73          StringBuffer result = new StringBuffer(1024);
74          char[] buffer = new char[32768];
75          int count;
76          while ((count = reader.read(buffer)) > 0) {
77              result.append(buffer, 0, count);
78          }
79          return result.toString();
80      }
81  
82      
83      /**
84       * Write an object of "our" type out to the provided 
85       * output stream.  The content type might modify the 
86       * result based on the content type parameters. 
87       * 
88       * @param object The object to write.
89       * @param contentType
90       *               The content mime type, including parameters.
91       * @param outputstream
92       *               The target output stream.
93       * 
94       * @throws IOException
95       */
96      public void writeTo(Object o, String contentType, OutputStream outputstream) throws IOException {
97          String s;
98          if (o instanceof String) {
99              s = (String) o;
100         } else if (o != null) {
101             s = o.toString();
102         } else {
103             return;
104         }
105         // process any encoding to make sure the chars get transformed into the 
106         // correct byte types. 
107         OutputStreamWriter writer;
108         try {
109             String charset = getCharSet(contentType);
110             writer = new OutputStreamWriter(outputstream, charset);
111         } catch (Exception ex) {
112             ex.printStackTrace(); 
113             throw new UnsupportedEncodingException(ex.toString());
114         }
115         writer.write(s);
116         writer.flush();
117     }
118     
119 
120     /**
121      * get the character set from content type
122      * @param contentType
123      * @return
124      * @throws ParseException
125      */
126     protected String getCharSet(String contentType) throws Exception {
127         ContentType type = new ContentType(contentType);
128         String charset = type.getParameter("charset");
129         if (charset == null) {
130             charset = "us-ascii";
131         }
132         return MimeUtility.javaCharset(charset);
133     }
134 }