Support for default scheme initialization; no args constructor for pooling connection and single connection managers

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1053367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-12-28 15:18:26 +00:00
parent b9851bc11c
commit 18de7cda9c
7 changed files with 80 additions and 32 deletions

View File

@ -33,9 +33,6 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManager;
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.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
@ -47,12 +44,7 @@ import org.apache.http.util.EntityUtils;
public class ClientEvictExpiredConnections { public class ClientEvictExpiredConnections {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Create and initialize scheme registry ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotal(100); cm.setMaxTotal(100);
HttpClient httpclient = new DefaultHttpClient(cm); HttpClient httpclient = new DefaultHttpClient(cm);

View File

@ -30,9 +30,6 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
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.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.BasicHttpContext;
@ -46,15 +43,10 @@ import org.apache.http.util.EntityUtils;
public class ClientMultiThreadedExecution { public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
// Create an HttpClient with the ThreadSafeClientConnManager. // Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will // This connection manager must be used if more than one thread will
// be using the HttpClient. // be using the HttpClient.
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
cm.setMaxTotal(100); cm.setMaxTotal(100);
HttpClient httpClient = new DefaultHttpClient(cm); HttpClient httpClient = new DefaultHttpClient(cm);

View File

@ -36,7 +36,6 @@ import org.apache.http.conn.scheme.LayeredSocketFactory;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
@ -210,6 +209,14 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock
return sslcontext; return sslcontext;
} }
private static SSLContext createDefaultSSLContext() {
try {
return createSSLContext(TLS, null, null, null, null, null);
} catch (Exception ex) {
throw new IllegalStateException("Failure initializing default SSL context", ex);
}
}
/** /**
* @deprecated Use {@link #SSLSocketFactory(String, KeyStore, String, KeyStore, SecureRandom, X509HostnameVerifier)} * @deprecated Use {@link #SSLSocketFactory(String, KeyStore, String, KeyStore, SecureRandom, X509HostnameVerifier)}
*/ */
@ -328,10 +335,7 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock
} }
private SSLSocketFactory() { private SSLSocketFactory() {
super(); this(createDefaultSSLContext());
this.socketfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
this.hostnameVerifier = null;
this.nameResolver = null;
} }
/** /**

View File

@ -53,10 +53,7 @@ import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionManagerFactory; import org.apache.http.conn.ClientConnectionManagerFactory;
import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.routing.HttpRoutePlanner; import org.apache.http.conn.routing.HttpRoutePlanner;
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.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.CookieSpecRegistry; import org.apache.http.cookie.CookieSpecRegistry;
import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.auth.BasicSchemeFactory; import org.apache.http.impl.auth.BasicSchemeFactory;
@ -64,6 +61,7 @@ import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.NTLMSchemeFactory; import org.apache.http.impl.auth.NTLMSchemeFactory;
import org.apache.http.impl.auth.NegotiateSchemeFactory; import org.apache.http.impl.auth.NegotiateSchemeFactory;
import org.apache.http.impl.conn.DefaultHttpRoutePlanner; import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.impl.conn.SingleClientConnManager; import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.cookie.BestMatchSpecFactory; import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory; import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
@ -194,7 +192,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
/** /**
* Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)} * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)}
* *
* @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it. * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it.
*/ */
@Override @Override
@ -239,11 +237,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
@Override @Override
protected ClientConnectionManager createClientConnectionManager() { protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry(); SchemeRegistry registry = SchemeRegistryFactory.createDefault();
registry.register(
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(
new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
ClientConnectionManager connManager = null; ClientConnectionManager connManager = null;
HttpParams params = getParams(); HttpParams params = getParams();

View File

@ -0,0 +1,51 @@
/*
* ====================================================================
* 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import org.apache.http.annotation.ThreadSafe;
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;
/**
* @since 4.1
*/
@ThreadSafe
public final class SchemeRegistryFactory {
public static SchemeRegistry createDefault() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(
new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
return registry;
}
}

View File

@ -130,6 +130,13 @@ public class SingleClientConnManager implements ClientConnectionManager {
this.isShutDown = false; this.isShutDown = false;
} }
/**
* @since 4.1
*/
public SingleClientConnManager() {
this(SchemeRegistryFactory.createDefault());
}
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {

View File

@ -43,6 +43,7 @@ import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.OperatedClientConnection; import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.impl.conn.DefaultClientConnectionOperator; import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.impl.conn.SchemeRegistryFactory;
/** /**
* Manages a pool of {@link OperatedClientConnection client connections} and * Manages a pool of {@link OperatedClientConnection client connections} and
@ -90,6 +91,13 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
this(schreg, -1, TimeUnit.MILLISECONDS); this(schreg, -1, TimeUnit.MILLISECONDS);
} }
/**
* @since 4.1
*/
public ThreadSafeClientConnManager() {
this(SchemeRegistryFactory.createDefault());
}
/** /**
* Creates a new thread safe connection manager. * Creates a new thread safe connection manager.
* *