CAMEL-9052: HttpHeaderFilterStrategy should filter any Camel header on consumer 
side as well


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/70990381
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/70990381
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/70990381

Branch: refs/heads/master
Commit: 70990381e240155232a90847f22480e350d1be51
Parents: 415f954
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Aug 5 10:24:01 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Aug 5 11:44:25 2015 +0200

----------------------------------------------------------------------
 .../http/common/HttpHeaderFilterStrategy.java   |   1 +
 .../http/HttpHeaderFilterStrategyTest.java      |   6 +-
 .../http4/HttpHeaderFilterStrategyTest.java     |  10 +-
 .../component/http4/HttpNoCamelHeaderTest.java  | 100 +++++++++++++++++++
 .../jetty/HttpFilterNoCamelHeadersTest.java     |  75 ++++++++++++++
 5 files changed, 186 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/70990381/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHeaderFilterStrategy.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHeaderFilterStrategy.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHeaderFilterStrategy.java
index 6df5b34..76c6421 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHeaderFilterStrategy.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHeaderFilterStrategy.java
@@ -45,5 +45,6 @@ public class HttpHeaderFilterStrategy extends 
DefaultHeaderFilterStrategy {
         // filter headers begin with "Camel" or "org.apache.camel"
         // must ignore case for Http based transports
         
setOutFilterPattern("(?i)(Camel|org\\.apache\\.camel)[\\.|a-z|A-z|0-9]*");
+        
setInFilterPattern("(?i)(Camel|org\\.apache\\.camel)[\\.|a-z|A-z|0-9]*");
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/70990381/components/camel-http/src/test/java/org/apache/camel/component/http/HttpHeaderFilterStrategyTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpHeaderFilterStrategyTest.java
 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpHeaderFilterStrategyTest.java
index 4f2ac75..4c2ff23 100644
--- 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpHeaderFilterStrategyTest.java
+++ 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpHeaderFilterStrategyTest.java
@@ -63,8 +63,9 @@ public class HttpHeaderFilterStrategyTest extends 
CamelTestSupport {
         assertFalse(filter.applyFilterToExternalHeaders("warning", "199 
Miscellaneous warning", exchange));
         assertFalse(filter.applyFilterToExternalHeaders("Warning", "199 
Miscellaneous warning", exchange));
 
-        assertFalse(filter.applyFilterToExternalHeaders("CamelHeader", "test", 
exchange));
-        
assertFalse(filter.applyFilterToExternalHeaders("org.apache.camel.header", 
"test", exchange));
+        // any Camel header should be filtered
+        assertTrue(filter.applyFilterToExternalHeaders("CamelHeader", "test", 
exchange));
+        
assertTrue(filter.applyFilterToExternalHeaders("org.apache.camel.header", 
"test", exchange));
 
         assertFalse(filter.applyFilterToExternalHeaders("notFilteredHeader", 
"test", exchange));
 
@@ -97,6 +98,7 @@ public class HttpHeaderFilterStrategyTest extends 
CamelTestSupport {
         assertTrue(filter.applyFilterToCamelHeaders("warning", "199 
Miscellaneous warning", exchange));
         assertTrue(filter.applyFilterToCamelHeaders("Warning", "199 
Miscellaneous warning", exchange));
 
+        // any Camel header should be filtered
         assertTrue(filter.applyFilterToCamelHeaders("CamelHeader", "test", 
exchange));
         assertTrue(filter.applyFilterToCamelHeaders("org.apache.camel.header", 
"test", exchange));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/70990381/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java
 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java
index 05ca837..3b7189e 100644
--- 
a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java
+++ 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java
@@ -63,8 +63,9 @@ public class HttpHeaderFilterStrategyTest extends 
CamelTestSupport {
         assertFalse(filter.applyFilterToExternalHeaders("warning", "199 
Miscellaneous warning", exchange));
         assertFalse(filter.applyFilterToExternalHeaders("Warning", "199 
Miscellaneous warning", exchange));
 
-        assertFalse(filter.applyFilterToExternalHeaders("CamelHeader", "test", 
exchange));
-        
assertFalse(filter.applyFilterToExternalHeaders("org.apache.camel.header", 
"test", exchange));
+        // any Camel header should be filtered
+        assertTrue(filter.applyFilterToExternalHeaders("CamelHeader", "test", 
exchange));
+        
assertTrue(filter.applyFilterToExternalHeaders("org.apache.camel.header", 
"test", exchange));
 
         assertFalse(filter.applyFilterToExternalHeaders("notFilteredHeader", 
"test", exchange));
 
@@ -97,8 +98,9 @@ public class HttpHeaderFilterStrategyTest extends 
CamelTestSupport {
         assertTrue(filter.applyFilterToCamelHeaders("warning", "199 
Miscellaneous warning", exchange));
         assertTrue(filter.applyFilterToCamelHeaders("Warning", "199 
Miscellaneous warning", exchange));
 
-        assertTrue(filter.applyFilterToCamelHeaders("CamelHeader", "test", 
exchange));
-        assertTrue(filter.applyFilterToCamelHeaders("org.apache.camel.header", 
"test", exchange));
+        // any Camel header should be filtered
+        assertTrue(filter.applyFilterToExternalHeaders("CamelHeader", "test", 
exchange));
+        
assertTrue(filter.applyFilterToExternalHeaders("org.apache.camel.header", 
"test", exchange));
 
         assertFalse(filter.applyFilterToCamelHeaders("notFilteredHeader", 
"test", exchange));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/70990381/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoCamelHeaderTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoCamelHeaderTest.java
 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoCamelHeaderTest.java
new file mode 100644
index 0000000..2d1dd23
--- /dev/null
+++ 
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoCamelHeaderTest.java
@@ -0,0 +1,100 @@
+/**
+ * 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.http4;
+
+import java.io.IOException;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class HttpNoCamelHeaderTest extends BaseHttpTest {
+
+    private HttpServer localServer;
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        localServer = ServerBootstrap.bootstrap().
+                setHttpProcessor(getBasicHttpProcessor()).
+                setConnectionReuseStrategy(getConnectionReuseStrategy()).
+                setResponseFactory(getHttpResponseFactory()).
+                setExpectationVerifier(getHttpExpectationVerifier()).
+                setSslContext(getSSLContext()).
+                registerHandler("/hello", new HttpRequestHandler() {
+                    @Override
+                    public void handle(HttpRequest request, HttpResponse 
response, HttpContext context) throws HttpException, IOException {
+                        response.setStatusCode(HttpStatus.SC_OK);
+                        Object header = 
request.getFirstHeader(Exchange.FILE_NAME);
+                        assertNull("There should be no Camel header", header);
+
+                        for (Header h : request.getAllHeaders()) {
+                            if (h.getName().startsWith("Camel") || 
h.getName().startsWith("org.apache.camel")) {
+                                assertNull("There should be no Camel header", 
h);
+                            }
+                        }
+
+                        // set ar regular and Camel header
+                        response.setHeader("MyApp", "dude");
+                        response.setHeader(Exchange.TO_ENDPOINT, "foo");
+                    }
+                }).create();
+        localServer.start();
+
+        super.setUp();
+    }
+
+    @After
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+
+    @Test
+    public void testNoCamelHeader() throws Exception {
+        Exchange out = template.request("http4://" + 
localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + 
"/hello", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.CONTENT_TYPE, 
"text/plain");
+                exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+                exchange.getIn().setBody("This is content");
+            }
+
+        });
+
+        assertNotNull(out);
+        assertFalse("Should not fail", out.isFailed());
+        assertEquals("dude", out.getOut().getHeader("MyApp"));
+        assertNull(out.getOut().getHeader(Exchange.TO_ENDPOINT));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/70990381/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterNoCamelHeadersTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterNoCamelHeadersTest.java
 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterNoCamelHeadersTest.java
new file mode 100644
index 0000000..93a1d27
--- /dev/null
+++ 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterNoCamelHeadersTest.java
@@ -0,0 +1,75 @@
+/**
+ * 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.jetty;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class HttpFilterNoCamelHeadersTest extends BaseJettyTest {
+
+    @Test
+    public void testFilterCamelHeaders() throws Exception {
+        // the Camel file name header should be preserved during routing
+        // but should not be sent over HTTP
+        // and jetty should not send back CamelDummy header
+
+        getMockEndpoint("mock:input").expectedMessageCount(1);
+        getMockEndpoint("mock:input").message(0).header("bar").isEqualTo(123);
+        
getMockEndpoint("mock:input").message(0).header(Exchange.FILE_NAME).isNull();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").message(0).header("bar").isEqualTo(123);
+        
getMockEndpoint("mock:result").message(0).header(Exchange.FILE_NAME).isEqualTo("test.txt");
+        
getMockEndpoint("mock:result").message(0).header("CamelDummy").isNull();
+
+        Exchange out = template.request("direct:start", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("World");
+                exchange.getIn().setHeader("bar", 123);
+            }
+        });
+
+        assertNotNull(out);
+        assertEquals("Bye World", out.getOut().getBody(String.class));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .setHeader(Exchange.FILE_NAME, constant("test.txt"))
+                    .to("jetty:http://localhost:{{port}}/test/filter";)
+                    .to("mock:result");
+
+                from("jetty:http://localhost:{{port}}/test/filter";)
+                    .to("mock:input")
+                    .setHeader("CamelDummy", constant("dummy"))
+                    .transform(simple("Bye ${body}"));
+            }
+        };
+    }
+
+}

Reply via email to