1 /*
2 * ====================================================================
3 *
4 * Copyright 2002-2004 The Apache Software Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 *
19 * This software consists of voluntary contributions made by many
20 * individuals on behalf of the Apache Software Foundation. For more
21 * information on the Apache Software Foundation, please see
22 * <http://www.apache.org/>.
23 *
24 * [Additional notices, if required by prior licensing conditions]
25 *
26 */
27
28 //package org.apache.commons.httpclient.contrib.ssl;
29
30 package autohit.common;
31
32 import java.io.IOException;
33 import java.net.InetAddress;
34 import java.net.Socket;
35 import java.net.UnknownHostException;
36 import javax.net.ssl.SSLSocketFactory;
37
38 import com.sun.net.ssl.SSLContext;
39 import com.sun.net.ssl.TrustManager;
40
41 //import org.apache.commons.httpclient.ConnectTimeoutException;
42 //import org.apache.commons.httpclient.HttpClientError;
43 //import org.apache.commons.httpclient.params.HttpConnectionParams;
44 //import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
45 //import org.apache.commons.httpclient.protocol.ReflectionSocketFactory;
46 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 /**
51 * <p>
52 * EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s
53 * that accept self-signed certificates.
54 * </p>
55 * <p>
56 * This socket factory SHOULD NOT be used for productive systems
57 * due to security reasons, unless it is a concious decision and
58 * you are perfectly aware of security implications of accepting
59 * self-signed certificates
60 * </p>
61 *
62 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
63 *
64 * DISCLAIMER: HttpClient developers DO NOT actively support this component.
65 * The component is provided as a reference material, which may be inappropriate
66 * to be used without additional customization.
67 */
68
69 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
70
71 /** Log object for this class. */
72 private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class);
73
74 private static SSLContext SSL_CONTEXT_SINGLETON = null;
75 /**
76 * Constructor for EasySSLProtocolSocketFactory.
77 *
78 * Code sample:
79 *
80 * <blockquote>
81 * Protocol easyhttps = new Protocol(
82 * "https", new EasySSLProtocolSocketFactory(), 443);
83 *
84 * HttpClient client = new HttpClient();
85 * client.getHostConfiguration().setHost("localhost", 443, easyhttps);
86 * </blockquote>
87 */
88 public EasySSLProtocolSocketFactory() {
89 super();
90 }
91
92 private static SSLContext createEasySSLContext() {
93 try {
94 SSLContext context = SSLContext.getInstance("SSL");
95 context.init(
96 null,
97 new TrustManager[] {new EasyX509TrustManager(null)},
98 null);
99 return context;
100 } catch (Exception e) {
101 LOG.error(e.getMessage(), e);
102 //throw new Exception(e.toString());
103 }
104 return null;
105 }
106
107 private static SSLSocketFactory getEasySSLSocketFactory() {
108 if (SSL_CONTEXT_SINGLETON == null) {
109 SSL_CONTEXT_SINGLETON = createEasySSLContext();
110 }
111 return SSL_CONTEXT_SINGLETON.getSocketFactory();
112 }
113
114 /**
115 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
116 */
117 public Socket createSocket(
118 String host,
119 int port,
120 InetAddress clientHost,
121 int clientPort)
122 throws IOException, UnknownHostException {
123
124 Socket socket = getEasySSLSocketFactory().createSocket(
125 host,
126 port,
127 clientHost,
128 clientPort
129 );
130 return socket;
131 }
132
133 /**
134 * Attempts to get a new socket connection to the given host within the given time limit.
135 * <p>
136 * This method employs several techniques to circumvent the limitations of older JREs that
137 * do not support connect timeout. When running in JRE 1.4 or above reflection is used to
138 * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older
139 * JREs a controller thread is executed. The controller thread attempts to create a new socket
140 * within the given limit of time. If socket constructor does not return until the timeout
141 * expires, the controller terminates and throws an {@link ConnectTimeoutException}
142 * </p>
143 *
144 * @param host the host name/IP
145 * @param port the port on the host
146 * @param clientHost the local host name/IP to bind the socket to
147 * @param clientPort the port on the local machine
148 * @param params {@link HttpConnectionParams Http connection parameters}
149 *
150 * @return Socket a new socket
151 *
152 * @throws IOException if an I/O error occurs while creating the socket
153 * @throws UnknownHostException if the IP address of the host cannot be
154 * determined
155 *//*
156 public Socket createSocket(
157 final String host,
158 final int port,
159 final InetAddress localAddress,
160 final int localPort
161 ) throws IOException, UnknownHostException {
162 if (params == null) {
163 throw new IllegalArgumentException("Parameters may not be null");
164 }
165 int timeout = params.getConnectionTimeout();
166 if (timeout == 0) {
167 return createSocket(host, port, localAddress, localPort);
168 } else {
169 // To be eventually deprecated when migrated to Java 1.4 or above
170 Socket socket = ReflectionSocketFactory.createSocket(
171 "javax.net.ssl.SSLSocketFactory", host, port, localAddress, localPort, timeout);
172 if (socket == null) {
173 socket = ControllerThreadSocketFactory.createSocket(
174 this, host, port, localAddress, localPort, timeout);
175 }
176 return socket;
177 }
178 }*/
179
180 /**
181 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
182 */
183 public Socket createSocket(String host, int port)
184 throws IOException, UnknownHostException {
185 return getEasySSLSocketFactory().createSocket(
186 host,
187 port
188 );
189 }
190
191 /**
192 * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
193 */
194 public Socket createSocket(
195 Socket socket,
196 String host,
197 int port,
198 boolean autoClose)
199 throws IOException, UnknownHostException {
200 return getEasySSLSocketFactory().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 }
|