KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > dsn > text_rfc822headers


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  * @(#)text_rfc822headers.java 1.3 06/02/27
24  *
25  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.dsn;
29
30 import java.io.*;
31 import java.awt.datatransfer.DataFlavor JavaDoc;
32 import javax.activation.*;
33 import javax.mail.*;
34 import javax.mail.internet.*;
35
36 /**
37  * DataContentHandler for text/rfc822-headers.
38  *
39  * @version 1.3, 06/02/27
40  */

41 public class text_rfc822headers implements DataContentHandler {
42     private static ActivationDataFlavor myDF = new ActivationDataFlavor(
43     MessageHeaders.class,
44     "text/rfc822-headers",
45     "RFC822 headers");
46     private static ActivationDataFlavor myDFs = new ActivationDataFlavor(
47     java.lang.String JavaDoc.class,
48     "text/rfc822-headers",
49     "RFC822 headers");
50
51     /**
52      * Return the DataFlavors for this <code>DataContentHandler</code>.
53      *
54      * @return The DataFlavors
55      */

56     public DataFlavor JavaDoc[] getTransferDataFlavors() {
57     return new DataFlavor JavaDoc[] { myDF, myDFs };
58     }
59
60     /**
61      * Return the Transfer Data of type DataFlavor from InputStream.
62      *
63      * @param df The DataFlavor
64      * @param ds The DataSource corresponding to the data
65      * @return String object
66      */

67     public Object JavaDoc getTransferData(DataFlavor JavaDoc df, DataSource ds)
68             throws IOException {
69     // use myDF.equals to be sure to get ActivationDataFlavor.equals,
70
// which properly ignores Content-Type parameters in comparison
71
if (myDF.equals(df))
72         return getContent(ds);
73     else if (myDFs.equals(df))
74         return getStringContent(ds);
75     else
76         return null;
77     }
78
79     public Object JavaDoc getContent(DataSource ds) throws IOException {
80     try {
81         return new MessageHeaders(ds.getInputStream());
82     } catch (MessagingException mex) {
83 //System.out.println("Exception creating MessageHeaders: " + mex);
84
throw new IOException("Exception creating MessageHeaders: " + mex);
85     }
86     }
87
88     private Object JavaDoc getStringContent(DataSource ds) throws IOException {
89     String JavaDoc enc = null;
90     InputStreamReader is = null;
91     
92     try {
93         enc = getCharset(ds.getContentType());
94         is = new InputStreamReader(ds.getInputStream(), enc);
95     } catch (IllegalArgumentException JavaDoc iex) {
96         /*
97          * An unknown charset of the form ISO-XXX-XXX will cause
98          * the JDK to throw an IllegalArgumentException. The
99          * JDK will attempt to create a classname using this string,
100          * but valid classnames must not contain the character '-',
101          * and this results in an IllegalArgumentException, rather than
102          * the expected UnsupportedEncodingException. Yikes.
103          */

104         throw new UnsupportedEncodingException(enc);
105     }
106
107     int pos = 0;
108     int count;
109     char buf[] = new char[1024];
110
111     while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
112         pos += count;
113         if (pos >= buf.length) {
114         int size = buf.length;
115         if (size < 256*1024)
116             size += size;
117         else
118             size += 256*1024;
119         char tbuf[] = new char[size];
120         System.arraycopy(buf, 0, tbuf, 0, pos);
121         buf = tbuf;
122         }
123     }
124     return new String JavaDoc(buf, 0, pos);
125     }
126     
127     /**
128      * Write the object to the output stream, using the specified MIME type.
129      */

130     public void writeTo(Object JavaDoc obj, String JavaDoc type, OutputStream os)
131             throws IOException {
132     if (obj instanceof MessageHeaders) {
133         MessageHeaders mh = (MessageHeaders)obj;
134         try {
135         mh.writeTo(os);
136         } catch (MessagingException mex) {
137         Exception JavaDoc ex = mex.getNextException();
138         if (ex instanceof IOException)
139             throw (IOException)ex;
140         else
141             throw new IOException("Exception writing headers: " + mex);
142         }
143         return;
144     }
145     if (!(obj instanceof String JavaDoc))
146         throw new IOException("\"" + myDFs.getMimeType() +
147         "\" DataContentHandler requires String object, " +
148         "was given object of type " + obj.getClass().toString());
149
150     String JavaDoc enc = null;
151     OutputStreamWriter osw = null;
152
153     try {
154         enc = getCharset(type);
155         osw = new OutputStreamWriter(os, enc);
156     } catch (IllegalArgumentException JavaDoc iex) {
157         /*
158          * An unknown charset of the form ISO-XXX-XXX will cause
159          * the JDK to throw an IllegalArgumentException. The
160          * JDK will attempt to create a classname using this string,
161          * but valid classnames must not contain the character '-',
162          * and this results in an IllegalArgumentException, rather than
163          * the expected UnsupportedEncodingException. Yikes.
164          */

165         throw new UnsupportedEncodingException(enc);
166     }
167
168     String JavaDoc s = (String JavaDoc)obj;
169     osw.write(s, 0, s.length());
170     osw.flush();
171     }
172
173     private String JavaDoc getCharset(String JavaDoc type) {
174     try {
175         ContentType ct = new ContentType(type);
176         String JavaDoc charset = ct.getParameter("charset");
177         if (charset == null)
178         // If the charset parameter is absent, use US-ASCII.
179
charset = "us-ascii";
180         return MimeUtility.javaCharset(charset);
181     } catch (Exception JavaDoc ex) {
182         return null;
183     }
184     }
185 }
186
Popular Tags