Author: ningjiang
Date: Mon Aug 15 13:00:13 2011
New Revision: 1157831

URL: http://svn.apache.org/viewvc?rev=1157831&view=rev
Log:
CAMEL-4336 camel-cxf payload should take consideration of the namespace which 
is defined in the soap envelope

Added:
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
   (with props)
Modified:
    
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerMessageTest.java
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java

Modified: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java?rev=1157831&r1=1157830&r2=1157831&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
 Mon Aug 15 13:00:13 2011
@@ -28,6 +28,7 @@ import java.util.TreeMap;
 
 import javax.activation.DataHandler;
 import javax.security.auth.Subject;
+import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.transform.Source;
 import javax.xml.transform.dom.DOMSource;
@@ -35,6 +36,7 @@ import javax.xml.ws.Holder;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import org.apache.camel.Exchange;
@@ -44,6 +46,8 @@ import org.apache.camel.spi.HeaderFilter
 import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.attachment.AttachmentImpl;
+import org.apache.cxf.binding.soap.Soap11;
+import org.apache.cxf.binding.soap.Soap12;
 import org.apache.cxf.binding.soap.SoapBindingConstants;
 import org.apache.cxf.binding.soap.SoapHeader;
 import org.apache.cxf.endpoint.Client;
@@ -618,9 +622,28 @@ public class DefaultCxfBinding implement
         }
         return answer;
     }
-    
+
+    protected static void addNamespace(Element element, Map<String, String> 
nsMap) {
+        for (String ns : nsMap.keySet()) {
+            element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, 
nsMap.get(ns));
+        }
+                     
+    }
 
     protected static List<Element> getPayloadBodyElements(Message message) {
+        // take the namespace attribute from soap envelop
+        Document soapEnv = (Document) message.getContent(Node.class);
+        Map<String, String> nsMap = new HashMap<String, String>();
+        if (soapEnv != null) {
+            NamedNodeMap attrs = soapEnv.getFirstChild().getAttributes();
+            for (int i = 0; i < attrs.getLength(); i++) {
+                Node node = attrs.item(i);
+                if (!node.getNodeValue().equals(Soap11.SOAP_NAMESPACE)
+                      && !node.getNodeValue().equals(Soap12.SOAP_NAMESPACE)) {
+                    nsMap.put(node.getLocalName(), node.getNodeValue());
+                }
+            }
+        }
         MessageContentsList inObjects = 
MessageContentsList.getContentsList(message);
         if (inObjects == null) {
             return null;
@@ -667,6 +690,7 @@ public class DefaultCxfBinding implement
                 }
 
                 if (element != null) {
+                    addNamespace(element, nsMap);
                     answer.add(element);
                 }
                 
@@ -675,6 +699,7 @@ public class DefaultCxfBinding implement
                               element == null ? "null" : 
XMLUtils.toString(element));
                 }
             } else if (part instanceof Element) {
+                addNamespace((Element)part, nsMap);
                 answer.add((Element)part);
             } else {
                 if (LOG.isDebugEnabled()) {

Modified: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerMessageTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerMessageTest.java?rev=1157831&r1=1157830&r2=1157831&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerMessageTest.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerMessageTest.java
 Mon Aug 15 13:00:13 2011
@@ -76,7 +76,7 @@ public class CxfConsumerMessageTest exte
     }
     
     @Test
-    public void testInvokingServiceFromCXFClient() throws Exception {
+    public void testInvokingServiceFromClient() throws Exception {
         ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean();
         ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
         clientBean.setAddress(simpleEndpointAddress);

Modified: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java?rev=1157831&r1=1157830&r2=1157831&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java
 Mon Aug 15 13:00:13 2011
@@ -39,6 +39,16 @@ public class CxfConsumerPayloadTest exte
             + "<arg0 xmlns=\"http://cxf.component.camel.apache.org/\";>Hello 
World!</arg0></ns1:echo>";
     private static final String ECHO_BOOLEAN_REQUEST = "<ns1:echoBoolean 
xmlns:ns1=\"http://cxf.component.camel.apache.org/\";>"
             + "<arg0 
xmlns=\"http://cxf.component.camel.apache.org/\";>true</arg0></ns1:echoBoolean>";
+    
+    private static final String ELEMENT_NAMESPACE = 
"http://cxf.component.camel.apache.org/";;
+    
+    protected void checkRequest(String expect, String request) {
+        if (expect.equals("ECHO_REQUEST")) {
+            assertEquals("Get a wrong request", ECHO_REQUEST, request);
+        } else {
+            assertEquals("Get a wrong request", ECHO_BOOLEAN_REQUEST, request);
+        }
+    }
 
     // START SNIPPET: payload
     protected RouteBuilder createRouteBuilder() {
@@ -54,11 +64,17 @@ public class CxfConsumerPayloadTest exte
                         String request = 
exchange.getIn().getBody(String.class);
                         XmlConverter converter = new XmlConverter();
                         String documentString = ECHO_RESPONSE;
+                        
+                        // Just check the element namespace
+                        if 
(!inElements.get(0).getNamespaceURI().equals(ELEMENT_NAMESPACE)) {
+                            throw new IllegalArgumentException("Wrong element 
namespace");
+                        }
                         if 
(inElements.get(0).getLocalName().equals("echoBoolean")) {
                             documentString = ECHO_BOOLEAN_RESPONSE;
-                            assertEquals("Get a wrong request", 
ECHO_BOOLEAN_REQUEST, request);
+                            checkRequest("ECHO_BOOLEAN_REQUEST", request);
                         } else {
-                            assertEquals("Get a wrong request", ECHO_REQUEST, 
request);
+                            documentString = ECHO_RESPONSE;
+                            checkRequest("ECHO_REQUEST", request);
                         }
                         Document outDocument = 
converter.toDOMDocument(documentString);
                         outElements.add(outDocument.getDocumentElement());

Added: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java?rev=1157831&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
 (added)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
 Mon Aug 15 13:00:13 2011
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.cxf;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.junit.Test;
+
+public class CxfConusmerNamespacePayLoadTest extends CxfConsumerPayloadTest {
+    private static final String ECHO_REQUEST = "<soap:Envelope 
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"; "
+        + "xmlns:ns1=\"http://cxf.component.camel.apache.org/\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; "
+        + "xmlns:ns2=\"http://cxf.component.camel.apache.org/\"; 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\";><soap:Body>"
+        + "<ns1:echo><ns2:arg0 xsi:type=\"xsd:string\">Hello 
World!</ns2:arg0></ns1:echo></soap:Body></soap:Envelope>";
+    
+    protected void checkRequest(String expect, String request) {
+        if (expect.equals(ECHO_REQUEST)) {
+            // just check the namespace of xsd
+            assertTrue("Expect to find the namesapce", 
request.indexOf("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";) > 0);
+        }
+    }
+    
+    @Test
+    public void testInvokingServiceFromClient() throws Exception {
+        // just send a request which has all the namespace in the soap header
+        HttpPost post = new HttpPost(simpleEndpointAddress);
+        post.addHeader("Accept" , "text/xml");
+        
+        StringEntity entity = new StringEntity(ECHO_REQUEST, "text/xml", 
"ISO-8859-1");
+        post.setEntity(entity);
+        HttpClient httpclient = new DefaultHttpClient();
+
+        try {
+            HttpResponse response = httpclient.execute(post);
+            //assertEquals(200, response.getStatusLine().getStatusCode());
+            String responseBody = EntityUtils.toString(response.getEntity());
+            System.out.println(responseBody);
+            
+            //assertEquals("Get a wrong response", correct, responseBody);
+        } finally {
+            httpclient.getConnectionManager().shutdown();
+        }
+
+    }
+
+}

Propchange: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConusmerNamespacePayLoadTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to