You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ra...@apache.org on 2014/09/09 16:50:12 UTC

git commit: CAMEL-7796 camel-cxfrs consumer: Allow setting a custom binding.

Repository: camel
Updated Branches:
  refs/heads/camel-2.12.x 3765611b6 -> e6768fcd4


CAMEL-7796 camel-cxfrs consumer: Allow setting a custom binding.


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

Branch: refs/heads/camel-2.12.x
Commit: e6768fcd4f92226e2318b38c664b0ca8d3e969d4
Parents: 3765611
Author: Raul Kripalani <ra...@apache.org>
Authored: Tue Sep 9 15:45:59 2014 +0100
Committer: Raul Kripalani <ra...@apache.org>
Committed: Tue Sep 9 15:49:36 2014 +0100

----------------------------------------------------------------------
 .../component/cxf/jaxrs/CxfRsEndpoint.java      |  29 ++++-
 .../CxfRsBindingConfigurationSelectionTest.java | 127 +++++++++++++++++++
 2 files changed, 153 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e6768fcd/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 8c3ff1b..a2f4d32 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
@@ -67,7 +67,13 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
          * 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.
          */
-        Default
+        Default,
+
+        /**
+         * A custom binding set by the user.
+         */
+        Custom
+
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(CxfRsEndpoint.class);
@@ -475,8 +481,25 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         if (headerFilterStrategy == null) {
             headerFilterStrategy = new CxfRsHeaderFilterStrategy();
         }
-        
-        binding = bindingStyle == null || bindingStyle == BindingStyle.Default ? new DefaultCxfRsBinding() : new SimpleCxfRsBinding();
+
+        // if the user explicitly selected the Custom binding style, he must provide a binding
+        if (bindingStyle == BindingStyle.Custom && binding == null) {
+            throw new IllegalArgumentException("Custom binding style selected, but no binding was supplied");
+        }
+
+        // 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) {
+            binding = new SimpleCxfRsBinding();
+        } else if (bindingStyle == BindingStyle.Custom) {
+            // do nothing
+        } else {
+            binding = new DefaultCxfRsBinding();
+        }
         
         if (binding instanceof HeaderFilterStrategyAware) {
             ((HeaderFilterStrategyAware) binding).setHeaderFilterStrategy(getHeaderFilterStrategy());

http://git-wip-us.apache.org/repos/asf/camel/blob/e6768fcd/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
new file mode 100644
index 0000000..e5d94af
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
@@ -0,0 +1,127 @@
+/**
+ * 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.jaxrs;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cxf.CXFTestSupport;
+import org.apache.camel.component.cxf.jaxrs.CxfRsEndpoint.BindingStyle;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Tests different binding configuration options of the CXFRS consumer. 
+ */
+public class CxfRsBindingConfigurationSelectionTest extends CamelTestSupport {
+    
+    private static final String RESOURCE_CLASS = "resourceClasses=org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerServiceResource";
+    private static final String CXF_RS_ENDPOINT_URI_CUSTOM = String.format("cxfrs://http://localhost:%s/CxfRsConsumerTest/rest?bindingStyle=Custom&", CXFTestSupport.getPort2()) 
+            + RESOURCE_CLASS + "&binding=#binding";
+    private static final String CXF_RS_ENDPOINT_URI_SIMPLE = String.format("cxfrs://http://localhost:%s/CxfRsConsumerTest/rest?bindingStyle=SimpleConsumer&", CXFTestSupport.getPort1()) 
+            + RESOURCE_CLASS;
+    private static final String CXF_RS_ENDPOINT_URI_DEFAULT = String.format("cxfrs://http://localhost:%s/CxfRsConsumerTest/rest?bindingStyle=Default&", CXFTestSupport.getPort3()) + RESOURCE_CLASS;
+    private static final String CXF_RS_ENDPOINT_URI_NONE = String.format("cxfrs://http://localhost:%s/CxfRsConsumerTest/rest?", CXFTestSupport.getPort4()) + RESOURCE_CLASS;
+    
+    @Test
+    public void testCxfRsBindingConfiguration() {
+        // check binding styles
+        assertEquals(BindingStyle.Custom, endpointForRouteId("custom").getBindingStyle());
+        assertEquals(BindingStyle.SimpleConsumer, endpointForRouteId("simple").getBindingStyle());
+        assertEquals(BindingStyle.Default, endpointForRouteId("default").getBindingStyle());
+        assertEquals(BindingStyle.Default, endpointForRouteId("none").getBindingStyle());
+        
+        // check binding implementations
+        assertEquals(DummyCxfRsBindingImplementation.class, endpointForRouteId("custom").getBinding().getClass());
+        assertEquals(SimpleCxfRsBinding.class, endpointForRouteId("simple").getBinding().getClass());
+        assertEquals(DefaultCxfRsBinding.class, endpointForRouteId("default").getBinding().getClass());
+        assertEquals(DefaultCxfRsBinding.class, endpointForRouteId("default").getBinding().getClass());
+    }
+    
+    private CxfRsEndpoint endpointForRouteId(String routeId) {
+        return (CxfRsEndpoint) context.getRoute(routeId).getConsumer().getEndpoint();
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry answer = super.createRegistry();
+        answer.bind("binding", new DummyCxfRsBindingImplementation());
+        return answer;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                
+                from(CXF_RS_ENDPOINT_URI_CUSTOM).routeId("custom")
+                    .to("log:foo");
+                
+                from(CXF_RS_ENDPOINT_URI_SIMPLE).routeId("simple")
+                    .to("log:foo");
+                
+                from(CXF_RS_ENDPOINT_URI_DEFAULT).routeId("default")
+                    .to("log:foo");
+                
+                from(CXF_RS_ENDPOINT_URI_NONE).routeId("none")
+                    .to("log:foo");
+                
+            }
+        };
+    }
+    
+    private final class DummyCxfRsBindingImplementation implements CxfRsBinding {
+        @Override
+        public void populateExchangeFromCxfRsRequest(org.apache.cxf.message.Exchange cxfExchange, Exchange camelExchange, Method method, Object[] paramArray) {
+        }
+
+        @Override
+        public Object populateCxfRsResponseFromExchange(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) throws Exception {
+            return null;
+        }
+
+        @Override
+        public Object bindResponseToCamelBody(Object response, Exchange camelExchange) throws Exception {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public Map<String, Object> bindResponseHeadersToCamelHeaders(Object response, Exchange camelExchange) throws Exception {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public Object bindCamelMessageBodyToRequestBody(Message camelMessage, Exchange camelExchange) throws Exception {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public MultivaluedMap<String, String> bindCamelHeadersToRequestHeaders(Map<String, Object> camelHeaders, Exchange camelExchange) throws Exception {
+            // TODO Auto-generated method stub
+            return null;
+        }
+    }
+
+}