1 30 package org.apache.commons.httpclient.contrib.ssl; 31 32 import java.io.IOException ; 33 import java.net.InetAddress ; 34 import java.net.InetSocketAddress ; 35 import java.net.Socket ; 36 import java.net.SocketAddress ; 37 import java.net.UnknownHostException ; 38 import javax.net.ssl.SSLSocketFactory; 39 import org.apache.commons.httpclient.ConnectTimeoutException; 40 import org.apache.commons.httpclient.params.HttpConnectionParams; 41 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; 42 43 57 public class SocketFactoryWrapper implements SecureProtocolSocketFactory { 58 59 private SSLSocketFactory socketFactory; 60 61 public SocketFactoryWrapper(SSLSocketFactory socketFactory) { 62 this.socketFactory = socketFactory; 63 } 64 65 public Socket createSocket(String host, int port) throws IOException , UnknownHostException { 66 return socketFactory.createSocket(host, port); 67 } 68 69 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) 70 throws IOException , UnknownHostException { 71 return socketFactory.createSocket(host, port, localAddress, localPort); 72 } 73 74 public Socket createSocket( 75 String host, 76 int port, InetAddress localAddress, int localPort, 77 HttpConnectionParams params) throws IOException , UnknownHostException , 78 ConnectTimeoutException { 79 Socket rval; 81 if (params == null) { 82 throw new IllegalArgumentException ("Parameters may not be null"); 83 } 84 int timeout = params.getConnectionTimeout(); 85 if (timeout == 0) { 86 rval = socketFactory.createSocket(host, port, localAddress, localPort); 87 } else { 88 rval = socketFactory.createSocket(); 89 SocketAddress localaddr = new InetSocketAddress (localAddress, localPort); 90 SocketAddress remoteaddr = new InetSocketAddress (host, port); 91 rval.bind(localaddr); 92 rval.connect(remoteaddr, timeout); 93 } 94 return rval; 95 } 96 97 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) 98 throws IOException , UnknownHostException { 99 return socketFactory.createSocket(socket, host, port, autoClose); 100 } 101 102 } 103 | Popular Tags |