KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestAttachmentSerialization


1 /*
2  * Copyright 2001-2004 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
17 package test.saaj;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.components.logger.LogFactory;
21 import org.apache.commons.logging.Log;
22
23 import javax.activation.DataHandler JavaDoc;
24 import javax.activation.FileDataSource JavaDoc;
25 import javax.xml.soap.AttachmentPart JavaDoc;
26 import javax.xml.soap.MessageFactory JavaDoc;
27 import javax.xml.soap.MimeHeaders JavaDoc;
28 import javax.xml.soap.SOAPBody JavaDoc;
29 import javax.xml.soap.SOAPElement JavaDoc;
30 import javax.xml.soap.SOAPEnvelope JavaDoc;
31 import javax.xml.soap.SOAPHeader JavaDoc;
32 import javax.xml.soap.SOAPMessage JavaDoc;
33 import javax.xml.soap.SOAPPart JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39
40
41 /** Test the attachments load/save sample code.
42  */

43 public class TestAttachmentSerialization extends TestCase {
44     static Log log = LogFactory.getLog(TestAttachmentSerialization.class.getName());
45
46     public TestAttachmentSerialization(String JavaDoc name) {
47         super(name);
48     }
49
50     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
51         TestAttachmentSerialization tester = new TestAttachmentSerialization("tester");
52         tester.testAttachments();
53     }
54
55     public void testAttachments() throws Exception JavaDoc {
56         try {
57             ByteArrayOutputStream JavaDoc bais = new ByteArrayOutputStream JavaDoc();
58             int count1 = saveMsgWithAttachments(bais);
59             int count2 = loadMsgWithAttachments(new ByteArrayInputStream JavaDoc(bais.toByteArray()));
60             assertEquals(count1, count2);
61         } catch (Exception JavaDoc e) {
62             e.printStackTrace();
63             throw new Exception JavaDoc("Fault returned from test: " + e);
64         }
65     }
66
67     public static final String JavaDoc MIME_MULTIPART_RELATED = "multipart/related";
68     public static final String JavaDoc MIME_APPLICATION_DIME = "application/dime";
69     public static final String JavaDoc NS_PREFIX = "jaxmtst";
70     public static final String JavaDoc NS_URI = "http://www.jcommerce.net/soap/jaxm/TestJaxm";
71
72     public int saveMsgWithAttachments(OutputStream JavaDoc os) throws Exception JavaDoc {
73         MessageFactory JavaDoc mf = MessageFactory.newInstance();
74         SOAPMessage JavaDoc msg = mf.createMessage();
75
76         SOAPPart JavaDoc sp = msg.getSOAPPart();
77         SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
78         SOAPHeader JavaDoc header = envelope.getHeader();
79         SOAPBody JavaDoc body = envelope.getBody();
80
81         SOAPElement JavaDoc el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI));
82         SOAPElement JavaDoc el2 = el.addChildElement("field4b", NS_PREFIX);
83         SOAPElement JavaDoc el3 = el2.addTextNode("field4value");
84
85         el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI));
86         el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
87         el2.addTextNode("bodyvalue3a");
88         el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
89         el2.addTextNode("bodyvalue3b");
90         el2 = el.addChildElement("datefield", NS_PREFIX);
91
92         AttachmentPart JavaDoc ap = msg.createAttachmentPart();
93         ap.setContent("some attachment text...", "text/plain");
94         msg.addAttachmentPart(ap);
95
96         String JavaDoc jpgfilename = "docs/images/axis.jpg";
97         File JavaDoc myfile = new File JavaDoc(jpgfilename);
98         FileDataSource JavaDoc fds = new FileDataSource JavaDoc(myfile);
99         DataHandler JavaDoc dh = new DataHandler JavaDoc(fds);
100         AttachmentPart JavaDoc ap2 = msg.createAttachmentPart(dh);
101         ap2.setContentType("image/jpg");
102         msg.addAttachmentPart(ap2);
103
104         // Test for Bug #17664
105
if(msg.saveRequired()) {
106             msg.saveChanges();
107         }
108         MimeHeaders JavaDoc headers = msg.getMimeHeaders();
109         assertTrue(headers != null);
110         String JavaDoc [] contentType = headers.getHeader("Content-Type");
111         assertTrue(contentType != null);
112         
113         msg.writeTo(os);
114         os.flush();
115         return msg.countAttachments();
116     }
117
118     public int loadMsgWithAttachments(InputStream JavaDoc is) throws Exception JavaDoc {
119         MimeHeaders JavaDoc headers = new MimeHeaders JavaDoc();
120         headers.setHeader("Content-Type", MIME_MULTIPART_RELATED);
121         MessageFactory JavaDoc mf = MessageFactory.newInstance();
122         SOAPMessage JavaDoc msg = mf.createMessage(headers, is);
123         SOAPPart JavaDoc sp = msg.getSOAPPart();
124         SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
125         assertTrue(sp != null);
126         assertTrue(envelope != null);
127         return msg.countAttachments();
128     }
129 }
130
131
132
Popular Tags