1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 /***
46 * Implementation of the commons-httpclient SSLProtocolSocketFactory so we
47 * can return SSLSockets whose trust manager is
48 * {@link org.archive.httpclient.ConfigurableX509TrustManager}.
49 *
50 * We also go to the heritrix cache to get IPs to use making connection.
51 * To this, we have dependency on {@link HeritrixProtocolSocketFactory};
52 * its assumed this class and it are used together.
53 * See {@link HeritrixProtocolSocketFactory#getHostAddress(ServerCache,String)}.
54 *
55 * @author stack
56 * @version $Id: HeritrixSSLProtocolSocketFactory.java 4553 2006-08-29 22:47:03Z stack-sf $
57 * @see org.archive.httpclient.ConfigurableX509TrustManager
58 */
59 public class HeritrixSSLProtocolSocketFactory
60 implements SecureProtocolSocketFactory {
61 /****
62 * Socket factory with default trust manager installed.
63 */
64 private SSLSocketFactory sslDefaultFactory = null;
65
66 /***
67 * Shutdown constructor.
68 * @throws KeyManagementException
69 * @throws KeyStoreException
70 * @throws NoSuchAlgorithmException
71 */
72 public HeritrixSSLProtocolSocketFactory()
73 throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException{
74
75 SSLContext context = SSLContext.getInstance("SSL");
76
77
78
79
80 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
102
103
104 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
128 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 }