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