2
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java,v 1.10 2004/05/13 04:01:22 mbecke Exp $
4
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
6
* ====================================================================
8
* Licensed to the Apache Software Foundation (ASF) under one or more
9
* contributor license agreements. See the NOTICE file distributed with
10
* this work for additional information regarding copyright ownership.
11
* The ASF licenses this file to You under the Apache License, Version 2.0
12
* (the "License"); you may not use this file except in compliance with
13
* the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing, software
18
* distributed under the License is distributed on an "AS IS" BASIS,
19
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
* See the License for the specific language governing permissions and
21
* limitations under the License.
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.commons.httpclient.protocol;
33
import java.io.IOException;
34
import java.net.InetAddress;
35
import java.net.Socket;
36
import java.net.UnknownHostException;
38
import javax.net.ssl.SSLSocketFactory;
40
import org.apache.commons.httpclient.ConnectTimeoutException;
41
import org.apache.commons.httpclient.params.HttpConnectionParams;
44
* A SecureProtocolSocketFactory that uses JSSE to create sockets.
46
* @author Michael Becke
47
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51
public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
54
* The factory singleton.
56
private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory();
59
* Gets an singleton instance of the SSLProtocolSocketFactory.
60
* @return a SSLProtocolSocketFactory
62
static SSLProtocolSocketFactory getSocketFactory() {
67
* Constructor for SSLProtocolSocketFactory.
69
public SSLProtocolSocketFactory() {
74
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
76
public Socket createSocket(
79
InetAddress clientHost,
81
throws IOException, UnknownHostException {
82
return SSLSocketFactory.getDefault().createSocket(
91
* Attempts to get a new socket connection to the given host within the given time limit.
93
* This method employs several techniques to circumvent the limitations of older JREs that
94
* do not support connect timeout. When running in JRE 1.4 or above reflection is used to
95
* call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older
96
* JREs a controller thread is executed. The controller thread attempts to create a new socket
97
* within the given limit of time. If socket constructor does not return until the timeout
98
* expires, the controller terminates and throws an {@link ConnectTimeoutException}
101
* @param host the host name/IP
102
* @param port the port on the host
103
* @param localAddress the local host name/IP to bind the socket to
104
* @param localPort the port on the local machine
105
* @param params {@link HttpConnectionParams Http connection parameters}
107
* @return Socket a new socket
109
* @throws IOException if an I/O error occurs while creating the socket
110
* @throws UnknownHostException if the IP address of the host cannot be
115
public Socket createSocket(
118
final InetAddress localAddress,
120
final HttpConnectionParams params
121
) throws IOException, UnknownHostException, ConnectTimeoutException {
122
if (params == null) {
123
throw new IllegalArgumentException("Parameters may not be null");
125
int timeout = params.getConnectionTimeout();
127
return createSocket(host, port, localAddress, localPort);
129
// To be eventually deprecated when migrated to Java 1.4 or above
130
Socket socket = ReflectionSocketFactory.createSocket(
131
"javax.net.ssl.SSLSocketFactory", host, port, localAddress, localPort, timeout);
132
if (socket == null) {
133
socket = ControllerThreadSocketFactory.createSocket(
134
this, host, port, localAddress, localPort, timeout);
141
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
143
public Socket createSocket(String host, int port)
144
throws IOException, UnknownHostException {
145
return SSLSocketFactory.getDefault().createSocket(
152
* @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
154
public Socket createSocket(
159
throws IOException, UnknownHostException {
160
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
169
* All instances of SSLProtocolSocketFactory are the same.
171
public boolean equals(Object obj) {
172
return ((obj != null) && obj.getClass().equals(getClass()));
176
* All instances of SSLProtocolSocketFactory have the same hash code.
178
public int hashCode() {
179
return getClass().hashCode();