Sync Integ to Master
[sdc.git] / utils / webseal-simulator / src / main / java / org / openecomp / sdc / webseal / simulator / SSL / DummySSLProtocolSocketFactory.java
1
2 package org.openecomp.sdc.webseal.simulator.SSL;
3
4 import java.io.IOException;
5 import java.net.InetAddress;
6 import java.net.Socket;
7 import java.net.UnknownHostException;
8
9 import javax.net.ssl.SSLContext;
10 import javax.net.ssl.TrustManager;
11
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;
17  
18 public class DummySSLProtocolSocketFactory implements SecureProtocolSocketFactory { 
19
20  
21   private SSLContext sslcontext = null; 
22  
23   /**
24    * Constructor for DummySSLProtocolSocketFactory. 
25    */ 
26   public DummySSLProtocolSocketFactory() { 
27     super(); 
28   } 
29  
30   private static SSLContext createEasySSLContext() { 
31     try { 
32       SSLContext context = SSLContext.getInstance("SSL"); 
33       context.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null); 
34       return context; 
35     } catch (Exception e) {
36       throw new HttpClientError(e.toString()); 
37     } 
38   } 
39  
40   private SSLContext getSSLContext() { 
41     if (this.sslcontext == null) { 
42       this.sslcontext = createEasySSLContext(); 
43     } 
44     return this.sslcontext; 
45   } 
46  
47   /**
48    * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int,InetAddress,int) 
49    */ 
50   public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, 
51           UnknownHostException { 
52  
53     return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); 
54   } 
55  
56   /**
57    * Attempts to get a new socket connection to the given host within the given 
58    * time limit. 
59    * <p> 
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} 
65    * </p> 
66    *  
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} 
72    *  
73    * @return Socket a new socket 
74    *  
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 
77    *         determined 
78    */ 
79   public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, 
80           final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { 
81     if (params == null) { 
82       throw new IllegalArgumentException("Parameters may not be null"); 
83     } 
84     int timeout = params.getConnectionTimeout(); 
85     if (timeout == 0) { 
86       return createSocket(host, port, localAddress, localPort); 
87     } else { 
88       // To be eventually deprecated when migrated to Java 1.4 or above 
89       return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout); 
90     } 
91   } 
92  
93   /**
94    * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(String,int) 
95    */ 
96   public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 
97     return getSSLContext().getSocketFactory().createSocket(host, port); 
98   } 
99  
100   /**
101    * @see org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory#createSocket(Socket,String,int,boolean) 
102    */ 
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); 
106   } 
107  
108   public boolean equals(Object obj) { 
109     return ((obj != null) && obj.getClass().equals(DummySSLProtocolSocketFactory.class)); 
110   } 
111  
112   public int hashCode() { 
113     return DummySSLProtocolSocketFactory.class.hashCode(); 
114   } 
115  
116 }