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 2015/01/18 04:24:39 UTC

[2/2] camel git commit: CAMEL-8205 CXFRS Server support to invoke on JAX-RS service implementations with thanks to Sergey.

CAMEL-8205 CXFRS Server support to invoke on JAX-RS service implementations with thanks to Sergey.


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

Branch: refs/heads/master
Commit: 1ca7fea702332410b7413cf9cbe785a088df8e0a
Parents: 46c5abc
Author: Willem Jiang <wi...@gmail.com>
Authored: Sun Jan 18 11:19:43 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Sun Jan 18 11:19:43 2015 +0800

----------------------------------------------------------------------
 .../component/cxf/jaxrs/CxfRsEndpoint.java      | 88 +++++++++++---------
 .../camel/component/cxf/jaxrs/CxfRsInvoker.java | 20 +++--
 .../cxf/jaxrs/CxfRsSpringConsumerTest.java      | 17 ++++
 .../component/cxf/jaxrs/CxfRsSpringConsumer.xml |  3 +
 4 files changed, 84 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1ca7fea7/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 b032e1b..70bf5d2 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
@@ -65,7 +65,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
          * It also also adds more flexibility and simplicity to the response mapping.
          */
         SimpleConsumer,
-        
+
         /**
          * This is the traditional binding style, which simply dumps the {@link org.apache.cxf.message.MessageContentsList} coming in from the CXF stack
          * onto the IN message body. The user is then responsible for processing it according to the contract defined by the JAX-RS method signature.
@@ -81,9 +81,9 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     private static final Logger LOG = LoggerFactory.getLogger(CxfRsEndpoint.class);
 
     protected Bus bus;
-    
+
     protected List<Object> entityProviders = new LinkedList<Object>();
-    
+
     protected List<String> schemaLocations;
 
     @UriPath(description = "To lookup an existing configured CxfRsEndpoint. Must used bean: as prefix.")
@@ -116,12 +116,14 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     private long continuationTimeout = 30000;
     @UriParam(defaultValue = "false")
     private boolean isSetDefaultBus;
+    @UriParam(defaultValue = "false")
+    private boolean performInvocation;
     @UriParam
-    private String modelRef;    
+    private String modelRef;
     private List<Feature> features = new ModCountCopyOnWriteArrayList<Feature>();
     private InterceptorHolder interceptorHolder = new InterceptorHolder();
     private Map<String, Object> properties;
-   
+
     public CxfRsEndpoint() {
     }
 
@@ -194,7 +196,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public CxfRsBinding getBinding() {
         return binding;
     }
-    
+
     public boolean isSkipFaultLogging() {
         return skipFaultLogging;
     }
@@ -202,7 +204,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public void setSkipFaultLogging(boolean skipFaultLogging) {
         this.skipFaultLogging = skipFaultLogging;
     }
-    
+
     protected void checkBeanType(Object object, Class<?> clazz) {
         if (!clazz.isAssignableFrom(object.getClass())) {
             throw new IllegalArgumentException("The configure bean is not the instance of " + clazz.getName());
@@ -220,13 +222,13 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         if (getResourceClasses() != null) {
             sfb.setResourceClasses(getResourceClasses());
         }
-        
+
         // setup the resource providers for interfaces
         List<ClassResourceInfo> cris = sfb.getServiceFactory().getClassResourceInfo();
         for (ClassResourceInfo cri : cris) {
-            final Class<?> serviceClass = cri.getServiceClass(); 
+            final Class<?> serviceClass = cri.getServiceClass();
             if (serviceClass.isInterface()) {
-                cri.setResourceProvider(new CamelResourceProvider(serviceClass)); 
+                cri.setResourceProvider(new CamelResourceProvider(serviceClass));
             }
         }
         setupCommonFactoryProperties(sfb);
@@ -248,7 +250,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         setupCommonFactoryProperties(cfb);
         cfb.setThreadSafe(true);
     }
-    
+
     protected void setupCommonFactoryProperties(AbstractJAXRSFactoryBean factory) {
         // let customer to override the default setting of provider
         if (!getProviders().isEmpty()) {
@@ -258,11 +260,11 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         if (!getFeatures().isEmpty()) {
             factory.getFeatures().addAll(getFeatures());
         }
-        
+
         // we need to avoid flushing the setting from spring or blueprint
         if (!interceptorHolder.getInInterceptors().isEmpty()) {
             factory.setInInterceptors(interceptorHolder.getInInterceptors());
-        } 
+        }
         if (!interceptorHolder.getOutInterceptors().isEmpty()) {
             factory.setOutInterceptors(interceptorHolder.getOutInterceptors());
         }
@@ -270,9 +272,9 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
             factory.setOutFaultInterceptors(interceptorHolder.getOutFaultInterceptors());
         }
         if (!interceptorHolder.getInFaultInterceptors().isEmpty()) {
-            factory.setInFaultInterceptors(interceptorHolder.getInFaultInterceptors()); 
+            factory.setInFaultInterceptors(interceptorHolder.getInFaultInterceptors());
         }
-        
+
         if (getProperties() != null) {
             if (factory.getProperties() != null) {
                 // add to existing properties
@@ -282,7 +284,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
             }
             LOG.debug("JAXRS FactoryBean: {} added properties: {}", factory, getProperties());
         }
-        
+
         if (isLoggingFeatureEnabled()) {
             if (getLoggingSizeLimit() > 0) {
                 factory.getFeatures().add(new LoggingFeature(getLoggingSizeLimit()));
@@ -297,15 +299,15 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
             factory.getProperties().put(FaultListener.class.getName(), new NullFaultListener());
         }
     }
-    
+
     protected JAXRSServerFactoryBean newJAXRSServerFactoryBean() {
         return new JAXRSServerFactoryBean();
     }
-    
+
     protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() {
         return new JAXRSClientFactoryBean();
     }
-    
+
     protected String resolvePropertyPlaceholders(String str) {
         try {
             if (getCamelContext() != null) {
@@ -324,12 +326,12 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         setupJAXRSServerFactoryBean(answer);
         return answer;
     }
-    
+
 
     public JAXRSClientFactoryBean createJAXRSClientFactoryBean() {
         return createJAXRSClientFactoryBean(getAddress());
     }
-    
+
     public JAXRSClientFactoryBean createJAXRSClientFactoryBean(String address) {
         JAXRSClientFactoryBean answer = newJAXRSClientFactoryBean();
         setupJAXRSClientFactoryBean(answer, address);
@@ -354,14 +356,13 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public void setResourceClasses(Class<?>... classes) {
         setResourceClasses(Arrays.asList(classes));
     }
-    
+
     public void setAddress(String address) {
         this.address = address;
     }
     public void setModelRef(String ref) {
         this.modelRef = ref;
     }
-
     public String getAddress() {
         return resolvePropertyPlaceholders(address);
     }
@@ -403,7 +404,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public int getMaxClientCacheSize() {
         return maxClientCacheSize;
     }
-    
+
     public void setBus(Bus bus) {
         this.bus = bus;
         if (isSetDefaultBus) {
@@ -415,7 +416,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public Bus getBus() {
         return bus;
     }
-    
+
     public void setSetDefaultBus(boolean isSetDefaultBus) {
         this.isSetDefaultBus = isSetDefaultBus;
     }
@@ -423,7 +424,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public boolean isSetDefaultBus() {
         return isSetDefaultBus;
     }
-    
+
     public boolean isIgnoreDeleteMethodMessageBody() {
         return ignoreDeleteMethodMessageBody;
     }
@@ -435,27 +436,27 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public BindingStyle getBindingStyle() {
         return bindingStyle;
     }
-    
+
     public List<?> getProviders() {
         return entityProviders;
     }
-    
+
     public void setProviders(List<?> providers) {
         this.entityProviders.addAll(providers);
     }
-    
+
     public void setProvider(Object provider) {
         entityProviders.add(provider);
     }
-    
+
     public void setSchemaLocation(String schema) {
-        setSchemaLocations(Collections.singletonList(schema));    
+        setSchemaLocations(Collections.singletonList(schema));
     }
-    
+
     public void setSchemaLocations(List<String> schemas) {
-        this.schemaLocations = schemas;    
+        this.schemaLocations = schemas;
     }
-    
+
     public List<String> getSchemaLocations() {
         return schemaLocations;
     }
@@ -491,7 +492,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public void setOutFaultInterceptors(List<Interceptor<? extends Message>> interceptors) {
         interceptorHolder.setOutFaultInterceptors(interceptors);
     }
-    
+
     public List<Feature> getFeatures() {
         return features;
     }
@@ -503,7 +504,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     public Map<String, Object> getProperties() {
         return properties;
     }
-    
+
     public void setProperties(Map<String, Object> properties) {
         if (this.properties == null) {
             this.properties = properties;
@@ -511,7 +512,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
             this.properties.putAll(properties);
         }
     }
-    
+
     /**
      * See documentation of {@link BindingStyle}.
      */
@@ -541,7 +542,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         // if the user has set a binding, do nothing, just make sure that BindingStyle = Custom for coherency purposes
         if (binding != null) {
             bindingStyle = BindingStyle.Custom;
-        } 
+        }
 
         // set the right binding based on the binding style
         if (bindingStyle == BindingStyle.SimpleConsumer) {
@@ -551,7 +552,7 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         } else {
             binding = new DefaultCxfRsBinding();
         }
-        
+
         if (binding instanceof HeaderFilterStrategyAware) {
             ((HeaderFilterStrategyAware) binding).setHeaderFilterStrategy(getHeaderFilterStrategy());
         }
@@ -574,4 +575,13 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
 
     private static class InterceptorHolder extends AbstractBasicInterceptorProvider {
     }
+
+
+    public boolean isPerformInvocation() {
+        return performInvocation;
+    }
+
+    public void setPerformInvocation(boolean performInvocation) {
+        this.performInvocation = performInvocation;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1ca7fea7/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 ec51874..179ec05 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
@@ -44,6 +44,10 @@ public class CxfRsInvoker extends JAXRSInvoker {
         
     protected Object performInvocation(Exchange cxfExchange, final Object serviceObject, Method method,
                                        Object[] paramArray) throws Exception {
+        Object response = null;
+        if (endpoint.isPerformInvocation()) {
+            response = super.performInvocation(cxfExchange, serviceObject, method, paramArray);
+        }
         paramArray = insertExchange(method, paramArray, cxfExchange);
         OperationResourceInfo ori = cxfExchange.get(OperationResourceInfo.class);        
         if (ori.isSubResourceLocator()) {
@@ -53,10 +57,10 @@ public class CxfRsInvoker extends JAXRSInvoker {
         Continuation continuation;
         if (!endpoint.isSynchronous() && (continuation = getContinuation(cxfExchange)) != null) {
             LOG.trace("Calling the Camel async processors.");
-            return asyncInvoke(cxfExchange, serviceObject, method, paramArray, continuation);
+            return asyncInvoke(cxfExchange, serviceObject, method, paramArray, continuation, response);
         } else {
             LOG.trace("Calling the Camel sync processors.");
-            return syncInvoke(cxfExchange, serviceObject, method, paramArray);
+            return syncInvoke(cxfExchange, serviceObject, method, paramArray, response);
         }
     }
     
@@ -67,7 +71,7 @@ public class CxfRsInvoker extends JAXRSInvoker {
     }
     
     private Object asyncInvoke(Exchange cxfExchange, final Object serviceObject, Method method,
-                              Object[] paramArray, final Continuation continuation) throws Exception {
+                              Object[] paramArray, final Continuation continuation, Object response) throws Exception {
         synchronized (continuation) {
             if (continuation.isNew()) {
                 ExchangePattern ep = ExchangePattern.InOut;
@@ -75,7 +79,9 @@ public class CxfRsInvoker extends JAXRSInvoker {
                     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);
                 // we want to handle the UoW
@@ -112,13 +118,17 @@ public class CxfRsInvoker extends JAXRSInvoker {
     }
     
     private Object syncInvoke(Exchange cxfExchange, final Object serviceObject, Method method,
-                              Object[] paramArray) throws Exception {
+                              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);
         // we want to handle the UoW

http://git-wip-us.apache.org/repos/asf/camel/blob/1ca7fea7/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java
index 0db5882..7c1381a 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java
@@ -23,7 +23,9 @@ import org.apache.camel.builder.NoErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.cxf.CXFTestSupport;
 import org.apache.camel.component.cxf.jaxrs.testbean.CustomException;
+import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -43,11 +45,19 @@ public class CxfRsSpringConsumerTest extends CamelSpringTestSupport {
                 throw new CustomException("Here is the exception");
             }  
         };
+        final Processor responseProcessor = new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                // do something else with the request properties as usual
+                // do something else with the response
+                exchange.getOut().getBody(Customer.class).setId(246);
+            }  
+        };
         return new RouteBuilder() {
             public void configure() {
                 errorHandler(new NoErrorHandlerBuilder());
                 from("cxfrs://bean://rsServer").process(testProcessor);
                 from("cxfrs://bean://rsServer2").process(testProcessor);
+                from("cxfrs://bean://rsServerInvoke?performInvocation=true").process(responseProcessor);
             }
         };
     }
@@ -68,6 +78,13 @@ public class CxfRsSpringConsumerTest extends CamelSpringTestSupport {
         String address = "http://localhost:" + port1 + "/CxfRsSpringConsumerTest2/customerservice/customers/126";
         doTestMappingException(address);
     }
+    @Test
+    public void testInvokeCxfRsConsumer() throws Exception {
+        String address = "http://localhost:" + port1 + "/CxfRsSpringConsumerInvokeService/customerservice/customers/123";
+        WebClient wc = WebClient.create(address);
+        Customer c = wc.accept("application/json").get(Customer.class);
+        assertEquals(246L, c.getId());
+    }
     
     private void doTestMappingException(String address) throws Exception {
         HttpGet get = new HttpGet(address);

http://git-wip-us.apache.org/repos/asf/camel/blob/1ca7fea7/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml
index 4198563..5cef7ca 100644
--- a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml
+++ b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml
@@ -42,6 +42,9 @@
     </cxf:providers>
   </cxf:rsServer>
   
+  <cxf:rsServer id="rsServerInvoke" address="http://localhost:${CXFTestSupport.port1}/CxfRsSpringConsumerInvokeService/"
+    serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/>
+  
   <bean id="exceptionMapper" class="org.apache.camel.component.cxf.jaxrs.CustomExceptionMapper"/>
   <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/>