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 javax.activation.ActivationDataFlavor;
23  import javax.activation.DataContentHandler;
24  import javax.activation.DataSource;
25  import javax.mail.internet.ContentType;
26  import javax.mail.Message;
27  import javax.mail.MessageAware;
28  import javax.mail.MessageContext;
29  import javax.mail.MessagingException;
30  import javax.mail.internet.MimeMessage;
31  import javax.mail.internet.MimeUtility;
32  import javax.mail.internet.ParseException;
33  import java.awt.datatransfer.DataFlavor;
34  import java.io.IOException;
35  import java.io.InputStreamReader;
36  import java.io.OutputStream;
37  import java.io.OutputStreamWriter;
38  import java.io.StringWriter;
39  import java.io.UnsupportedEncodingException;
40  
41  public class MessageHandler implements DataContentHandler {
42      /**
43       * Field dataFlavor
44       */
45      ActivationDataFlavor dataFlavor;
46  
47      public MessageHandler(){
48          dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text");
49      }
50  
51  
52      /**
53       * Method getDF
54       *
55       * @return dataflavor
56       */
57      protected ActivationDataFlavor getDF() {
58          return dataFlavor;
59      }
60  
61      /**
62       * Method getTransferDataFlavors
63       *
64       * @return dataflavors
65       */
66      public DataFlavor[] getTransferDataFlavors() {
67          return (new DataFlavor[]{dataFlavor});
68      }
69  
70      /**
71       * Method getTransferData
72       *
73       * @param dataflavor
74       * @param datasource
75       * @return
76       * @throws IOException
77       */
78      public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
79              throws IOException {
80          if (getDF().equals(dataflavor)) {
81              return getContent(datasource);
82          }
83          return null;
84      }
85  
86      /**
87       * Method getContent
88       *
89       * @param datasource
90       * @return
91       * @throws IOException
92       */
93      public Object getContent(DataSource datasource) throws IOException {
94  
95          try {
96              // if this is a proper message, it implements the MessageAware interface.  We need this to
97              // get the associated session.
98              if (datasource instanceof MessageAware) {
99                  MessageContext context = ((MessageAware)datasource).getMessageContext();
100                 // construct a mime message instance from the stream, associating it with the
101                 // data source session.
102                 return new MimeMessage(context.getSession(), datasource.getInputStream());
103             }
104         } catch (MessagingException e) {
105             // we need to transform any exceptions into an IOException.
106             throw new IOException("Exception writing MimeMultipart: " + e.toString());
107         }
108         return null;
109     }
110 
111     /**
112      * Method writeTo
113      *
114      * @param object
115      * @param s
116      * @param outputstream
117      * @throws IOException
118      */
119     public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
120         // proper message type?
121         if (object instanceof Message) {
122             try {
123                 ((Message)object).writeTo(outputstream);
124             } catch (MessagingException e) {
125                 throw new IOException("Error parsing message: " + e.toString());
126             }
127         }
128     }
129 }
130