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