You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/03/01 09:23:40 UTC

camel git commit: CAMEL-8383: CXFRS Consumer processors should be able to use JAX-RS contexts. Thanks to Sergey for the patch.

Repository: camel
Updated Branches:
  refs/heads/master 6560d5bbf -> e086d29fa


CAMEL-8383: CXFRS Consumer processors should be able to use JAX-RS contexts. Thanks to Sergey for the patch.


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

Branch: refs/heads/master
Commit: e086d29fa73cf4bcd628f3874a450d8bb662593b
Parents: 6560d5b
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Mar 1 09:24:49 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Mar 1 09:24:49 2015 +0100

----------------------------------------------------------------------
 .../component/cxf/jaxrs/CxfRsEndpoint.java      | 10 ++++
 .../camel/component/cxf/jaxrs/CxfRsInvoker.java | 56 ++++++++++++--------
 .../component/cxf/jaxrs/CxfRsConsumerTest.java  | 28 +++++++++-
 3 files changed, 71 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e086d29f/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
index 3680c58..68a0cbe 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
@@ -121,6 +121,8 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     @UriParam
     private boolean performInvocation;
     @UriParam
+    private boolean propagateContexts;
+    @UriParam
     private String modelRef;
     private List<Feature> features = new ModCountCopyOnWriteArrayList<Feature>();
     private InterceptorHolder interceptorHolder = new InterceptorHolder();
@@ -621,4 +623,12 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public void setPerformInvocation(boolean performInvocation) {
         this.performInvocation = performInvocation;
     }
+
+    public boolean isPropagateContexts() {
+        return propagateContexts;
+    }
+
+    public void setPropagateContexts(boolean propagateContexts) {
+        this.propagateContexts = propagateContexts;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e086d29f/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
index 179ec05..fb999f0 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
@@ -19,6 +19,10 @@ package org.apache.camel.component.cxf.jaxrs;
 import java.lang.reflect.Method;
 
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.ExchangePattern;
@@ -26,6 +30,10 @@ import org.apache.camel.RuntimeCamelException;
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationProvider;
 import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
+import org.apache.cxf.jaxrs.impl.RequestImpl;
+import org.apache.cxf.jaxrs.impl.SecurityContextImpl;
+import org.apache.cxf.jaxrs.impl.UriInfoImpl;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.message.Exchange;
 import org.slf4j.Logger;
@@ -74,16 +82,7 @@ public class CxfRsInvoker extends JAXRSInvoker {
                               Object[] paramArray, final Continuation continuation, Object response) throws Exception {
         synchronized (continuation) {
             if (continuation.isNew()) {
-                ExchangePattern ep = ExchangePattern.InOut;
-                if (method.getReturnType() == Void.class) {
-                    ep = ExchangePattern.InOnly;
-                } 
-                final org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
-                if (response != null) {
-                    camelExchange.getOut().setBody(response);
-                }
-                CxfRsBinding binding = endpoint.getBinding();
-                binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
+                final org.apache.camel.Exchange camelExchange = prepareExchange(cxfExchange, method, paramArray, response);
                 // we want to handle the UoW
                 cxfRsConsumer.createUoW(camelExchange);
                 // Now we don't set up the timeout value
@@ -120,17 +119,7 @@ public class CxfRsInvoker extends JAXRSInvoker {
     private Object syncInvoke(Exchange cxfExchange, final Object serviceObject, Method method,
                               Object[] paramArray,
                               Object response) throws Exception {
-        ExchangePattern ep = ExchangePattern.InOut;
-        
-        if (method.getReturnType() == Void.class) {
-            ep = ExchangePattern.InOnly;
-        } 
-        org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
-        if (response != null) {
-            camelExchange.getOut().setBody(response);
-        }
-        CxfRsBinding binding = endpoint.getBinding();
-        binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
+        final org.apache.camel.Exchange camelExchange = prepareExchange(cxfExchange, method, paramArray, response);
         // we want to handle the UoW
         cxfRsConsumer.createUoW(camelExchange);
 
@@ -147,6 +136,31 @@ public class CxfRsInvoker extends JAXRSInvoker {
         }
     }
     
+    private org.apache.camel.Exchange prepareExchange(Exchange cxfExchange, Method method,
+            Object[] paramArray, Object response) {
+        ExchangePattern ep = ExchangePattern.InOut;
+        if (method.getReturnType() == Void.class) {
+            ep = ExchangePattern.InOnly;
+        } 
+        final org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
+        if (response != null) {
+            camelExchange.getOut().setBody(response);
+        }
+        CxfRsBinding binding = endpoint.getBinding();
+        binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
+        
+        // REVISIT: It can be done inside a binding but a propagateContext would need to be passed along as
+        // the CXF in message property. Question: where should this property name be set up ? 
+        if (endpoint.isPropagateContexts()) {
+            camelExchange.setProperty(UriInfo.class.getName(), new UriInfoImpl(cxfExchange.getInMessage()));
+            camelExchange.setProperty(Request.class.getName(), new RequestImpl(cxfExchange.getInMessage()));
+            camelExchange.setProperty(HttpHeaders.class.getName(), new HttpHeadersImpl(cxfExchange.getInMessage()));
+            camelExchange.setProperty(SecurityContext.class.getName(), new SecurityContextImpl(cxfExchange.getInMessage()));
+        }
+        
+        return camelExchange;
+    }
+    
     private Object returnResponse(Exchange cxfExchange, org.apache.camel.Exchange camelExchange) throws Exception {
         if (camelExchange.getException() != null) {
             Throwable exception = camelExchange.getException();

http://git-wip-us.apache.org/repos/asf/camel/blob/e086d29f/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
index d5153dd..fafccf3 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
@@ -28,7 +28,9 @@ import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -65,10 +67,14 @@ public class CxfRsConsumerTest extends CamelTestSupport {
     private static final String CXF_RS_ENDPOINT_URI4 = 
             "cxfrs://http://localhost:" + CXT + "/rest4?"
             + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml";
-    
+    private static final String CXF_RS_ENDPOINT_URI5 = 
+            "cxfrs://http://localhost:" + CXT + "/rest5?"
+            + "propagateContexts=true&"
+            + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml";
     protected RouteBuilder createRouteBuilder() throws Exception {
         final Processor testProcessor = new TestProcessor();
         final Processor testProcessor2 = new TestProcessor2();
+        final Processor testProcessor3 = new TestProcessor3();
         return new RouteBuilder() {
             public void configure() {
                 errorHandler(new NoErrorHandlerBuilder());
@@ -76,6 +82,7 @@ public class CxfRsConsumerTest extends CamelTestSupport {
                 from(CXF_RS_ENDPOINT_URI2).process(testProcessor);
                 from(CXF_RS_ENDPOINT_URI3).process(testProcessor);
                 from(CXF_RS_ENDPOINT_URI4).process(testProcessor2);
+                from(CXF_RS_ENDPOINT_URI5).process(testProcessor3);
             }
         };
     }
@@ -121,7 +128,10 @@ public class CxfRsConsumerTest extends CamelTestSupport {
         assertEquals(333L, c.getId());
         assertEquals("Barry", c.getName());
     }
-    
+    @Test
+    public void testGetCustomerDefaultHandlerAndModelAndContexts() throws Exception {
+        doTestGetCustomer("rest5");
+    }
     private void doTestGetCustomer(String contextUri) throws Exception {
         invokeGetCustomer("http://localhost:" + CXT + "/" + contextUri + "/customerservice/customers/126",
                           "{\"Customer\":{\"id\":126,\"name\":\"Willem\"}}");
@@ -263,4 +273,18 @@ public class CxfRsConsumerTest extends CamelTestSupport {
         }
             
     }
+    private static class TestProcessor3 extends AbstractTestProcessor {
+        public void process(Exchange exchange) throws Exception {
+            UriInfo ui = exchange.getProperty(UriInfo.class.getName(), UriInfo.class);
+            String path = ui.getPath();
+            
+            Request req = exchange.getProperty(Request.class.getName(), Request.class);
+            String httpMethod = req.getMethod();
+            
+            if (path.startsWith("customerservice/customers") && HttpMethod.GET.equals(httpMethod)) {
+                processGetCustomer(exchange);
+            }
+        }
+            
+    }
 }