CAMEL-9972: Explicitly add Connection Close HTTP header with a parameter in URI - camel-ahc
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/989886a9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/989886a9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/989886a9 Branch: refs/heads/master Commit: 989886a96a6d3b13f169b7b9dac3fc5e7c32af9e Parents: c504e01 Author: Andrea Cosentino <anco...@gmail.com> Authored: Wed May 18 08:05:27 2016 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Wed May 18 08:05:27 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/component/ahc/AhcEndpoint.java | 15 ++++- .../camel/component/ahc/DefaultAhcBinding.java | 4 ++ .../AhcProducerConnectionCloseHeadersTest.java | 63 ++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/989886a9/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java index 45cee12..b488a73 100644 --- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java +++ b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java @@ -63,6 +63,8 @@ public class AhcEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade private AsyncHttpClientConfig clientConfig; @UriParam(label = "advanced", prefix = "clientConfig.", multiValue = true) private Map<String, Object> clientConfigOptions; + @UriParam(label = "producer", defaultValue = "false") + private boolean connectionClose; public AhcEndpoint(String endpointUri, AhcComponent component, URI httpUri) { super(endpointUri, component); @@ -230,7 +232,18 @@ public class AhcEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade this.clientConfigOptions = clientConfigOptions; } - @Override + public boolean isConnectionClose() { + return connectionClose; + } + + /** + * Define if the Connection Close header has to be added to HTTP Request. This parameter is false by default + */ + public void setConnectionClose(boolean connectionClose) { + this.connectionClose = connectionClose; + } + + @Override protected void doStart() throws Exception { super.doStart(); if (client == null) { http://git-wip-us.apache.org/repos/asf/camel/blob/989886a9/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java ---------------------------------------------------------------------- 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 15fea80..f7160aa 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 @@ -109,6 +109,10 @@ public class DefaultAhcBinding implements AhcBinding { builder.addHeader(entry.getKey(), headerValue); } } + + if (endpoint.isConnectionClose()) { + builder.addHeader("Connection", "close"); + } } protected void populateBody(RequestBuilder builder, AhcEndpoint endpoint, Exchange exchange) throws CamelExchangeException { http://git-wip-us.apache.org/repos/asf/camel/blob/989886a9/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerConnectionCloseHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerConnectionCloseHeadersTest.java b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerConnectionCloseHeadersTest.java new file mode 100644 index 0000000..946184d --- /dev/null +++ b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerConnectionCloseHeadersTest.java @@ -0,0 +1,63 @@ +/** + * 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.ahc; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class AhcProducerConnectionCloseHeadersTest extends BaseAhcTest { + + @Test + public void testAhcProduce() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + 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); + getMockEndpoint("mock:result").expectedHeaderReceived("Connection", "close"); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put("foo", 123); + headers.put("bar", "cool"); + + template.sendBodyAndHeaders("direct:start", null, headers); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to(getAhcEndpointUri() + "?connectionClose=true") + .to("mock:result"); + + from(getTestServerEndpointUri()) + // Remove the message header here + .removeHeaders("*") + .transform(constant("Bye World")); + } + }; + } +}