Updated Branches: refs/heads/master d4aa9aece -> 070c795ca
CAMEL-6327: More work on new camel-netty-http component. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/070c795c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/070c795c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/070c795c Branch: refs/heads/master Commit: 070c795ca74da7b72a3e7efd31813b06080385c9 Parents: d4aa9ae Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Jun 17 10:18:33 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Jun 17 10:18:55 2013 +0200 ---------------------------------------------------------------------- .../component/jetty/JettyHttpHeadersTest.java | 54 ++++++++++++++++++++ .../netty/http/DefaultNettyHttpBinding.java | 28 ++++++++-- .../component/netty/http/NettyHttpBinding.java | 4 +- .../netty/http/NettyHttpComponent.java | 6 ++- .../netty/http/NettyHttpConfiguration.java | 9 ++++ .../netty/http/NettyHttpHeadersTest.java | 53 +++++++++++++++++++ 6 files changed, 146 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java new file mode 100644 index 0000000..31b6e45 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java @@ -0,0 +1,54 @@ +/** + * 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.builder.RouteBuilder; +import org.junit.Test; + +public class JettyHttpHeadersTest extends BaseJettyTest { + + @Test + public void testHttpHeaders() throws Exception { + getMockEndpoint("mock:input").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:input").expectedHeaderReceived("beer", "yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URL, "http://localhost:" + getPort() + "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URI, "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_QUERY, "beer=yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_PATH, ""); + + String out = template.requestBody("http://localhost:{{port}}/foo?beer=yes", "Hello World", String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://0.0.0.0:{{port}}/foo") + .to("mock:input") + .transform().constant("Bye World"); + } + }; + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/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 121e98e..adef7b9 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 @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.io.StringWriter; +import java.net.URI; import java.net.URLDecoder; import java.nio.charset.Charset; import java.util.Iterator; @@ -83,14 +84,31 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { public void populateCamelHeaders(HttpRequest request, Map<String, Object> headers, Exchange exchange, NettyHttpConfiguration configuration) throws Exception { LOG.trace("populateCamelHeaders: {}", request); - String uri = request.getUri(); + // NOTE: these headers is applied using the same logic as camel-http/camel-jetty to be consistent headers.put(Exchange.HTTP_METHOD, request.getMethod().getName()); - headers.put(Exchange.HTTP_URI, uri); + // strip query parameters from the uri + String s = request.getUri(); + if (s.contains("?")) { + s = ObjectHelper.before(s, "?"); + } + headers.put(Exchange.HTTP_URL, s); + // uri is without the host and port + URI uri = new URI(request.getUri()); + // uri is path and query parameters + headers.put(Exchange.HTTP_URI, uri.getPath()); + headers.put(Exchange.HTTP_QUERY, uri.getQuery()); + + // strip the starting endpoint path so the path is relative to the endpoint uri + String path = uri.getPath(); + if (configuration.getPath() != null && configuration.getPath().startsWith(path)) { + path = path.substring(configuration.getPath().length()); + } + headers.put(Exchange.HTTP_PATH, path); if (LOG.isTraceEnabled()) { LOG.trace("HTTP-Method {}", request.getMethod().getName()); - LOG.trace("HTTP-Uri {}", uri); + LOG.trace("HTTP-Uri {}", request.getUri()); } for (String name : request.getHeaderNames()) { @@ -113,8 +131,8 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { } // add uri parameters as headers to the Camel message - if (uri.contains("?")) { - String query = ObjectHelper.after(uri, "?"); + if (request.getUri().contains("?")) { + String query = ObjectHelper.after(request.getUri(), "?"); Map<String, Object> uriParameters = URISupport.parseQuery(query); for (Map.Entry<String, Object> entry : uriParameters.entrySet()) { http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java index cd67e96..bfc08e3 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java @@ -30,7 +30,7 @@ import org.jboss.netty.handler.codec.http.HttpResponse; public interface NettyHttpBinding { /** - * Binds from Netty {@link HttpRequest} to Camel {@Message}. + * Binds from Netty {@link HttpRequest} to Camel {@link Message}. * <p/> * Will use {@link #populateCamelHeaders(org.jboss.netty.handler.codec.http.HttpRequest, java.util.Map, org.apache.camel.Exchange, NettyHttpConfiguration)} * for populating the headers. @@ -55,7 +55,7 @@ public interface NettyHttpBinding { void populateCamelHeaders(HttpRequest request, Map<String, Object> headers, Exchange exchange, NettyHttpConfiguration configuration) throws Exception; /** - * Binds from Netty {@link HttpResponse} to Camel {@Message}. + * Binds from Netty {@link HttpResponse} to Camel {@link Message}. * <p/> * Will use {@link #populateCamelHeaders(org.jboss.netty.handler.codec.http.HttpResponse, java.util.Map, org.apache.camel.Exchange, NettyHttpConfiguration)} * for populating the headers. http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/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 6e5c00e..cccbf2b 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 @@ -35,7 +35,6 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt // - bridgeEndpoint // - matchOnUriPrefix // - urlrewrite - // - various CamelHttpUri headers with details about the url in use // TODO: producer // - add support for HTTP_URI / HTTP_QUERY overrides @@ -92,6 +91,11 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt configuration.setProtocol("tcp"); configuration.setTextline(false); + if (configuration instanceof NettyHttpConfiguration) { + URI uri = new URI(remaining); + ((NettyHttpConfiguration) configuration).setPath(uri.getPath()); + } + return configuration; } http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java index 26a636f..a89ef96 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java @@ -34,6 +34,7 @@ public class NettyHttpConfiguration extends NettyConfiguration { private boolean compression; private boolean throwExceptionOnFailure = true; private boolean transferException; + private String path; public NettyHttpConfiguration() { // we need sync=true as http is request/reply by nature @@ -106,4 +107,12 @@ public class NettyHttpConfiguration extends NettyConfiguration { public void setMapHeaders(boolean mapHeaders) { this.mapHeaders = mapHeaders; } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/070c795c/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java new file mode 100644 index 0000000..e452346 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeadersTest.java @@ -0,0 +1,53 @@ +/** + * 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.junit.Test; + +public class NettyHttpHeadersTest extends BaseNettyTest { + + @Test + public void testHttpHeaders() throws Exception { + getMockEndpoint("mock:input").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:input").expectedHeaderReceived("beer", "yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URL, "http://localhost:" + getPort() + "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URI, "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_QUERY, "beer=yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_PATH, ""); + + String out = template.requestBody("netty-http:http://localhost:{{port}}/foo?beer=yes", "Hello World", String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("netty-http:http://0.0.0.0:{{port}}/foo") + .to("mock:input") + .transform().constant("Bye World"); + } + }; + } + +}