You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/05/23 10:23:00 UTC

svn commit: r659468 - in /activemq/camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/ main/java/org/apache/camel/component/cxf/invoker/ test/java/org/apache/camel/component/cxf/

Author: ningjiang
Date: Fri May 23 01:22:59 2008
New Revision: 659468

URL: http://svn.apache.org/viewvc?rev=659468&view=rev
Log:
CAMEL-536 added request and response context support in CXF-Camel component

Added:
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerContextTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/invoker/CxfClient.java
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerRouterTest.java
    activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java?rev=659468&r1=659467&r2=659468&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java Fri May 23 01:22:59 2008
@@ -17,9 +17,14 @@
 package org.apache.camel.component.cxf;
 
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.jaxws.support.ContextPropertiesMapping;
 import org.apache.cxf.message.Message;
 
 /**
@@ -86,6 +91,13 @@
         }
     }
 
+    public static void storeCXfResponseContext(Message response, Map<String, Object> context) {
+        if (context != null) {
+            ContextPropertiesMapping.mapResponsefromCxf2Jaxws(context);
+            response.put(Client.RESPONSE_CONTEXT, context);
+        }
+    }
+
     public static void storeCxfResponse(CxfExchange exchange, Object response) {
         CxfMessage out = exchange.getOut();
         if (response != null) {
@@ -99,4 +111,25 @@
             fault.setBody(getBody(message));
         }
     }
+
+
+    public static Map<String, Object> propogateContext(Message message, Map<String, Object> context) {
+        Map<String, Object> requestContext = CastUtils.cast((Map)message.get(Client.REQUEST_CONTEXT));
+        Map<String, Object> responseContext = CastUtils.cast((Map)message.get(Client.RESPONSE_CONTEXT));
+        // TODO map the JAXWS properties to cxf
+        if (requestContext != null) {
+            ContextPropertiesMapping.mapRequestfromJaxws2Cxf(requestContext);
+        }
+
+        if (responseContext == null) {
+            responseContext = new HashMap<String, Object>();
+        } else {
+            // clear the response context
+            responseContext.clear();
+        }
+        context.put(Client.REQUEST_CONTEXT, requestContext);
+        context.put(Client.RESPONSE_CONTEXT, responseContext);
+        return responseContext;
+
+    }
 }

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java?rev=659468&r1=659467&r2=659468&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java Fri May 23 01:22:59 2008
@@ -185,26 +185,19 @@
                 }
                 String operationName = (String)inMessage.get(CxfConstants.OPERATION_NAME);
                 String operationNameSpace = (String)inMessage.get(CxfConstants.OPERATION_NAMESPACE);
+                // Get context from message
+                Map<String, Object> context = new HashMap<String, Object>();
+                Map<String, Object> responseContext = CxfBinding.propogateContext(inMessage, context);
                 Message response = new MessageImpl();
                 if (operationName != null) {
                     // we need to check out the operation Namespace
                     try {
                         Object[] result = null;
-                        if (operationNameSpace == null) {
-                            if (endpoint.isWrapped()) {
-                                result = client.invokeWrapped(operationName, parameters.toArray());
-                            } else {
-                                result = client.invoke(operationName, parameters.toArray());
-                            }
-                        } else {
-                            QName operation = new QName(operationNameSpace, operationName);
-                            if (endpoint.isWrapped()) {
-                                result = client.invokeWrapped(operation, parameters.toArray());
-                            } else {
-                                result = client.invoke(operation, parameters.toArray());
-                            }
-                        }
+                        // call for the client with the parameters
+                        result = invokeClient(operationNameSpace, operationName, parameters, context);
                         response.setContent(Object[].class, result);
+                        // copy the response context to the response
+                        CxfBinding.storeCXfResponseContext(response, responseContext);
                         CxfBinding.storeCxfResponse(exchange, response);
                     } catch (Exception ex) {
                         response.setContent(Exception.class, ex);
@@ -230,7 +223,7 @@
                     invokingContext = InvokingContextFactory.createContext(dataFormat);
                     ex.put(InvokingContext.class, invokingContext);
                 }
-                Map params = invokingContext.getRequestContent(inMessage);
+                Map<Class, Object> params = invokingContext.getRequestContent(inMessage);
                 // invoke the stream message with the exchange context
                 CxfClient cxfClient = (CxfClient)client;
                 // need to get the binding object to create the message
@@ -247,17 +240,13 @@
                 response.setExchange(ex);
                 // invoke the message prepare the context
                 Map<String, Object> context = new HashMap<String, Object>();
-                Map<String, Object> requestContext = new HashMap<String, Object>();
-                Map<String, Object> responseContext = new HashMap<String, Object>();
-                // TODO Get the requestContext from the CamelExchange
-                context.put(CxfClient.REQUEST_CONTEXT, requestContext);
-                context.put(CxfClient.RESPONSE_CONTEXT, responseContext);
+                Map<String, Object> responseContext = CxfBinding.propogateContext(inMessage, context);
                 try {
                     Object result = cxfClient.dispatch(params, context, ex);
                     ex.setOutMessage(response);
                     invokingContext.setResponseContent(response, result);
                     // copy the response context to the response
-                    response.putAll(responseContext);
+                    CxfBinding.storeCXfResponseContext(response, responseContext);
                     CxfBinding.storeCxfResponse(exchange, response);
                 } catch (Exception e) {
                     response.setContent(Exception.class, e);
@@ -271,6 +260,7 @@
 
     }
 
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
@@ -281,4 +271,26 @@
         super.doStop();
     }
 
+    private Object[] invokeClient(String operationNameSpace, String operationName, List parameters, Map<String, Object> context) throws Exception {
+
+        QName operationQName = null;
+        if (operationNameSpace == null) {
+            operationQName = new QName(client.getEndpoint().getService().getName().getNamespaceURI(), operationName);
+        } else {
+            operationQName = new QName(operationNameSpace, operationName);
+        }
+        BindingOperationInfo op = client.getEndpoint().getEndpointInfo().getBinding().getOperation(operationQName);
+        if (op == null) {
+            throw new RuntimeCamelException("No operation found in the CXF client, the operation is " + operationQName);
+        }
+        if (!endpoint.isWrapped()) {
+            if (op.isUnwrappedCapable()) {
+                op = op.getUnwrappedOperation();
+            }
+        }
+        Object[] result = client.invoke(op, parameters.toArray(), context);
+
+        return result;
+    }
+
 }

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/invoker/CxfClient.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/invoker/CxfClient.java?rev=659468&r1=659467&r2=659468&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/invoker/CxfClient.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/invoker/CxfClient.java Fri May 23 01:22:59 2008
@@ -108,6 +108,8 @@
         }
 
         Message message = prepareMessage(exchange, requestContext, param, invokingContext);
+        // add the invocation context
+        message.put(Message.INVOCATION_CONTEXT, context);
 
         // add the endpoint props to the message
         Endpoint ep = getEndpoint();

Added: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerContextTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerContextTest.java?rev=659468&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerContextTest.java (added)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerContextTest.java Fri May 23 01:22:59 2008
@@ -0,0 +1,72 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.ws.BindingProvider;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.message.Message;
+
+// We use context to change the producer's endpoint address here
+public class CxfProducerContextTest extends CxfProducerTest {
+
+
+    protected String getSimpleEndpointUri() {
+        return "cxf://http://localhost:9000/simple?serviceClass=org.apache.camel.component.cxf.HelloService";
+    }
+
+    protected String getJaxwsEndpointUri() {
+        return "cxf://http://localhost:9000/jaxws?serviceClass=org.apache.hello_world_soap_http.Greeter";
+
+    }
+    protected CxfExchange sendSimpleMessage() {
+        CxfExchange exchange = (CxfExchange)template.send(getSimpleEndpointUri(), new Processor() {
+            public void process(final Exchange exchange) {
+                final List<String> params = new ArrayList<String>();
+                params.add(TEST_MESSAGE);
+                Map<String, Object> requestContext = new HashMap<String, Object>();
+                requestContext.put(Message.ENDPOINT_ADDRESS, SIMPLE_SERVER_ADDRESS);
+                exchange.getIn().setBody(params);
+                exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext);
+                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION);
+            }
+        });
+        return exchange;
+
+    }
+    protected CxfExchange sendJaxWsMessage() {
+        CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), new Processor() {
+            public void process(final Exchange exchange) {
+                final List<String> params = new ArrayList<String>();
+                params.add(TEST_MESSAGE);
+                Map<String, Object> requestContext = new HashMap<String, Object>();
+                requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, JAXWS_SERVER_ADDRESS);
+                exchange.getIn().setBody(params);
+                exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext);
+                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
+            }
+        });
+        return exchange;
+    }
+}

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

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

Modified: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerRouterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerRouterTest.java?rev=659468&r1=659467&r2=659468&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerRouterTest.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerRouterTest.java Fri May 23 01:22:59 2008
@@ -18,6 +18,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -27,8 +28,10 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.cxf.bus.CXFBusFactory;
+import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.ServerImpl;
 import org.apache.cxf.frontend.ServerFactoryBean;
+import org.apache.cxf.helpers.CastUtils;
 
 public class CxfProducerRouterTest extends ContextTestSupport {
     private static final transient Log LOG = LogFactory.getLog(CxfProducerRouterTest.class);
@@ -69,6 +72,7 @@
         };
     }
 
+
     public void testInvokingSimpleServerWithParams() throws Exception {
         Exchange senderExchange = new DefaultExchange(context, ExchangePattern.InOut);
         final List<String> params = new ArrayList<String>();
@@ -81,7 +85,10 @@
         org.apache.camel.Message out = exchange.getOut();
         Object[] output = (Object[])out.getBody();
         LOG.info("Received output text: " + output[0]);
-        assertEquals("reply body on Camel", "echo " + TEST_MESSAGE, output[0]);
+        Map<String, Object> responseContext = CastUtils.cast((Map)out.getHeader(Client.RESPONSE_CONTEXT));
+        assertNotNull(responseContext);
+        assertEquals("We should get the response context here", "UTF-8", responseContext.get(org.apache.cxf.message.Message.ENCODING));
+        assertEquals("Reply body on Camel is wrong", "echo " + TEST_MESSAGE, output[0]);
     }
 
 

Modified: activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java?rev=659468&r1=659467&r2=659468&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java (original)
+++ activemq/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfProducerTest.java Fri May 23 01:22:59 2008
@@ -18,6 +18,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import javax.xml.ws.Endpoint;
 
@@ -30,20 +31,24 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.cxf.bus.CXFBusFactory;
+import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.ServerImpl;
 import org.apache.cxf.frontend.ServerFactoryBean;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.hello_world_soap_http.GreeterImpl;
 
 /**
  * @version $Revision$
  */
 public class CxfProducerTest extends TestCase {
+    protected static final String ECHO_OPERATION = "echo";
+    protected static final String GREET_ME_OPERATION = "greetMe";
+    protected static final String TEST_MESSAGE = "Hello World!";
+    protected static final String SIMPLE_SERVER_ADDRESS = "http://localhost:28080/test";
+    protected static final String JAXWS_SERVER_ADDRESS = "http://localhost:28081/test";
+
     private static final transient Log LOG = LogFactory.getLog(CxfProducerTest.class);
-    private static final String SIMPLE_SERVER_ADDRESS = "http://localhost:28080/test";
-    private static final String JAXWS_SERVER_ADDRESS = "http://localhost:28081/test";
-    private static final String ECHO_OPERATION = "echo";
-    private static final String GREET_ME_OPERATION = "greetMe";
-    private static final String TEST_MESSAGE = "Hello World!";
+
     protected CamelContext camelContext = new DefaultCamelContext();
     protected CamelTemplate<CxfExchange> template = new CamelTemplate<CxfExchange>(camelContext);
 
@@ -78,47 +83,63 @@
         }
     }
 
+
     public void testInvokingSimpleServerWithParams() throws Exception {
 
-        CxfExchange exchange = (CxfExchange)template.send(getSimpleEndpointUri(), new Processor() {
-            public void process(final Exchange exchange) {
-                final List<String> params = new ArrayList<String>();
-                params.add(TEST_MESSAGE);
-                exchange.getIn().setBody(params);
-                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION);
-            }
-        });
+        CxfExchange exchange = sendSimpleMessage();
 
         org.apache.camel.Message out = exchange.getOut();
         Object[] output = (Object[])out.getBody();
         LOG.info("Received output text: " + output[0]);
+        Map<String, Object> responseContext = CastUtils.cast((Map)out.getHeader(Client.RESPONSE_CONTEXT));
+        assertNotNull(responseContext);
+        assertEquals("We should get the response context here", "UTF-8", responseContext.get(org.apache.cxf.message.Message.ENCODING));
         assertEquals("reply body on Camel", "echo " + TEST_MESSAGE, output[0]);
     }
 
 
-    public void testInvokingJawsServerWithParams() throws Exception {
-        CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), new Processor() {
-            public void process(final Exchange exchange) {
-                final List<String> params = new ArrayList<String>();
-                params.add(TEST_MESSAGE);
-                exchange.getIn().setBody(params);
-                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
-            }
-        });
+    public void testInvokingJaxWsServerWithParams() throws Exception {
+        CxfExchange exchange = sendJaxWsMessage();
 
         org.apache.camel.Message out = exchange.getOut();
         Object[] output = (Object[])out.getBody();
         LOG.info("Received output text: " + output[0]);
+        Map<String, Object> responseContext = CastUtils.cast((Map)out.getHeader(Client.RESPONSE_CONTEXT));
+        assertNotNull(responseContext);
+        assertEquals("Get the wrong wsdl opertion name", "{http://apache.org/hello_world_soap_http}greetMe", responseContext.get("javax.xml.ws.wsdl.operation").toString());
         assertEquals("reply body on Camel", "Hello " + TEST_MESSAGE, output[0]);
     }
 
-    private String getSimpleEndpointUri() {
+    protected String getSimpleEndpointUri() {
         return "cxf://" + SIMPLE_SERVER_ADDRESS
             + "?serviceClass=org.apache.camel.component.cxf.HelloService";
     }
 
-    private String getJaxwsEndpointUri() {
+    protected String getJaxwsEndpointUri() {
         return "cxf://" + JAXWS_SERVER_ADDRESS + "?serviceClass=org.apache.hello_world_soap_http.Greeter";
 
     }
+    protected CxfExchange sendSimpleMessage() {
+        CxfExchange exchange = (CxfExchange)template.send(getSimpleEndpointUri(), new Processor() {
+            public void process(final Exchange exchange) {
+                final List<String> params = new ArrayList<String>();
+                params.add(TEST_MESSAGE);
+                exchange.getIn().setBody(params);
+                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION);
+            }
+        });
+        return exchange;
+
+    }
+    protected CxfExchange sendJaxWsMessage() {
+        CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), new Processor() {
+            public void process(final Exchange exchange) {
+                final List<String> params = new ArrayList<String>();
+                params.add(TEST_MESSAGE);
+                exchange.getIn().setBody(params);
+                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
+            }
+        });
+        return exchange;
+    }
 }