1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.httpclient.contrib.ssl;
17
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22 import java.net.SocketAddress;
23 import java.net.UnknownHostException;
24
25 import javax.net.SocketFactory;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.TrustManager;
28
29 import org.apache.commons.httpclient.ConnectTimeoutException;
30 import org.apache.commons.httpclient.HttpClientError;
31 import org.apache.commons.httpclient.params.HttpConnectionParams;
32 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
84
85
86 private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class);
87
88 private SSLContext sslcontext = null;
89
90
91
92
93 public EasySSLProtocolSocketFactory() {
94 super();
95 }
96
97 private static SSLContext createEasySSLContext() {
98 try {
99 SSLContext context = SSLContext.getInstance("SSL");
100 context.init(
101 null,
102 new TrustManager[] {new EasyX509TrustManager(null)},
103 null);
104 return context;
105 } catch (Exception e) {
106 LOG.error(e.getMessage(), e);
107 throw new HttpClientError(e.toString());
108 }
109 }
110
111 private SSLContext getSSLContext() {
112 if (this.sslcontext == null) {
113 this.sslcontext = createEasySSLContext();
114 }
115 return this.sslcontext;
116 }
117
118
119
120
121 public Socket createSocket(
122 String host,
123 int port,
124 InetAddress clientHost,
125 int clientPort)
126 throws IOException, UnknownHostException {
127
128 return getSSLContext().getSocketFactory().createSocket(
129 host,
130 port,
131 clientHost,
132 clientPort
133 );
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public Socket createSocket(
158 final String host,
159 final int port,
160 final InetAddress localAddress,
161 final int localPort,
162 final HttpConnectionParams params
163 ) throws IOException, UnknownHostException, ConnectTimeoutException {
164 if (params == null) {
165 throw new IllegalArgumentException("Parameters may not be null");
166 }
167 int timeout = params.getConnectionTimeout();
168 SocketFactory socketfactory = getSSLContext().getSocketFactory();
169 if (timeout == 0) {
170 return socketfactory.createSocket(host, port, localAddress, localPort);
171 } else {
172 Socket socket = socketfactory.createSocket();
173 SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
174 SocketAddress remoteaddr = new InetSocketAddress(host, port);
175 socket.bind(localaddr);
176 socket.connect(remoteaddr, timeout);
177 return socket;
178 }
179 }
180
181
182
183
184 public Socket createSocket(String host, int port)
185 throws IOException, UnknownHostException {
186 return getSSLContext().getSocketFactory().createSocket(
187 host,
188 port
189 );
190 }
191
192
193
194
195 public Socket createSocket(
196 Socket socket,
197 String host,
198 int port,
199 boolean autoClose)
200 throws IOException, UnknownHostException {
201 return getSSLContext().getSocketFactory().createSocket(
202 socket,
203 host,
204 port,
205 autoClose
206 );
207 }
208
209 public boolean equals(Object obj) {
210 return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
211 }
212
213 public int hashCode() {
214 return EasySSLProtocolSocketFactory.class.hashCode();
215 }
216
217 }