camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [2/4] git commit: CAMEL-7141 Avoiding the sideeffect of setHeaderFilterStrategy of camel-netty-http component with thanks to Joe
Date Tue, 08 Apr 2014 14:24:04 GMT
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/08929530
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/08929530
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/08929530

Branch: refs/heads/camel-2.12.x
Commit: 0892953016de8de8bb0db1bc1412806c3c57e823
Parents: 17ed696
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Tue Apr 8 20:45:01 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Tue Apr 8 21:34:32 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/08929530/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 f6c6c45..e1d700a 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/08929530/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/08929530/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/08929530/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");
+        }   
+    }
+}


Mime
View raw message