KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.apache.axis2.om;
17
18 import junit.framework.TestCase;
19 import org.apache.axis2.attachments.ByteArrayDataSource;
20 import org.apache.axis2.soap.SOAPFactory;
21
22 import javax.activation.DataHandler JavaDoc;
23 import javax.mail.MessagingException JavaDoc;
24 import javax.mail.internet.MimeBodyPart JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26 import javax.mail.internet.MimeMultipart JavaDoc;
27 import javax.mail.internet.MimePartDataSource JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * @author Thilina
35  */

36 public class MIMEOutputUtilsTest extends TestCase {
37     byte[] buffer;
38     byte[] byteArray = new byte[] { 13, 56, 65, 32, 12, 12, 7, -3, -2, -1,
39             98 };
40
41     protected void setUp() throws Exception JavaDoc {
42         super.setUp();
43         SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
44         ByteArrayOutputStream JavaDoc outStream;
45         String JavaDoc boundary = "----TemporaryBoundary";
46
47         String JavaDoc contentType = MIMEOutputUtils.getContentTypeForMime(boundary);
48         DataHandler JavaDoc dataHandler;
49         dataHandler = new DataHandler JavaDoc(new ByteArrayDataSource(byteArray));
50         OMText textData = factory.createText(dataHandler, true);
51         
52         DataHandler JavaDoc dataHandler2 = new DataHandler JavaDoc(
53                 "Apache Software Foundation", "text/plain");
54         OMText text = factory.createText(dataHandler2, true);
55         outStream = new ByteArrayOutputStream JavaDoc();
56         outStream.write(("Content-Type: "+contentType).getBytes());
57         outStream.write(new byte[]{ 13, 10 });
58         //outStream.write("\n\n".getBytes());
59
MIMEOutputUtils.startWritingMime(outStream, boundary);
60         MimeBodyPart JavaDoc part1 = MIMEOutputUtils.createMimeBodyPart(textData);
61         MIMEOutputUtils.writeBodyPart(outStream, part1, boundary);
62         MimeBodyPart JavaDoc part2 = MIMEOutputUtils.createMimeBodyPart(text);
63         MIMEOutputUtils.writeBodyPart(outStream, part2, boundary);
64         MIMEOutputUtils.finishWritingMime(outStream);
65         buffer = outStream.toByteArray();
66         System.out.println(new String JavaDoc(buffer));
67         System.out.println("Axis2");
68     }
69
70     public void testMIMEWriting() throws IOException JavaDoc, MessagingException JavaDoc {
71         ByteArrayInputStream JavaDoc inStream = new ByteArrayInputStream JavaDoc(buffer);
72         Properties JavaDoc props = new Properties JavaDoc();
73         javax.mail.Session JavaDoc session = javax.mail.Session
74                 .getInstance(props, null);
75         MimeMessage JavaDoc mimeMessage = new MimeMessage JavaDoc(session, inStream);
76         DataHandler JavaDoc dh = mimeMessage.getDataHandler();
77         MimeMultipart JavaDoc multiPart = new MimeMultipart JavaDoc((MimePartDataSource JavaDoc) dh
78                 .getDataSource());
79         MimeBodyPart JavaDoc mimeBodyPart2 = (MimeBodyPart JavaDoc) multiPart.getBodyPart(0);
80         Object JavaDoc object = mimeBodyPart2.getContent();
81         MimeBodyPart JavaDoc mimeBodyPart1 = (MimeBodyPart JavaDoc) multiPart.getBodyPart(0);
82         
83     }
84
85 }
Popular Tags