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.log4j.Logger;
34
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 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
83
84
85 private static final Logger LOG = Logger.getLogger(EasySSLProtocolSocketFactory.class);
86
87 private SSLContext sslcontext = null;
88
89
90
91
92 public EasySSLProtocolSocketFactory() {
93 super();
94 }
95
96 private static SSLContext createEasySSLContext() {
97 try {
98 SSLContext context = SSLContext.getInstance("SSL");
99 context.init(
100 null,
101 new TrustManager[] {new EasyX509TrustManager(null)},
102 null);
103 return context;
104 } catch (Exception e) {
105 LOG.error(e.getMessage(), e);
106 throw new HttpClientError(e.toString());
107 }
108 }
109
110 private SSLContext getSSLContext() {
111 if (this.sslcontext == null) {
112 this.sslcontext = createEasySSLContext();
113 }
114 return this.sslcontext;
115 }
116
117
118
119
120 public Socket createSocket(
121 String host,
122 int port,
123 InetAddress clientHost,
124 int clientPort)
125 throws IOException, UnknownHostException {
126
127 return getSSLContext().getSocketFactory().createSocket(
128 host,
129 port,
130 clientHost,
131 clientPort
132 );
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public Socket createSocket(
157 final String host,
158 final int port,
159 final InetAddress localAddress,
160 final int localPort,
161 final HttpConnectionParams params
162 ) throws IOException, UnknownHostException, ConnectTimeoutException {
163 if (params == null) {
164 throw new IllegalArgumentException("Parameters may not be null");
165 }
166 int timeout = params.getConnectionTimeout();
167 SocketFactory socketfactory = getSSLContext().getSocketFactory();
168 if (timeout == 0) {
169 return socketfactory.createSocket(host, port, localAddress, localPort);
170 } else {
171 Socket socket = socketfactory.createSocket();
172 SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
173 SocketAddress remoteaddr = new InetSocketAddress(host, port);
174 socket.bind(localaddr);
175 socket.connect(remoteaddr, timeout);
176 return socket;
177 }
178 }
179
180
181
182
183 public Socket createSocket(String host, int port)
184 throws IOException, UnknownHostException {
185 return getSSLContext().getSocketFactory().createSocket(
186 host,
187 port
188 );
189 }
190
191
192
193
194 public Socket createSocket(
195 Socket socket,
196 String host,
197 int port,
198 boolean autoClose)
199 throws IOException, UnknownHostException {
200 return getSSLContext().getSocketFactory().createSocket(
201 socket,
202 host,
203 port,
204 autoClose
205 );
206 }
207
208 public boolean equals(Object obj) {
209 return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
210 }
211
212 public int hashCode() {
213 return EasySSLProtocolSocketFactory.class.hashCode();
214 }
215
216 }