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.geronimo.mail.handlers;
21  
22  import java.awt.datatransfer.DataFlavor;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.io.OutputStream;
28  import java.io.OutputStreamWriter;
29  import java.io.StringWriter;
30  import java.io.UnsupportedEncodingException;
31  
32  import javax.activation.ActivationDataFlavor;
33  import javax.activation.DataContentHandler;
34  import javax.activation.DataSource;
35  import javax.mail.internet.ContentType;
36  import javax.mail.internet.MimeUtility;
37  import javax.mail.internet.ParseException;
38  
39  public class TextHandler implements DataContentHandler {
40      /**
41       * Field dataFlavor
42       */
43      ActivationDataFlavor dataFlavor;
44  
45      public TextHandler(){
46          dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
47      }
48  
49      /**
50       * Constructor TextHandler
51       *
52       * @param dataFlavor
53       */
54      public TextHandler(ActivationDataFlavor dataFlavor) {
55          this.dataFlavor = dataFlavor;
56      }
57  
58      /**
59       * Method getDF
60       *
61       * @return dataflavor
62       */
63      protected ActivationDataFlavor getDF() {
64          return dataFlavor;
65      }
66  
67      /**
68       * Method getTransferDataFlavors
69       *
70       * @return dataflavors
71       */
72      public DataFlavor[] getTransferDataFlavors() {
73          return (new DataFlavor[]{dataFlavor});
74      }
75  
76      /**
77       * Method getTransferData
78       *
79       * @param dataflavor
80       * @param datasource
81       * @return
82       * @throws IOException
83       */
84      public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
85              throws IOException {
86          if (getDF().equals(dataflavor)) {
87              return getContent(datasource);
88          }
89          return null;
90      }
91  
92      /**
93       * Method getContent
94       *
95       * @param datasource
96       * @return
97       * @throws IOException
98       */
99      public Object getContent(DataSource datasource) throws IOException {
100         InputStream is = datasource.getInputStream(); 
101         ByteArrayOutputStream os = new ByteArrayOutputStream(); 
102         
103         int count;  
104         byte[] buffer = new byte[1000]; 
105             
106         try {
107             while ((count = is.read(buffer, 0, buffer.length)) > 0) {
108                 os.write(buffer, 0, count); 
109             }
110         } finally {
111             is.close(); 
112         }
113         try {   
114             return os.toString(getCharSet(datasource.getContentType())); 
115         } catch (ParseException e) {
116             throw new UnsupportedEncodingException(e.getMessage()); 
117         }
118     }
119 
120     /**
121      * Method writeTo
122      *
123      * @param object
124      * @param s
125      * @param outputstream
126      * @throws IOException
127      */
128     public void writeTo(Object object, String s, OutputStream outputstream)
129             throws IOException {
130         OutputStreamWriter os;
131         try {
132             String charset = getCharSet(s);
133             os = new OutputStreamWriter(outputstream, charset);
134         } catch (Exception ex) {
135             throw new UnsupportedEncodingException(ex.toString());
136         }
137         String content = (String) object;
138         os.write(content, 0, content.length());
139         os.flush();
140     }
141 
142     /**
143      * get the character set from content type
144      * @param contentType
145      * @return
146      * @throws ParseException
147      */
148     protected String getCharSet(String contentType) throws ParseException {
149         ContentType type = new ContentType(contentType);
150         String charset = type.getParameter("charset");
151         if (charset == null) {
152             charset = "us-ascii";
153         }
154         return MimeUtility.javaCharset(charset);
155     }
156 }