KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > handlers > message_rfc822


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)message_rfc822.java 1.8 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27 package com.sun.mail.handlers;
28
29 import java.io.*;
30 import java.util.Properties JavaDoc;
31 import java.awt.datatransfer.DataFlavor JavaDoc;
32 import javax.activation.*;
33 import javax.mail.*;
34 import javax.mail.internet.*;
35
36
37 /**
38  * @version 1.8, 05/08/29
39  * @author Christopher Cotton
40  */

41
42
43 public class message_rfc822 implements DataContentHandler {
44
45     ActivationDataFlavor ourDataFlavor = new ActivationDataFlavor(
46     javax.mail.Message JavaDoc.class,
47     "message/rfc822",
48     "Message");
49
50     /**
51      * return the DataFlavors for this <code>DataContentHandler</code>
52      * @return The DataFlavors.
53      */

54     public DataFlavor JavaDoc[] getTransferDataFlavors() {
55     return new DataFlavor JavaDoc[] { ourDataFlavor };
56     }
57
58     /**
59      * return the Transfer Data of type DataFlavor from InputStream
60      * @param df The DataFlavor.
61      * @param ins The InputStream corresponding to the data.
62      * @return a Message object
63      */

64     public Object JavaDoc getTransferData(DataFlavor JavaDoc df, DataSource ds)
65                 throws IOException {
66     // make sure we can handle this DataFlavor
67
if (ourDataFlavor.equals(df))
68         return getContent(ds);
69     else
70         return null;
71     }
72     
73     /**
74      * Return the content.
75      */

76     public Object JavaDoc getContent(DataSource ds) throws IOException {
77     // create a new MimeMessage
78
try {
79         Session session;
80         if (ds instanceof MessageAware) {
81         MessageContext mc = ((MessageAware)ds).getMessageContext();
82         session = mc.getSession();
83         } else {
84         // Hopefully a rare case. Also hopefully the application
85
// has created a default Session that can just be returned
86
// here. If not, the one we create here is better than
87
// nothing, but overall not a really good answer.
88
session = Session.getDefaultInstance(new Properties JavaDoc(), null);
89         }
90         return new MimeMessage(session, ds.getInputStream());
91     } catch (MessagingException me) {
92         throw new IOException("Exception creating MimeMessage in " +
93             "message/rfc822 DataContentHandler: " + me.toString());
94     }
95     }
96     
97     /**
98      * construct an object from a byte stream
99      * (similar semantically to previous method, we are deciding
100      * which one to support)
101      */

102     public void writeTo(Object JavaDoc obj, String JavaDoc mimeType, OutputStream os)
103             throws IOException {
104     // if the object is a message, we know how to write that out
105
if (obj instanceof Message) {
106         Message m = (Message)obj;
107         try {
108         m.writeTo(os);
109         } catch (MessagingException me) {
110         throw new IOException(me.toString());
111         }
112         
113     } else {
114         throw new IOException("unsupported object");
115     }
116     }
117 }
118
Popular Tags