Author: ningjiang Date: Tue Sep 15 03:52:41 2009 New Revision: 814987 URL: http://svn.apache.org/viewvc?rev=814987&view=rev Log: CAMEL-2014, CAMEL-2015 made some improvements on the camel-cxfrs component
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpointTest.java camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringProducer.xml Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java Tue Sep 15 03:52:41 2009 @@ -88,7 +88,7 @@ } // if endpoint is strict (not lenient) and we have unknown parameters configured then - // fail if there are parameters that could not be set, then they are probably miss spelt or not supported at all + // fail if there are parameters that could not be set, then they are probably misspell or not supported at all if (!endpoint.isLenientProperties()) { validateParameters(uri, parameters, null); } Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsComponent.java Tue Sep 15 03:52:41 2009 @@ -69,9 +69,10 @@ // endpoint URI does not specify a bean answer = new CxfRsEndpoint(remaining, this); } - + answer.setParameters((Map<String, String>)parameters); setEndpointHeaderFilterStrategy(answer); return answer; } - + + } Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java Tue Sep 15 03:52:41 2009 @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.camel.CamelContext; @@ -43,9 +44,11 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware { private static final Log LOG = LogFactory.getLog(CxfRsEndpoint.class); + private Map<String, String> parameters; private List<Class<?>> resourceClasses; private HeaderFilterStrategy headerFilterStrategy; private CxfRsBinding binding; + private boolean httpClientAPI; private AtomicBoolean bindingInitialized = new AtomicBoolean(false); @@ -57,6 +60,27 @@ super(endpointUri, component); } + public void setParameters(Map<String, String> param) { + parameters = param; + } + + public Map<String, String> getParameters() { + return parameters; + } + + public void setHttpClientAPI(boolean clientAPI) { + httpClientAPI = clientAPI; + } + + public boolean isHttpClientAPI() { + return httpClientAPI; + } + + @Override + public boolean isLenientProperties() { + return true; + } + public HeaderFilterStrategy getHeaderFilterStrategy() { if (headerFilterStrategy == null) { headerFilterStrategy = new CxfRsHeaderFilterStrategy(); Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java Tue Sep 15 03:52:41 2009 @@ -19,6 +19,8 @@ import java.lang.reflect.Method; import java.util.List; +import java.util.Map; +import java.util.Set; import javax.ws.rs.core.Response; @@ -57,7 +59,11 @@ Message inMessage = exchange.getIn(); Boolean httpClientAPI = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.class); - if (httpClientAPI != null && httpClientAPI.booleanValue()) { + // set the value with endpoint's option + if (httpClientAPI == null) { + httpClientAPI = ((CxfRsEndpoint)getEndpoint()).isHttpClientAPI(); + } + if (httpClientAPI.booleanValue()) { invokeHttpClient(exchange); } else { invokeProxyClient(exchange); @@ -83,9 +89,17 @@ // set the path if (path != null) { client.path(path); - } + } + + CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint)getEndpoint(); + Map<String, String> maps = cxfRsEndpoint.getParameters(); + if (maps != null) { + for (Map.Entry<String, String> entry : maps.entrySet()) { + client.query(entry.getKey(), entry.getValue()); + } + } - CxfRsBinding binding = ((CxfRsEndpoint)getEndpoint()).getBinding(); + CxfRsBinding binding = cxfRsEndpoint.getBinding(); // set the body Object body = null; Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpointTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpointTest.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpointTest.java (original) +++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpointTest.java Tue Sep 15 03:52:41 2009 @@ -30,9 +30,21 @@ + "java.lang.String ; org.apache.camel.component.cxf.jaxrs.testbean.Order"); assertNotNull("The endpoint should not be null ", endpoint); - assertEquals("Get a wrong address ", endpoint.getEndpointUri(), "http://localhost:9000"); - assertEquals("Get a wrong size of resouces classes", endpoint.getResourceClasses().size(), 3); - assertEquals("Get a wrong resources class", endpoint.getResourceClasses().get(0), CustomerService.class); + assertEquals("Get a wrong address ", "http://localhost:9000", endpoint.getEndpointUri()); + assertEquals("Get a wrong size of resouces classes", 3, endpoint.getResourceClasses().size()); + assertEquals("Get a wrong resources class", CustomerService.class, endpoint.getResourceClasses().get(0)); + } + + @Test + public void testCxfRsEndpointParameters() throws Exception { + CxfRsComponent component = new CxfRsComponent(context); + CxfRsEndpoint endpoint = (CxfRsEndpoint)component.createEndpoint("cxfrs://http://localhost:9000/templatetest/TID/ranges/start=0;end=1?" + + "httpClientAPI=true&q1=11&q2=12"); + + assertEquals("Get a wrong URI ", "http://localhost:9000/templatetest/TID/ranges/start=0;end=1", endpoint.getEndpointUri()); + assertEquals("Get a wrong usingClientAPI option", true, endpoint.isHttpClientAPI()); + assertNotNull("The Parameter should not be null" + endpoint.getParameters()); + assertEquals("Get a wrong parameter map", "{q1=11, q2=12}", endpoint.getParameters().toString()); } } Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java (original) +++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java Tue Sep 15 03:52:41 2009 @@ -16,6 +16,8 @@ */ package org.apache.camel.component.cxf.jaxrs; +import java.io.InputStream; + import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.Message; @@ -28,6 +30,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; public class CxfRsProducerTest extends CamelSpringTestSupport { + + public static class JettyProcessor implements Processor { + public void process(Exchange exchange) throws Exception { + // check the query + Message inMessage = exchange.getIn(); + exchange.getOut().setBody(inMessage.getHeader(Exchange.HTTP_QUERY, String.class)); + } + } @Override protected AbstractXmlApplicationContext createApplicationContext() { @@ -93,5 +103,59 @@ // END SNIPPET: example-http } + @Test + public void testGetConstumerWithCxfRsEndpoint() { + + Exchange exchange = template.send("cxfrs://http://localhost:9002?httpClientAPI=true", new Processor() { + + public void process(Exchange exchange) throws Exception { + exchange.setPattern(ExchangePattern.InOut); + Message inMessage = exchange.getIn(); + // set the Http method + inMessage.setHeader(Exchange.HTTP_METHOD, "GET"); + // set the relative path + inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123"); + // Specify the response class , cxfrs will use InputStream as the response object type + inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class); + // since we use the Get method, so we don't need to set the message body + inMessage.setBody(null); + } + + }); + + // get the response message + Customer response = (Customer) exchange.getOut().getBody(); + + assertNotNull("The response should not be null ", response); + assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123"); + assertEquals("Get a wrong customer name", response.getName(), "John"); + + } + + @Test + public void testProducerWithQueryParameters() { + + Exchange exchange = template.send("cxfrs://http://localhost:9003/testQuery?httpClientAPI=true&q1=12&q2=13", new Processor() { + + public void process(Exchange exchange) throws Exception { + exchange.setPattern(ExchangePattern.InOut); + Message inMessage = exchange.getIn(); + // set the Http method + inMessage.setHeader(Exchange.HTTP_METHOD, "GET"); + inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class); + inMessage.setBody(null); + } + + }); + + // get the response message + String response = exchange.getOut().getBody(String.class); + + assertNotNull("The response should not be null ", response); + assertEquals("The response value is wrong", "q1=12&q2=13", response); + + + } + } Modified: camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringProducer.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringProducer.xml?rev=814987&r1=814986&r2=814987&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringProducer.xml (original) +++ camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringProducer.xml Tue Sep 15 03:52:41 2009 @@ -50,6 +50,12 @@ <from uri="direct://http"/> <to uri="cxfrs://bean://rsClientHttp"/> </route> + <route> + <from uri="jetty://http://localhost:9003/testQuery"/> + <process ref="myProcessor" /> + </route> </camelContext> + <bean id="myProcessor" class="org.apache.camel.component.cxf.jaxrs.CxfRsProducerTest$JettyProcessor"/> + </beans>