Updated Branches: refs/heads/camel-2.12.x a54774e74 -> a72a3c255
CAMEL-6880: camel-http4 should support custom component names, and each may be configured different SSL context etc. Also getEndpointUri should use the custom component name. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a72a3c25 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a72a3c25 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a72a3c25 Branch: refs/heads/camel-2.12.x Commit: a72a3c255c43bbd102ac1035c4909a4ac5f48b64 Parents: a54774e Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Oct 21 14:40:22 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Oct 21 14:51:11 2013 +0200 ---------------------------------------------------------------------- .../camel/component/http4/HttpComponent.java | 71 ++++++++++++-------- .../camel/component/http4/HttpEndpoint.java | 3 - .../HttpBodyWithOtherProtocalNameTest.java | 2 +- .../http4/HttpCustomComponentNameTest.java | 52 ++++++++++++++ .../component/http4/HttpProxyServerTest.java | 3 +- ...woComponentsSslContextParametersGetTest.java | 71 ++++++++++++++++++++ 6 files changed, 168 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java index cd40cd8..4d0d230 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java @@ -29,6 +29,7 @@ import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; +import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.apache.camel.util.jsse.SSLContextParameters; import org.apache.http.auth.params.AuthParamBean; import org.apache.http.client.CookieStore; @@ -168,10 +169,6 @@ public class HttpComponent extends HeaderFilterStrategyComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - String addressUri = uri; - if (!uri.startsWith("http4:") && !uri.startsWith("https4:")) { - addressUri = remaining; - } Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters); // http client can be configured from URI options HttpParams clientParams = configureHttpParams(parameters); @@ -208,13 +205,52 @@ public class HttpComponent extends HeaderFilterStrategyComponent { HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class); UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class); - boolean secure = HttpHelper.isSecureConnection(uri); + boolean secure = HttpHelper.isSecureConnection(uri) || sslContextParameters != null; + + // need to set scheme on address uri depending on if its secure or not + String addressUri = remaining.startsWith("http") ? remaining : null; + if (addressUri == null) { + if (secure) { + addressUri = "https://" + remaining; + } else { + addressUri = "http://" + remaining; + } + } + addressUri = UnsafeUriCharactersEncoder.encode(addressUri); + URI uriHttpUriAddress = new URI(addressUri); + + // validate http uri that end-user did not duplicate the http part that can be a common error + int pos = uri.indexOf("//"); + if (pos != -1) { + String part = uri.substring(pos + 2); + if (part.startsWith("http:") || part.startsWith("https:")) { + throw new ResolveEndpointFailedException(uri, + "The uri part is not configured correctly. You have duplicated the http(s) protocol."); + } + } // create the configurer to use for this endpoint HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, secure); - URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters); + URI endpointUri = URISupport.createRemainingURI(uriHttpUriAddress, httpClientParameters); + + + // the endpoint uri should use the component name as scheme, so we need to re-create it once more + String scheme = ObjectHelper.before(uri, "://"); + endpointUri = URISupport.createRemainingURI( + new URI(scheme, + endpointUri.getUserInfo(), + endpointUri.getHost(), + endpointUri.getPort(), + endpointUri.getPath(), + endpointUri.getQuery(), + endpointUri.getFragment()), + httpClientParameters); + // create the endpoint and set the http uri to be null - HttpEndpoint endpoint = new HttpEndpoint(endpointUri.toString(), this, clientParams, clientConnectionManager, configurer); + String endpointUriString = endpointUri.toString(); + + LOG.debug("Creating endpoint uri {}", endpointUriString); + HttpEndpoint endpoint = new HttpEndpoint(endpointUriString, this, clientParams, clientConnectionManager, configurer); if (urlRewrite != null) { // let CamelContext deal with the lifecycle of the url rewrite // this ensures its being shutdown when Camel shutdown etc. @@ -223,27 +259,6 @@ public class HttpComponent extends HeaderFilterStrategyComponent { } // configure the endpoint setProperties(endpoint, parameters); - // The httpUri should be start with http or https - String httpUriAddress = addressUri; - if (addressUri.startsWith("http4")) { - httpUriAddress = "http" + addressUri.substring(5); - } - if (addressUri.startsWith("https4")) { - httpUriAddress = "https" + addressUri.substring(6); - } - // restructure uri to be based on the parameters left as we dont want to include the Camel internal options - // build up the http uri - URI uriHttpUriAddress = new URI(httpUriAddress); - - // validate http uri that end-user did not duplicate the http part that can be a common error - String part = uriHttpUriAddress.getSchemeSpecificPart(); - if (part != null) { - part = part.toLowerCase(); - if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://") || part.startsWith("//https://")) { - throw new ResolveEndpointFailedException(uri, - "The uri part is not configured correctly. You have duplicated the http(s) protocol."); - } - } // determine the portnumber (special case: default portnumber) int port = getPort(uriHttpUriAddress); http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java index 06bc133..0c9a355 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java @@ -30,14 +30,11 @@ import org.apache.camel.util.ObjectHelper; import org.apache.http.HttpHost; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; -import org.apache.http.client.protocol.ClientContext; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; -import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.DefaultedHttpContext; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java index bc10135..9185765 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpBodyWithOtherProtocalNameTest.java @@ -25,7 +25,7 @@ public class HttpBodyWithOtherProtocalNameTest extends HttpBodyTest { @Override public void setUp() throws Exception { super.setUp(); - setProtocolString("newHttp://http://"); + setProtocolString("newHttp://"); } public CamelContext createCamelContext() throws Exception { http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCustomComponentNameTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCustomComponentNameTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCustomComponentNameTest.java new file mode 100644 index 0000000..7a33b83 --- /dev/null +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCustomComponentNameTest.java @@ -0,0 +1,52 @@ +/** + * 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 org.apache.camel.Component; +import org.apache.camel.Endpoint; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.camel.util.ServiceHelper; +import org.junit.Test; + +public class HttpCustomComponentNameTest extends CamelTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testCustomName() throws Exception { + context.start(); + + int port = AvailablePortFinder.getNextAvailable(24400); + + Component custom = new HttpComponent(); + context.addComponent("http-foo", custom); + ServiceHelper.startService(custom); + + String uri = "http-foo://www.somewhere.com:" + port + "?q=Camel"; + Endpoint endpoint = context.getEndpoint(uri); + assertNotNull(endpoint); + // the endpoint uri should use the custom component name as scheme + assertEquals(uri, endpoint.getEndpointUri()); + + context.stop(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java index b374701..b92f313 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProxyServerTest.java @@ -87,7 +87,6 @@ public class HttpProxyServerTest extends BaseHttpTest { HttpEndpoint http1 = context.getEndpoint("http4://www.google.com?proxyAuthHost=myproxy&proxyAuthPort=1234", HttpEndpoint.class); HttpEndpoint http2 = context.getEndpoint("http4://www.google.com?test=parameter&proxyAuthHost=myotherproxy&proxyAuthPort=2345", HttpEndpoint.class); - HttpClient client1 = http1.createHttpClient(); HttpHost proxy1 = (HttpHost)client1.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY); assertEquals("myproxy", proxy1.getHostName()); @@ -98,7 +97,7 @@ public class HttpProxyServerTest extends BaseHttpTest { assertEquals("myotherproxy", proxy2.getHostName()); assertEquals(2345, proxy2.getPort()); - //As the endpointUri is recreated, so the parameter could be in different place, so we use the URISupport.normalizeUri + //As the endpointUri is recreated, so the parameter could be in different place, so we use the URISupport.normalizeUri assertEquals("Get a wrong endpoint uri of http1", "http4://www.google.com?proxyAuthHost=myproxy&proxyAuthPort=1234", URISupport.normalizeUri(http1.getEndpointUri())); assertEquals("Get a wrong endpoint uri of http2", "http4://www.google.com?proxyAuthHost=myotherproxy&proxyAuthPort=2345&test=parameter", URISupport.normalizeUri(http2.getEndpointUri())); http://git-wip-us.apache.org/repos/asf/camel/blob/a72a3c25/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpsTwoComponentsSslContextParametersGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpsTwoComponentsSslContextParametersGetTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpsTwoComponentsSslContextParametersGetTest.java new file mode 100644 index 0000000..9a9793f --- /dev/null +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpsTwoComponentsSslContextParametersGetTest.java @@ -0,0 +1,71 @@ +/** + * 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 org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.util.jsse.SSLContextParameters; +import org.apache.http.conn.ssl.AllowAllHostnameVerifier; +import org.junit.Test; + +public class HttpsTwoComponentsSslContextParametersGetTest extends BaseHttpsTest { + + private int port2; + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("x509HostnameVerifier", new AllowAllHostnameVerifier()); + registry.bind("sslContextParameters", new SSLContextParameters()); + registry.bind("sslContextParameters2", new SSLContextParameters()); + + registry.bind("http4s-foo", new HttpComponent()); + registry.bind("http4s-bar", new HttpComponent()); + + return registry; + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void httpsTwoDifferentSSLContextNotSupported() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + port2 = AvailablePortFinder.getNextAvailable(getPort()); + + from("direct:foo") + .to("http4s-foo://127.0.0.1:" + getPort() + "/mail?x509HostnameVerifier=x509HostnameVerifier&sslContextParametersRef=sslContextParameters"); + + from("direct:bar") + .to("http4s-bar://127.0.0.1:" + port2 + "/mail?x509HostnameVerifier=x509HostnameVerifier&sslContextParametersRef=sslContextParameters2"); + } + }); + + context.start(); + + // should be able to startup + Thread.sleep(500); + + context.stop(); + } + +}