Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(openid4java) : migrate apache http client version to 4.5.1 #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

3 changes: 1 addition & 2 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ For general information about the package please see the README document.

Requirements:

- Java 1.5 JVM (or higher)

- Java version between 1.5 and 1.7 (1.8 is not supported)
- Ant (optional: needed for building the package and running the tests)
- Maven (optional: alternative to Ant)
- JUnit (optional: needed for running the tests)
Expand Down
Binary file removed lib/httpclient-4.2.2.jar
Binary file not shown.
Binary file added lib/httpclient-4.5.1.jar
Binary file not shown.
Binary file removed lib/httpcore-4.2.2.jar
Binary file not shown.
Binary file added lib/httpcore-4.4.9.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.openid4java</groupId>
<artifactId>openid4java</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>OpenID4Java</name>
<description>
Expand Down Expand Up @@ -478,7 +478,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down
131 changes: 45 additions & 86 deletions src/org/openid4java/util/HttpClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.AllClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import javax.net.ssl.SSLContext;

Expand All @@ -33,9 +26,9 @@
*
* @author Kevin
*/
public class HttpClientFactory
{
private HttpClientFactory() {}
public class HttpClientFactory {
private HttpClientFactory() {
}

/**
* proxy properties for HTTPClient calls
Expand All @@ -44,13 +37,11 @@ private HttpClientFactory() {}

private static boolean multiThreadedHttpClient = true;

public static ProxyProperties getProxyProperties()
{
public static ProxyProperties getProxyProperties() {
return proxyProperties;
}

public static void setProxyProperties(ProxyProperties proxyProperties)
{
public static void setProxyProperties(ProxyProperties proxyProperties) {
HttpClientFactory.proxyProperties = proxyProperties;
}

Expand All @@ -63,90 +54,58 @@ public static boolean isMultiThreadedHttpClient() {
*
* @param multiThreadedHttpClient if true, MultiThreadedHttpConnectionManager's are constructed;
* if false - SimpleHttpConnectionManager's.
*
*/
public static void setMultiThreadedHttpClient(boolean multiThreadedHttpClient) {
HttpClientFactory.multiThreadedHttpClient = multiThreadedHttpClient;
}

public static HttpClient getInstance(int maxRedirects,
Boolean allowCircularRedirects,
int connTimeout, int socketTimeout,
String cookiePolicy)
{
return getInstance(maxRedirects, allowCircularRedirects, connTimeout, socketTimeout, cookiePolicy, null, null);
Boolean allowCircularRedirects,
int connTimeout, int socketTimeout,
String cookiePolicy) {
return getInstance(maxRedirects, allowCircularRedirects, connTimeout, socketTimeout, cookiePolicy, null, null);
}

public static HttpClient getInstance(int maxRedirects,
Boolean allowCircularRedirects,
int connTimeout, int socketTimeout,
String cookiePolicy, SSLContext sslContext,
X509HostnameVerifier hostnameVerifier)
{
HttpParams httpParams = new BasicHttpParams();
X509HostnameVerifier hostnameVerifier) {

SchemeRegistry registry = new SchemeRegistry();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
SSLSocketFactory sslSocketFactory;
if (null == sslContext)
{
sslSocketFactory = SSLSocketFactory.getSocketFactory();
}
else
{
sslSocketFactory = new SSLSocketFactory(sslContext);
}
if (null != hostnameVerifier)
{
sslSocketFactory.setHostnameVerifier(hostnameVerifier);
}
registry.register(new Scheme("https", 443, sslSocketFactory));

ClientConnectionManager connManager;
if (multiThreadedHttpClient)
connManager = new ThreadSafeClientConnManager(registry);
else
connManager = new SingleClientConnManager(registry);

DefaultHttpClient client = new DefaultHttpClient(connManager, httpParams);

client.getParams().setParameter(AllClientPNames.MAX_REDIRECTS,
new Integer(maxRedirects));
client.getParams().setParameter(AllClientPNames.ALLOW_CIRCULAR_REDIRECTS,
allowCircularRedirects);
client.getParams().setParameter(AllClientPNames.SO_TIMEOUT,
new Integer(socketTimeout));
client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT,
new Integer(connTimeout));

if (cookiePolicy == null)
{
client.setCookieStore(null);
}
else
{
client.getParams().setParameter(AllClientPNames.COOKIE_POLICY,
cookiePolicy);
}

requestConfigBuilder.setCircularRedirectsAllowed(allowCircularRedirects)
.setMaxRedirects(maxRedirects)
.setConnectTimeout(connTimeout)
.setSocketTimeout(socketTimeout);
if (cookiePolicy != null)
requestConfigBuilder.setCookieSpec(cookiePolicy);

if (sslContext != null)
httpClientBuilder.setSSLContext(sslContext);

if (proxyProperties != null)
{
HttpHost proxy = new HttpHost(
proxyProperties.getProxyHostName(),
proxyProperties.getProxyPort());
if(hostnameVerifier != null)
httpClientBuilder.setHostnameVerifier(hostnameVerifier);

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (proxyProperties != null) {

//now set headers for auth
AuthScope authScope = new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
Credentials credentials = proxyProperties.getCredentials();
client.getCredentialsProvider().setCredentials(authScope, credentials);
requestConfigBuilder.setProxy(new HttpHost(
proxyProperties.getProxyHostName(),
proxyProperties.getProxyPort()));

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME),
new UsernamePasswordCredentials(proxyProperties.getUserName(), proxyProperties.getPassword()));

httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}

return client;
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

return httpClientBuilder.build();
}
}