Repository: camel Updated Branches: refs/remotes/origin/camel-2.13.x 653f8d6f2 -> f0ddb5ef7
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/c7d05d4c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c7d05d4c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c7d05d4c Branch: refs/remotes/origin/camel-2.13.x Commit: c7d05d4cf659914234c3c1a043c428c1acec911c Parents: 9fca194 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:25 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/c7d05d4c/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 dec2747..d3e29c2 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,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); @@ -473,8 +479,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/c7d05d4c/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; + } + } + +}