diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java index 006a82058..08b7d64dc 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteSOCKS.java @@ -89,8 +89,16 @@ public class ClientExecuteSOCKS { static class MySchemeSocketFactory implements SchemeSocketFactory { - public Socket createSocket() throws IOException { - return new Socket(); + public Socket createSocket(final HttpParams params) throws IOException { + if (params == null) { + throw new IllegalArgumentException("HTTP parameters may not be null"); + } + String proxyHost = (String) params.getParameter("socks.host"); + Integer proxyPort = (Integer) params.getParameter("socks.port"); + + InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort); + Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); + return new Socket(proxy); } public Socket connectSocket( @@ -105,12 +113,12 @@ public class ClientExecuteSOCKS { if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } - String proxyHost = (String) params.getParameter("socks.host"); - Integer proxyPort = (Integer) params.getParameter("socks.port"); - - InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort); - Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); - Socket sock = new Socket(proxy); + Socket sock; + if (socket != null) { + sock = socket; + } else { + sock = createSocket(params); + } if (localAddress != null) { sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); sock.bind(localAddress); diff --git a/httpclient/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java b/httpclient/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java index 2001fda50..1845d98ad 100644 --- a/httpclient/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java +++ b/httpclient/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java @@ -84,6 +84,13 @@ public final class PlainSocketFactory implements SocketFactory, SchemeSocketFact this.nameResolver = null; } + /** + * @since 4.1 + */ + public Socket createSocket(final HttpParams params) { + return new Socket(); + } + public Socket createSocket() { return new Socket(); } diff --git a/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactory.java b/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactory.java index cc30e6571..666fdfeed 100644 --- a/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactory.java +++ b/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactory.java @@ -47,11 +47,12 @@ public interface SchemeSocketFactory { * Creates a new, unconnected socket. The socket should subsequently be passed to * {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}. * + * @param params additional {@link HttpParams parameters} * @return a new socket * * @throws IOException if an I/O error occurs while creating the socket */ - Socket createSocket() throws IOException; + Socket createSocket(HttpParams params) throws IOException; /** * Connects a socket to the target host with the given remote address. diff --git a/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactoryAdaptor.java b/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactoryAdaptor.java index 8b02e4e0c..5fc3808e2 100644 --- a/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactoryAdaptor.java +++ b/httpclient/src/main/java/org/apache/http/conn/scheme/SchemeSocketFactoryAdaptor.java @@ -62,7 +62,7 @@ class SchemeSocketFactoryAdaptor implements SchemeSocketFactory { return this.factory.connectSocket(sock, host, port, local, localPort, params); } - public Socket createSocket() throws IOException { + public Socket createSocket(final HttpParams params) throws IOException { return this.factory.createSocket(); } diff --git a/httpclient/src/main/java/org/apache/http/conn/scheme/SocketFactoryAdaptor.java b/httpclient/src/main/java/org/apache/http/conn/scheme/SocketFactoryAdaptor.java index 1d922214e..c955caf17 100644 --- a/httpclient/src/main/java/org/apache/http/conn/scheme/SocketFactoryAdaptor.java +++ b/httpclient/src/main/java/org/apache/http/conn/scheme/SocketFactoryAdaptor.java @@ -34,6 +34,7 @@ import java.net.Socket; import java.net.UnknownHostException; import org.apache.http.conn.ConnectTimeoutException; +import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; @Deprecated @@ -47,7 +48,8 @@ class SocketFactoryAdaptor implements SocketFactory { } public Socket createSocket() throws IOException { - return this.factory.createSocket(); + HttpParams params = new BasicHttpParams(); + return this.factory.createSocket(params); } public Socket connectSocket( 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 686e8ff37..e1eb3beab 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 @@ -334,6 +334,14 @@ public class SSLSocketFactory implements LayeredSchemeSocketFactory, LayeredSock this.nameResolver = null; } + /** + * @since 4.1 + */ + public Socket createSocket(final HttpParams params) throws IOException { + // the cast makes sure that the factory is working as expected + return (SSLSocket) this.socketfactory.createSocket(); + } + @SuppressWarnings("cast") public Socket createSocket() throws IOException { // the cast makes sure that the factory is working as expected diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java index 01c7e4a3d..7aa56afe1 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java @@ -133,7 +133,7 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator InetAddress address = addresses[i]; boolean last = i == addresses.length - 1; - Socket sock = sf.createSocket(); + Socket sock = sf.createSocket(params); conn.opening(sock, target); InetSocketAddress remoteAddress = new InetSocketAddress(address, port); @@ -164,7 +164,7 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator } if (this.log.isDebugEnabled()) { this.log.debug("Connect to " + remoteAddress + " timed out. " + - "Connection will be retried using another IP address"); + "Connection will be retried using another IP address"); } } } diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMWithServer.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMWithServer.java index 2c1396ae6..167b0e0cb 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMWithServer.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestTSCCMWithServer.java @@ -812,11 +812,11 @@ public class TestTSCCMWithServer extends ServerTestBase { return socket; } - public Socket createSocket() throws IOException { + public Socket createSocket(final HttpParams params) throws IOException { if(waitPolicy == WaitPolicy.BEFORE_CREATE) latch(); - return delegate.createSocket(); + return delegate.createSocket(params); } public boolean isSecure(Socket sock) throws IllegalArgumentException { diff --git a/httpclient/src/test/java/org/apache/http/mockup/SocketFactoryMockup.java b/httpclient/src/test/java/org/apache/http/mockup/SocketFactoryMockup.java index 30aa80753..5c556034f 100644 --- a/httpclient/src/test/java/org/apache/http/mockup/SocketFactoryMockup.java +++ b/httpclient/src/test/java/org/apache/http/mockup/SocketFactoryMockup.java @@ -58,7 +58,7 @@ public class SocketFactoryMockup implements SchemeSocketFactory { return "SocketFactoryMockup." + mockup_name; } - public Socket createSocket() { + public Socket createSocket(final HttpParams params) { throw new UnsupportedOperationException("I'm a mockup!"); }