2 package org.openecomp.sdc.webseal.simulator.SSL;
4 import java.io.IOException;
5 import java.net.InetAddress;
6 import java.net.Socket;
7 import java.net.UnknownHostException;
9 import javax.net.ssl.SSLContext;
10 import javax.net.ssl.TrustManager;
12 import org.apache.commons.httpclient.ConnectTimeoutException;
13 import org.apache.commons.httpclient.HttpClientError;
14 import org.apache.commons.httpclient.params.HttpConnectionParams;
15 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
16 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
18 public class DummySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
21 private SSLContext sslcontext = null;
24 * Constructor for DummySSLProtocolSocketFactory.
26 public DummySSLProtocolSocketFactory() {
30 private static SSLContext createEasySSLContext() {
32 SSLContext context = SSLContext.getInstance("SSL");
33 context.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null);
35 } catch (Exception e) {
36 throw new HttpClientError(e.toString());
40 private SSLContext getSSLContext() {
41 if (this.sslcontext == null) {
42 this.sslcontext = createEasySSLContext();
44 return this.sslcontext;
48 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int,InetAddress,int)
50 public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException,
51 UnknownHostException {
53 return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
57 * Attempts to get a new socket connection to the given host within the given
60 * To circumvent the limitations of older JREs that do not support connect
61 * timeout a controller thread is executed. The controller thread attempts to
62 * create a new socket within the given limit of time. If socket constructor
63 * does not return until the timeout expires, the controller terminates and
64 * throws an {@link ConnectTimeoutException}
67 * @param host the host name/IP
68 * @param port the port on the host
69 * @param localAddress the local host name/IP to bind the socket to
70 * @param localPort the port on the local machine
71 * @param params {@link HttpConnectionParams Http connection parameters}
73 * @return Socket a new socket
75 * @throws IOException if an I/O error occurs while creating the socket
76 * @throws UnknownHostException if the IP address of the host cannot be
79 public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
80 final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
82 throw new IllegalArgumentException("Parameters may not be null");
84 int timeout = params.getConnectionTimeout();
86 return createSocket(host, port, localAddress, localPort);
88 // To be eventually deprecated when migrated to Java 1.4 or above
89 return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
94 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int)
96 public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
97 return getSSLContext().getSocketFactory().createSocket(host, port);
101 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(Socket,String,int,boolean)
103 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
104 UnknownHostException {
105 return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
108 public boolean equals(Object obj) {
109 return ((obj != null) && obj.getClass().equals(DummySSLProtocolSocketFactory.class));
112 public int hashCode() {
113 return DummySSLProtocolSocketFactory.class.hashCode();