1 23 package org.archive.crawler.fetcher; 24 25 import java.io.IOException ; 26 import java.net.InetAddress ; 27 import java.net.InetSocketAddress ; 28 import java.net.Socket ; 29 import java.net.SocketTimeoutException ; 30 import java.net.UnknownHostException ; 31 import java.security.KeyManagementException ; 32 import java.security.KeyStoreException ; 33 import java.security.NoSuchAlgorithmException ; 34 35 import javax.net.ssl.SSLContext; 36 import javax.net.ssl.SSLSocketFactory; 37 import javax.net.ssl.TrustManager; 38 39 import org.apache.commons.httpclient.params.HttpConnectionParams; 40 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; 41 import org.archive.crawler.datamodel.ServerCache; 42 import org.archive.httpclient.ConfigurableX509TrustManager; 43 44 45 59 public class HeritrixSSLProtocolSocketFactory 60 implements SecureProtocolSocketFactory { 61 64 private SSLSocketFactory sslDefaultFactory = null; 65 66 72 public HeritrixSSLProtocolSocketFactory() 73 throws KeyManagementException , KeyStoreException , NoSuchAlgorithmException { 74 SSLContext context = SSLContext.getInstance("SSL"); 76 77 context.init(null, new TrustManager[] { 81 new ConfigurableX509TrustManager( 82 ConfigurableX509TrustManager.DEFAULT)}, null); 83 this.sslDefaultFactory = context.getSocketFactory(); 84 } 85 86 public Socket createSocket(String host, int port, InetAddress clientHost, 87 int clientPort) 88 throws IOException , UnknownHostException { 89 return this.sslDefaultFactory.createSocket(host, port, 90 clientHost, clientPort); 91 } 92 93 public Socket createSocket(String host, int port) 94 throws IOException , UnknownHostException { 95 return this.sslDefaultFactory.createSocket(host, port); 96 } 97 98 public synchronized Socket createSocket(String host, int port, 99 InetAddress localAddress, int localPort, HttpConnectionParams params) 100 throws IOException , UnknownHostException { 101 if (params == null) { 105 throw new IllegalArgumentException ("Parameters may not be null"); 106 } 107 Socket socket = null; 108 int timeout = params.getConnectionTimeout(); 109 if (timeout == 0) { 110 socket = createSocket(host, port, localAddress, localPort); 111 } else { 112 SSLSocketFactory factory = (SSLSocketFactory)params. 113 getParameter(FetchHTTP.SSL_FACTORY_KEY); 114 SSLSocketFactory f = (factory != null)? factory: this.sslDefaultFactory; 115 socket = f.createSocket(); 116 ServerCache cache = (ServerCache)params. 117 getParameter(FetchHTTP.SERVER_CACHE_KEY); 118 InetAddress hostAddress = (cache != null)? 119 HeritrixProtocolSocketFactory.getHostAddress(cache, host): null; 120 InetSocketAddress address = (hostAddress != null)? 121 new InetSocketAddress (hostAddress, port): 122 new InetSocketAddress (host, port); 123 socket.bind(new InetSocketAddress (localAddress, localPort)); 124 try { 125 socket.connect(address, timeout); 126 } catch (SocketTimeoutException e) { 127 throw new SocketTimeoutException (e.getMessage() + 129 ": timeout set at " + Integer.toString(timeout) + "ms."); 130 } 131 assert socket.isConnected(): "Socket not connected " + host; 132 } 133 return socket; 134 } 135 136 public Socket createSocket(Socket socket, String host, int port, 137 boolean autoClose) 138 throws IOException , UnknownHostException { 139 return this.sslDefaultFactory.createSocket(socket, host, 140 port, autoClose); 141 } 142 143 public boolean equals(Object obj) { 144 return ((obj != null) && obj.getClass(). 145 equals(HeritrixSSLProtocolSocketFactory.class)); 146 } 147 148 public int hashCode() { 149 return HeritrixSSLProtocolSocketFactory.class.hashCode(); 150 } 151 } | Popular Tags |