From c526245f6de45f24c78745ef4c33f4e336ea3606 Mon Sep 17 00:00:00 2001 From: rfeng Date: Sun, 30 Sep 2012 15:12:41 +0000 Subject: Enhance the http client configuration to make it customizable git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1392033 13f79535-47bb-0310-9956-ffa450edef68 --- .../trunk/modules/host-http/META-INF/MANIFEST.MF | 1 + .../sca/host/http/client/HttpClientFactory.java | 80 +++++++++++++++++++--- 2 files changed, 71 insertions(+), 10 deletions(-) (limited to 'sca-java-2.x/trunk/modules/host-http') diff --git a/sca-java-2.x/trunk/modules/host-http/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/host-http/META-INF/MANIFEST.MF index 2621509ce4..0e04353e8a 100644 --- a/sca-java-2.x/trunk/modules/host-http/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/host-http/META-INF/MANIFEST.MF @@ -22,6 +22,7 @@ Import-Package: javax.servlet, org.apache.http.conn.params;resolution:=optional, org.apache.http.conn.scheme;resolution:=optional, org.apache.http.conn.ssl;resolution:=optional, + org.apache.http.impl;resolution:=optional, org.apache.http.impl.client;resolution:=optional, org.apache.http.impl.conn;resolution:=optional, org.apache.http.impl.conn.tsccm;resolution:=optional, diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java index 339d2f433e..53b24a2f11 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java +++ b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java @@ -19,10 +19,17 @@ package org.apache.tuscany.sca.host.http.client; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.http.ConnectionReuseStrategy; +import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; +import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLInitializationException; import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.impl.NoConnectionReuseStrategy; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.impl.conn.SchemeRegistryFactory; @@ -30,6 +37,7 @@ import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; +import org.apache.http.protocol.HttpContext; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.core.UtilityExtensionPoint; @@ -38,6 +46,13 @@ import org.apache.tuscany.sca.core.UtilityExtensionPoint; * @version $Rev$ $Date$ */ public class HttpClientFactory implements LifeCycleListener { + private int soTimeout = 30000; + private int connectionTimeout = 60000; + private boolean staleCheckingEnabled = false; + private long timeToLive = 60; // seconds + private int maxPerRoute = 256; + private int maxTotal = 1024; + private boolean sslHostVerificationEnabled = false; private HttpClient httpClient; @@ -46,12 +61,50 @@ public class HttpClientFactory implements LifeCycleListener { return utilities.getUtility(HttpClientFactory.class); } + public HttpClientFactory() { + + } + + public HttpClientFactory(ExtensionPointRegistry registry, Map attributes) { + if (attributes != null) { + String val = attributes.get("soTimeout"); + if (val != null) { + this.soTimeout = Integer.parseInt(val); + } + val = attributes.get("connectionTimeout"); + if (val != null) { + this.connectionTimeout = Integer.parseInt(val); + } + val = attributes.get("staleCheckingEnabled"); + if (val != null) { + this.staleCheckingEnabled = Boolean.parseBoolean(val); + } + val = attributes.get("timeToLive"); + if (val != null) { + this.timeToLive = Long.parseLong(val); + } + val = attributes.get("sslHostVerificationEnabled"); + if (val != null) { + this.sslHostVerificationEnabled = Boolean.parseBoolean(val); + } + val = attributes.get("maxTotal"); + if (val != null) { + this.maxTotal = Integer.parseInt(val); + } + val = attributes.get("maxPerRoute"); + if (val != null) { + this.maxPerRoute = Integer.parseInt(val); + } + } + } + public HttpClient createHttpClient() { HttpParams defaultParameters = new BasicHttpParams(); HttpProtocolParams.setContentCharset(defaultParameters, "UTF-8"); - HttpConnectionParams.setConnectionTimeout(defaultParameters, 60000); - HttpConnectionParams.setSoTimeout(defaultParameters, 60000); + HttpConnectionParams.setConnectionTimeout(defaultParameters, connectionTimeout); + HttpConnectionParams.setSoTimeout(defaultParameters, soTimeout); + HttpConnectionParams.setStaleCheckingEnabled(defaultParameters, staleCheckingEnabled); // See https://issues.apache.org/jira/browse/HTTPCLIENT-1138 SchemeRegistry supportedSchemes = null; @@ -62,17 +115,24 @@ public class HttpClientFactory implements LifeCycleListener { supportedSchemes = SchemeRegistryFactory.createDefault(); } - // FIXME: By pass host name verification - SSLSocketFactory socketFactory = (SSLSocketFactory)supportedSchemes.getScheme("https").getSchemeSocketFactory(); - socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + if (!sslHostVerificationEnabled) { + // FIXME: By pass host name verification + SSLSocketFactory socketFactory = + (SSLSocketFactory)supportedSchemes.getScheme("https").getSchemeSocketFactory(); + socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + } PoolingClientConnectionManager connectionManager = - new PoolingClientConnectionManager(supportedSchemes); + new PoolingClientConnectionManager(supportedSchemes, timeToLive, TimeUnit.SECONDS); - connectionManager.setDefaultMaxPerRoute(256); - connectionManager.setMaxTotal(1024); - - return new DefaultHttpClient(connectionManager, defaultParameters); + connectionManager.setDefaultMaxPerRoute(maxPerRoute); + connectionManager.setMaxTotal(maxTotal); + + DefaultHttpClient client = new DefaultHttpClient(connectionManager, defaultParameters); + if (timeToLive <= 0) { + client.setReuseStrategy(new NoConnectionReuseStrategy()); + } + return client; } @Override -- cgit v1.2.3