Updated Branches: refs/heads/master 0db69352e -> 57359cef3
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/57359cef Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/57359cef Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/57359cef Branch: refs/heads/master Commit: 57359cef3db63c86db2feb8e5cf6ff753ca310c1 Parents: 0db6935 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Jun 17 17:14:34 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Jun 17 17:23:34 2013 +0200 ---------------------------------------------------------------------- .../netty/http/NettyHttpComponent.java | 3 - .../component/netty/http/NettyHttpHelper.java | 55 ++++++++++++++ .../component/netty/http/NettyHttpProducer.java | 12 ++-- .../NettyHttpEndpointUriEncodingIssueTest.java | 10 --- .../http/NettyHttpProducerQueryParamTest.java | 75 ++++++++++++++++++++ 5 files changed, 136 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/57359cef/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 cccbf2b..b884897 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 @@ -36,9 +36,6 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt // - matchOnUriPrefix // - urlrewrite - // TODO: producer - // - add support for HTTP_URI / HTTP_QUERY overrides - private NettyHttpBinding nettyHttpBinding; private HeaderFilterStrategy headerFilterStrategy; http://git-wip-us.apache.org/repos/asf/camel/blob/57359cef/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java index 4f0ce98..22d89a3 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java @@ -19,14 +19,19 @@ package org.apache.camel.component.netty.http; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.Message; +import org.apache.camel.RuntimeExchangeException; import org.apache.camel.converter.IOConverter; import org.apache.camel.util.IOHelper; +import org.apache.camel.util.URISupport; +import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpResponse; @@ -166,4 +171,54 @@ public final class NettyHttpHelper { return answer; } + /** + * Creates the URL to invoke. + * + * @param exchange the exchange + * @param endpoint the endpoint + * @return the URL to invoke + */ + public static String createURL(Exchange exchange, NettyHttpEndpoint endpoint, String uriParameters) throws URISyntaxException { + String uri = endpoint.getEndpointUri(); + if (uriParameters != null) { + uri += "?" + uriParameters; + } + + // resolve placeholders in uri + try { + uri = exchange.getContext().resolvePropertyPlaceholders(uri); + } catch (Exception e) { + throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uri, exchange, e); + } + + // ensure uri is encoded to be valid + uri = UnsafeUriCharactersEncoder.encode(uri); + + return uri; + } + + /** + * Creates the URI to invoke. + * + * @param exchange the exchange + * @param url the url to invoke + * @param endpoint the endpoint + * @return the URI to invoke + */ + public static URI createURI(Exchange exchange, String url, NettyHttpEndpoint endpoint) throws URISyntaxException { + URI uri = new URI(url); + // is a query string provided in the endpoint URI or in a header (header overrules endpoint) + String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); + if (queryString == null) { + // use raw as we encode just below + queryString = uri.getRawQuery(); + } + if (queryString != null) { + // need to encode query string + queryString = UnsafeUriCharactersEncoder.encode(queryString); + uri = URISupport.createURIWithQuery(uri, queryString); + } + return uri; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/57359cef/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java index 2e5772e..7a7ceda 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.netty.http; +import java.net.URI; + import org.apache.camel.AsyncCallback; import org.apache.camel.Exchange; import org.apache.camel.component.netty.NettyConfiguration; @@ -52,13 +54,11 @@ public class NettyHttpProducer extends NettyProducer { @Override protected Object getRequestBody(Exchange exchange) throws Exception { - String uri = getEndpoint().getEndpointUri(); - - if (uriParameters != null) { - uri += "?" + uriParameters; - } + // creating the url to use takes 2-steps + String uri = NettyHttpHelper.createURL(exchange, getEndpoint(), uriParameters); + URI u = NettyHttpHelper.createURI(exchange, uri, getEndpoint()); - HttpRequest request = getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getIn(), uri, getConfiguration()); + HttpRequest request = getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getIn(), u.toString(), getConfiguration()); String actualUri = request.getUri(); exchange.getIn().setHeader(Exchange.HTTP_URL, actualUri); http://git-wip-us.apache.org/repos/asf/camel/blob/57359cef/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriEncodingIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriEncodingIssueTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriEncodingIssueTest.java index 50f9248..ebb27f0 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriEncodingIssueTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriEncodingIssueTest.java @@ -19,7 +19,6 @@ package org.apache.camel.component.netty.http; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; -import org.junit.Ignore; import org.junit.Test; public class NettyHttpEndpointUriEncodingIssueTest extends BaseNettyTest { @@ -40,15 +39,6 @@ public class NettyHttpEndpointUriEncodingIssueTest extends BaseNettyTest { assertEquals("We got claus,s\u00F8ren columns", out); } - @Test - @Ignore("Implement the HTTP_URI override") - public void testEndpointHeaderUriEncodingIssue() throws Exception { - String uri = "netty-http:http://localhost:{{port}}/myapp/mytest?columns=totalsens,upsens&username=apiuser"; - String out = template.requestBodyAndHeader("netty-http:http://localhost/dummy", null, Exchange.HTTP_URI, uri, String.class); - - assertEquals("We got totalsens,upsens columns", out); - } - @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { http://git-wip-us.apache.org/repos/asf/camel/blob/57359cef/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerQueryParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerQueryParamTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerQueryParamTest.java new file mode 100644 index 0000000..0b7fe09 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerQueryParamTest.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.netty.http; + +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class NettyHttpProducerQueryParamTest extends BaseNettyTest { + + private String url = "netty-http:http://0.0.0.0:" + getPort() + "/cheese"; + + @Test + public void testQueryParameters() throws Exception { + Exchange exchange = template.request(url + "?quote=Camel%20rocks", null); + assertNotNull(exchange); + + String body = exchange.getOut().getBody(String.class); + Map<?, ?> headers = exchange.getOut().getHeaders(); + + assertEquals("Bye World", body); + assertEquals("Carlsberg", headers.get("beer")); + } + + @Test + public void testQueryParametersWithHeader() throws Exception { + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_QUERY, "quote=Camel rocks"); + } + }); + assertNotNull(exchange); + + String body = exchange.getOut().getBody(String.class); + Map<?, ?> headers = exchange.getOut().getHeaders(); + + assertEquals("Bye World", body); + assertEquals("Carlsberg", headers.get("beer")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from(url).process(new Processor() { + public void process(Exchange exchange) throws Exception { + String quote = exchange.getIn().getHeader("quote", String.class); + assertEquals("Camel rocks", quote); + + exchange.getOut().setBody("Bye World"); + exchange.getOut().setHeader("beer", "Carlsberg"); + } + }); + } + }; + } +}