KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > ssl > EasySSLProtocolSocketFactory


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java,v 1.7 2004/06/11 19:26:27 olegk Exp $
3  * $Revision$
4  * $Date$
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  */

29
30 package org.apache.commons.httpclient.contrib.ssl;
31
32 import java.io.IOException JavaDoc;
33 import java.net.InetAddress JavaDoc;
34 import java.net.InetSocketAddress JavaDoc;
35 import java.net.Socket JavaDoc;
36 import java.net.SocketAddress JavaDoc;
37 import java.net.UnknownHostException JavaDoc;
38
39 import org.apache.commons.httpclient.ConnectTimeoutException;
40 import org.apache.commons.httpclient.HttpClientError;
41 import org.apache.commons.httpclient.params.HttpConnectionParams;
42 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 import javax.net.SocketFactory;
47 import javax.net.ssl.SSLContext;
48 import javax.net.ssl.TrustManager;
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  * <p>
63  * Example of using custom protocol socket factory for a specific host:
64  * <pre>
65  * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
66  *
67  * HttpClient client = new HttpClient();
68  * client.getHostConfiguration().setHost("localhost", 443, easyhttps);
69  * // use relative url only
70  * GetMethod httpget = new GetMethod("/");
71  * client.executeMethod(httpget);
72  * </pre>
73  * </p>
74  * <p>
75  * Example of using custom protocol socket factory per default instead of the standard one:
76  * <pre>
77  * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
78  * Protocol.registerProtocol("https", easyhttps);
79  *
80  * HttpClient client = new HttpClient();
81  * GetMethod httpget = new GetMethod("https://localhost/");
82  * client.executeMethod(httpget);
83  * </pre>
84  * </p>
85  *
86  * @author <a HREF="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
87  *
88  * <p>
89  * DISCLAIMER: HttpClient developers DO NOT actively support this component.
90  * The component is provided as a reference material, which may be inappropriate
91  * for use without additional customization.
92  * </p>
93  */

94
95 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
96
97     /** Log object for this class. */
98     private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class);
99
100     private SSLContext sslcontext = null;
101
102     /**
103      * Constructor for EasySSLProtocolSocketFactory.
104      */

105     public EasySSLProtocolSocketFactory() {
106         super();
107     }
108
109     private static SSLContext createEasySSLContext() {
110         try {
111             SSLContext context = SSLContext.getInstance("SSL");
112             context.init(
113               null,
114               new TrustManager[] {new EasyX509TrustManager(null)},
115               null);
116             return context;
117         } catch (Exception JavaDoc e) {
118             LOG.error(e.getMessage(), e);
119             throw new HttpClientError(e.toString());
120         }
121     }
122
123     private SSLContext getSSLContext() {
124         if (this.sslcontext == null) {
125             this.sslcontext = createEasySSLContext();
126         }
127         return this.sslcontext;
128     }
129
130     /**
131      * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
132      */

133     public Socket JavaDoc createSocket(
134         String JavaDoc host,
135         int port,
136         InetAddress JavaDoc clientHost,
137         int clientPort)
138         throws IOException JavaDoc, UnknownHostException JavaDoc {
139
140         return getSSLContext().getSocketFactory().createSocket(
141             host,
142             port,
143             clientHost,
144             clientPort
145         );
146     }
147
148     /**
149      * Attempts to get a new socket connection to the given host within the given time limit.
150      * <p>
151      * To circumvent the limitations of older JREs that do not support connect timeout a
152      * controller thread is executed. The controller thread attempts to create a new socket
153      * within the given limit of time. If socket constructor does not return until the
154      * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
155      * </p>
156      *
157      * @param host the host name/IP
158      * @param port the port on the host
159      * @param clientHost the local host name/IP to bind the socket to
160      * @param clientPort the port on the local machine
161      * @param params {@link HttpConnectionParams Http connection parameters}
162      *
163      * @return Socket a new socket
164      *
165      * @throws IOException if an I/O error occurs while creating the socket
166      * @throws UnknownHostException if the IP address of the host cannot be
167      * determined
168      */

169     public Socket JavaDoc createSocket(
170         final String JavaDoc host,
171         final int port,
172         final InetAddress JavaDoc localAddress,
173         final int localPort,
174         final HttpConnectionParams params
175     ) throws IOException JavaDoc, UnknownHostException JavaDoc, ConnectTimeoutException {
176         if (params == null) {
177             throw new IllegalArgumentException JavaDoc("Parameters may not be null");
178         }
179         int timeout = params.getConnectionTimeout();
180         SocketFactory socketfactory = getSSLContext().getSocketFactory();
181         if (timeout == 0) {
182             return socketfactory.createSocket(host, port, localAddress, localPort);
183         } else {
184             Socket JavaDoc socket = socketfactory.createSocket();
185             SocketAddress JavaDoc localaddr = new InetSocketAddress JavaDoc(localAddress, localPort);
186             SocketAddress JavaDoc remoteaddr = new InetSocketAddress JavaDoc(host, port);
187             socket.bind(localaddr);
188             socket.connect(remoteaddr, timeout);
189             return socket;
190         }
191     }
192
193     /**
194      * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
195      */

196     public Socket JavaDoc createSocket(String JavaDoc host, int port)
197         throws IOException JavaDoc, UnknownHostException JavaDoc {
198         return getSSLContext().getSocketFactory().createSocket(
199             host,
200             port
201         );
202     }
203
204     /**
205      * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
206      */

207     public Socket JavaDoc createSocket(
208         Socket JavaDoc socket,
209         String JavaDoc host,
210         int port,
211         boolean autoClose)
212         throws IOException JavaDoc, UnknownHostException JavaDoc {
213         return getSSLContext().getSocketFactory().createSocket(
214             socket,
215             host,
216             port,
217             autoClose
218         );
219     }
220
221     public boolean equals(Object JavaDoc obj) {
222         return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
223     }
224
225     public int hashCode() {
226         return EasySSLProtocolSocketFactory.class.hashCode();
227     }
228
229 }
230
Popular Tags