diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java b/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java index ddb876082..5d934b158 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientEvictExpiredConnections.java @@ -33,9 +33,6 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; 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.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.util.EntityUtils; @@ -47,12 +44,7 @@ import org.apache.http.util.EntityUtils; public class ClientEvictExpiredConnections { 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())); - - ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); + ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(); cm.setMaxTotal(100); HttpClient httpclient = new DefaultHttpClient(cm); diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java b/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java index 656d89bd1..52a6dbd80 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java @@ -30,9 +30,6 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; 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.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.protocol.BasicHttpContext; @@ -46,15 +43,10 @@ import org.apache.http.util.EntityUtils; public class ClientMultiThreadedExecution { 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. // This connection manager must be used if more than one thread will // be using the HttpClient. - ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); + ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(); cm.setMaxTotal(100); HttpClient httpClient = new DefaultHttpClient(cm); diff --git a/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java b/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java index 9c8ffe176..b402f3dba 100644 --- a/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java +++ b/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java @@ -36,7 +36,6 @@ import org.apache.http.conn.scheme.LayeredSocketFactory; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; -import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -210,6 +209,14 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock 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)} */ @@ -328,10 +335,7 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock } private SSLSocketFactory() { - super(); - this.socketfactory = HttpsURLConnection.getDefaultSSLSocketFactory(); - this.hostnameVerifier = null; - this.nameResolver = null; + this(createDefaultSSLContext()); } /** diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 719311492..622261179 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -53,10 +53,7 @@ import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManagerFactory; import org.apache.http.conn.ConnectionKeepAliveStrategy; 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.ssl.SSLSocketFactory; import org.apache.http.cookie.CookieSpecRegistry; import org.apache.http.impl.DefaultConnectionReuseStrategy; 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.NegotiateSchemeFactory; 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.cookie.BestMatchSpecFactory; 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)} - * + * * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it. */ @Override @@ -239,11 +237,7 @@ public class DefaultHttpClient extends AbstractHttpClient { @Override protected ClientConnectionManager createClientConnectionManager() { - SchemeRegistry registry = new SchemeRegistry(); - registry.register( - new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); - registry.register( - new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); + SchemeRegistry registry = SchemeRegistryFactory.createDefault(); ClientConnectionManager connManager = null; HttpParams params = getParams(); diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/SchemeRegistryFactory.java b/httpclient/src/main/java/org/apache/http/impl/conn/SchemeRegistryFactory.java new file mode 100644 index 000000000..52ee80468 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/conn/SchemeRegistryFactory.java @@ -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 + * . + * + */ +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; + } + +} + diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java index 5157ec495..0ddbe43fa 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java @@ -130,6 +130,13 @@ public class SingleClientConnManager implements ClientConnectionManager { this.isShutDown = false; } + /** + * @since 4.1 + */ + public SingleClientConnManager() { + this(SchemeRegistryFactory.createDefault()); + } + @Override protected void finalize() throws Throwable { try { diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java index fd36821d2..f8846544b 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java @@ -43,6 +43,7 @@ import org.apache.http.conn.ManagedClientConnection; import org.apache.http.conn.OperatedClientConnection; import org.apache.http.params.HttpParams; import org.apache.http.impl.conn.DefaultClientConnectionOperator; +import org.apache.http.impl.conn.SchemeRegistryFactory; /** * Manages a pool of {@link OperatedClientConnection client connections} and @@ -90,6 +91,13 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager { this(schreg, -1, TimeUnit.MILLISECONDS); } + /** + * @since 4.1 + */ + public ThreadSafeClientConnManager() { + this(SchemeRegistryFactory.createDefault()); + } + /** * Creates a new thread safe connection manager. *