Repository: camel Updated Branches: refs/heads/camel-2.12.x eb9ccf421 -> 94c01b98d refs/heads/camel-2.13.x 68d3f233b -> 956b2d2b6
CAMEL-7357 support jax-rs exception mappers in camel-cxf Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/956b2d2b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/956b2d2b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/956b2d2b Branch: refs/heads/camel-2.13.x Commit: 956b2d2b6a01b63b60b90f268bb614de14ab930e Parents: 68d3f23 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Thu Apr 10 17:35:40 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Thu Apr 10 17:41:01 2014 +0800 ---------------------------------------------------------------------- .../camel/component/cxf/jaxrs/CxfRsInvoker.java | 9 +-- .../cxf/jaxrs/CustomExceptionMapper.java | 33 +++++++++ .../cxf/jaxrs/CxfRsSpringConsumerTest.java | 75 ++++++++++++++++++++ .../cxf/jaxrs/testbean/CustomException.java | 27 +++++++ .../component/cxf/jaxrs/CxfRsSpringConsumer.xml | 45 ++++++++++++ 5 files changed, 182 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/956b2d2b/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 3e2c0fd..4f617a7 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 @@ -19,7 +19,6 @@ package org.apache.camel.component.cxf.jaxrs; import java.lang.reflect.Method; import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; import org.apache.camel.AsyncCallback; import org.apache.camel.ExchangePattern; @@ -156,12 +155,8 @@ public class CxfRsInvoker extends JAXRSInvoker { } else { throw (WebApplicationException)exception; } - } else { - // Send the exception message back - WebApplicationException webApplicationException = new WebApplicationException(exception, Response.serverError().entity(exception.toString()).build()); - throw webApplicationException; - } - + } + //CAMEL-7357 throw out other exception to make sure the ExceptionMapper work } return endpoint.getBinding().populateCxfRsResponseFromExchange(camelExchange, cxfExchange); } http://git-wip-us.apache.org/repos/asf/camel/blob/956b2d2b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CustomExceptionMapper.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CustomExceptionMapper.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CustomExceptionMapper.java new file mode 100644 index 0000000..475b047 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CustomExceptionMapper.java @@ -0,0 +1,33 @@ +/** + * 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 javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.apache.camel.component.cxf.jaxrs.testbean.CustomException; + +public class CustomExceptionMapper implements ExceptionMapper<CustomException> { + + @Override + public Response toResponse(CustomException exception) { + Response.Status status; + status = Response.Status.INTERNAL_SERVER_ERROR; + return Response.status(status).header("exception", exception.getMessage()).build(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/956b2d2b/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 new file mode 100644 index 0000000..d149905 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumerTest.java @@ -0,0 +1,75 @@ +/** + * 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 org.apache.camel.Exchange; +import org.apache.camel.Processor; +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.test.spring.CamelSpringTestSupport; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class CxfRsSpringConsumerTest extends CamelSpringTestSupport { + private static int port1 = CXFTestSupport.getPort1(); + + + protected RouteBuilder createRouteBuilder() throws 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"); + } + }); + } + }; + } + + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml"); + } + + @Test + public void testMappingException() throws Exception { + HttpGet get = new HttpGet("http://localhost:" + port1 + "/CxfRsSpringConsumerTest/customerservice/customers/126"); + get.addHeader("Accept" , "application/json"); + CloseableHttpClient httpclient = HttpClientBuilder.create().build(); + + try { + HttpResponse response = httpclient.execute(get); + assertEquals("Get a wrong status code", 500, response.getStatusLine().getStatusCode()); + assertEquals("Get a worng message header", "exception: Here is the exception", response.getHeaders("exception")[0].toString()); + } finally { + httpclient.close(); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/956b2d2b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomException.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomException.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomException.java new file mode 100644 index 0000000..69033b8 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomException.java @@ -0,0 +1,27 @@ +/** + * 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.testbean; + +public class CustomException extends Exception { + + private static final long serialVersionUID = 1L; + + public CustomException(String message) { + super(message); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/956b2d2b/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 new file mode 100644 index 0000000..99b708c --- /dev/null +++ b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringConsumer.xml @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:cxf="http://camel.apache.org/schema/cxf" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + + <cxf:rsServer id="rsServer" address="http://localhost:${CXFTestSupport.port1}/CxfRsSpringConsumerTest/" + serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"> + <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"/> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + </camelContext> + + + +</beans>