Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B433B10AC7 for ; Tue, 8 Apr 2014 12:46:45 +0000 (UTC) Received: (qmail 16636 invoked by uid 500); 8 Apr 2014 12:46:44 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 16550 invoked by uid 500); 8 Apr 2014 12:46:40 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 16541 invoked by uid 99); 8 Apr 2014 12:46:37 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Apr 2014 12:46:37 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 2225994E021; Tue, 8 Apr 2014 12:46:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ningjiang@apache.org To: commits@camel.apache.org Message-Id: <79826c761cb740a191f085d18f340da4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: CAMEL-7141 Avoiding the sideeffect of setHeaderFilterStrategy of camel-netty-http component with thanks to Joe Date: Tue, 8 Apr 2014 12:46:37 +0000 (UTC) Repository: camel Updated Branches: refs/heads/master cd8da7d36 -> 5a74dfca3 CAMEL-7141 Avoiding the sideeffect of setHeaderFilterStrategy of camel-netty-http component with thanks to Joe Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5a74dfca Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5a74dfca Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5a74dfca Branch: refs/heads/master Commit: 5a74dfca350a2c87e3ba7b423b39deb7f125b6b3 Parents: cd8da7d Author: Willem Jiang Authored: Tue Apr 8 20:45:01 2014 +0800 Committer: Willem Jiang Committed: Tue Apr 8 20:45:55 2014 +0800 ---------------------------------------------------------------------- .../netty/http/DefaultNettyHttpBinding.java | 13 +++- .../netty/http/NettyHttpComponent.java | 24 +++---- .../component/netty/http/NettyHttpEndpoint.java | 4 +- ...dpointUriCustomHeaderFilterStrategyTest.java | 70 ++++++++++++++++++++ 4 files changed, 90 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5a74dfca/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java ---------------------------------------------------------------------- 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 40bb99e..00eda1e 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 @@ -32,6 +32,7 @@ import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.TypeConverter; import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.util.ExchangeHelper; @@ -55,10 +56,10 @@ import org.slf4j.LoggerFactory; /** * Default {@link NettyHttpBinding}. */ -public class DefaultNettyHttpBinding implements NettyHttpBinding { +public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable { private static final Logger LOG = LoggerFactory.getLogger(DefaultNettyHttpBinding.class); - private HeaderFilterStrategy headerFilterStrategy; + private HeaderFilterStrategy headerFilterStrategy = new NettyHttpHeaderFilterStrategy(); public DefaultNettyHttpBinding() { } @@ -66,6 +67,14 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { public DefaultNettyHttpBinding(HeaderFilterStrategy headerFilterStrategy) { this.headerFilterStrategy = headerFilterStrategy; } + + public DefaultNettyHttpBinding copy() { + try { + return (DefaultNettyHttpBinding)this.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeCamelException(e); + } + } @Override public Message toCamelMessage(HttpRequest request, Exchange exchange, NettyHttpConfiguration configuration) throws Exception { http://git-wip-us.apache.org/repos/asf/camel/blob/5a74dfca/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java index 0e4ccae..e4142d7 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java @@ -49,6 +49,10 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt private NettyHttpSecurityConfiguration securityConfiguration; public NettyHttpComponent() { + // use the http configuration and filter strategy + setConfiguration(new NettyHttpConfiguration()); + setHeaderFilterStrategy(new NettyHttpHeaderFilterStrategy()); + setNettyHttpBinding(new DefaultNettyHttpBinding(getHeaderFilterStrategy())); } @Override @@ -95,8 +99,11 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt answer.setTimer(getTimer()); // set component options on endpoint as defaults + // As the component's NettyHttpBinding could be override by the setHeaderFilterStrategy + // Here we just create a new DefaultNettyHttpBinding here if (answer.getNettyHttpBinding() == null) { - answer.setNettyHttpBinding(getNettyHttpBinding()); + DefaultNettyHttpBinding nettyHttpBinding = (DefaultNettyHttpBinding)getNettyHttpBinding(); + answer.setNettyHttpBinding(nettyHttpBinding.copy()); } if (answer.getHeaderFilterStrategy() == null) { answer.setHeaderFilterStrategy(getHeaderFilterStrategy()); @@ -142,9 +149,6 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt } public NettyHttpBinding getNettyHttpBinding() { - if (nettyHttpBinding == null) { - nettyHttpBinding = new DefaultNettyHttpBinding(getHeaderFilterStrategy()); - } return nettyHttpBinding; } @@ -153,9 +157,6 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt } public HeaderFilterStrategy getHeaderFilterStrategy() { - if (headerFilterStrategy == null) { - headerFilterStrategy = new NettyHttpHeaderFilterStrategy(); - } return headerFilterStrategy; } @@ -194,15 +195,6 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt } @Override - protected void doStart() throws Exception { - if (getConfiguration() == null) { - setConfiguration(new NettyHttpConfiguration()); - } - - super.doStart(); - } - - @Override protected void doStop() throws Exception { super.doStop(); http://git-wip-us.apache.org/repos/asf/camel/blob/5a74dfca/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java index 7d68050..173aa25 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java @@ -137,9 +137,7 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) { this.headerFilterStrategy = headerFilterStrategy; - if (getNettyHttpBinding() != null) { - getNettyHttpBinding().setHeaderFilterStrategy(headerFilterStrategy); - } + getNettyHttpBinding().setHeaderFilterStrategy(headerFilterStrategy); } public boolean isTraceEnabled() { http://git-wip-us.apache.org/repos/asf/camel/blob/5a74dfca/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriCustomHeaderFilterStrategyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriCustomHeaderFilterStrategyTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriCustomHeaderFilterStrategyTest.java new file mode 100644 index 0000000..ddbb06b --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriCustomHeaderFilterStrategyTest.java @@ -0,0 +1,70 @@ +/** + * 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.netty.http; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultHeaderFilterStrategy; +import org.apache.camel.impl.JndiRegistry; +import org.junit.Test; + +public class NettyHttpEndpointUriCustomHeaderFilterStrategyTest extends BaseNettyTest { + + @Test + public void testEndpointUriWithCustomHeaderStrategy() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:outbound"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("Date", "31-03-2014"); + + Exchange out = template.request("direct:request", null); + + assertMockEndpointsSatisfied(); + + String date = out.getOut().getHeader("sub-date", String.class); + assertNull(date); + } + + @Override protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("customHeaderFilterStrategy", new CustomHeaderFilterStrategy()); + return registry; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:request") + .setHeader("Date", constant("31-03-2014")) + .to("netty-http:http://localhost:{{port}}/myapp/mytest?headerFilterStrategy=#customHeaderFilterStrategy"); + + from("netty-http:http://localhost:{{port}}/myapp/mytest") + .to("mock:outbound") + .setHeader("sub-date", constant("31-05-2014")); + } + }; + } + + private class CustomHeaderFilterStrategy extends DefaultHeaderFilterStrategy { + public CustomHeaderFilterStrategy() { + // allow all outbound headers to pass through but only filter out below inbound header + getInFilter().add("sub-date"); + } + } +}