[OLINGO-233] HTTP proxy enabled HttpClientFactory provided
This commit is contained in:
parent
16d3b02886
commit
1e8eaecf06
|
@ -27,16 +27,20 @@ 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.
|
||||
* Implementation for working with Basic Authentication.
|
||||
*/
|
||||
public abstract class AbstractBasicAuthHttpClientFactory extends DefaultHttpClientFactory {
|
||||
public class BasicAuthHttpClientFactory extends DefaultHttpClientFactory {
|
||||
|
||||
private static final long serialVersionUID = 7985626503125490244L;
|
||||
|
||||
protected abstract String getUsername();
|
||||
private final String username;
|
||||
|
||||
protected abstract String getPassword();
|
||||
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) {
|
||||
|
@ -44,7 +48,7 @@ public abstract class AbstractBasicAuthHttpClientFactory extends DefaultHttpClie
|
|||
|
||||
httpclient.getCredentialsProvider().setCredentials(
|
||||
new AuthScope(uri.getHost(), uri.getPort()),
|
||||
new UsernamePasswordCredentials(getUsername(), getPassword()));
|
||||
new UsernamePasswordCredentials(username, password));
|
||||
|
||||
return httpclient;
|
||||
}
|
|
@ -29,8 +29,7 @@ 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.
|
||||
* 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.
|
||||
|
@ -38,15 +37,26 @@ import org.apache.olingo.client.api.http.HttpMethod;
|
|||
* @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 {
|
||||
public class NTLMAuthHttpClientFactory extends DefaultHttpClientFactory {
|
||||
|
||||
protected abstract String getUsername();
|
||||
private static final long serialVersionUID = 9060120943020134668L;
|
||||
|
||||
protected abstract String getPassword();
|
||||
private final String username;
|
||||
|
||||
protected abstract String getWorkstation();
|
||||
private final String password;
|
||||
|
||||
protected abstract String getDomain();
|
||||
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) {
|
||||
|
@ -54,7 +64,7 @@ public abstract class AbstractNTLMAuthHttpClientFactory extends DefaultHttpClien
|
|||
|
||||
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(AuthScope.ANY,
|
||||
new NTCredentials(getUsername(), getPassword(), getWorkstation(), getDomain()));
|
||||
new NTCredentials(username, password, workstation, domain));
|
||||
|
||||
httpclient.setCredentialsProvider(credsProvider);
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue