From a4828fa2849dbb3424fb41c83bf6d89ed64f279f Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Sat, 11 Jul 2015 09:57:37 +0200 Subject: [PATCH] CAMEL-6469: http component now have header with response status line text as well (the code is already there) --- camel-core/src/main/java/org/apache/camel/Exchange.java | 1 + .../org/apache/camel/component/ahc/DefaultAhcBinding.java | 1 + .../apache/camel/component/ahc/AhcProduceGetHeadersTest.java | 1 + .../ahc/AhcProduceNoThrowExceptionOnFailureTest.java | 1 + .../org/apache/camel/component/http/HttpPollingConsumer.java | 1 + .../java/org/apache/camel/component/http/HttpProducer.java | 1 + .../apache/camel/component/http4/HttpPollingConsumer.java | 3 +++ .../java/org/apache/camel/component/http4/HttpProducer.java | 3 +++ .../org/apache/camel/component/http4/HttpMethodsTest.java | 1 + .../org/apache/camel/component/http4/HttpRedirectTest.java | 1 + .../component/jetty/JettyCallHttpThenExceptionTest.java | 1 + .../camel/component/netty/http/DefaultNettyHttpBinding.java | 2 +- .../camel/component/netty/http/NettyHttpConstants.java | 5 ++++- .../netty/http/NettyHttpFilterCamelHeadersTest.java | 2 +- .../camel/component/netty4/http/DefaultNettyHttpBinding.java | 2 +- .../camel/component/netty4/http/NettyHttpConstants.java | 5 ++++- .../netty4/http/NettyHttpFilterCamelHeadersTest.java | 2 +- 17 files changed, 27 insertions(+), 6 deletions(-) diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java index 5cfe78ea31478..8e7cc8485ad15 100644 --- a/camel-core/src/main/java/org/apache/camel/Exchange.java +++ b/camel-core/src/main/java/org/apache/camel/Exchange.java @@ -145,6 +145,7 @@ public interface Exchange { String HTTP_QUERY = "CamelHttpQuery"; String HTTP_RAW_QUERY = "CamelHttpRawQuery"; String HTTP_RESPONSE_CODE = "CamelHttpResponseCode"; + String HTTP_RESPONSE_TEXT = "CamelHttpResponseText"; String HTTP_URI = "CamelHttpUri"; String HTTP_URL = "CamelHttpUrl"; String HTTP_CHUNKED = "CamelHttpChunked"; diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java index 8c57cd9adea59..a7fd405d35afc 100644 --- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java +++ b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java @@ -191,6 +191,7 @@ public void onStatusReceived(AhcEndpoint endpoint, Exchange exchange, HttpRespon // Just filter the http protocol headers MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), httpProtocolHeaderFilterStrategy, false); exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseStatus.getStatusCode()); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_TEXT, responseStatus.getStatusText()); } @Override diff --git a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceGetHeadersTest.java b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceGetHeadersTest.java index d445aa7816d62..16447ebf6e102 100644 --- a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceGetHeadersTest.java +++ b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceGetHeadersTest.java @@ -31,6 +31,7 @@ public void testAhcProduce() throws Exception { getMockEndpoint("mock:result").expectedHeaderReceived("foo", 123); getMockEndpoint("mock:result").expectedHeaderReceived("bar", "cool"); getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_TEXT, "OK"); getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.CONTENT_LENGTH, 9); Map headers = new HashMap(); diff --git a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceNoThrowExceptionOnFailureTest.java b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceNoThrowExceptionOnFailureTest.java index 45020203e37bd..41b0290e02c16 100644 --- a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceNoThrowExceptionOnFailureTest.java +++ b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProduceNoThrowExceptionOnFailureTest.java @@ -27,6 +27,7 @@ public class AhcProduceNoThrowExceptionOnFailureTest extends BaseAhcTest { public void testAhcProduce() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Does not work"); getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 500); + getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_TEXT, "Server Error"); template.sendBody("direct:start", null); diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java index 63603ea4ba03c..403bb027f5e44 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java @@ -91,6 +91,7 @@ protected Exchange doReceive(int timeout) { } } message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode); + message.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText()); return exchange; } catch (IOException e) { diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java index 1519362271456..052b09e1cc65b 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java @@ -173,6 +173,7 @@ protected void populateResponse(Exchange exchange, HttpMethod method, Message in Message answer = exchange.getOut(); answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode); + answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText()); answer.setBody(response); // propagate HTTP response headers diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java index 758c1c9c3b22a..ff74cee5d71d9 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java @@ -100,6 +100,9 @@ protected Exchange doReceive(int timeout) { } } message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode); + if (response.getStatusLine() != null) { + message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase()); + } return exchange; } catch (IOException e) { diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java index c2790ccf7bc8e..8e52f6b0d22c5 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java @@ -189,6 +189,9 @@ protected void populateResponse(Exchange exchange, HttpRequestBase httpRequest, Message answer = exchange.getOut(); answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode); + if (httpResponse.getStatusLine() != null) { + answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, httpResponse.getStatusLine().getReasonPhrase()); + } answer.setBody(response); // propagate HTTP response headers diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java index 620e6cb603ce7..f9c7a5bd31c89 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java @@ -97,6 +97,7 @@ public void process(Exchange exchange) throws Exception { Message out = exchange.getOut(); Map headers = out.getHeaders(); assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, headers.get(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("Not Implemented", headers.get(Exchange.HTTP_RESPONSE_TEXT)); assertEquals("26", headers.get("Content-Length")); assertNotNull("Should have Content-Type header", headers.get("Content-Type")); assertEquals("PATCH method not supported", out.getBody(String.class)); diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpRedirectTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpRedirectTest.java index 6acee6ed17ec5..ffb8f840f677b 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpRedirectTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpRedirectTest.java @@ -101,6 +101,7 @@ public void process(Exchange exchange) throws Exception { assertNotNull(out); assertEquals(HttpStatus.SC_OK, out.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OK", out.getOut().getHeader(Exchange.HTTP_RESPONSE_TEXT)); assertEquals("Bye World", out.getOut().getBody(String.class)); } diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyCallHttpThenExceptionTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyCallHttpThenExceptionTest.java index 7468083f7254f..b12c57e625182 100644 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyCallHttpThenExceptionTest.java +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyCallHttpThenExceptionTest.java @@ -42,6 +42,7 @@ public void process(Exchange exchange) throws Exception { assertNotNull(reply); assertTrue(reply.getOut().getBody(String.class).startsWith("java.lang.IllegalArgumentException: I cannot do this")); assertEquals(500, reply.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("Server Error", reply.getOut().getHeader(Exchange.HTTP_RESPONSE_TEXT)); } @Override diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java index bbdbfc35d368e..863adce7ee9cb 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java @@ -265,7 +265,7 @@ public void populateCamelHeaders(HttpResponse response, Map head LOG.trace("populateCamelHeaders: {}", response); headers.put(Exchange.HTTP_RESPONSE_CODE, response.getStatus().getCode()); - headers.put(NettyHttpConstants.HTTP_RESPONSE_TEXT, response.getStatus().getReasonPhrase()); + headers.put(Exchange.HTTP_RESPONSE_TEXT, response.getStatus().getReasonPhrase()); for (String name : response.headers().names()) { // mapping the content-type diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java index df31626595823..ba140452131d4 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.netty.http; +import org.apache.camel.Exchange; + /** * Netty HTTP constants. */ @@ -23,7 +25,8 @@ public final class NettyHttpConstants { public static final String CONTENT_TYPE_JAVA_SERIALIZED_OBJECT = "application/x-java-serialized-object"; public static final String CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; - public static final String HTTP_RESPONSE_TEXT = "CamelHttpResponseText"; + @Deprecated + public static final String HTTP_RESPONSE_TEXT = Exchange.HTTP_RESPONSE_TEXT; public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication"; private NettyHttpConstants() { diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpFilterCamelHeadersTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpFilterCamelHeadersTest.java index bdc9c5563bf40..478fdb306a35f 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpFilterCamelHeadersTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpFilterCamelHeadersTest.java @@ -42,7 +42,7 @@ public void process(Exchange exchange) throws Exception { // except for the response code and response text Map headers = out.getOut().getHeaders(); for (String key : headers.keySet()) { - if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(NettyHttpConstants.HTTP_RESPONSE_TEXT)) { + if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_TEXT)) { assertTrue("Should not contain any Camel internal headers", !key.toLowerCase().startsWith("camel")); } } diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java index 29f6bdf021d93..2b33170437e20 100644 --- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java +++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java @@ -267,7 +267,7 @@ public void populateCamelHeaders(FullHttpResponse response, Map LOG.trace("populateCamelHeaders: {}", response); headers.put(Exchange.HTTP_RESPONSE_CODE, response.getStatus().code()); - headers.put(NettyHttpConstants.HTTP_RESPONSE_TEXT, response.getStatus().reasonPhrase()); + headers.put(Exchange.HTTP_RESPONSE_TEXT, response.getStatus().reasonPhrase()); for (String name : response.headers().names()) { // mapping the content-type diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConstants.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConstants.java index 2dd5869002546..deae4e3833b45 100644 --- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConstants.java +++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpConstants.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.netty4.http; +import org.apache.camel.Exchange; + /** * Netty HTTP constants. */ @@ -23,7 +25,8 @@ public final class NettyHttpConstants { public static final String CONTENT_TYPE_JAVA_SERIALIZED_OBJECT = "application/x-java-serialized-object"; public static final String CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; - public static final String HTTP_RESPONSE_TEXT = "CamelHttpResponseText"; + @Deprecated + public static final String HTTP_RESPONSE_TEXT = Exchange.HTTP_RESPONSE_TEXT; public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication"; private NettyHttpConstants() { diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpFilterCamelHeadersTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpFilterCamelHeadersTest.java index 62e4fcf52ca02..99c7a413c48a4 100644 --- a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpFilterCamelHeadersTest.java +++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpFilterCamelHeadersTest.java @@ -42,7 +42,7 @@ public void process(Exchange exchange) throws Exception { // except for the response code and response text Map headers = out.getOut().getHeaders(); for (String key : headers.keySet()) { - if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(NettyHttpConstants.HTTP_RESPONSE_TEXT)) { + if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_TEXT)) { assertTrue("Should not contain any Camel internal headers", !key.toLowerCase().startsWith("camel")); } }