2 package org.openecomp.sdc.webseal.simulator.SSL;
4 import org.apache.commons.httpclient.ConnectTimeoutException;
5 import org.apache.commons.httpclient.HttpClientError;
6 import org.apache.commons.httpclient.params.HttpConnectionParams;
7 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
8 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
10 import javax.net.ssl.SSLContext;
11 import javax.net.ssl.TrustManager;
12 import java.io.IOException;
13 import java.net.InetAddress;
14 import java.net.Socket;
15 import java.net.UnknownHostException;
17 public class DummySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
20 private SSLContext sslcontext = null;
23 * Constructor for DummySSLProtocolSocketFactory.
25 public DummySSLProtocolSocketFactory() {
29 private static SSLContext createEasySSLContext() {
31 SSLContext context = SSLContext.getInstance("SSL");
32 context.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null);
34 } catch (Exception e) {
35 throw new HttpClientError(e.toString());
39 private SSLContext getSSLContext() {
40 if (this.sslcontext == null) {
41 this.sslcontext = createEasySSLContext();
43 return this.sslcontext;
47 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int,InetAddress,int)
49 public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException,
50 UnknownHostException {
52 return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
56 * Attempts to get a new socket connection to the given host within the given
59 * To circumvent the limitations of older JREs that do not support connect
60 * timeout a controller thread is executed. The controller thread attempts to
61 * create a new socket within the given limit of time. If socket constructor
62 * does not return until the timeout expires, the controller terminates and
63 * throws an {@link ConnectTimeoutException}
66 * @param host the host name/IP
67 * @param port the port on the host
68 * @param localAddress the local host name/IP to bind the socket to
69 * @param localPort the port on the local machine
70 * @param params {@link HttpConnectionParams Http connection parameters}
72 * @return Socket a new socket
74 * @throws IOException if an I/O error occurs while creating the socket
75 * @throws UnknownHostException if the IP address of the host cannot be
78 public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
79 final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
81 throw new IllegalArgumentException("Parameters may not be null");
83 int timeout = params.getConnectionTimeout();
85 return createSocket(host, port, localAddress, localPort);
87 // To be eventually deprecated when migrated to Java 1.4 or above
88 return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
93 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int)
95 public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
96 return getSSLContext().getSocketFactory().createSocket(host, port);
100 * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(Socket,String,int,boolean)
102 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
103 UnknownHostException {
104 return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
107 public boolean equals(Object obj) {
108 return ((obj != null) && obj.getClass().equals(DummySSLProtocolSocketFactory.class));
111 public int hashCode() {
112 return DummySSLProtocolSocketFactory.class.hashCode();