1 17 package org.apache.servicemix.soap.marshalers; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.net.URI ; 24 import java.util.Iterator ; 25 import java.util.Properties ; 26 27 import javax.activation.DataHandler ; 28 import javax.activation.FileDataSource ; 29 import javax.mail.Session ; 30 import javax.mail.internet.MimeBodyPart ; 31 import javax.mail.internet.MimeMessage ; 32 import javax.mail.internet.MimeMultipart ; 33 import javax.xml.namespace.QName ; 34 import javax.xml.transform.dom.DOMSource ; 35 import javax.xml.transform.stream.StreamSource ; 36 37 import junit.framework.TestCase; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.apache.servicemix.jbi.jaxp.BytesSource; 42 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 43 import org.apache.servicemix.jbi.util.DOMUtil; 44 import org.apache.servicemix.jbi.util.FileUtil; 45 import org.apache.xpath.CachedXPathAPI; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.DocumentFragment ; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.traversal.NodeIterator; 51 52 57 public class SoapMessageMarshalerTest extends TestCase { 58 59 private static final Log log = LogFactory.getLog(SoapMessageMarshalerTest.class); 60 61 private SourceTransformer sourceTransformer = new SourceTransformer(); 62 63 public void testReadNonSoapMessage() throws Exception { 64 SoapMarshaler marshaler = new SoapMarshaler(false); 65 SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml")); 66 assertNotNull(msg); 67 assertFalse(msg.hasAttachments()); 68 assertFalse(msg.hasHeaders()); 69 assertNotNull(msg.getSource()); 70 Node node = sourceTransformer.toDOMNode(msg.getSource()); 71 checkServiceNameNamespace(node); 72 checkUserIdNamespace(node); 73 } 74 75 public void testWriteAndReadNonSoapMessage() throws Exception { 76 SoapMarshaler marshaler = new SoapMarshaler(false); 77 SoapMessage msg = new SoapMessage(); 78 msg.setSource(new StreamSource (getClass().getResourceAsStream("soap.xml"))); 79 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 80 marshaler.createWriter(msg).write(baos); 81 log.info(baos.toString()); 82 83 SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream (baos.toByteArray())); 84 assertNotNull(msg2); 85 assertFalse(msg2.hasAttachments()); 86 assertFalse(msg2.hasHeaders()); 87 assertNotNull(msg2.getSource()); 88 Node node = sourceTransformer.toDOMNode(msg2.getSource()); 89 checkServiceNameNamespace(node); 90 checkUserIdNamespace(node); 91 } 92 93 public void testWriteAndReadNonSoapMessageWithAttachments() throws Exception { 94 SoapMarshaler marshaler = new SoapMarshaler(false); 95 SoapMessage msg = new SoapMessage(); 96 msg.setSource(new StreamSource (getClass().getResourceAsStream("soap.xml"))); 97 msg.addAttachment("servicemix.jpg", new DataHandler (new FileDataSource (new File (new URI (getClass().getResource("servicemix.jpg").toString()))))); 98 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 99 SoapWriter writer = marshaler.createWriter(msg); 100 writer.write(baos); 101 log.info(baos.toString()); 102 103 SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream (baos.toByteArray()), writer.getContentType()); 104 assertNotNull(msg2); 105 assertTrue(msg2.hasAttachments()); 106 assertEquals(1, msg2.getAttachments().size()); 107 DataHandler handler1 = (DataHandler ) msg.getAttachments().get(msg.getAttachments().keySet().iterator().next()); 108 DataHandler handler2 = (DataHandler ) msg2.getAttachments().get(msg2.getAttachments().keySet().iterator().next()); 109 assertNotNull(handler2); 110 InputStream is1 = handler1.getInputStream(); 111 InputStream is2 = handler2.getInputStream(); 112 ByteArrayOutputStream baos1 = new ByteArrayOutputStream (); 113 ByteArrayOutputStream baos2 = new ByteArrayOutputStream (); 114 FileUtil.copyInputStream(is1, baos1); 115 FileUtil.copyInputStream(is2, baos2); 116 assertEquals(baos1.toString(), baos2.toString()); 117 assertFalse(msg2.hasHeaders()); 118 assertNotNull(msg2.getSource()); 119 Node node = sourceTransformer.toDOMNode(msg2.getSource()); 120 checkServiceNameNamespace(node); 121 checkUserIdNamespace(node); 122 } 123 124 public void testWriteNonSoapMessageWithHeaders() throws Exception { 125 SoapMarshaler marshaler = new SoapMarshaler(false); 126 SoapMessage msg = new SoapMessage(); 127 msg.addHeader(new QName ("test"), null); 128 msg.setSource(null); 129 try { 130 marshaler.createWriter(msg).write(new ByteArrayOutputStream ()); 131 fail("Exception should have been thrown"); 132 } catch (Exception e) { 133 } 135 } 136 137 public void testSoapMessage() throws Exception { 138 SoapMarshaler marshaler = new SoapMarshaler(true); 139 140 SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml")); 141 assertNotNull(msg); 142 assertFalse(msg.hasAttachments()); 143 assertTrue(msg.hasHeaders()); 144 assertNotNull(msg.getSource()); 145 146 Iterator headers = msg.getHeaders().values().iterator(); 147 assertTrue(headers.hasNext()); 148 assertNotNull(headers.next()); 149 assertTrue(headers.hasNext()); 150 checkServiceNameNamespace((DocumentFragment ) headers.next()); 151 assertFalse(headers.hasNext()); 152 153 Node node2 = sourceTransformer.toDOMNode(msg.getSource()); 154 checkUserIdNamespace(node2); 155 156 msg.setSource(new DOMSource (node2)); 157 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 158 marshaler.createWriter(msg).write(baos); 159 Node node = sourceTransformer.toDOMNode(new BytesSource(baos.toByteArray())); 160 checkUserIdNamespace(node); 161 } 162 163 public void testSoapMessage2() throws Exception { 164 SoapMarshaler marshaler = new SoapMarshaler(true); 165 SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap2.xml")); 166 assertNotNull(msg); 167 } 168 169 public void testSoap11Message() throws Exception { 170 SoapMarshaler marshaler = new SoapMarshaler(true); 171 marshaler.setSoapUri(SoapMarshaler.SOAP_11_URI); 172 SoapMessage msg = new SoapMessage(); 173 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 174 marshaler.createWriter(msg).write(baos); 175 Node node = sourceTransformer.toDOMNode(new BytesSource(baos.toByteArray())); 176 assertNotNull(node); 177 Element root = ((Document ) node).getDocumentElement(); 178 assertEquals(SoapMarshaler.SOAP_11_URI, root.getNamespaceURI()); 179 } 180 181 public void testWriteAndReadSoapMessageWithAttachments() throws Exception { 182 SoapMarshaler marshaler = new SoapMarshaler(true); 183 SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml")); 184 msg.addAttachment("servicemix.jpg", new DataHandler (new FileDataSource (new File (new URI (getClass().getResource("servicemix.jpg").toString()))))); 185 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 186 SoapWriter writer = marshaler.createWriter(msg); 187 writer.write(baos); 188 log.info(baos.toString()); 189 190 SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream (baos.toByteArray()), writer.getContentType()); 191 assertNotNull(msg2); 192 assertTrue(msg2.hasAttachments()); 193 assertEquals(1, msg2.getAttachments().size()); 194 DataHandler handler = (DataHandler ) msg2.getAttachments().get(msg2.getAttachments().keySet().iterator().next()); 195 assertNotNull(handler); 196 assertNotNull(msg2.getSource()); 197 checkUserIdNamespace(sourceTransformer.toDOMNode(msg2.getSource())); 198 assertTrue(msg2.hasHeaders()); 199 Iterator headers = msg2.getHeaders().values().iterator(); 200 assertTrue(headers.hasNext()); 201 assertNotNull(headers.next()); 202 assertTrue(headers.hasNext()); 203 checkServiceNameNamespace((DocumentFragment ) headers.next()); 204 assertFalse(headers.hasNext()); 205 } 206 207 public void testReadNonSoapMessageWithAttachmentsAndNoSource() throws Exception { 208 Session session = Session.getDefaultInstance(new Properties (), null); 209 MimeMessage message = new MimeMessage (session); 210 MimeMultipart multipart = new MimeMultipart (); 211 MimeBodyPart mimePart = new MimeBodyPart (); 212 mimePart.setText("This is a dummy text"); 213 mimePart.setContentID("<bla1>"); 214 multipart.addBodyPart(mimePart); 215 mimePart = new MimeBodyPart (); 216 mimePart.setContentID("<bla2>"); 217 mimePart.setContent("<html>dummy</html>", "text/html"); 218 multipart.addBodyPart(mimePart); 219 message.setContent(multipart); 220 message.setHeader("Content-Type", multipart.getContentType()); 221 message.saveChanges(); 222 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 223 message.writeTo(baos); 224 log.info(baos.toString()); 225 226 SoapMarshaler marshaler = new SoapMarshaler(true); 227 SoapMessage msg = marshaler.createReader().read(new ByteArrayInputStream (baos.toByteArray()), multipart.getContentType()); 228 assertTrue(msg.getSource().equals(SoapReader.EMPTY_CONTENT)); 229 SoapWriter writer = marshaler.createWriter(msg); 230 baos = new ByteArrayOutputStream (); 231 writer.write(baos); 232 log.info(baos.toString()); 233 } 234 235 protected void checkUserIdNamespace(Node node) throws Exception { 236 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 237 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']"); 238 Element root = (Element ) iterator.nextNode(); 239 QName qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type")); 240 assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI()); 241 assertEquals("string", qname.getLocalPart()); 242 } 243 244 protected void checkServiceNameNamespace(Node node) throws Exception { 245 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 246 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'ServiceName']"); 247 Element root = (Element ) iterator.nextNode(); 248 assertEquals(new QName ("http://schemas.xmlsoap.org/ws/2003/03/addressing", "ServiceName"), 249 new QName (root.getNamespaceURI(), root.getLocalName())); 250 QName qname = DOMUtil.createQName(root, DOMUtil.getElementText(root)); 251 assertEquals(new QName ("uri:test", "MyConsumerService"), qname); 252 } 253 254 } 255 | Popular Tags |