View Javadoc

1   /*
2    * Copyright 2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.geronimo.mail.handlers;
17  
18  import javax.activation.ActivationDataFlavor;
19  import javax.activation.DataContentHandler;
20  import javax.activation.DataSource;
21  import javax.mail.internet.ContentType;
22  import javax.mail.Message;
23  import javax.mail.MessageAware;
24  import javax.mail.MessageContext;
25  import javax.mail.MessagingException;
26  import javax.mail.internet.MimeMessage;
27  import javax.mail.internet.MimeUtility;
28  import javax.mail.internet.ParseException;
29  import java.awt.datatransfer.DataFlavor;
30  import java.io.IOException;
31  import java.io.InputStreamReader;
32  import java.io.OutputStream;
33  import java.io.OutputStreamWriter;
34  import java.io.StringWriter;
35  import java.io.UnsupportedEncodingException;
36  
37  public class MessageHandler implements DataContentHandler {
38      /**
39       * Field dataFlavor
40       */
41      ActivationDataFlavor dataFlavor;
42  
43      public MessageHandler(){
44          dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text");
45      }
46  
47  
48      /**
49       * Method getDF
50       *
51       * @return dataflavor
52       */
53      protected ActivationDataFlavor getDF() {
54          return dataFlavor;
55      }
56  
57      /**
58       * Method getTransferDataFlavors
59       *
60       * @return dataflavors
61       */
62      public DataFlavor[] getTransferDataFlavors() {
63          return (new DataFlavor[]{dataFlavor});
64      }
65  
66      /**
67       * Method getTransferData
68       *
69       * @param dataflavor
70       * @param datasource
71       * @return
72       * @throws IOException
73       */
74      public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
75              throws IOException {
76          if (getDF().equals(dataflavor)) {
77              return getContent(datasource);
78          }
79          return null;
80      }
81  
82      /**
83       * Method getContent
84       *
85       * @param datasource
86       * @return
87       * @throws IOException
88       */
89      public Object getContent(DataSource datasource) throws IOException {
90  
91          try {
92              // if this is a proper message, it implements the MessageAware interface.  We need this to
93              // get the associated session.
94              if (datasource instanceof MessageAware) {
95                  MessageContext context = ((MessageAware)datasource).getMessageContext();
96                  // construct a mime message instance from the stream, associating it with the
97                  // data source session.
98                  return new MimeMessage(context.getSession(), datasource.getInputStream());
99              }
100         } catch (MessagingException e) {
101             // we need to transform any exceptions into an IOException.
102             throw new IOException("Exception writing MimeMultipart: " + e.toString());
103         }
104         return null;
105     }
106 
107     /**
108      * Method writeTo
109      *
110      * @param object
111      * @param s
112      * @param outputstream
113      * @throws IOException
114      */
115     public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
116         // proper message type?
117         if (object instanceof Message) {
118             try {
119                 ((Message)object).writeTo(outputstream);
120             } catch (MessagingException e) {
121                 throw new IOException("Error parsing message: " + e.toString());
122             }
123         }
124     }
125 }
126