View Javadoc

1   /*
2       Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
18  */
19  package net.sf.jameleon.plugin.htmlunit.util;
20  
21  import net.sf.jameleon.util.X509TrustEverythingManager;
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  import java.net.InetSocketAddress;
26  import java.net.Socket;
27  import java.net.SocketAddress;
28  import java.net.UnknownHostException;
29  
30  import org.apache.commons.httpclient.ConnectTimeoutException;
31  import org.apache.commons.httpclient.HttpClientError;
32  import org.apache.commons.httpclient.params.HttpConnectionParams;
33  import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
34  
35  import javax.net.SocketFactory;
36  import javax.net.ssl.SSLContext;
37  import javax.net.ssl.TrustManager;
38  
39  /***
40   * An implementation of SecureProtocolSocketFactory that allows invalid certs through through
41   */
42  public class TrustEverythingSSLProtocolSocketFactory implements SecureProtocolSocketFactory {
43  
44      private SSLContext context = null;
45  
46      private static SSLContext createTrustEverythingSSLContext() {
47          try {
48              SSLContext cntxt = SSLContext.getInstance("SSL");
49              cntxt.init(
50                        null, 
51                        new TrustManager[] {new X509TrustEverythingManager(null)}, 
52                        null);
53              return cntxt;
54          } catch (Exception e) {
55              throw new HttpClientError(e.toString());
56          }
57      }
58  
59      private SSLContext getSSLContext() {
60          if (context == null) {
61              context = createTrustEverythingSSLContext();
62          }
63          return context;
64      }
65  
66      public Socket createSocket( String host,
67                                  int port,
68                                  InetAddress clientHost,
69                                  int clientPort
70                                ) throws IOException, UnknownHostException {
71          return getSSLContext().getSocketFactory().createSocket(
72                                                                host,
73                                                                port,
74                                                                clientHost,
75                                                                clientPort
76                                                                );
77      }
78  
79      public Socket createSocket( final String host,
80                                  final int port,
81                                  final InetAddress localAddress,
82                                  final int localPort,
83                                  final HttpConnectionParams params
84                                ) throws IOException, 
85      UnknownHostException,
86      ConnectTimeoutException {
87          if (params == null) {
88              throw new IllegalArgumentException("params parameter must be set!");
89          }
90          int timeout = params.getConnectionTimeout();
91          SocketFactory socketfactory = getSSLContext().getSocketFactory();
92          if (timeout == 0) {
93              return socketfactory.createSocket(host, port, localAddress, localPort);
94          } else {
95              Socket socket = socketfactory.createSocket();
96              SocketAddress local = new InetSocketAddress(localAddress, localPort);
97              SocketAddress remote = new InetSocketAddress(host, port);
98              socket.bind(local);
99              socket.connect(remote, timeout);
100             return socket;
101         }
102     }
103 
104     public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
105         return getSSLContext().getSocketFactory().createSocket(host, port);
106     }
107 
108     public Socket createSocket( Socket socket,
109                                 String host,
110                                 int port,
111                                 boolean autoClose
112                               ) throws 
113                                     IOException,
114                                     UnknownHostException {
115 
116         return getSSLContext().getSocketFactory().createSocket(socket,
117                                                                host,
118                                                                port,
119                                                                autoClose);
120     }
121 
122     public boolean equals(Object obj) {
123         boolean equals = ((obj != null) && 
124                                obj.getClass().equals(TrustEverythingSSLProtocolSocketFactory.class));
125         return equals;
126     }
127 
128     public int hashCode() {
129         return TrustEverythingSSLProtocolSocketFactory.class.hashCode();
130     }
131 
132 }
133