You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2018/04/26 17:09:02 UTC

[camel] branch camel-2.21.x updated: [CAMEL-12428] Make sure response code and content type are propagated to cxfrs

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch camel-2.21.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.21.x by this push:
     new 4280d66  [CAMEL-12428] Make sure response code and content type are propagated to cxfrs
4280d66 is described below

commit 4280d660ed7bdce2c79d7d5d3da8b63567391f5d
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Mon Apr 9 16:18:54 2018 -0400

    [CAMEL-12428] Make sure response code and content type are propagated to cxfrs
    
    (cherry picked from commit e4ad40f2eebb68cfde175b171538e26b278f63f6)
---
 .../component/cxf/jaxrs/DefaultCxfRsBinding.java   | 45 +++++++++++++++++++++-
 .../component/cxf/jaxrs/CxfRsConsumerTest.java     | 23 +++++++++--
 2 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
index 1658a94..0ecfd7b 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
@@ -19,10 +19,12 @@ package org.apache.camel.component.cxf.jaxrs;
 
 import java.lang.reflect.Method;
 import java.nio.charset.Charset;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TreeMap;
 
 import javax.security.auth.Subject;
 import javax.ws.rs.client.Entity;
@@ -38,6 +40,7 @@ import org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.HttpHeaderHelper;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
@@ -82,7 +85,47 @@ public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAw
             LOG.trace("Get the response from the in message");
         }
 
-        return response.getBody();
+        Object o = response.getBody();
+        if (!(o instanceof Response)) {
+            //not a JAX-RS Response object, we need to set the headers from the Camel values
+            
+            
+            if (response.getHeader(org.apache.cxf.message.Message.PROTOCOL_HEADERS) != null) {
+                Map<String, Object> headers = CastUtils.cast((Map<?, ?>)response.getHeader(org.apache.cxf.message.Message.PROTOCOL_HEADERS));
+                cxfExchange.getOutMessage().putIfAbsent(org.apache.cxf.message.Message.PROTOCOL_HEADERS, 
+                                                        new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
+                final Map<String, List<String>> cxfHeaders =
+                    CastUtils.cast((Map<?, ?>) cxfExchange.getOutMessage().get(org.apache.cxf.message.Message.PROTOCOL_HEADERS));
+                
+                for (Map.Entry<String, Object> ent : headers.entrySet()) {
+                    List<String> v;
+                    if (ent.getValue() instanceof List) {
+                        v = CastUtils.cast((List<?>)ent.getValue());
+                    } else {
+                        v = Arrays.asList(ent.getValue().toString());
+                    }
+                    cxfHeaders.put(ent.getKey(), v);
+                }
+            }
+            
+            
+            if (response.getHeader(Exchange.HTTP_RESPONSE_CODE) != null && !cxfExchange.containsKey(org.apache.cxf.message.Message.RESPONSE_CODE)) {
+                cxfExchange.put(org.apache.cxf.message.Message.RESPONSE_CODE, response.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class));
+            }
+            if (response.getHeader(Exchange.CONTENT_TYPE) != null) {
+                cxfExchange.getOutMessage().putIfAbsent(org.apache.cxf.message.Message.PROTOCOL_HEADERS, 
+                                                        new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
+                final Map<String, List<String>> cxfHeaders =
+                    CastUtils.cast((Map<?, ?>) cxfExchange.getOutMessage().get(org.apache.cxf.message.Message.PROTOCOL_HEADERS));
+
+                if (!cxfHeaders.containsKey(Exchange.CONTENT_TYPE)) {
+                    List<String> a = Arrays.asList((String)response.getHeader(Exchange.CONTENT_TYPE));
+                    cxfHeaders.put(Exchange.CONTENT_TYPE, a);
+                    cxfExchange.getOutMessage().put(Exchange.CONTENT_TYPE, response.getHeader(Exchange.CONTENT_TYPE));
+                }
+            }
+        }
+        return o;
     }
 
     public void populateExchangeFromCxfRsRequest(org.apache.cxf.message.Exchange cxfExchange,
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 0d61ebf..7420732 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
@@ -145,7 +145,17 @@ public class CxfRsConsumerTest extends CamelTestSupport {
     
     @Test
     public void testGetWrongCustomer() throws Exception {
-        URL url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/456");
+        URL url;
+        
+        url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/789");
+        try {
+            url.openStream();
+            fail("Expect to get exception here");
+        } catch (IOException exception) {
+            // expect the Internal error exception
+        }
+        
+        url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/456");
         try {
             url.openStream();
             fail("Expect to get exception here");
@@ -221,12 +231,19 @@ public class CxfRsConsumerTest extends CamelTestSupport {
                     return;
                 }
                 if ("/customerservice/customers/456".equals(path)) {
-                    Response r = Response.status(404).entity("Can't found the customer with uri " + path).build();
+                    Response r = Response.status(404).entity("Can't found the customer with uri " + path)
+                        .header("Content-Type", "text/plain").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();
+                    Response r = Response.status(404).entity("Can't found the customer with uri " + path)
+                        .header("Content-Type", "text/plain").build();
                     exchange.getOut().setBody(r);
                     exchange.getOut().setFault(true);
+                } else if ("/customerservice/customers/789".equals(path)) {
+                    exchange.getOut().setBody("Can't found the customer with uri " + path);
+                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/plain");
+                    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, "404");                    
+                    exchange.getOut().setFault(true);
                 } else {
                     throw new RuntimeCamelException("Can't found the customer with uri " + path);
                 }

-- 
To stop receiving notification emails like this one, please contact
dkulp@apache.org.