servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r367116 - in /incubator/servicemix/trunk/servicemix-components/src: main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java
Date Sun, 08 Jan 2006 22:59:32 GMT
Author: gnodet
Date: Sun Jan  8 14:59:26 2006
New Revision: 367116

URL: http://svn.apache.org/viewcvs?rev=367116&view=rev
Log:
Fix remaining problem with SaajMarshaler and namespaces.

Modified:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java
    incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java?rev=367116&r1=367115&r2=367116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java
Sun Jan  8 14:59:26 2006
@@ -20,12 +20,16 @@
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
 
 import javax.activation.DataHandler;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.xml.XMLConstants;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.soap.AttachmentPart;
 import javax.xml.soap.MessageFactory;
 import javax.xml.soap.MimeHeader;
@@ -36,7 +40,6 @@
 import javax.xml.soap.SOAPMessage;
 import javax.xml.soap.SOAPPart;
 import javax.xml.transform.TransformerException;
-import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
 
 import java.io.ByteArrayOutputStream;
@@ -114,7 +117,7 @@
         addNmsAttachments(normalizedMessage, soapMessage);
     }
 
-    public SOAPMessage createSOAPMessage(NormalizedMessage normalizedMessage) throws SOAPException,
IOException, TransformerException {
+    public SOAPMessage createSOAPMessage(NormalizedMessage normalizedMessage) throws SOAPException,
IOException, TransformerException, MessagingException, ParserConfigurationException, SAXException
{
         SOAPMessage soapMessage = getMessageFactory().createMessage();
 
         addSoapProperties(soapMessage, normalizedMessage);
@@ -124,9 +127,21 @@
         SOAPBody body = envelope.getBody();
 
         // lets turn the payload into a DOM Node to avoid blatting over the envelope
-        DOMResult result = new DOMResult(null);
-        transformer.toResult(normalizedMessage.getContent(), result);
-        Document document = (Document) result.getNode();
+        // Do not use DOMResult to transform as namespaces are lost (why ?)
+        //DOMResult result = new DOMResult(null);
+        //transformer.toResult(normalizedMessage.getContent(), result);
+        //Document document = (Document) result.getNode();
+        Node node = transformer.toDOMNode(normalizedMessage);
+        Document document = null;
+        if (node instanceof Document) {
+        	document = (Document) node;
+        } else if (node instanceof Element) {
+        	document = transformer.createDocument();
+        	document.appendChild(node.cloneNode(true));
+        } else {
+        	throw new TransformerException("Could not create Document from Source");
+        }
+        
         body.addDocument(document);
 
         addSoapAttachments(soapMessage, normalizedMessage);

Modified: incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java?rev=367116&r1=367115&r2=367116&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/test/java/org/apache/servicemix/components/saaj/SaajMarshalerTest.java
Sun Jan  8 14:59:26 2006
@@ -15,6 +15,7 @@
  */
 package org.apache.servicemix.components.saaj;
 
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 
 import javax.jbi.messaging.NormalizedMessage;
@@ -22,8 +23,14 @@
 import javax.xml.soap.MessageFactory;
 import javax.xml.soap.MimeHeaders;
 import javax.xml.soap.SOAPMessage;
+import javax.xml.transform.stream.StreamSource;
 
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
 import org.apache.servicemix.jbi.util.DOMUtil;
 import org.apache.xpath.CachedXPathAPI;
@@ -31,14 +38,18 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.traversal.NodeIterator;
 
-import junit.framework.TestCase;
-
 public class SaajMarshalerTest extends TestCase {
 
-
-	public void testAxis() throws Exception {
+	private static final transient Log log = LogFactory.getLog(SaajMarshalerTest.class); 
+	
+	public void testAxisToNMS() throws Exception {
 		MessageFactory messageFactory = new org.apache.axis.soap.MessageFactoryImpl();
-		testFactory(messageFactory);
+		testToNMS(messageFactory);
+	}
+	
+	public void testAxisCreateSOAPMessage() throws Exception {
+		MessageFactory messageFactory = new org.apache.axis.soap.MessageFactoryImpl();
+		testCreateSOAPMessage(messageFactory);
 	}
 	
 	/*
@@ -48,12 +59,19 @@
 	}
 	*/
 	
-	public void testSun() throws Exception {
+	public void testSunToNMS() throws Exception {
 		MessageFactory messageFactory = new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl();
-		testFactory(messageFactory);
+		testToNMS(messageFactory);
+	}
+	
+	/*
+	public void testSunCreateSOAPMessage() throws Exception {
+		MessageFactory messageFactory = new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl();
+		testCreateSOAPMessage(messageFactory);
 	}
+	*/
 	
-	protected void testFactory(MessageFactory messageFactory) throws Exception {
+	protected void testToNMS(MessageFactory messageFactory) throws Exception {
 		MimeHeaders headers = new MimeHeaders();
 		headers.addHeader("Content-Type", "text/xml;");
 		InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/http/soap-response.xml");
@@ -62,7 +80,7 @@
 		new SaajMarshaler().toNMS(nm, sm);
 
         Node node = new SourceTransformer().toDOMNode(new SourceTransformer().toStreamSource(nm.getContent()));
-        System.out.println(new SourceTransformer().toString(node));
+        log.info(new SourceTransformer().toString(node));
 		
         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name()
= 'userId']");
@@ -72,4 +90,33 @@
         assertEquals("string", qname.getLocalPart());
 	}
 	
+	protected void testCreateSOAPMessage(MessageFactory messageFactory) throws Exception { 
+        MimeHeaders headers = new MimeHeaders(); 
+        headers.addHeader("Content-Type", "text/xml;"); 
+         
+        InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/saaj/xml-request.xml");
        
+        log.info("Raw XML: " + new SourceTransformer().toString(new StreamSource(is))); 
+         
+        is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/saaj/xml-request.xml");
        
+        NormalizedMessage nm = new NormalizedMessageImpl(); 
+        nm.setContent(new StreamSource(is)); 
+           
+        SaajMarshaler marshaler = new SaajMarshaler();
+        marshaler.setMessageFactory(messageFactory);
+        SOAPMessage msg = marshaler.createSOAPMessage(nm);
+        
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        msg.writeTo(baos);
+        String soapEnv = new String(baos.toByteArray());
+        log.info("Prepared SOAP: " + soapEnv); 
+        Node node2 = new SourceTransformer().toDOMNode(new StringSource(soapEnv)); 
+          
+        CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 
+        NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node2, "//*[local-name()
= 'userId']"); 
+        Element root = (Element) iterator.nextNode(); 
+        QName qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
"type")); 
+        assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI()); 
+        assertEquals("string", qname.getLocalPart()); 
+    } 
+
 }



Mime
View raw message