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 java.awt.datatransfer.DataFlavor;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  
29  import javax.mail.internet.MimeMultipart;
30  import javax.mail.internet.MimeMessage;
31  import javax.mail.MessagingException;
32  
33  public class MultipartHandler implements DataContentHandler {
34      /**
35       * Field dataFlavor
36       */
37      ActivationDataFlavor dataFlavor;
38  
39      public MultipartHandler(){
40          dataFlavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart.class, "multipart/mixed", "Multipart");
41      }
42  
43      /**
44       * Constructor TextHandler
45       *
46       * @param dataFlavor
47       */
48      public MultipartHandler(ActivationDataFlavor dataFlavor) {
49          this.dataFlavor = dataFlavor;
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          try {
95              return new MimeMultipart(datasource);
96          } catch (MessagingException e) {
97              // if there is a syntax error from the datasource parsing, the content is
98              // just null.
99              return null;
100         }
101     }
102 
103     /**
104      * Method writeTo
105      *
106      * @param object
107      * @param s
108      * @param outputstream
109      * @throws IOException
110      */
111     public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
112         // if this object is a MimeMultipart, then delegate to the part.
113         if (object instanceof MimeMultipart) {
114             try {
115                 ((MimeMultipart)object).writeTo(outputstream);
116             } catch (MessagingException e) {
117                 // we need to transform any exceptions into an IOException.
118                 throw new IOException("Exception writing MimeMultipart: " + e.toString());
119             }
120         }
121     }
122 }