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/13 12:27:40 UTC
[3/3] camel git commit: CAMEL-8212 CXFRS Consumer should always be
able to work with interfaces only with thanks to Sergey
CAMEL-8212 CXFRS Consumer should always be able to work with interfaces only 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/51ef6ad5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/51ef6ad5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/51ef6ad5
Branch: refs/heads/master
Commit: 51ef6ad53dcfeae4ca64c80003db5910dc77b73b
Parents: 73321d8
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue Jan 13 19:27:01 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Jan 13 19:27:01 2015 +0800
----------------------------------------------------------------------
.../component/cxf/jaxrs/CxfRsEndpoint.java | 15 +-
.../component/cxf/jaxrs/CxfRsConsumerTest.java | 139 ++++++++++---------
.../cxf/jaxrs/CxfRsSpringConsumerTest.java | 26 +++-
.../component/cxf/jaxrs/CxfRsSpringConsumer.xml | 8 ++
4 files changed, 112 insertions(+), 76 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/51ef6ad5/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 d86c477..b89eded 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
@@ -48,6 +48,7 @@ import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.logging.FaultListener;
import org.apache.cxf.message.Message;
import org.slf4j.Logger;
@@ -213,12 +214,16 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
sfb.setAddress(getAddress());
}
if (getResourceClasses() != null) {
- List<Class<?>> res = getResourceClasses();
- // setup the resource providers
- for (Class<?> clazz : res) {
- sfb.setResourceProvider(clazz, new CamelResourceProvider(clazz));
+ sfb.setResourceClasses(getResourceClasses());
+ }
+
+ // setup the resource providers for interfaces
+ List<ClassResourceInfo> cris = sfb.getServiceFactory().getClassResourceInfo();
+ for (ClassResourceInfo cri : cris) {
+ final Class<?> serviceClass = cri.getServiceClass();
+ if (serviceClass.isInterface()) {
+ cri.setResourceProvider(new CamelResourceProvider(serviceClass));
}
- sfb.setResourceClasses(res);
}
setupCommonFactoryProperties(sfb);
sfb.setStart(false);
http://git-wip-us.apache.org/repos/asf/camel/blob/51ef6ad5/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 9394699..b37cefa 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
@@ -49,73 +49,15 @@ public class CxfRsConsumerTest extends CamelTestSupport {
private static final String CXT = CXFTestSupport.getPort1() + "/CxfRsConsumerTest";
// START SNIPPET: example
private static final String CXF_RS_ENDPOINT_URI = "cxfrs://http://localhost:" + CXT + "/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource";
+ private static final String CXF_RS_ENDPOINT_URI2 = "cxfrs://http://localhost:" + CXT + "/rest2?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
protected RouteBuilder createRouteBuilder() throws Exception {
+ final Processor testProcessor = new TestProcessor();
return new RouteBuilder() {
public void configure() {
errorHandler(new NoErrorHandlerBuilder());
- from(CXF_RS_ENDPOINT_URI).process(new Processor() {
-
- public void process(Exchange exchange) throws Exception {
- Message inMessage = exchange.getIn();
- // Get the operation name from in message
- String operationName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
- if ("getCustomer".equals(operationName)) {
- String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
- assertEquals("Get a wrong http method", "GET", httpMethod);
- String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class);
- // The parameter of the invocation is stored in the body of in message
- String id = inMessage.getBody(String.class);
- if ("/customerservice/customers/126".equals(path)) {
- Customer customer = new Customer();
- customer.setId(Long.parseLong(id));
- customer.setName("Willem");
- // We just put the response Object into the out message body
- exchange.getOut().setBody(customer);
- } else {
- if ("/customerservice/customers/400".equals(path)) {
- // We return the remote client IP address this time
- org.apache.cxf.message.Message cxfMessage = inMessage.getHeader(CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class);
- ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
- // Just make sure the request object is not null
- assertNotNull("The request object should not be null", request);
- Response r = Response.status(200).entity("The remoteAddress is 127.0.0.1").build();
- exchange.getOut().setBody(r);
- return;
- }
- if ("/customerservice/customers/123".equals(path)) {
- // send a customer response back
- Response r = Response.status(200).entity("customer response back!").build();
- exchange.getOut().setBody(r);
- return;
- }
- if ("/customerservice/customers/456".equals(path)) {
- Response r = Response.status(404).entity("Can't found the customer with uri " + path).build();
- throw new WebApplicationException(r);
- } else if ("/customerservice/customers/234".equals(path)) {
- Response r = Response.status(404).entity("Can't found the customer with uri " + path).build();
- exchange.getOut().setBody(r);
- exchange.getOut().setFault(true);
- } else {
- throw new RuntimeCamelException("Can't found the customer with uri " + path);
- }
- }
- }
- if ("updateCustomer".equals(operationName)) {
- assertEquals("Get a wrong customer message header", "header1;header2", inMessage.getHeader("test"));
- String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
- assertEquals("Get a wrong http method", "PUT", httpMethod);
- Customer customer = inMessage.getBody(Customer.class);
- assertNotNull("The customer should not be null.", customer);
- // Now you can do what you want on the customer object
- assertEquals("Get a wrong customer name.", "Mary", customer.getName());
- // set the response back
- exchange.getOut().setBody(Response.ok().build());
- }
-
- }
-
- });
+ from(CXF_RS_ENDPOINT_URI).process(testProcessor);
+ from(CXF_RS_ENDPOINT_URI2).process(testProcessor);
}
};
}
@@ -146,7 +88,16 @@ public class CxfRsConsumerTest extends CamelTestSupport {
"The remoteAddress is 127.0.0.1");
}
-
+ @Test
+ public void testGetCustomer2() throws Exception {
+ invokeGetCustomer("http://localhost:" + CXT + "/rest2/customerservice/customers/126",
+ "{\"Customer\":{\"id\":126,\"name\":\"Willem\"}}");
+ invokeGetCustomer("http://localhost:" + CXT + "/rest2/customerservice/customers/123",
+ "customer response back!");
+ invokeGetCustomer("http://localhost:" + CXT + "/rest2/customerservice/customers/400",
+ "The remoteAddress is 127.0.0.1");
+
+ }
@Test
@@ -195,5 +146,65 @@ public class CxfRsConsumerTest extends CamelTestSupport {
}
}
-
+ private static class TestProcessor implements Processor {
+ public void process(Exchange exchange) throws Exception {
+ Message inMessage = exchange.getIn();
+ // Get the operation name from in message
+ String operationName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
+ if ("getCustomer".equals(operationName)) {
+ String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
+ assertEquals("Get a wrong http method", "GET", httpMethod);
+ String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class);
+ // The parameter of the invocation is stored in the body of in message
+ String id = inMessage.getBody(String.class);
+ if ("/customerservice/customers/126".equals(path)) {
+ Customer customer = new Customer();
+ customer.setId(Long.parseLong(id));
+ customer.setName("Willem");
+ // We just put the response Object into the out message body
+ exchange.getOut().setBody(customer);
+ } else {
+ if ("/customerservice/customers/400".equals(path)) {
+ // We return the remote client IP address this time
+ org.apache.cxf.message.Message cxfMessage = inMessage.getHeader(CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class);
+ ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
+ // Just make sure the request object is not null
+ assertNotNull("The request object should not be null", request);
+ Response r = Response.status(200).entity("The remoteAddress is 127.0.0.1").build();
+ exchange.getOut().setBody(r);
+ return;
+ }
+ if ("/customerservice/customers/123".equals(path)) {
+ // send a customer response back
+ Response r = Response.status(200).entity("customer response back!").build();
+ exchange.getOut().setBody(r);
+ return;
+ }
+ if ("/customerservice/customers/456".equals(path)) {
+ Response r = Response.status(404).entity("Can't found the customer with uri " + path).build();
+ throw new WebApplicationException(r);
+ } else if ("/customerservice/customers/234".equals(path)) {
+ Response r = Response.status(404).entity("Can't found the customer with uri " + path).build();
+ exchange.getOut().setBody(r);
+ exchange.getOut().setFault(true);
+ } else {
+ throw new RuntimeCamelException("Can't found the customer with uri " + path);
+ }
+ }
+ }
+ if ("updateCustomer".equals(operationName)) {
+ assertEquals("Get a wrong customer message header", "header1;header2", inMessage.getHeader("test"));
+ String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
+ assertEquals("Get a wrong http method", "PUT", httpMethod);
+ Customer customer = inMessage.getBody(Customer.class);
+ assertNotNull("The customer should not be null.", customer);
+ // Now you can do what you want on the customer object
+ assertEquals("Get a wrong customer name.", "Mary", customer.getName());
+ // set the response back
+ exchange.getOut().setBody(Response.ok().build());
+ }
+
+ }
+
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/51ef6ad5/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 5a05ed0..0db5882 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
@@ -37,15 +37,17 @@ public class CxfRsSpringConsumerTest extends CamelSpringTestSupport {
protected RouteBuilder createRouteBuilder() throws Exception {
+ final Processor testProcessor = new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ // just throw the CustomException here
+ throw new CustomException("Here is the exception");
+ }
+ };
return new RouteBuilder() {
public void configure() {
errorHandler(new NoErrorHandlerBuilder());
- from("cxfrs://bean://rsServer").process(new Processor() {
- public void process(Exchange exchange) throws Exception {
- // just throw the CustomException here
- throw new CustomException("Here is the exception");
- }
- });
+ from("cxfrs://bean://rsServer").process(testProcessor);
+ from("cxfrs://bean://rsServer2").process(testProcessor);
}
};
}
@@ -58,7 +60,17 @@ public class CxfRsSpringConsumerTest extends CamelSpringTestSupport {
@Test
public void testMappingException() throws Exception {
- HttpGet get = new HttpGet("http://localhost:" + port1 + "/CxfRsSpringConsumerTest/customerservice/customers/126");
+ String address = "http://localhost:" + port1 + "/CxfRsSpringConsumerTest/customerservice/customers/126";
+ doTestMappingException(address);
+ }
+ @Test
+ public void testMappingException2() throws Exception {
+ String address = "http://localhost:" + port1 + "/CxfRsSpringConsumerTest2/customerservice/customers/126";
+ doTestMappingException(address);
+ }
+
+ private void doTestMappingException(String address) throws Exception {
+ HttpGet get = new HttpGet(address);
get.addHeader("Accept" , "application/json");
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
http://git-wip-us.apache.org/repos/asf/camel/blob/51ef6ad5/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 99b708c..4198563 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
@@ -34,6 +34,14 @@
</cxf:providers>
</cxf:rsServer>
+ <cxf:rsServer id="rsServer2" address="http://localhost:${CXFTestSupport.port1}/CxfRsSpringConsumerTest2/"
+ serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource">
+ <cxf:providers>
+ <ref bean="exceptionMapper"/>
+ <ref bean="jsonProvider"/>
+ </cxf:providers>
+ </cxf:rsServer>
+
<bean id="exceptionMapper" class="org.apache.camel.component.cxf.jaxrs.CustomExceptionMapper"/>
<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/>