Author: ningjiang Date: Tue Jun 21 13:31:47 2011 New Revision: 1138001 URL: http://svn.apache.org/viewvc?rev=1138001&view=rev Log: CAMEL-4128 set the status code into message header for CxfRsProducer
Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java 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=1138001&r1=1138000&r2=1138001&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 Jun 21 13:31:47 2011 @@ -148,6 +148,7 @@ public class CxfRsProducer extends Defau // Get the collection member type first Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments(); response = client.invokeAndGetCollection(httpMethod, body, (Class) actualTypeArguments[0]); + } else { throw new CamelExchangeException("Header " + CxfConstants.CAMEL_CXF_RS_RESPONSE_GENERIC_TYPE + " not found in message", exchange); } @@ -155,6 +156,7 @@ public class CxfRsProducer extends Defau response = client.invoke(httpMethod, body, responseClass); } } + int statesCode = client.getResponse().getStatus(); //Throw exception on a response > 207 //http://en.wikipedia.org/wiki/List_of_HTTP_status_codes if (throwException) { @@ -170,6 +172,7 @@ public class CxfRsProducer extends Defau LOG.trace("Response body = {}", response); exchange.getOut().setBody(binding.bindResponseToCamelBody(response, exchange)); exchange.getOut().setHeaders(binding.bindResponseHeadersToCamelHeaders(response, exchange)); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, statesCode); } } @@ -200,7 +203,7 @@ public class CxfRsProducer extends Defau // Will send out the message to // Need to deal with the sub resource class Object response = method.invoke(target, parameters); - + int statesCode = target.getResponse().getStatus(); if (throwException) { if (response instanceof Response) { Integer respCode = ((Response) response).getStatus(); @@ -211,10 +214,12 @@ public class CxfRsProducer extends Defau } CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint(); CxfRsBinding binding = cxfRsEndpoint.getBinding(); + if (exchange.getPattern().isOutCapable()) { LOG.trace("Response body = {}", response); exchange.getOut().setBody(binding.bindResponseToCamelBody(response, exchange)); exchange.getOut().setHeaders(binding.bindResponseHeadersToCamelHeaders(response, exchange)); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, statesCode); } } Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java?rev=1138001&r1=1138000&r2=1138001&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java Tue Jun 21 13:31:47 2011 @@ -202,8 +202,6 @@ public class DefaultCxfRsBinding impleme } } - // put response code in Camel message header - answer.put(Exchange.HTTP_RESPONSE_CODE, ((Response)response).getStatus()); } return answer; 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=1138001&r1=1138000&r2=1138001&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 Jun 21 13:31:47 2011 @@ -79,6 +79,7 @@ public class CxfRsProducerTest extends C 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"); + assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); // END SNIPPET: ProxyExample } @@ -105,6 +106,7 @@ public class CxfRsProducerTest extends C assertNotNull("The response should not be null ", response); assertEquals("Get a wrong customer id ", String.valueOf(response.get(0).getId()), "113"); assertEquals("Get a wrong customer name", response.get(0).getName(), "Dan"); + assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); } @Test @@ -134,6 +136,7 @@ public class CxfRsProducerTest extends C 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"); + assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); // END SNIPPET: HttpExample } @@ -159,6 +162,7 @@ public class CxfRsProducerTest extends C 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"); + assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); } @Test @@ -242,10 +246,10 @@ public class CxfRsProducerTest extends C // get the response message Customer response = (Customer) exchange.getOut().getBody(); - System.out.println("Response is " + exchange.getOut().getHeaders()); assertNotNull("The response should not be null ", response); assertTrue("Get a wrong customer id ", response.getId() != 8888); assertEquals("Get a wrong customer name", response.getName(), "Willem"); + assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); } @Test