Repository: camel Updated Branches: refs/heads/master b528f7788 -> e21187e82
CAMEL-7152 - HttpProducer loses response data (specifically which headers where returned): 1) Added copyHeaders to http and http4 components 2) Fixed some "online" tests which were outdated Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f8503218 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f8503218 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f8503218 Branch: refs/heads/master Commit: f8503218d55b80f1ff9bb9e59a9b4d07db4a9074 Parents: b528f77 Author: Robert Budźko <robert.krzysztof.bud...@gmail.com> Authored: Sat Jul 18 12:35:47 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Jul 19 10:16:36 2015 +0200 ---------------------------------------------------------------------- .../camel/component/http/HttpEndpoint.java | 24 ++++++++-- .../camel/component/http/HttpProducer.java | 10 ++-- ...ponentConfigurationAndDocumentationTest.java | 9 +++- .../http/HttpGetHeadersNotCopiedTest.java | 46 ++++++++++++++++++ .../camel/component/http/HttpGetTest.java | 2 +- .../component/http/HttpGetWithHeadersTest.java | 3 +- .../component/http/HttpPostWithBodyTest.java | 4 +- .../component/http/HttpQueryGoogleTest.java | 5 +- .../camel/component/http4/HttpEndpoint.java | 12 +++++ .../camel/component/http4/HttpProducer.java | 10 ++-- .../http4/HttpCamelHeadersNotCopiedTest.java | 41 ++++++++++++++++ .../component/http4/HttpCamelHeadersTest.java | 50 +++++++++++++------- 12 files changed, 179 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java index fcdd6a4..bcf6cb6 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java @@ -110,6 +110,10 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg @UriParam(label = "producer", description = "If this option is true, The http producer won't read response body and cache the input stream") private boolean ignoreResponseBody; + @UriParam(defaultValue = "true", + description = "If this option is true then IN exchange headers will be copied to OUT exchange headers" + + " according to copy strategy.") + private boolean copyHeaders = true; @UriParam(label = "consumer", description = "Whether to eager check whether the HTTP requests has content if the content-length header is 0 or not present." + " This can be turned on in case HTTP clients do not send streamed data.") @@ -125,7 +129,7 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException { this(endPointURI, component, httpURI, new HttpClientParams(), httpConnectionManager, null); } - + public HttpEndpoint(String endPointURI, HttpComponent component, HttpClientParams clientParams, HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException { this(endPointURI, component, null, clientParams, httpConnectionManager, clientConfigurer); @@ -338,7 +342,7 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure) { this.throwExceptionOnFailure = throwExceptionOnFailure; } - + public boolean isBridgeEndpoint() { return bridgeEndpoint; } @@ -350,7 +354,7 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public void setBridgeEndpoint(boolean bridge) { this.bridgeEndpoint = bridge; } - + public boolean isMatchOnUriPrefix() { return matchOnUriPrefix; } @@ -363,7 +367,7 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public void setMatchOnUriPrefix(boolean match) { this.matchOnUriPrefix = match; } - + public boolean isDisableStreamCache() { return this.disableStreamCache; } @@ -382,7 +386,7 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg public void setDisableStreamCache(boolean disable) { this.disableStreamCache = disable; } - + public boolean isChunked() { return this.chunked; } @@ -496,6 +500,16 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg this.ignoreResponseBody = ignoreResponseBody; } + /** + * If this option is true then IN exchange headers will be copied to OUT exchange headers according to copy strategy. + */ + public boolean isCopyHeaders() { + return copyHeaders; + } + + public void setCopyHeaders(boolean copyHeaders) { + this.copyHeaders = copyHeaders; + } public boolean isEagerCheckContentAvailable() { return eagerCheckContentAvailable; } http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java ---------------------------------------------------------------------- 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 052b09e..4edde30 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 @@ -193,10 +193,12 @@ public class HttpProducer extends DefaultProducer { } } - // preserve headers from in by copying any non existing headers - // to avoid overriding existing headers with old values - // Just filter the http protocol headers - MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false); + // endpoint might be configured to copy headers from in to out + // to avoid overriding existing headers with old values just + // filter the http protocol headers + if (getEndpoint().isCopyHeaders()) { + MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false); + } } protected Exception populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException, ClassNotFoundException { http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentConfigurationAndDocumentationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentConfigurationAndDocumentationTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentConfigurationAndDocumentationTest.java index bf985fa..169fb01 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentConfigurationAndDocumentationTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpComponentConfigurationAndDocumentationTest.java @@ -31,10 +31,17 @@ public class HttpComponentConfigurationAndDocumentationTest extends CamelTestSup @Test public void testComponentConfiguration() throws Exception { HttpComponent comp = context.getComponent("http", HttpComponent.class); - EndpointConfiguration conf = comp.createConfiguration("http://www.google.com?proxyHost=myotherproxy&proxyPort=2345"); + + StringBuilder url = new StringBuilder("http://www.google.com"); + url.append("?proxyHost=myotherproxy"); + url.append("&proxyPort=2345"); + url.append("©Headers=false"); + + EndpointConfiguration conf = comp.createConfiguration(url.toString()); assertEquals("myotherproxy", conf.getParameter("proxyHost")); assertEquals("2345", conf.getParameter("proxyPort")); + assertEquals("false", conf.getParameter("copyHeaders")); ComponentConfiguration compConf = comp.createComponentConfiguration(); String json = compConf.createParameterJsonSchema(); http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetHeadersNotCopiedTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetHeadersNotCopiedTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetHeadersNotCopiedTest.java new file mode 100644 index 0000000..cf24c4d --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetHeadersNotCopiedTest.java @@ -0,0 +1,46 @@ +/** + * 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.http; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Before; + +import java.util.Map; + +public class HttpGetHeadersNotCopiedTest extends HttpGetWithHeadersTest { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:start") + .setHeader("TestHeader", constant("test")) + .setHeader("Content-Length", constant(0)) + .setHeader("Accept-Language", constant("pl")) + .to("http://www.google.com/search?copyHeaders=false") + .to("mock:results"); + } + }; + } + + @Override + protected void checkHeaders(Map<String, Object> headers) { + assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); + assertFalse("TestHeader should not be copied.", headers.containsKey("TestHeader")); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java index 02effb5..29e49f3 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java @@ -72,7 +72,7 @@ public class HttpGetTest extends CamelTestSupport { public void configure() { getContext().addComponent("http2", new HttpComponent()); from("direct:start").setHeader(Exchange.HTTP_QUERY, constant("hl=en&q=activemq")) - .to("http2://http://www.google.com/search").to("mock:results"); + .to("http2://www.google.com/search").to("mock:results"); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java index 8df8b31..4fefd10 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java @@ -44,7 +44,8 @@ public class HttpGetWithHeadersTest extends HttpGetTest { expectedText = "Szukaj"; super.setUp(); } - + + @Override protected void checkHeaders(Map<String, Object> headers) { assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); assertEquals("Should get the TestHeader", "test", headers.get("TestHeader")); http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java index ede7fa3..83f5aac 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java @@ -100,8 +100,8 @@ public class HttpPostWithBodyTest extends CamelTestSupport { log.debug("Body: " + body); assertNotNull("Should have a body!", body); - assertTrue("body should contain: <html>", body.contains("<html>")); - + assertTrue("body should contain: <html", body.contains("<html")); + assertTrue("body should contain: </html>", body.contains("</html>")); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryGoogleTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryGoogleTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryGoogleTest.java index 7867b9d..785d6df 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryGoogleTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpQueryGoogleTest.java @@ -16,10 +16,13 @@ */ package org.apache.camel.component.http; +import org.apache.camel.Exchange; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Ignore; import org.junit.Test; +import static org.apache.camel.language.constant.ConstantLanguage.constant; + /** * Unit test to query Google using GET with endpoint having the query parameters. */ @@ -32,7 +35,7 @@ public class HttpQueryGoogleTest extends CamelTestSupport { @Test @Ignore("Run manually") public void testQueryGoogle() throws Exception { - Object out = template.requestBody("http://www.google.com/search?q=Camel", ""); + Object out = template.requestBodyAndHeader("http://www.google.com/search?q=Camel", "", Exchange.HTTP_METHOD, constant("GET")); assertNotNull(out); String data = context.getTypeConverter().convertTo(String.class, out); assertTrue("Camel should be in search result from Google", data.indexOf("Camel") > -1); http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java index a8e0132..8b10cb8 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java @@ -89,6 +89,8 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg private boolean clearExpiredCookies = true; @UriParam(label = "producer") private boolean ignoreResponseBody; + @UriParam(label = "producer", defaultValue = "true") + private boolean copyHeaders = true; @UriParam(label = "consumer") private boolean eagerCheckContentAvailable; @@ -504,4 +506,14 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg this.eagerCheckContentAvailable = eagerCheckContentAvailable; } + /** + * If this option is true then IN exchange headers will be copied to OUT exchange headers according to copy strategy. + */ + public boolean isCopyHeaders() { + return copyHeaders; + } + + public void setCopyHeaders(boolean copyHeaders) { + this.copyHeaders = copyHeaders; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java ---------------------------------------------------------------------- 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 8e52f6b..258c6a4 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 @@ -210,10 +210,12 @@ public class HttpProducer extends DefaultProducer { } } - // preserve headers from in by copying any non existing headers - // to avoid overriding existing headers with old values - // Just filter the http protocol headers - MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false); + // endpoint might be configured to copy headers from in to out + // to avoid overriding existing headers with old values just + // filter the http protocol headers + if (getEndpoint().isCopyHeaders()) { + MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false); + } } protected Exception populateHttpOperationFailedException(Exchange exchange, HttpRequestBase httpRequest, HttpResponse httpResponse, int responseCode) throws IOException, ClassNotFoundException { http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersNotCopiedTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersNotCopiedTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersNotCopiedTest.java new file mode 100644 index 0000000..fa80631 --- /dev/null +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersNotCopiedTest.java @@ -0,0 +1,41 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.http4; + +import org.apache.camel.Exchange; +import org.apache.http.HttpStatus; + +import java.util.Map; + +public class HttpCamelHeadersNotCopiedTest extends HttpCamelHeadersTest { + + @Override + protected void assertHeaders(Map<String, Object> headers) { + assertEquals("Should return " + HttpStatus.SC_OK, HttpStatus.SC_OK, headers.get(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("Should return mocked 12 CL", "12", headers.get("Content-Length")); + + assertNotNull("Should have any Content-Type header propagated", headers.get("Content-Type")); + + assertNull("Should not copy TestHeader from in to out", headers.get("TestHeader")); + assertNull("Should not copy Accept-Language from in to out", headers.get("Accept-Language")); + } + + @Override + protected String setupEndpointParams() { + return "?copyHeaders=false"; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/f8503218/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersTest.java index 98741e1..f8fd6fb 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCamelHeadersTest.java @@ -36,19 +36,19 @@ import org.junit.Test; /** * - * @version + * @version */ public class HttpCamelHeadersTest extends BaseHttpTest { - private HttpServer localServer; - + protected HttpServer localServer; + @Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<String, String>(); expectedHeaders.put("TestHeader", "test"); expectedHeaders.put("Accept-Language", "pl"); - + localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). @@ -70,18 +70,10 @@ public class HttpCamelHeadersTest extends BaseHttpTest { localServer.stop(); } } - + @Test public void httpHeadersShouldPresent() throws Exception { - Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/", new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.getIn().setHeader("TestHeader", "test"); - exchange.getIn().setHeader("Accept-Language", "pl"); - exchange.getIn().setHeader(Exchange.HTTP_PROTOCOL_VERSION, "HTTP/1.0"); - } - }); - - assertExchange(exchange); + assertExchange(doExchange()); } @Override @@ -91,16 +83,38 @@ public class HttpCamelHeadersTest extends BaseHttpTest { assertEquals("test", headers.get("TestHeader")); assertEquals("pl", headers.get("Accept-Language")); } - + + private Exchange doExchange() { + return template.request( + "http4://" + + localServer.getInetAddress().getHostName() + + ":" + + localServer.getLocalPort() + + "/" + + setupEndpointParams(), + new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader("TestHeader", "test"); + exchange.getIn().setHeader("Accept-Language", "pl"); + exchange.getIn().setHeader(Exchange.HTTP_PROTOCOL_VERSION, "HTTP/1.0"); + } + } + ); + } + + protected String setupEndpointParams() { + return ""; + } + class MyHeaderValidationHandler extends HeaderValidationHandler { private String expectProtocolVersion; - public MyHeaderValidationHandler(String expectedMethod, String protocolVersion, + public MyHeaderValidationHandler(String expectedMethod, String protocolVersion, String responseContent, Map<String, String> expectedHeaders) { super(expectedMethod, null, null, responseContent, expectedHeaders); expectProtocolVersion = protocolVersion; } - + public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException { if (!expectProtocolVersion.equals(request.getProtocolVersion().toString())) { @@ -109,6 +123,6 @@ public class HttpCamelHeadersTest extends BaseHttpTest { } super.handle(request, response, context); } - + } } \ No newline at end of file