Repository: olingo-odata4 Updated Branches: refs/heads/master 16d3b0288 -> 1e8eaecf0
[OLINGO-233] HTTP proxy enabled HttpClientFactory provided Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/1e8eaecf Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/1e8eaecf Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/1e8eaecf Branch: refs/heads/master Commit: 1e8eaecf066817802945595a5410a104132f1283 Parents: 16d3b02 Author: Francesco Chicchiriccò <[email protected]> Authored: Wed Apr 2 16:44:39 2014 +0200 Committer: Francesco Chicchiriccò <[email protected]> Committed: Wed Apr 2 16:44:39 2014 +0200 ---------------------------------------------------------------------- .../AbstractBasicAuthHttpClientFactory.java | 51 ------------ .../http/AbstractNTLMAuthHttpClientFactory.java | 63 -------------- .../core/http/BasicAuthHttpClientFactory.java | 55 +++++++++++++ .../core/http/NTLMAuthHttpClientFactory.java | 73 +++++++++++++++++ .../http/ProxyWrapperHttpClientFactory.java | 86 ++++++++++++++++++++ .../it/v3/AuthEntityRetrieveTestITCase.java | 16 +--- 6 files changed, 216 insertions(+), 128 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractBasicAuthHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractBasicAuthHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractBasicAuthHttpClientFactory.java deleted file mode 100644 index 806bfb5..0000000 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractBasicAuthHttpClientFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.olingo.client.core.http; - -import java.net.URI; - -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.olingo.client.api.http.HttpMethod; - -/** - * Base implementation for working with Basic Authentication: needs to be subclassed in order to provide actual username - * and password. - */ -public abstract class AbstractBasicAuthHttpClientFactory extends DefaultHttpClientFactory { - - private static final long serialVersionUID = 7985626503125490244L; - - protected abstract String getUsername(); - - protected abstract String getPassword(); - - @Override - public HttpClient createHttpClient(final HttpMethod method, final URI uri) { - final DefaultHttpClient httpclient = (DefaultHttpClient) super.createHttpClient(method, uri); - - httpclient.getCredentialsProvider().setCredentials( - new AuthScope(uri.getHost(), uri.getPort()), - new UsernamePasswordCredentials(getUsername(), getPassword())); - - return httpclient; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractNTLMAuthHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractNTLMAuthHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractNTLMAuthHttpClientFactory.java deleted file mode 100644 index e7c0ca0..0000000 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/AbstractNTLMAuthHttpClientFactory.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.olingo.client.core.http; - -import java.net.URI; - -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.NTCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.olingo.client.api.http.HttpMethod; - -/** - * Base implementation for working with NTLM Authentication via embedded HttpClient features: needs to be subclassed in - * order to provide all needed login information. - * <br/> - * External NTLM engine such as <a href="http://jcifs.samba.org/">JCIFS</a> library developed by the - * <a href="http://www.samba.org/">Samba</a> project as a part of their Windows interoperability suite of programs. - * - * @see NTCredentials - * @see http://hc.apache.org/httpcomponents-client-ga/ntlm.html#Using_Samba_JCIFS_as_an_alternative_NTLM_engine - */ -public abstract class AbstractNTLMAuthHttpClientFactory extends DefaultHttpClientFactory { - - protected abstract String getUsername(); - - protected abstract String getPassword(); - - protected abstract String getWorkstation(); - - protected abstract String getDomain(); - - @Override - public HttpClient createHttpClient(final HttpMethod method, final URI uri) { - final DefaultHttpClient httpclient = (DefaultHttpClient) super.createHttpClient(method, uri); - - final CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials(AuthScope.ANY, - new NTCredentials(getUsername(), getPassword(), getWorkstation(), getDomain())); - - httpclient.setCredentialsProvider(credsProvider); - - return httpclient; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/main/java/org/apache/olingo/client/core/http/BasicAuthHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/BasicAuthHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/BasicAuthHttpClientFactory.java new file mode 100644 index 0000000..b978621 --- /dev/null +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/BasicAuthHttpClientFactory.java @@ -0,0 +1,55 @@ +/* + * 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.olingo.client.core.http; + +import java.net.URI; + +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.olingo.client.api.http.HttpMethod; + +/** + * Implementation for working with Basic Authentication. + */ +public class BasicAuthHttpClientFactory extends DefaultHttpClientFactory { + + private static final long serialVersionUID = 7985626503125490244L; + + private final String username; + + private final String password; + + public BasicAuthHttpClientFactory(final String username, final String password) { + this.username = username; + this.password = password; + } + + @Override + public HttpClient createHttpClient(final HttpMethod method, final URI uri) { + final DefaultHttpClient httpclient = (DefaultHttpClient) super.createHttpClient(method, uri); + + httpclient.getCredentialsProvider().setCredentials( + new AuthScope(uri.getHost(), uri.getPort()), + new UsernamePasswordCredentials(username, password)); + + return httpclient; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/main/java/org/apache/olingo/client/core/http/NTLMAuthHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/NTLMAuthHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/NTLMAuthHttpClientFactory.java new file mode 100644 index 0000000..202fc9a --- /dev/null +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/NTLMAuthHttpClientFactory.java @@ -0,0 +1,73 @@ +/* + * 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.olingo.client.core.http; + +import java.net.URI; + +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.NTCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.olingo.client.api.http.HttpMethod; + +/** + * Implementation for working with NTLM Authentication via embedded HttpClient features. + * <br/> + * External NTLM engine such as <a href="http://jcifs.samba.org/">JCIFS</a> library developed by the + * <a href="http://www.samba.org/">Samba</a> project as a part of their Windows interoperability suite of programs. + * + * @see NTCredentials + * @see http://hc.apache.org/httpcomponents-client-ga/ntlm.html#Using_Samba_JCIFS_as_an_alternative_NTLM_engine + */ +public class NTLMAuthHttpClientFactory extends DefaultHttpClientFactory { + + private static final long serialVersionUID = 9060120943020134668L; + + private final String username; + + private final String password; + + private final String workstation; + + private final String domain; + + public NTLMAuthHttpClientFactory(final String username, final String password, + final String workstation, final String domain) { + + this.username = username; + this.password = password; + this.workstation = workstation; + this.domain = domain; + } + + @Override + public HttpClient createHttpClient(final HttpMethod method, final URI uri) { + final DefaultHttpClient httpclient = (DefaultHttpClient) super.createHttpClient(method, uri); + + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(AuthScope.ANY, + new NTCredentials(username, password, workstation, domain)); + + httpclient.setCredentialsProvider(credsProvider); + + return httpclient; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java new file mode 100644 index 0000000..ab57901 --- /dev/null +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java @@ -0,0 +1,86 @@ +/* + * 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.olingo.client.core.http; + +import java.net.URI; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.HttpClient; +import org.apache.http.conn.params.ConnRoutePNames; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.olingo.client.api.http.HttpClientFactory; +import org.apache.olingo.client.api.http.HttpMethod; + +/** + * Implementation for working behind an HTTP proxy (possibly requiring authentication); requires another concrete + * {@link HttpClientFactory} implementation acting as real HTTP client factory. + */ +public class ProxyWrapperHttpClientFactory implements HttpClientFactory { + + private final URI proxy; + + private String proxyUsername; + + private String proxyPassword; + + private final DefaultHttpClientFactory wrapped; + + public ProxyWrapperHttpClientFactory(final URI proxy) { + this(proxy, null, null, new DefaultHttpClientFactory()); + } + + public ProxyWrapperHttpClientFactory(final URI proxy, final String proxyUsername, final String proxyPassword) { + this(proxy, proxyUsername, proxyPassword, new DefaultHttpClientFactory()); + } + + public ProxyWrapperHttpClientFactory(final URI proxy, final DefaultHttpClientFactory wrapped) { + this(proxy, null, null, wrapped); + } + + public ProxyWrapperHttpClientFactory(final URI proxy, + final String proxyUsername, final String proxyPassword, final DefaultHttpClientFactory wrapped) { + + this.proxy = proxy; + this.proxyUsername = proxyUsername; + this.proxyPassword = proxyPassword; + this.wrapped = wrapped; + } + + @Override + public HttpClient createHttpClient(final HttpMethod method, final URI uri) { + // Use wrapped factory to obtain an httpclient instance for given method and uri + final DefaultHttpClient httpclient = (DefaultHttpClient) wrapped.createHttpClient(method, uri); + + final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort()); + + // Sets usage of HTTP proxy + httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); + + // Sets proxy authentication, if credentials were provided + if (proxyUsername != null && proxyPassword != null) { + httpclient.getCredentialsProvider().setCredentials( + new AuthScope(proxyHost), + new UsernamePasswordCredentials(proxyUsername, proxyPassword)); + } + + return httpclient; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1e8eaecf/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v3/AuthEntityRetrieveTestITCase.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v3/AuthEntityRetrieveTestITCase.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v3/AuthEntityRetrieveTestITCase.java index 9d98270..686e5ca 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v3/AuthEntityRetrieveTestITCase.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v3/AuthEntityRetrieveTestITCase.java @@ -18,7 +18,7 @@ */ package org.apache.olingo.client.core.it.v3; -import org.apache.olingo.client.core.http.AbstractBasicAuthHttpClientFactory; +import org.apache.olingo.client.core.http.BasicAuthHttpClientFactory; import org.apache.olingo.client.core.http.DefaultHttpClientFactory; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -27,19 +27,7 @@ public class AuthEntityRetrieveTestITCase extends EntityRetrieveTestITCase { @BeforeClass public static void enableBasicAuth() { - client.getConfiguration().setHttpClientFactory(new AbstractBasicAuthHttpClientFactory() { - private static final long serialVersionUID = 1L; - - @Override - protected String getUsername() { - return "odatajclient"; - } - - @Override - protected String getPassword() { - return "odatajclient"; - } - }); + client.getConfiguration().setHttpClientFactory(new BasicAuthHttpClientFactory("odatajclient", "odatajclient")); } @AfterClass
