KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > MIMEOutputUtils


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om;
17
18 import javax.activation.DataHandler JavaDoc;
19 import javax.mail.MessagingException JavaDoc;
20 import javax.mail.internet.ContentType JavaDoc;
21 import javax.mail.internet.MimeBodyPart JavaDoc;
22 import javax.xml.stream.XMLStreamException;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:thilina@opensource.lk">Thilina Gunarathne </a>
30  */

31 public class MIMEOutputUtils {
32     
33     private static byte[] CRLF = { 13, 10 };
34     
35     static String JavaDoc SOAP_PART_CONTENT_ID = "<http://apache.org/soappart>";
36     
37     public static void complete(OutputStream JavaDoc outStream,
38             OutputStream JavaDoc bufferedSoapOutStream, LinkedList JavaDoc binaryNodeList,
39             String JavaDoc boundary) throws XMLStreamException {
40         try {
41             startWritingMime(outStream, boundary);
42             
43             DataHandler JavaDoc dh = new DataHandler JavaDoc(bufferedSoapOutStream.toString(),
44             "text/xml");
45             MimeBodyPart JavaDoc rootMimeBodyPart = new MimeBodyPart JavaDoc();
46             rootMimeBodyPart.setDataHandler(dh);
47             ContentType JavaDoc partContentType = new ContentType JavaDoc("application/xop+xml");
48             partContentType.setParameter("charset","UTF-8");
49             partContentType.setParameter("type","application/soap+xml");
50             rootMimeBodyPart.addHeader("Content-Type",partContentType.toString() );
51             rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "8bit");
52             rootMimeBodyPart.addHeader("Content-ID", SOAP_PART_CONTENT_ID);
53             
54             writeBodyPart(outStream, rootMimeBodyPart, boundary);
55             
56             Iterator JavaDoc binaryNodeIterator = binaryNodeList.iterator();
57             while (binaryNodeIterator.hasNext()) {
58                 OMText binaryNode = (OMText) binaryNodeIterator.next();
59                 writeBodyPart(outStream, createMimeBodyPart(binaryNode),
60                         boundary);
61             }
62             finishWritingMime(outStream);
63         } catch (IOException JavaDoc e) {
64             throw new OMException("Problem with the OutputStream.",e);
65         } catch (MessagingException JavaDoc e) {
66             throw new OMException("Problem writing Mime Parts." ,e);
67         }
68     }
69     
70     protected static MimeBodyPart JavaDoc createMimeBodyPart(OMText node)
71     throws MessagingException JavaDoc {
72         MimeBodyPart JavaDoc mimeBodyPart = new MimeBodyPart JavaDoc();
73         mimeBodyPart.setDataHandler(node.getDataHandler());
74         mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
75         mimeBodyPart.addHeader("Content-ID", "<" + node.getContentID() + ">");
76         return mimeBodyPart;
77         
78     }
79     
80     /**
81      * @throws IOException
82      * This will write the boundary to output Stream
83      */

84     protected static void writeMimeBoundary(OutputStream JavaDoc outStream,
85             String JavaDoc boundary) throws IOException JavaDoc {
86         outStream.write(new byte[] { 45, 45 });
87         outStream.write(boundary.getBytes());
88     }
89     
90     /**
91      * @throws IOException
92      * This will write the boundary with CRLF
93      */

94     protected static void startWritingMime(OutputStream JavaDoc outStream, String JavaDoc boundary)
95     throws IOException JavaDoc {
96         writeMimeBoundary(outStream, boundary);
97         //outStream.write(CRLF);
98
}
99     
100     /**
101      * this will write a CRLF for the earlier boudary then the BodyPart data
102      * with headers followed by boundary. Writes only the boundary. No more
103      * CRLF's are wriiting after that.
104      *
105      * @throws IOException
106      * @throws MessagingException
107      */

108     protected static void writeBodyPart(OutputStream JavaDoc outStream,
109             MimeBodyPart JavaDoc part, String JavaDoc boundary) throws IOException JavaDoc,
110             MessagingException JavaDoc {
111         outStream.write(CRLF);
112         part.writeTo(outStream);
113         outStream.write(CRLF);
114         writeMimeBoundary(outStream, boundary);
115     }
116     
117     /**
118      * @throws IOException
119      * This will write "--" to the end of last boundary
120      */

121     protected static void finishWritingMime(OutputStream JavaDoc outStream)
122     throws IOException JavaDoc {
123         outStream.write(new byte[] { 45, 45 });
124     }
125     
126     public static String JavaDoc getContentTypeForMime(String JavaDoc boundary) {
127         ContentType JavaDoc contentType = new ContentType JavaDoc();
128         contentType.setPrimaryType("multipart");
129         contentType.setSubType("related");
130         contentType.setParameter("boundary", boundary);
131         contentType.setParameter("start", MIMEOutputUtils.SOAP_PART_CONTENT_ID);
132         contentType.setParameter("type", "application/xop+xml");
133         //TODO theres something called action that can be set with
134
// following. May be SOAPAction. Better check.
135
contentType.setParameter("start-info", "application/soap+xml");
136         return contentType.toString();
137     }
138     
139 }
Popular Tags