KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > attachment > AttachmentSAAJTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.attachment;
23
24 import junit.framework.Test;
25 import org.jboss.test.JBossTestCase;
26 import org.jboss.test.webservice.WebserviceTestBase;
27
28 import javax.activation.DataHandler JavaDoc;
29 import javax.mail.internet.MimeBodyPart JavaDoc;
30 import javax.mail.internet.MimeMultipart JavaDoc;
31 import javax.xml.soap.AttachmentPart JavaDoc;
32 import javax.xml.soap.MessageFactory JavaDoc;
33 import javax.xml.soap.Name JavaDoc;
34 import javax.xml.soap.SOAPBody JavaDoc;
35 import javax.xml.soap.SOAPBodyElement JavaDoc;
36 import javax.xml.soap.SOAPConnection JavaDoc;
37 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
38 import javax.xml.soap.SOAPElement JavaDoc;
39 import javax.xml.soap.SOAPEnvelope JavaDoc;
40 import javax.xml.soap.SOAPException JavaDoc;
41 import javax.xml.soap.SOAPMessage JavaDoc;
42 import javax.xml.soap.SOAPPart JavaDoc;
43 import java.awt.Image JavaDoc;
44 import java.awt.Toolkit JavaDoc;
45 import java.io.File JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.net.MalformedURLException JavaDoc;
48 import java.net.URL JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51 /**
52  * Test SOAP with Attachements (SwA) through the SAAJ layer.
53  * This tests the server side handling of Attachments.
54  * The JAXRPC client is not tested.
55  *
56  * [ 1039881 ] Cannot create multipart/mixed SOAP attachment
57  *
58  * @author Thomas.Diesler@jboss.org
59  * @since Nov 16, 2004
60  */

61 public class AttachmentSAAJTestCase extends WebserviceTestBase
62 {
63    private static final String JavaDoc NS_PREFIX = "ns1";
64    private static final String JavaDoc NS_URI = "http://org.jboss.webservice/attachment";
65    private static final String JavaDoc CID_MIMEPART = "<mimepart=123123@ws.jboss.org>";
66
67    public AttachmentSAAJTestCase(String JavaDoc name)
68    {
69       super(name);
70    }
71
72    /** Deploy the test ear */
73    public static Test suite() throws Exception JavaDoc
74    {
75       return getDeploySetup(AttachmentSAAJTestCase.class, "ws4ee-attachment.war");
76    }
77
78    /** Send a multipart message with a text/plain attachment part
79     */

80    public void testSendMimeImageGIF() throws Exception JavaDoc
81    {
82       String JavaDoc rpcMethodName = "sendMimeImageGIF";
83       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
84
85       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.gif").toURL();
86
87       // On Linux the X11 server must be installed properly to create images successfully.
88
// If the image cannot be created in the test VM, we assume it cannot be done on the
89
// server either, so we just skip the test
90
Image JavaDoc image = null;
91       try
92       {
93          image = Toolkit.getDefaultToolkit().createImage(url);
94       }
95       catch (Throwable JavaDoc th)
96       {
97          log.warn("Cannot create Image: " + th);
98       }
99
100       if (image != null)
101       {
102          addAttachmentPart(msg, "image/gif", new DataHandler JavaDoc(url));
103          //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
104
sendAndValidateMimeMessage(rpcMethodName, msg);
105       }
106    }
107
108    /** Send a multipart message with a text/plain attachment part
109     */

110    public void testSendMimeImageJPEG() throws Exception JavaDoc
111    {
112       String JavaDoc rpcMethodName = "sendMimeImageJPEG";
113       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
114
115       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.jpeg").toURL();
116
117       // On Linux the X11 server must be installed properly to create images successfully.
118
// If the image cannot be created in the test VM, we assume it cannot be done on the
119
// server either, so we just skip the test
120
Image JavaDoc image = null;
121       try
122       {
123          image = Toolkit.getDefaultToolkit().createImage(url);
124       }
125       catch (Throwable JavaDoc th)
126       {
127          log.warn("Cannot create Image: " + th);
128       }
129
130       if (image != null)
131       {
132          addAttachmentPart(msg, "image/jpeg", new DataHandler JavaDoc(url));
133          //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
134
sendAndValidateMimeMessage(rpcMethodName, msg);
135       }
136    }
137
138    /** Send a multipart message with a text/plain attachment part
139     */

140    public void testSendMimeTextPlain() throws Exception JavaDoc
141    {
142       String JavaDoc rpcMethodName = "sendMimeTextPlain";
143       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
144
145       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
146       addAttachmentPart(msg, "text/plain", new DataHandler JavaDoc(url));
147
148       //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
149

150       sendAndValidateMimeMessage(rpcMethodName, msg);
151    }
152
153    /** Send a multipart message with a text/plain attachment part
154     */

155    public void testSendMimeMultipart() throws Exception JavaDoc
156    {
157       String JavaDoc rpcMethodName = "sendMimeMultipart";
158       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
159
160       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
161       MimeMultipart JavaDoc multipart = new MimeMultipart JavaDoc("mixed");
162       MimeBodyPart JavaDoc bodyPart = new MimeBodyPart JavaDoc();
163       bodyPart.setDataHandler(new DataHandler JavaDoc(url));
164       String JavaDoc bpct = bodyPart.getContentType();
165       bodyPart.setHeader("Content-Type", bpct);
166
167       multipart.addBodyPart(bodyPart);
168       multipart.writeTo(new FileOutputStream JavaDoc(rpcMethodName + "_Multipart.txt"));
169
170       String JavaDoc contentType = multipart.getContentType();
171       AttachmentPart JavaDoc ap = msg.createAttachmentPart(multipart, contentType);
172       ap.setContentId(CID_MIMEPART);
173       msg.addAttachmentPart(ap);
174
175       //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
176

177       sendAndValidateMimeMessage(rpcMethodName, msg);
178    }
179
180    /** Send a multipart message with a text/plain attachment part
181     */

182    public void testSendMimeTextXML() throws Exception JavaDoc
183    {
184       String JavaDoc rpcMethodName = "sendMimeTextXML";
185       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
186
187       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.xml").toURL();
188       addAttachmentPart(msg, "text/xml", new DataHandler JavaDoc(url));
189
190       //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
191
sendAndValidateMimeMessage(rpcMethodName, msg);
192    }
193
194    /** Send a multipart message with a text/plain attachment part
195     */

196    public void testSendMimeApplicationXML() throws Exception JavaDoc
197    {
198       String JavaDoc rpcMethodName = "sendMimeApplicationXML";
199       SOAPMessage JavaDoc msg = setupMimeMessage(rpcMethodName);
200
201       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.xml").toURL();
202       addAttachmentPart(msg, "application/xml", new DataHandler JavaDoc(url));
203
204       //msg.writeTo(new FileOutputStream(rpcMethodName + "_RequestMessage.txt"));
205
sendAndValidateMimeMessage(rpcMethodName, msg);
206    }
207
208    /** Setup the multipart/related MIME message
209     */

210    private SOAPMessage JavaDoc setupMimeMessage(String JavaDoc rpcMethodName)
211       throws Exception JavaDoc
212    {
213       MessageFactory JavaDoc mf = MessageFactory.newInstance();
214
215       // Create a soap message from the message factory.
216
SOAPMessage JavaDoc msg = mf.createMessage();
217
218       // Message creation takes care of creating the SOAPPart - a required part of the message as per the SOAP 1.1 spec.
219
SOAPPart JavaDoc sp = msg.getSOAPPart();
220
221       // Retrieve the envelope from the soap part to start building the soap message.
222
SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
223
224       // Create a soap body from the envelope.
225
SOAPBody JavaDoc bdy = envelope.getBody();
226
227       // Add a soap body element
228
SOAPBodyElement JavaDoc sbe = bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI));
229
230       // Add a some child elements
231
sbe.addChildElement(envelope.createName("message")).addTextNode("Some text message");
232       sbe.addChildElement(envelope.createName("mimepart")).addAttribute(envelope.createName("href"), CID_MIMEPART);
233       return msg;
234    }
235
236    private void addAttachmentPart(SOAPMessage JavaDoc msg, String JavaDoc contentType, DataHandler JavaDoc dataHandler)
237    {
238       // Create the attachment part
239
AttachmentPart JavaDoc ap = msg.createAttachmentPart(dataHandler);
240       ap.setContentType(contentType);
241       ap.setContentId(CID_MIMEPART);
242
243       // Add the attachments to the message.
244
msg.addAttachmentPart(ap);
245    }
246
247    /** Send the message and validate the result
248     */

249    private void sendAndValidateMimeMessage(String JavaDoc rpcMethodName, SOAPMessage JavaDoc msg)
250       throws SOAPException JavaDoc, MalformedURLException JavaDoc
251    {
252       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
253       SOAPConnection JavaDoc con = conFactory.createConnection();
254       SOAPMessage JavaDoc resMessage = con.call(msg, new URL JavaDoc("http://" + getServerHost() + ":8080/ws4ee-attachment"));
255       SOAPBody JavaDoc soapBody = resMessage.getSOAPBody();
256       SOAPEnvelope JavaDoc soapEnvelope = (SOAPEnvelope JavaDoc)soapBody.getParentElement();
257
258       Name JavaDoc rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI);
259       Iterator JavaDoc childElements = soapBody.getChildElements(rpcName);
260       assertTrue("Expexted child: " + rpcName, childElements.hasNext());
261
262       SOAPElement JavaDoc bodyChild = (SOAPElement JavaDoc)childElements.next();
263       Name JavaDoc resName = soapEnvelope.createName("result");
264       SOAPElement JavaDoc resElement = (SOAPElement JavaDoc)bodyChild.getChildElements(resName).next();
265       String JavaDoc value = resElement.getValue();
266
267       assertEquals("[pass]", value);
268    }
269 }
270
Popular Tags