KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Message.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 java.util.Properties JavaDoc;
26 import javax.activation.*;
27 import javax.mail.MessageAware JavaDoc;
28 import javax.mail.MessageContext JavaDoc;
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Session JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32
33 /**
34  * A JAF data content handler for the message/* family of MIME content
35  * types.
36  */

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

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

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

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

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

92   public Object JavaDoc getContent(DataSource source)
93     throws IOException
94   {
95     try
96     {
97       Session JavaDoc session = null;
98       if (source instanceof MessageAware JavaDoc)
99       {
100         MessageAware JavaDoc ma = (MessageAware JavaDoc)source;
101         MessageContext JavaDoc context = ma.getMessageContext();
102         session = context.getSession();
103       }
104       else
105       {
106         Properties JavaDoc props = null;
107         /* Do we want to pass the system properties in? */
108         session = Session.getDefaultInstance(props, null);
109       }
110       InputStream in = source.getInputStream();
111       return new MimeMessage JavaDoc(session, in);
112     }
113     catch (MessagingException JavaDoc e)
114     {
115       e.printStackTrace();
116     }
117     catch (IOException e)
118     {
119       e.printStackTrace();
120     }
121     return null;
122   }
123
124   /**
125    * Convert the object to a byte stream of the specified MIME type and
126    * write it to the output stream.
127    * @param object the object to be converted
128    * @param mimeType the requested MIME content type to write as
129    * @param out the output stream into which to write the converted object
130    */

131   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream out)
132     throws IOException
133   {
134     if (object instanceof javax.mail.Message JavaDoc)
135     {
136       try
137       {
138         ((javax.mail.Message JavaDoc)object).writeTo(out);
139       }
140       catch (MessagingException JavaDoc e)
141       {
142         /* not brilliant as we lose any associated exception */
143         throw new IOException(e.getMessage());
144       }
145     }
146     else
147       throw new UnsupportedDataTypeException();
148   }
149
150 }
151
Popular Tags