KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > MimePartDataSource


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  * @(#)MimePartDataSource.java 1.12 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import javax.mail.*;
31 import javax.activation.*;
32 import java.io.*;
33 import java.net.UnknownServiceException JavaDoc;
34
35 /**
36  * A utility class that implements a DataSource out of
37  * a MimePart. This class is primarily meant for service providers.
38  *
39  * @see javax.mail.internet.MimePart
40  * @see javax.activation.DataSource
41  * @author John Mani
42  */

43
44 public class MimePartDataSource implements DataSource, MessageAware {
45     /**
46      * The MimePart that provides the data for this DataSource.
47      *
48      * @since JavaMail 1.4
49      */

50     protected MimePart JavaDoc part;
51
52     private MessageContext context;
53
54     private static boolean ignoreMultipartEncoding = true;
55
56     static {
57     try {
58         String JavaDoc s = System.getProperty("mail.mime.ignoremultipartencoding");
59         // default to true
60
ignoreMultipartEncoding = s == null || !s.equalsIgnoreCase("false");
61     } catch (SecurityException JavaDoc sex) {
62         // ignore it
63
}
64     }
65
66     /**
67      * Constructor, that constructs a DataSource from a MimePart.
68      */

69     public MimePartDataSource(MimePart JavaDoc part) {
70     this.part = part;
71     }
72
73     /**
74      * Returns an input stream from this MimePart. <p>
75      *
76      * This method applies the appropriate transfer-decoding, based
77      * on the Content-Transfer-Encoding attribute of this MimePart.
78      * Thus the returned input stream is a decoded stream of bytes.<p>
79      *
80      * This implementation obtains the raw content from the Part
81      * using the <code>getContentStream()</code> method and decodes
82      * it using the <code>MimeUtility.decode()</code> method.
83      *
84      * @see javax.mail.internet.MimeMessage#getContentStream
85      * @see javax.mail.internet.MimeBodyPart#getContentStream
86      * @see javax.mail.internet.MimeUtility#decode
87      * @return decoded input stream
88      */

89     public InputStream getInputStream() throws IOException {
90     InputStream is;
91
92     try {
93         if (part instanceof MimeBodyPart JavaDoc)
94         is = ((MimeBodyPart JavaDoc)part).getContentStream();
95         else if (part instanceof MimeMessage JavaDoc)
96         is = ((MimeMessage JavaDoc)part).getContentStream();
97         else
98         throw new MessagingException("Unknown part");
99         
100         String JavaDoc encoding = restrictEncoding(part.getEncoding(), part);
101         if (encoding != null)
102         return MimeUtility.decode(is, encoding);
103         else
104         return is;
105     } catch (MessagingException mex) {
106         throw new IOException(mex.getMessage());
107     }
108     }
109
110     /**
111      * Restrict the encoding to values allowed for the
112      * Content-Type of the specified MimePart. Returns
113      * either the original encoding or null.
114      */

115     private static String JavaDoc restrictEncoding(String JavaDoc encoding, MimePart JavaDoc part)
116                 throws MessagingException {
117     if (!ignoreMultipartEncoding || encoding == null)
118         return encoding;
119
120     if (encoding.equalsIgnoreCase("7bit") ||
121         encoding.equalsIgnoreCase("8bit") ||
122         encoding.equalsIgnoreCase("binary"))
123         return encoding; // these encodings are always valid
124

125     String JavaDoc type = part.getContentType();
126     if (type == null)
127         return encoding;
128
129     try {
130         /*
131          * multipart and message types aren't allowed to have
132          * encodings except for the three mentioned above.
133          * If it's one of these types, ignore the encoding.
134          */

135         ContentType JavaDoc cType = new ContentType JavaDoc(type);
136         if (cType.match("multipart/*") || cType.match("message/*"))
137         return null;
138     } catch (ParseException JavaDoc pex) {
139         // ignore it
140
}
141     return encoding;
142     }
143
144
145     /**
146      * DataSource method to return an output stream. <p>
147      *
148      * This implementation throws the UnknownServiceException.
149      */

150     public OutputStream getOutputStream() throws IOException {
151     throw new UnknownServiceException JavaDoc();
152     }
153
154     /**
155      * Returns the content-type of this DataSource. <p>
156      *
157      * This implementation just invokes the <code>getContentType</code>
158      * method on the MimePart.
159      */

160     public String JavaDoc getContentType() {
161     try {
162         return part.getContentType();
163     } catch (MessagingException mex) {
164         return null;
165     }
166     }
167
168     /**
169      * DataSource method to return a name. <p>
170      *
171      * This implementation just returns an empty string.
172      */

173     public String JavaDoc getName() {
174     try {
175         if (part instanceof MimeBodyPart JavaDoc)
176         return ((MimeBodyPart JavaDoc)part).getFileName();
177     } catch (MessagingException mex) {
178         // ignore it
179
}
180     return "";
181     }
182
183     /**
184      * Return the <code>MessageContext</code> for the current part.
185      * @since JavaMail 1.1
186      */

187     public synchronized MessageContext getMessageContext() {
188     if (context == null)
189         context = new MessageContext(part);
190     return context;
191     }
192 }
193
Popular Tags