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     /**
122      * Write an object of "our" type out to the provided 
123      * output stream.  The content type might modify the 
124      * result based on the content type parameters. 
125      * 
126      * @param object The object to write.
127      * @param contentType
128      *               The content mime type, including parameters.
129      * @param outputstream
130      *               The target output stream.
131      * 
132      * @throws IOException
133      */
134     public void writeTo(Object object, String contentType, OutputStream outputstream)
135             throws IOException {
136         OutputStreamWriter os;
137         try {
138             String charset = getCharSet(contentType);
139             os = new OutputStreamWriter(outputstream, charset);
140         } catch (Exception ex) {
141             throw new UnsupportedEncodingException(ex.toString());
142         }
143         String content = (String) object;
144         os.write(content, 0, content.length());
145         os.flush();
146     }
147 
148     /**
149      * get the character set from content type
150      * @param contentType
151      * @return
152      * @throws ParseException
153      */
154     protected String getCharSet(String contentType) throws ParseException {
155         ContentType type = new ContentType(contentType);
156         String charset = type.getParameter("charset");
157         if (charset == null) {
158             charset = "us-ascii";
159         }
160         return MimeUtility.javaCharset(charset);
161     }
162 }