This is an automated email from the ASF dual-hosted git repository. zregvart pushed a commit to branch camel-2.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.x by this push: new 1511dc6 CAMEL-13249: invoke `applyFilterToCamelHeaders`... 1511dc6 is described below commit 1511dc6f4af1bef64e30fdb57168244a9f9c1f09 Author: Zoran Regvart <zregv...@apache.org> AuthorDate: Fri Feb 22 18:03:41 2019 +0100 CAMEL-13249: invoke `applyFilterToCamelHeaders`... ... instead of `applyFilterToExternalHeaders` This changes the invocation in `HttpRestHeaderFilterStrategy::applyFilterToCamelHeaders` to delegate to `super::applyFilterToCamelHeaders` instead of `super::applyFilterToExternalHeaders`. Also adds integration test demonstrating the issue and refactors `HttpRestHeaderFilterStrategyTest` from `components/camel-http4` to `/components/camel-http-common` as it doesn't depend on `http4` component and tests a class in `camel-http-common`. --- components/camel-http-common/pom.xml | 6 ++ .../http/common/HttpRestHeaderFilterStrategy.java | 2 +- .../http/common}/HttpHeaderFilterStrategyTest.java | 2 +- .../common/HttpRestHeaderFilterStrategyTest.java | 39 ++++++++ components/camel-http4/pom.xml | 6 ++ .../camel/component/http4/HeaderFilteringTest.java | 108 +++++++++++++++++++++ .../http/NettyHttpRestHeaderFilterStrategy.java | 2 +- 7 files changed, 162 insertions(+), 3 deletions(-) diff --git a/components/camel-http-common/pom.xml b/components/camel-http-common/pom.xml index 9d78644..99c4b09 100644 --- a/components/camel-http-common/pom.xml +++ b/components/camel-http-common/pom.xml @@ -78,6 +78,12 @@ <artifactId>log4j-slf4j-impl</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-version}</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategy.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategy.java index 762fe03..4a930d5 100644 --- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategy.java +++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategy.java @@ -31,7 +31,7 @@ public class HttpRestHeaderFilterStrategy extends HttpHeaderFilterStrategy { @Override public boolean applyFilterToCamelHeaders(String headerName, Object headerValue, Exchange exchange) { - boolean answer = super.applyFilterToExternalHeaders(headerName, headerValue, exchange); + boolean answer = super.applyFilterToCamelHeaders(headerName, headerValue, exchange); // using rest producer then headers are mapping to uri and query parameters using {key} syntax // if there is a match to an existing Camel Message header, then we should filter (=true) this // header as its already been mapped by the RestProducer from camel-core, and we do not want diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHeaderFilterStrategyTest.java similarity index 99% rename from components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java rename to components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHeaderFilterStrategyTest.java index 803e05f..c2e79ca 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpHeaderFilterStrategyTest.java +++ b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHeaderFilterStrategyTest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.http4; +package org.apache.camel.http.common; import org.apache.camel.Exchange; import org.apache.camel.http.common.HttpHeaderFilterStrategy; diff --git a/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategyTest.java b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategyTest.java new file mode 100644 index 0000000..d2c0141 --- /dev/null +++ b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpRestHeaderFilterStrategyTest.java @@ -0,0 +1,39 @@ +/** + * 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.http.common; + +import org.apache.camel.Exchange; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HttpRestHeaderFilterStrategyTest { + + private static final Exchange NOT_USED = null; + + @Test + public void shouldDecideOnApplingHeaderFilterToTemplateTokens() { + final HttpRestHeaderFilterStrategy strategy = new HttpRestHeaderFilterStrategy("{uriToken1}{uriToken2}", + "q1=%7BqueryToken1%7D%26q2=%7BqueryToken2%7D%26"); + + assertThat(strategy.applyFilterToCamelHeaders("uriToken1", "value", NOT_USED)).isTrue(); + assertThat(strategy.applyFilterToCamelHeaders("uriToken2", "value", NOT_USED)).isTrue(); + assertThat(strategy.applyFilterToCamelHeaders("queryToken1", "value", NOT_USED)).isTrue(); + assertThat(strategy.applyFilterToCamelHeaders("queryToken2", "value", NOT_USED)).isTrue(); + assertThat(strategy.applyFilterToCamelHeaders("unknown", "value", NOT_USED)).isFalse(); + } +} diff --git a/components/camel-http4/pom.xml b/components/camel-http4/pom.xml index 32db988..db8816b 100644 --- a/components/camel-http4/pom.xml +++ b/components/camel-http4/pom.xml @@ -104,5 +104,11 @@ <version>${jetty-version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-version}</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HeaderFilteringTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HeaderFilteringTest.java new file mode 100644 index 0000000..3815a25 --- /dev/null +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HeaderFilteringTest.java @@ -0,0 +1,108 @@ +/** + * 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.ByteArrayInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Collections; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; + +import org.apache.camel.Producer; +import org.apache.camel.http.common.HttpOperationFailedException; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.impl.DefaultMessage; +import org.apache.camel.spi.RestConfiguration; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +public class HeaderFilteringTest { + + private static final String BODY = "{\"example\":\"json\"}"; + + private int port; + + private HttpServer server; + + @Test + public void shouldFilterIncomingHttpHeadersInProducer() throws Exception { + final HttpComponent http = new HttpComponent(); + + final DefaultCamelContext context = new DefaultCamelContext(); + final Producer producer = http.createProducer(context, "http://localhost:" + port, "GET", "/test", null, null, + "application/json", "application/json", new RestConfiguration(), Collections.emptyMap()); + + final DefaultExchange exchange = new DefaultExchange(context); + final DefaultMessage in = new DefaultMessage(context); + in.setHeader("Host", "www.not-localhost.io"); + in.setBody(BODY); + exchange.setIn(in); + + try { + producer.process(exchange); + } catch (final HttpOperationFailedException e) { + fail(e.getMessage() + "\n%s", e.getResponseBody()); + } + } + + @Before + public void startHttpServer() throws IOException { + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/test", this::handleTest); + server.start(); + + port = server.getAddress().getPort(); + } + + @After + public void stopHttpServer() { + server.stop(0); + } + + void handleTest(final HttpExchange exchange) throws IOException { + try (final OutputStream responseBody = exchange.getResponseBody()) { + try { + assertThat(exchange.getRequestBody()) + .hasSameContentAs(new ByteArrayInputStream(BODY.getBytes(StandardCharsets.UTF_8))); + assertThat(exchange.getRequestHeaders()).containsEntry("Host", + Collections.singletonList("localhost:" + port)); + + exchange.sendResponseHeaders(200, 0); + } catch (final AssertionError error) { + final StringWriter out = new StringWriter(); + error.printStackTrace(new PrintWriter(out)); + + final String failure = out.toString(); + final byte[] failureBytes = failure.getBytes(StandardCharsets.UTF_8); + exchange.sendResponseHeaders(500, failureBytes.length); + responseBody.write(failureBytes); + } + } + } +} diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpRestHeaderFilterStrategy.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpRestHeaderFilterStrategy.java index c168ed0..a208baf 100644 --- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpRestHeaderFilterStrategy.java +++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpRestHeaderFilterStrategy.java @@ -35,7 +35,7 @@ public class NettyHttpRestHeaderFilterStrategy extends NettyHttpHeaderFilterStra @Override public boolean applyFilterToCamelHeaders(String headerName, Object headerValue, Exchange exchange) { - boolean answer = super.applyFilterToExternalHeaders(headerName, headerValue, exchange); + boolean answer = super.applyFilterToCamelHeaders(headerName, headerValue, exchange); // using rest producer then headers are mapping to uri and query parameters using {key} syntax // if there is a match to an existing Camel Message header, then we should filter (=true) this // header as its already been mapped by the RestProducer from camel-core, and we do not want