camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject camel git commit: CAMEL-8313 CxfPayload AddNamespace should not override the element's namespace
Date Wed, 04 Feb 2015 13:03:55 GMT
Repository: camel
Updated Branches:
  refs/heads/master 8494f7b5a -> b79716d7b


CAMEL-8313 CxfPayload AddNamespace should not override the element's namespace


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b79716d7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b79716d7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b79716d7

Branch: refs/heads/master
Commit: b79716d7b8c0a50f36dca155a398ebbb24925a0d
Parents: 8494f7b
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Wed Feb 4 20:57:02 2015 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Wed Feb 4 20:57:02 2015 +0800

----------------------------------------------------------------------
 .../apache/camel/component/cxf/CxfPayload.java  | 10 ++++-
 .../camel/component/cxf/DefaultCxfBinding.java  |  9 +++-
 .../component/cxf/DefaultCxfBindingTest.java    | 45 +++++++++++++++++---
 3 files changed, 53 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b79716d7/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java
index 1330b0f..abdf682 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java
@@ -29,6 +29,7 @@ import javax.xml.transform.dom.DOMSource;
 import org.w3c.dom.Element;
 
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.staxutils.StaxUtils;
 
 
@@ -110,10 +111,15 @@ public class CxfPayload<T> {
     protected static void addNamespace(Element element, Map<String, String> nsMap)
{
         if (nsMap != null) {
             for (String ns : nsMap.keySet()) {
+                // We should not override the namespace setting of the element
                 if (XMLConstants.XMLNS_ATTRIBUTE.equals(ns)) {
-                    element.setAttribute(ns, nsMap.get(ns));
+                    if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE)))
{
+                        element.setAttribute(ns, nsMap.get(ns));
+                    }
                 } else {
-                    element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, nsMap.get(ns));
+                    if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE
+ ":" + ns))) {
+                        element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, nsMap.get(ns));
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/b79716d7/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
index 1c79a4c..7616568 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
@@ -735,10 +735,15 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware
 
     protected static void addNamespace(Element element, Map<String, String> nsMap)
{
         for (String ns : nsMap.keySet()) {
+            // We should not override the namespace setting of the element
             if (XMLConstants.XMLNS_ATTRIBUTE.equals(ns)) {
-                element.setAttribute(ns, nsMap.get(ns));
+                if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE)))
{
+                    element.setAttribute(ns, nsMap.get(ns));
+                }
             } else {
-                element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, nsMap.get(ns));
+                if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE
+ ":" + ns))) {
+                    element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, nsMap.get(ns));
+                }
             }
         }
                      

http://git-wip-us.apache.org/repos/asf/camel/blob/b79716d7/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
index 2097f3e..c82050a 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
@@ -61,10 +61,16 @@ import org.junit.Test;
  */
 public class DefaultCxfBindingTest extends Assert {
     
-    private static final String SOAP_MESSAGE = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
+    private static final String SOAP_MESSAGE_1 = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
         + " xmlns=\"http://www.mycompany.com/test/\" xmlns:ns1=\"http://www.mycompany.com/test/1/\">"
         + " <soap:Body> <request> <ns1:identifier>TEST</ns1:identifier>
</request>"
         + " </soap:Body> </soap:Envelope>";
+    
+    private static final String SOAP_MESSAGE_2 = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
+        + " xmlns=\"http://www.mycompany.com/test/\" xmlns:ns1=\"http://www.mycompany.com/test/1/\">"
+        + " <soap:Body> <ns1:identifier xmlns:ns1=\"http://www.mycompany.com/test/\"
xmlns=\"http://www.mycompany.com/test/1/\">TEST</ns1:identifier>"
+        + " </soap:Body> </soap:Envelope>";
+    
     private DefaultCamelContext context = new DefaultCamelContext();
 
     @Test
@@ -76,22 +82,28 @@ public class DefaultCxfBindingTest extends Assert {
         assertSame("The header filter strategy is set", hfs, cxfBinding.getHeaderFilterStrategy());
     }
     
-    @Test
-    public void testPayloadBodyNamespace() throws Exception {
-        MessageImpl message = new MessageImpl();
+    private Document getDocument(String soapMessage) throws Exception {
+        
         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
         documentBuilderFactory.setNamespaceAware(true);
         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-        Document document = documentBuilder.parse(IOConverter.toInputStream(SOAP_MESSAGE,
null));
+        Document document = documentBuilder.parse(IOConverter.toInputStream(soapMessage,
null));
         document.getDocumentElement().normalize();
-        message.setContent(Node.class, document);
+       
+        return document;
+    }
+    
+    @Test
+    public void testPayloadBodyNamespace() throws Exception {
+        MessageImpl message = new MessageImpl();
         Map<String, String> nsMap = new HashMap<String, String>();
+        Document document = getDocument(SOAP_MESSAGE_1);
+        message.setContent(Node.class, document);
         DefaultCxfBinding.getPayloadBodyElements(message, nsMap);
         
         assertEquals(2, nsMap.size());
         assertEquals("http://www.mycompany.com/test/", nsMap.get("xmlns"));
         
-        document = documentBuilder.newDocument();
         Element element = document.createElement("tag");
         DefaultCxfBinding.addNamespace(element, nsMap);
         assertEquals("http://www.mycompany.com/test/", element.getAttribute("xmlns"));
@@ -99,6 +111,25 @@ public class DefaultCxfBindingTest extends Assert {
     }
     
     @Test
+    public void testOverridePayloadBodyNamespace() throws Exception {
+        MessageImpl message = new MessageImpl();
+        Map<String, String> nsMap = new HashMap<String, String>();
+        Document document = getDocument(SOAP_MESSAGE_2);
+        message.setContent(Node.class, document);
+        DefaultCxfBinding.getPayloadBodyElements(message, nsMap);
+        
+        assertEquals(2, nsMap.size());
+        assertEquals("http://www.mycompany.com/test/", nsMap.get("xmlns"));
+        
+        Element element = (Element)document.getElementsByTagName("ns1:identifier").item(0);
+        assertNotNull("We should get the element", element);
+        DefaultCxfBinding.addNamespace(element, nsMap);
+        assertEquals("http://www.mycompany.com/test/1/", element.getAttribute("xmlns"));
+        assertEquals("http://www.mycompany.com/test/", element.getAttribute("xmlns:ns1"));
+    }
+
+    
+    @Test
     public void testSetCharsetWithContentType() {
         DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
         cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());


Mime
View raw message