KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > handler > Multipart


1 /*
2  * Multipart.java
3  * Copyright (C) 2002 dog <dog@dog.net.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package gnu.mail.handler;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
24 import java.io.*;
25 import javax.activation.*;
26 import javax.mail.MessagingException JavaDoc;
27 import javax.mail.internet.MimeMultipart JavaDoc;
28
29 /**
30  * A JAF data content handler for the multipart/* family of MIME content
31  * types.
32  * This provides the basic behaviour for any number of MimeMultipart-handling
33  * subtypes which simply need to override their default constructor to provide
34  * the correct MIME content-type and description.
35  */

36 public abstract class Multipart
37   implements DataContentHandler
38 {
39
40   /**
41    * Our favorite data flavor.
42    */

43   protected DataFlavor JavaDoc flavor;
44   
45   /**
46    * Constructor specifying the data flavor.
47    * @param mimeType the MIME content type
48    * @param description the description of the content type
49    */

50   protected Multipart(String JavaDoc mimeType, String JavaDoc description)
51   {
52     flavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart JavaDoc.class,
53         mimeType, description);
54   }
55
56   /**
57    * Returns an array of DataFlavor objects indicating the flavors the data
58    * can be provided in.
59    * @return the DataFlavors
60    */

61   public DataFlavor JavaDoc[] getTransferDataFlavors()
62   {
63     DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[1];
64     flavors[0] = flavor;
65     return flavors;
66   }
67
68   /**
69    * Returns an object which represents the data to be transferred.
70    * The class of the object returned is defined by the representation class
71    * of the flavor.
72    * @param flavor the data flavor representing the requested type
73    * @param source the data source representing the data to be converted
74    * @return the constructed object
75    */

76   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor, DataSource source)
77     throws UnsupportedFlavorException JavaDoc, IOException
78   {
79     if (this.flavor.equals(flavor))
80       return getContent(source);
81     return null;
82   }
83
84   /**
85    * Return an object representing the data in its most preferred form.
86    * Generally this will be the form described by the first data flavor
87    * returned by the <code>getTransferDataFlavors</code> method.
88    * @param source the data source representing the data to be converted
89    * @return a byte array
90    */

91   public Object JavaDoc getContent(DataSource source)
92     throws IOException
93   {
94     try
95     {
96       return new MimeMultipart JavaDoc(source);
97     }
98     catch (MessagingException JavaDoc e)
99     {
100       /* This loses any attached exception */
101       throw new IOException(e.getMessage());
102     }
103   }
104
105   /**
106    * Convert the object to a byte stream of the specified MIME type and
107    * write it to the output stream.
108    * @param object the object to be converted
109    * @param mimeType the requested MIME content type to write as
110    * @param out the output stream into which to write the converted object
111    */

112   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream out)
113     throws IOException
114   {
115     if (object instanceof MimeMultipart JavaDoc)
116     {
117       try
118       {
119         ((MimeMultipart JavaDoc)object).writeTo(out);
120       }
121       catch (MessagingException JavaDoc e)
122       {
123         /* This loses any attached exception */
124         throw new IOException(e.getMessage());
125       }
126     }
127     else
128       throw new UnsupportedDataTypeException();
129   }
130
131 }
132
Popular Tags