1 29 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 39 import org.apache.commons.httpclient.ConnectTimeoutException; 40 import org.apache.commons.httpclient.HttpClientError; 41 import org.apache.commons.httpclient.params.HttpConnectionParams; 42 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 import javax.net.SocketFactory; 47 import javax.net.ssl.SSLContext; 48 import javax.net.ssl.TrustManager; 49 50 94 95 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory { 96 97 98 private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class); 99 100 private SSLContext sslcontext = null; 101 102 105 public EasySSLProtocolSocketFactory() { 106 super(); 107 } 108 109 private static SSLContext createEasySSLContext() { 110 try { 111 SSLContext context = SSLContext.getInstance("SSL"); 112 context.init( 113 null, 114 new TrustManager[] {new EasyX509TrustManager(null)}, 115 null); 116 return context; 117 } catch (Exception e) { 118 LOG.error(e.getMessage(), e); 119 throw new HttpClientError(e.toString()); 120 } 121 } 122 123 private SSLContext getSSLContext() { 124 if (this.sslcontext == null) { 125 this.sslcontext = createEasySSLContext(); 126 } 127 return this.sslcontext; 128 } 129 130 133 public Socket createSocket( 134 String host, 135 int port, 136 InetAddress clientHost, 137 int clientPort) 138 throws IOException , UnknownHostException { 139 140 return getSSLContext().getSocketFactory().createSocket( 141 host, 142 port, 143 clientHost, 144 clientPort 145 ); 146 } 147 148 169 public Socket createSocket( 170 final String host, 171 final int port, 172 final InetAddress localAddress, 173 final int localPort, 174 final HttpConnectionParams params 175 ) throws IOException , UnknownHostException , ConnectTimeoutException { 176 if (params == null) { 177 throw new IllegalArgumentException ("Parameters may not be null"); 178 } 179 int timeout = params.getConnectionTimeout(); 180 SocketFactory socketfactory = getSSLContext().getSocketFactory(); 181 if (timeout == 0) { 182 return socketfactory.createSocket(host, port, localAddress, localPort); 183 } else { 184 Socket socket = socketfactory.createSocket(); 185 SocketAddress localaddr = new InetSocketAddress (localAddress, localPort); 186 SocketAddress remoteaddr = new InetSocketAddress (host, port); 187 socket.bind(localaddr); 188 socket.connect(remoteaddr, timeout); 189 return socket; 190 } 191 } 192 193 196 public Socket createSocket(String host, int port) 197 throws IOException , UnknownHostException { 198 return getSSLContext().getSocketFactory().createSocket( 199 host, 200 port 201 ); 202 } 203 204 207 public Socket createSocket( 208 Socket socket, 209 String host, 210 int port, 211 boolean autoClose) 212 throws IOException , UnknownHostException { 213 return getSSLContext().getSocketFactory().createSocket( 214 socket, 215 host, 216 port, 217 autoClose 218 ); 219 } 220 221 public boolean equals(Object obj) { 222 return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class)); 223 } 224 225 public int hashCode() { 226 return EasySSLProtocolSocketFactory.class.hashCode(); 227 } 228 229 } 230 | Popular Tags |