1 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 ; 29 import javax.mail.internet.MimeBodyPart ; 30 import javax.mail.internet.MimeMultipart ; 31 import javax.xml.soap.AttachmentPart ; 32 import javax.xml.soap.MessageFactory ; 33 import javax.xml.soap.Name ; 34 import javax.xml.soap.SOAPBody ; 35 import javax.xml.soap.SOAPBodyElement ; 36 import javax.xml.soap.SOAPConnection ; 37 import javax.xml.soap.SOAPConnectionFactory ; 38 import javax.xml.soap.SOAPElement ; 39 import javax.xml.soap.SOAPEnvelope ; 40 import javax.xml.soap.SOAPException ; 41 import javax.xml.soap.SOAPMessage ; 42 import javax.xml.soap.SOAPPart ; 43 import java.awt.Image ; 44 import java.awt.Toolkit ; 45 import java.io.File ; 46 import java.io.FileOutputStream ; 47 import java.net.MalformedURLException ; 48 import java.net.URL ; 49 import java.util.Iterator ; 50 51 61 public class AttachmentSAAJTestCase extends WebserviceTestBase 62 { 63 private static final String NS_PREFIX = "ns1"; 64 private static final String NS_URI = "http://org.jboss.webservice/attachment"; 65 private static final String CID_MIMEPART = "<mimepart=123123@ws.jboss.org>"; 66 67 public AttachmentSAAJTestCase(String name) 68 { 69 super(name); 70 } 71 72 73 public static Test suite() throws Exception 74 { 75 return getDeploySetup(AttachmentSAAJTestCase.class, "ws4ee-attachment.war"); 76 } 77 78 80 public void testSendMimeImageGIF() throws Exception 81 { 82 String rpcMethodName = "sendMimeImageGIF"; 83 SOAPMessage msg = setupMimeMessage(rpcMethodName); 84 85 URL url = new File ("resources/webservice/attachment/attach.gif").toURL(); 86 87 Image image = null; 91 try 92 { 93 image = Toolkit.getDefaultToolkit().createImage(url); 94 } 95 catch (Throwable th) 96 { 97 log.warn("Cannot create Image: " + th); 98 } 99 100 if (image != null) 101 { 102 addAttachmentPart(msg, "image/gif", new DataHandler (url)); 103 sendAndValidateMimeMessage(rpcMethodName, msg); 105 } 106 } 107 108 110 public void testSendMimeImageJPEG() throws Exception 111 { 112 String rpcMethodName = "sendMimeImageJPEG"; 113 SOAPMessage msg = setupMimeMessage(rpcMethodName); 114 115 URL url = new File ("resources/webservice/attachment/attach.jpeg").toURL(); 116 117 Image image = null; 121 try 122 { 123 image = Toolkit.getDefaultToolkit().createImage(url); 124 } 125 catch (Throwable th) 126 { 127 log.warn("Cannot create Image: " + th); 128 } 129 130 if (image != null) 131 { 132 addAttachmentPart(msg, "image/jpeg", new DataHandler (url)); 133 sendAndValidateMimeMessage(rpcMethodName, msg); 135 } 136 } 137 138 140 public void testSendMimeTextPlain() throws Exception 141 { 142 String rpcMethodName = "sendMimeTextPlain"; 143 SOAPMessage msg = setupMimeMessage(rpcMethodName); 144 145 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 146 addAttachmentPart(msg, "text/plain", new DataHandler (url)); 147 148 150 sendAndValidateMimeMessage(rpcMethodName, msg); 151 } 152 153 155 public void testSendMimeMultipart() throws Exception 156 { 157 String rpcMethodName = "sendMimeMultipart"; 158 SOAPMessage msg = setupMimeMessage(rpcMethodName); 159 160 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 161 MimeMultipart multipart = new MimeMultipart ("mixed"); 162 MimeBodyPart bodyPart = new MimeBodyPart (); 163 bodyPart.setDataHandler(new DataHandler (url)); 164 String bpct = bodyPart.getContentType(); 165 bodyPart.setHeader("Content-Type", bpct); 166 167 multipart.addBodyPart(bodyPart); 168 multipart.writeTo(new FileOutputStream (rpcMethodName + "_Multipart.txt")); 169 170 String contentType = multipart.getContentType(); 171 AttachmentPart ap = msg.createAttachmentPart(multipart, contentType); 172 ap.setContentId(CID_MIMEPART); 173 msg.addAttachmentPart(ap); 174 175 177 sendAndValidateMimeMessage(rpcMethodName, msg); 178 } 179 180 182 public void testSendMimeTextXML() throws Exception 183 { 184 String rpcMethodName = "sendMimeTextXML"; 185 SOAPMessage msg = setupMimeMessage(rpcMethodName); 186 187 URL url = new File ("resources/webservice/attachment/attach.xml").toURL(); 188 addAttachmentPart(msg, "text/xml", new DataHandler (url)); 189 190 sendAndValidateMimeMessage(rpcMethodName, msg); 192 } 193 194 196 public void testSendMimeApplicationXML() throws Exception 197 { 198 String rpcMethodName = "sendMimeApplicationXML"; 199 SOAPMessage msg = setupMimeMessage(rpcMethodName); 200 201 URL url = new File ("resources/webservice/attachment/attach.xml").toURL(); 202 addAttachmentPart(msg, "application/xml", new DataHandler (url)); 203 204 sendAndValidateMimeMessage(rpcMethodName, msg); 206 } 207 208 210 private SOAPMessage setupMimeMessage(String rpcMethodName) 211 throws Exception 212 { 213 MessageFactory mf = MessageFactory.newInstance(); 214 215 SOAPMessage msg = mf.createMessage(); 217 218 SOAPPart sp = msg.getSOAPPart(); 220 221 SOAPEnvelope envelope = sp.getEnvelope(); 223 224 SOAPBody bdy = envelope.getBody(); 226 227 SOAPBodyElement sbe = bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI)); 229 230 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 msg, String contentType, DataHandler dataHandler) 237 { 238 AttachmentPart ap = msg.createAttachmentPart(dataHandler); 240 ap.setContentType(contentType); 241 ap.setContentId(CID_MIMEPART); 242 243 msg.addAttachmentPart(ap); 245 } 246 247 249 private void sendAndValidateMimeMessage(String rpcMethodName, SOAPMessage msg) 250 throws SOAPException , MalformedURLException 251 { 252 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); 253 SOAPConnection con = conFactory.createConnection(); 254 SOAPMessage resMessage = con.call(msg, new URL ("http://" + getServerHost() + ":8080/ws4ee-attachment")); 255 SOAPBody soapBody = resMessage.getSOAPBody(); 256 SOAPEnvelope soapEnvelope = (SOAPEnvelope )soapBody.getParentElement(); 257 258 Name rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI); 259 Iterator childElements = soapBody.getChildElements(rpcName); 260 assertTrue("Expexted child: " + rpcName, childElements.hasNext()); 261 262 SOAPElement bodyChild = (SOAPElement )childElements.next(); 263 Name resName = soapEnvelope.createName("result"); 264 SOAPElement resElement = (SOAPElement )bodyChild.getChildElements(resName).next(); 265 String value = resElement.getValue(); 266 267 assertEquals("[pass]", value); 268 } 269 } 270 | Popular Tags |