Author: davsclaus Date: Wed Apr 14 13:43:06 2010 New Revision: 933947 URL: http://svn.apache.org/viewvc?rev=933947&view=rev Log: CAMEL-2645: Make it easier to configure authentication and proxy authentication on camel-http.
Added: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java (with props) camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java (with props) camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/NTMLAuthenticationHttpClientConfigurer.java - copied, changed from r933856, camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java (contents, props changed) - copied, changed from r933856, camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpClientConfigurerTest.java camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java (with props) camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java (with props) camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthComponentConfiguredTest.java - copied, changed from r933856, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java Removed: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/ProxyHttpClientConfigurer.java camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpClientConfigurerTest.java Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java Added: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java?rev=933947&view=auto ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java (added) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java Wed Apr 14 13:43:06 2010 @@ -0,0 +1,11 @@ +package org.apache.camel.component.http; + +/** + * Authentication policy + * + * @version $Revision$ + */ +public enum AuthMethod { + + Basic, Digest, NTML; +} Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/AuthMethod.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java Wed Apr 14 13:43:06 2010 @@ -18,31 +18,38 @@ package org.apache.camel.component.http; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.NTCredentials; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; public class BasicAuthenticationHttpClientConfigurer implements HttpClientConfigurer { + private final boolean proxy; private final String username; private final String password; - private final String domain; - private final String host; - - public BasicAuthenticationHttpClientConfigurer(String user, String pwd, String domain, String host) { - username = user; - password = pwd; - this.domain = domain; - this.host = host; + + public BasicAuthenticationHttpClientConfigurer(boolean proxy, String user, String pwd) { + this.proxy = proxy; + this.username = user; + this.password = pwd; } public void configureHttpClient(HttpClient client) { - Credentials defaultcreds; - if (domain != null) { - defaultcreds = new NTCredentials(username, password, host, domain); + Credentials credentials = new UsernamePasswordCredentials(username, password); + if (proxy) { + client.getState().setProxyCredentials(AuthScope.ANY, credentials); } else { - defaultcreds = new UsernamePasswordCredentials(username, password); + client.getState().setCredentials(AuthScope.ANY, credentials); } - client.getState().setCredentials(AuthScope.ANY, defaultcreds); } + public boolean isProxy() { + return proxy; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } } Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java Wed Apr 14 13:43:06 2010 @@ -17,6 +17,7 @@ package org.apache.camel.component.http; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.commons.httpclient.HttpClient; @@ -53,4 +54,7 @@ public class CompositeHttpConfigurer imp } } + public List<HttpClientConfigurer> getConfigurers() { + return Collections.unmodifiableList(configurers); + } } Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java Wed Apr 14 13:43:06 2010 @@ -24,6 +24,7 @@ import org.apache.camel.ResolveEndpointF import org.apache.camel.impl.HeaderFilterStrategyComponent; import org.apache.camel.util.CastUtils; import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; @@ -39,8 +40,8 @@ public class HttpComponent extends Heade protected HttpClientConfigurer httpClientConfigurer; protected HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); protected HttpBinding httpBinding; - - + protected HttpConfiguration httpConfiguration; + /** * Connects the URL specified on the endpoint to the specified processor. * @@ -76,38 +77,98 @@ public class HttpComponent extends Heade // fallback to component configured configurer = getHttpClientConfigurer(); } - - // check the user name and password for basic authentication - String username = getAndRemoveParameter(parameters, "username", String.class); - String password = getAndRemoveParameter(parameters, "password", String.class); - String domain = getAndRemoveParameter(parameters, "domain", String.class); - String host = getAndRemoveParameter(parameters, "host", String.class); - if (username != null && password != null) { - configurer = CompositeHttpConfigurer.combineConfigurers( - configurer, - new BasicAuthenticationHttpClientConfigurer(username, password, domain, host)); - } - - // check the proxy details for proxy configuration - String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class); - Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class); - if (proxyHost != null && proxyPort != null) { - String proxyUsername = getAndRemoveParameter(parameters, "proxyUsername", String.class); - String proxyPassword = getAndRemoveParameter(parameters, "proxyPassword", String.class); - String proxyDomain = getAndRemoveParameter(parameters, "proxyDomain", String.class); - String proxyNtHost = getAndRemoveParameter(parameters, "proxyNtHost", String.class); - if (proxyUsername != null && proxyPassword != null) { - configurer = CompositeHttpConfigurer.combineConfigurers( - configurer, new ProxyHttpClientConfigurer(proxyHost, proxyPort, proxyUsername, proxyPassword, proxyDomain, proxyNtHost)); - } else { - configurer = CompositeHttpConfigurer.combineConfigurers( - configurer, new ProxyHttpClientConfigurer(proxyHost, proxyPort)); - } + + // authentication can be endpoint configured + String authUsername = getAndRemoveParameter(parameters, "authUsername", String.class); + AuthMethod authMethod = getAndRemoveParameter(parameters, "authMethod", AuthMethod.class); + // validate that if auth username is given then the auth method is also provided + if (authUsername != null && authMethod == null) { + throw new IllegalArgumentException("Option authMethod must be provided to use authentication"); + } + if (authMethod != null) { + String authPassword = getAndRemoveParameter(parameters, "authPassword", String.class); + String authDomain = getAndRemoveParameter(parameters, "authDomain", String.class); + String authHost = getAndRemoveParameter(parameters, "authHost", String.class); + configurer = configureAuth(configurer, authMethod, authUsername, authPassword, authDomain, authHost); + } else if (httpConfiguration != null) { + // or fallback to use component configuration + configurer = configureAuth(configurer, httpConfiguration.getAuthMethod(), httpConfiguration.getAuthUsername(), + httpConfiguration.getAuthPassword(), httpConfiguration.getAuthDomain(), httpConfiguration.getAuthHost()); + } + + // proxy authentication can be endpoint configured + String proxyAuthUsername = getAndRemoveParameter(parameters, "proxyAuthUsername", String.class); + AuthMethod proxyAuthMethod = getAndRemoveParameter(parameters, "proxyAuthMethod", AuthMethod.class); + // validate that if proxy auth username is given then the proxy auth method is also provided + if (proxyAuthUsername != null && proxyAuthMethod == null) { + throw new IllegalArgumentException("Option proxyAuthMethod must be provided to use proxy authentication"); + } + if (proxyAuthMethod != null) { + String proxyAuthPassword = getAndRemoveParameter(parameters, "proxyAuthPassword", String.class); + String proxyAuthDomain = getAndRemoveParameter(parameters, "proxyAuthDomain", String.class); + String proxyAuthHost = getAndRemoveParameter(parameters, "proxyAuthHost", String.class); + configurer = configureProxyAuth(configurer, proxyAuthMethod, proxyAuthUsername, proxyAuthPassword, proxyAuthDomain, proxyAuthHost); + } else if (httpConfiguration != null) { + // or fallback to use component configuration + configurer = configureProxyAuth(configurer, httpConfiguration.getProxyAuthMethod(), httpConfiguration.getProxyAuthUsername(), + httpConfiguration.getProxyAuthPassword(), httpConfiguration.getProxyAuthDomain(), httpConfiguration.getProxyAuthHost()); } return configurer; } + + /** + * Configures the authentication method to be used + * + * @return configurer to used + */ + protected HttpClientConfigurer configureAuth(HttpClientConfigurer configurer, AuthMethod authMethod, String username, String password, String domain, String host) { + if (authMethod == null) { + return configurer; + } + + ObjectHelper.notNull(username, "authUsername"); + ObjectHelper.notNull(password, "authPassword"); + + if (authMethod == AuthMethod.Basic || authMethod == AuthMethod.Digest) { + return CompositeHttpConfigurer.combineConfigurers(configurer, + new BasicAuthenticationHttpClientConfigurer(false, username, password)); + } else if (authMethod == AuthMethod.NTML) { + // domain is mandatory for NTML + ObjectHelper.notNull(domain, "authDomain"); + return CompositeHttpConfigurer.combineConfigurers(configurer, + new NTMLAuthenticationHttpClientConfigurer(false, username, password, domain, host)); + } + + throw new IllegalArgumentException("Unknown authMethod " + authMethod); + } + /** + * Configures the proxy authentication method to be used + * + * @return configurer to used + */ + protected HttpClientConfigurer configureProxyAuth(HttpClientConfigurer configurer, AuthMethod authMethod, String username, String password, String domain, String host) { + if (authMethod == null) { + return configurer; + } + + ObjectHelper.notNull(username, "proxyAuthUsername"); + ObjectHelper.notNull(password, "proxyAuthPassword"); + + if (authMethod == AuthMethod.Basic || authMethod == AuthMethod.Digest) { + return CompositeHttpConfigurer.combineConfigurers(configurer, + new BasicAuthenticationHttpClientConfigurer(true, username, password)); + } else if (authMethod == AuthMethod.NTML) { + // domain is mandatory for NTML + ObjectHelper.notNull(domain, "proxyAuthDomain"); + return CompositeHttpConfigurer.combineConfigurers(configurer, + new NTMLAuthenticationHttpClientConfigurer(true, username, password, domain, host)); + } + + throw new IllegalArgumentException("Unknown proxyAuthMethod " + authMethod); + } + @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { @@ -120,6 +181,8 @@ public class HttpComponent extends Heade Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, "throwExceptionOnFailure", Boolean.class); Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class); Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class); + String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class); + Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class); // http client can be configured from URI options HttpClientParams clientParams = new HttpClientParams(); IntrospectionSupport.setProperties(clientParams, parameters, "httpClient."); @@ -165,6 +228,13 @@ public class HttpComponent extends Heade if (matchOnUriPrefix != null) { endpoint.setMatchOnUriPrefix(matchOnUriPrefix); } + if (proxyHost != null) { + endpoint.setProxyHost(proxyHost); + endpoint.setProxyPort(proxyPort); + } else if (httpConfiguration != null) { + endpoint.setProxyHost(httpConfiguration.getProxyHost()); + endpoint.setProxyPort(httpConfiguration.getProxyPort()); + } setProperties(endpoint, parameters); return endpoint; @@ -199,4 +269,13 @@ public class HttpComponent extends Heade public void setHttpBinding(HttpBinding httpBinding) { this.httpBinding = httpBinding; } + + public HttpConfiguration getHttpConfiguration() { + return httpConfiguration; + } + + public void setHttpConfiguration(HttpConfiguration httpConfiguration) { + this.httpConfiguration = httpConfiguration; + } + } Added: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java?rev=933947&view=auto ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java (added) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java Wed Apr 14 13:43:06 2010 @@ -0,0 +1,136 @@ +/** + * 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.http; + +import java.io.Serializable; + +/** + * @version $Revision$ + */ +public class HttpConfiguration implements Serializable { + + private AuthMethod authMethod; + private String authUsername; + private String authPassword; + private String authDomain; + private String authHost; + + private AuthMethod proxyAuthMethod; + private String proxyAuthUsername; + private String proxyAuthPassword; + private String proxyAuthDomain; + private String proxyAuthHost; + + private String proxyHost; + private int proxyPort; + + public String getAuthUsername() { + return authUsername; + } + + public void setAuthUsername(String authUsername) { + this.authUsername = authUsername; + } + + public String getAuthPassword() { + return authPassword; + } + + public void setAuthPassword(String authPassword) { + this.authPassword = authPassword; + } + + public String getAuthDomain() { + return authDomain; + } + + public void setAuthDomain(String authDomain) { + this.authDomain = authDomain; + } + + public String getAuthHost() { + return authHost; + } + + public void setAuthHost(String authHost) { + this.authHost = authHost; + } + + public String getProxyAuthUsername() { + return proxyAuthUsername; + } + + public void setProxyAuthUsername(String proxyAuthUsername) { + this.proxyAuthUsername = proxyAuthUsername; + } + + public String getProxyAuthPassword() { + return proxyAuthPassword; + } + + public void setProxyAuthPassword(String proxyAuthPassword) { + this.proxyAuthPassword = proxyAuthPassword; + } + + public String getProxyAuthDomain() { + return proxyAuthDomain; + } + + public void setProxyAuthDomain(String proxyAuthDomain) { + this.proxyAuthDomain = proxyAuthDomain; + } + + public String getProxyAuthHost() { + return proxyAuthHost; + } + + public void setProxyAuthHost(String proxyAuthHost) { + this.proxyAuthHost = proxyAuthHost; + } + + public AuthMethod getAuthMethod() { + return authMethod; + } + + public void setAuthMethod(AuthMethod authMethod) { + this.authMethod = authMethod; + } + + public AuthMethod getProxyAuthMethod() { + return proxyAuthMethod; + } + + public void setProxyAuthMethod(AuthMethod proxyAuthMethod) { + this.proxyAuthMethod = proxyAuthMethod; + } + + public String getProxyHost() { + return proxyHost; + } + + public void setProxyHost(String proxyHost) { + this.proxyHost = proxyHost; + } + + public int getProxyPort() { + return proxyPort; + } + + public void setProxyPort(int proxyPort) { + this.proxyPort = proxyPort; + } +} Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConfiguration.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Wed Apr 14 13:43:06 2010 @@ -49,6 +49,8 @@ public class HttpEndpoint extends Defaul private boolean bridgeEndpoint; private boolean matchOnUriPrefix; private boolean chunked = true; + private String proxyHost; + private int proxyPort; public HttpEndpoint() { } @@ -99,6 +101,13 @@ public class HttpEndpoint extends Defaul answer.getHostConfiguration().setProxy(host, port); } + if (proxyHost != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Using proxy: " + proxyHost + ":" + proxyPort); + } + answer.getHostConfiguration().setProxy(proxyHost, proxyPort); + } + answer.setHttpConnectionManager(httpConnectionManager); HttpClientConfigurer configurer = getHttpClientConfigurer(); if (configurer != null) { @@ -243,4 +252,20 @@ public class HttpEndpoint extends Defaul public void setChunked(boolean chunked) { this.chunked = chunked; } + + public String getProxyHost() { + return proxyHost; + } + + public void setProxyHost(String proxyHost) { + this.proxyHost = proxyHost; + } + + public int getProxyPort() { + return proxyPort; + } + + public void setProxyPort(int proxyPort) { + this.proxyPort = proxyPort; + } } Copied: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/NTMLAuthenticationHttpClientConfigurer.java (from r933856, camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/NTMLAuthenticationHttpClientConfigurer.java?p2=camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/NTMLAuthenticationHttpClientConfigurer.java&p1=camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java&r1=933856&r2=933947&rev=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/BasicAuthenticationHttpClientConfigurer.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/NTMLAuthenticationHttpClientConfigurer.java Wed Apr 14 13:43:06 2010 @@ -19,30 +19,49 @@ package org.apache.camel.component.http; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NTCredentials; -import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; -public class BasicAuthenticationHttpClientConfigurer implements HttpClientConfigurer { +public class NTMLAuthenticationHttpClientConfigurer implements HttpClientConfigurer { + private final boolean proxy; private final String username; private final String password; private final String domain; private final String host; - - public BasicAuthenticationHttpClientConfigurer(String user, String pwd, String domain, String host) { - username = user; - password = pwd; + + public NTMLAuthenticationHttpClientConfigurer(boolean proxy, String user, String pwd, String domain, String host) { + this.proxy = proxy; + this.username = user; + this.password = pwd; this.domain = domain; this.host = host; } public void configureHttpClient(HttpClient client) { - Credentials defaultcreds; - if (domain != null) { - defaultcreds = new NTCredentials(username, password, host, domain); + Credentials credentials = new NTCredentials(username, password, host, domain); + if (proxy) { + client.getState().setProxyCredentials(AuthScope.ANY, credentials); } else { - defaultcreds = new UsernamePasswordCredentials(username, password); + client.getState().setCredentials(AuthScope.ANY, credentials); } - client.getState().setCredentials(AuthScope.ANY, defaultcreds); } -} + public boolean isProxy() { + return proxy; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getDomain() { + return domain; + } + + public String getHost() { + return host; + } +} \ No newline at end of file Copied: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java (from r933856, camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpClientConfigurerTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java?p2=camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java&p1=camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpClientConfigurerTest.java&r1=933856&r2=933947&rev=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpClientConfigurerTest.java (original) +++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java Wed Apr 14 13:43:06 2010 @@ -23,22 +23,37 @@ import org.junit.Test; /** * @version $Revision: 905992 $ */ -public class HttpClientConfigurerTest extends CamelTestSupport { - private HttpClientConfigurer configurer; - +public class HttpProxyAuthBasicTest extends CamelTestSupport { + @Test - public void testHttpClientConfigurer() throws Exception { - HttpClientConfigurer gotConfigurer = getMandatoryEndpoint("http://www.google.com/search", HttpEndpoint.class).getHttpClientConfigurer(); - assertSame(configurer, gotConfigurer); + public void testProxyAuthBasic() throws Exception { + HttpClientConfigurer configurer = getMandatoryEndpoint("http://www.google.com/search", HttpEndpoint.class).getHttpClientConfigurer(); + assertNotNull(configurer); + + CompositeHttpConfigurer comp = assertIsInstanceOf(CompositeHttpConfigurer.class, configurer); + assertEquals(1, comp.getConfigurers().size()); + + BasicAuthenticationHttpClientConfigurer basic = assertIsInstanceOf(BasicAuthenticationHttpClientConfigurer.class, comp.getConfigurers().get(0)); + assertTrue(basic.isProxy()); + assertEquals("myUser", basic.getUsername()); + assertEquals("myPassword", basic.getPassword()); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { - // add configurer to http component - configurer = new ProxyHttpClientConfigurer("proxyhost", 80, "user", "password", null, null); - getContext().getComponent("http", HttpComponent.class).setHttpClientConfigurer(configurer); + // setup proxy details + HttpConfiguration config = new HttpConfiguration(); + config.setProxyHost("myProxyHosy"); + config.setProxyPort(1234); + // proxy requires auth as well + config.setProxyAuthMethod(AuthMethod.Basic); + config.setProxyAuthUsername("myUser"); + config.setProxyAuthPassword("myPassword"); + + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setHttpConfiguration(config); from("direct:start") .to("http://www.google.com/search"); Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthBasicTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java?rev=933947&view=auto ============================================================================== --- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java (added) +++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java Wed Apr 14 13:43:06 2010 @@ -0,0 +1,60 @@ +/** + * 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.http; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * @version $Revision$ + */ +public class HttpProxyAuthDigestTest extends CamelTestSupport { + + @Test + public void testProxyAuthDigest() throws Exception { + HttpClientConfigurer configurer = getMandatoryEndpoint("http://www.google.com/search", HttpEndpoint.class).getHttpClientConfigurer(); + assertNotNull(configurer); + + CompositeHttpConfigurer comp = assertIsInstanceOf(CompositeHttpConfigurer.class, configurer); + assertEquals(1, comp.getConfigurers().size()); + + BasicAuthenticationHttpClientConfigurer basic = assertIsInstanceOf(BasicAuthenticationHttpClientConfigurer.class, comp.getConfigurers().get(0)); + assertTrue(basic.isProxy()); + assertEquals("myUser", basic.getUsername()); + assertEquals("myPassword", basic.getPassword()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // setup proxy details + HttpConfiguration config = new HttpConfiguration(); + config.setProxyAuthMethod(AuthMethod.Digest); + config.setProxyAuthUsername("myUser"); + config.setProxyAuthPassword("myPassword"); + + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setHttpConfiguration(config); + + from("direct:start") + .to("http://www.google.com/search"); + } + }; + } +} \ No newline at end of file Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthDigestTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java?rev=933947&view=auto ============================================================================== --- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java (added) +++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java Wed Apr 14 13:43:06 2010 @@ -0,0 +1,67 @@ +/** + * 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.http; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * @version $Revision$ + */ +public class HttpProxyAuthNTLMTest extends CamelTestSupport { + + @Test + public void testProxyAuthNTLM() throws Exception { + HttpClientConfigurer configurer = getMandatoryEndpoint("http://www.google.com/search", HttpEndpoint.class).getHttpClientConfigurer(); + assertNotNull(configurer); + + CompositeHttpConfigurer comp = assertIsInstanceOf(CompositeHttpConfigurer.class, configurer); + assertEquals(1, comp.getConfigurers().size()); + + NTMLAuthenticationHttpClientConfigurer ntlm = assertIsInstanceOf(NTMLAuthenticationHttpClientConfigurer.class, comp.getConfigurers().get(0)); + assertTrue(ntlm.isProxy()); + assertEquals("myUser", ntlm.getUsername()); + assertEquals("myPassword", ntlm.getPassword()); + assertEquals("myDomain", ntlm.getDomain()); + assertEquals("myHost", ntlm.getHost()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // setup proxy details + HttpConfiguration config = new HttpConfiguration(); + config.setProxyHost("myProxyHosy"); + config.setProxyPort(1234); + + config.setProxyAuthMethod(AuthMethod.NTML); + config.setProxyAuthUsername("myUser"); + config.setProxyAuthPassword("myPassword"); + config.setProxyAuthDomain("myDomain"); + config.setProxyAuthHost("myHost"); + + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setHttpConfiguration(config); + + from("direct:start") + .to("http://www.google.com/search"); + } + }; + } +} \ No newline at end of file Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyAuthNTLMTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyTest.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyTest.java (original) +++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProxyTest.java Wed Apr 14 13:43:06 2010 @@ -18,7 +18,6 @@ package org.apache.camel.component.http; import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.commons.httpclient.HttpClient; -import org.junit.Ignore; import org.junit.Test; /** @@ -27,7 +26,6 @@ import org.junit.Test; public class HttpProxyTest extends CamelTestSupport { @Test - @Ignore("ignore online tests, will be improved in Camel 2.3") public void testNoHttpProxyConfigured() throws Exception { HttpEndpoint http = context.getEndpoint("http://www.google.com", HttpEndpoint.class); @@ -37,7 +35,6 @@ public class HttpProxyTest extends Camel } @Test - @Ignore("ignore online tests, will be improved in Camel 2.3") public void testHttpProxyConfigured() throws Exception { HttpEndpoint http = context.getEndpoint("http://www.google.com", HttpEndpoint.class); @@ -55,7 +52,6 @@ public class HttpProxyTest extends Camel } @Test - @Ignore("ignore online tests, will be improved in Camel 2.3") public void testHttpProxyEndpointConfigured() throws Exception { HttpEndpoint http = context.getEndpoint("http://www.google.com?proxyHost=myotherproxy&proxyPort=2345", HttpEndpoint.class); @@ -72,4 +68,28 @@ public class HttpProxyTest extends Camel } } + @Test + public void testHttpProxyComponentConfigured() throws Exception { + HttpConfiguration config = new HttpConfiguration(); + config.setProxyHost("myotherproxy"); + config.setProxyPort(2345); + + HttpComponent comp = context.getComponent("http", HttpComponent.class); + comp.setHttpConfiguration(config); + + HttpEndpoint http = context.getEndpoint("http://www.google.com", HttpEndpoint.class); + + context.getProperties().put("http.proxyHost", "myproxy"); + context.getProperties().put("http.proxyPort", "1234"); + + try { + HttpClient client = http.createHttpClient(); + assertEquals("myotherproxy", client.getHostConfiguration().getProxyHost()); + assertEquals(2345, client.getHostConfiguration().getProxyPort()); + } finally { + context.getProperties().remove("http.proxyHost"); + context.getProperties().remove("http.proxyPort"); + } + } + } Copied: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthComponentConfiguredTest.java (from r933856, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthComponentConfiguredTest.java?p2=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthComponentConfiguredTest.java&p1=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java&r1=933856&r2=933947&rev=933947&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthComponentConfiguredTest.java Wed Apr 14 13:43:06 2010 @@ -18,12 +18,14 @@ package org.apache.camel.component.jetty import java.io.IOException; import java.security.Principal; - import javax.servlet.http.HttpServletRequest; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.AuthMethod; +import org.apache.camel.component.http.HttpComponent; +import org.apache.camel.component.http.HttpConfiguration; import org.apache.camel.impl.JndiRegistry; import org.apache.camel.test.junit4.CamelTestSupport; import org.eclipse.jetty.http.security.Constraint; @@ -37,7 +39,7 @@ import org.junit.Test; /** * @version $Revision$ */ -public class HttpBasicAuthTest extends CamelTestSupport { +public class HttpBasicAuthComponentConfiguredTest extends CamelTestSupport { @Override protected JndiRegistry createRegistry() throws Exception { @@ -57,7 +59,7 @@ public class HttpBasicAuthTest extends C ConstraintSecurityHandler sh = new ConstraintSecurityHandler(); sh.setAuthenticator(new BasicAuthenticator()); sh.setConstraintMappings(new ConstraintMapping[] {cm}); - + HashLoginService loginService = new HashLoginService("MyRealm", "src/test/resources/myRealm.properties"); sh.setLoginService(loginService); sh.setConstraintMappings(new ConstraintMapping[]{cm}); @@ -67,7 +69,7 @@ public class HttpBasicAuthTest extends C @Test public void testHttpBaiscAuth() throws Exception { - String out = template.requestBody("http://localhost:9080/test?username=donald&password=duck", "Hello World", String.class); + String out = template.requestBody("http://localhost:9080/test", "Hello World", String.class); assertEquals("Bye World", out); } @@ -76,6 +78,14 @@ public class HttpBasicAuthTest extends C return new RouteBuilder() { @Override public void configure() throws Exception { + HttpConfiguration config = new HttpConfiguration(); + config.setAuthMethod(AuthMethod.Basic); + config.setAuthUsername("donald"); + config.setAuthPassword("duck"); + + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setHttpConfiguration(config); + from("jetty://http://localhost:9080/test?handlers=myAuthHandler") .process(new Processor() { public void process(Exchange exchange) throws Exception { @@ -90,4 +100,4 @@ public class HttpBasicAuthTest extends C } }; } -} +} \ No newline at end of file Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java?rev=933947&r1=933946&r2=933947&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java Wed Apr 14 13:43:06 2010 @@ -23,7 +23,9 @@ import javax.servlet.http.HttpServletReq import org.apache.camel.Exchange; import org.apache.camel.Processor; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpOperationFailedException; import org.apache.camel.impl.JndiRegistry; import org.apache.camel.test.junit4.CamelTestSupport; import org.eclipse.jetty.http.security.Constraint; @@ -67,10 +69,20 @@ public class HttpBasicAuthTest extends C @Test public void testHttpBaiscAuth() throws Exception { - String out = template.requestBody("http://localhost:9080/test?username=donald&password=duck", "Hello World", String.class); + String out = template.requestBody("http://localhost:9080/test?authMethod=Basic&authUsername=donald&authPassword=duck", "Hello World", String.class); assertEquals("Bye World", out); } + @Test + public void testHttpBaiscAuthInvalidPassword() throws Exception { + try { + template.requestBody("http://localhost:9080/test?authMethod=Basic&authUsername=donald&authPassword=sorry", "Hello World", String.class); + } catch (RuntimeCamelException e) { + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(401, cause.getStatusCode()); + } + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() {