1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.ssl;
31
32 import java.io.IOException;
33 import java.net.InetAddress;
34 import java.net.Socket;
35 import java.net.URL;
36 import java.net.UnknownHostException;
37 import java.security.KeyStore;
38
39 import org.apache.commons.httpclient.ConnectTimeoutException;
40 import org.apache.commons.httpclient.params.HttpConnectionParams;
41 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
42 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
43 import org.apache.commons.httpclient.server.SimpleSocketFactory;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 import com.sun.net.ssl.SSLContext;
48 import com.sun.net.ssl.TrustManager;
49 import com.sun.net.ssl.TrustManagerFactory;
50
51 public class SimpleSSLTestProtocolSocketFactory implements SecureProtocolSocketFactory {
52
53 private static final Log LOG = LogFactory.getLog(SimpleSSLTestProtocolSocketFactory.class);
54
55 private static SSLContext SSLCONTEXT = null;
56
57 private static SSLContext createSSLContext() {
58 try {
59 ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
60 URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
61 KeyStore keystore = KeyStore.getInstance("jks");
62 keystore.load(url.openStream(), "nopassword".toCharArray());
63 TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
64 TrustManagerFactory.getDefaultAlgorithm());
65 tmfactory.init(keystore);
66 TrustManager[] trustmanagers = tmfactory.getTrustManagers();
67 SSLContext sslcontext = SSLContext.getInstance("TLS");
68 sslcontext.init(null, trustmanagers, null);
69 return sslcontext;
70 } catch (Exception ex) {
71
72
73 LOG.error(ex.getMessage(), ex);
74 throw new IllegalStateException(ex.getMessage());
75 }
76
77 }
78
79 private static SSLContext getSSLContext() {
80 if (SSLCONTEXT == null) {
81 SSLCONTEXT = createSSLContext();
82 }
83 return SSLCONTEXT;
84 }
85
86 public SimpleSSLTestProtocolSocketFactory() {
87 super();
88 }
89
90 public Socket createSocket(
91 final String host,
92 final int port,
93 final InetAddress localAddress,
94 final int localPort,
95 final HttpConnectionParams params
96 ) throws IOException, UnknownHostException, ConnectTimeoutException {
97 if (params == null) {
98 throw new IllegalArgumentException("Parameters may not be null");
99 }
100 int timeout = params.getConnectionTimeout();
101 if (timeout == 0) {
102 return createSocket(host, port, localAddress, localPort);
103 } else {
104
105 return ControllerThreadSocketFactory.createSocket(
106 this, host, port, localAddress, localPort, timeout);
107 }
108 }
109
110 public Socket createSocket(
111 String host,
112 int port,
113 InetAddress clientHost,
114 int clientPort)
115 throws IOException, UnknownHostException
116 {
117 return getSSLContext().getSocketFactory().createSocket(
118 host,
119 port,
120 clientHost,
121 clientPort
122 );
123 }
124
125 public Socket createSocket(String host, int port)
126 throws IOException, UnknownHostException
127 {
128 return getSSLContext().getSocketFactory().createSocket(
129 host,
130 port
131 );
132 }
133
134 public Socket createSocket(
135 Socket socket,
136 String host,
137 int port,
138 boolean autoClose)
139 throws IOException, UnknownHostException
140 {
141 return getSSLContext().getSocketFactory().createSocket(
142 socket,
143 host,
144 port,
145 autoClose
146 );
147 }
148 }