1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/ssl/SimpleSSLTestProtocolSocketFactory.java,v 1.1 2004/12/11 22:35:26 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient.ssl;
31  
32  import java.io.IOException;
33  import java.net.InetAddress;
34  import java.net.Socket;
35  import java.net.URL;
36  import java.net.UnknownHostException;
37  import java.security.KeyStore;
38  
39  import org.apache.commons.httpclient.ConnectTimeoutException;
40  import org.apache.commons.httpclient.params.HttpConnectionParams;
41  import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
42  import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
43  import org.apache.commons.httpclient.server.SimpleSocketFactory;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import com.sun.net.ssl.SSLContext;
48  import com.sun.net.ssl.TrustManager;
49  import com.sun.net.ssl.TrustManagerFactory;
50  
51  public class SimpleSSLTestProtocolSocketFactory implements SecureProtocolSocketFactory {
52  
53      private static final Log LOG = LogFactory.getLog(SimpleSSLTestProtocolSocketFactory.class);
54  
55      private static SSLContext SSLCONTEXT = null;
56      
57      private static SSLContext createSSLContext() {
58          try {
59              ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
60              URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
61              KeyStore keystore  = KeyStore.getInstance("jks");
62              keystore.load(url.openStream(), "nopassword".toCharArray());
63              TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
64                      TrustManagerFactory.getDefaultAlgorithm());
65              tmfactory.init(keystore);
66              TrustManager[] trustmanagers = tmfactory.getTrustManagers(); 
67              SSLContext sslcontext = SSLContext.getInstance("TLS");
68              sslcontext.init(null, trustmanagers, null);
69              return sslcontext;
70          } catch (Exception ex) {
71              // this is not the way a sane exception handling should be done
72              // but for our simple HTTP testing framework this will suffice
73              LOG.error(ex.getMessage(), ex);
74              throw new IllegalStateException(ex.getMessage());
75          }
76      
77      }
78      
79      private static SSLContext getSSLContext() {
80          if (SSLCONTEXT == null) {
81              SSLCONTEXT = createSSLContext();
82          }
83          return SSLCONTEXT;
84      }
85      
86      public SimpleSSLTestProtocolSocketFactory() {
87          super();
88      }
89      
90      public Socket createSocket(
91          final String host,
92          final int port,
93          final InetAddress localAddress,
94          final int localPort,
95          final HttpConnectionParams params
96      ) throws IOException, UnknownHostException, ConnectTimeoutException {
97          if (params == null) {
98              throw new IllegalArgumentException("Parameters may not be null");
99          }
100         int timeout = params.getConnectionTimeout();
101         if (timeout == 0) {
102             return createSocket(host, port, localAddress, localPort);
103         } else {
104             // To be eventually deprecated when migrated to Java 1.4 or above
105             return ControllerThreadSocketFactory.createSocket(
106                     this, host, port, localAddress, localPort, timeout);
107         }
108     }
109 
110     public Socket createSocket(
111         String host,
112         int port,
113         InetAddress clientHost,
114         int clientPort)
115         throws IOException, UnknownHostException
116    {
117        return getSSLContext().getSocketFactory().createSocket(
118             host,
119             port,
120             clientHost,
121             clientPort
122         );
123     }
124 
125     public Socket createSocket(String host, int port)
126         throws IOException, UnknownHostException
127     {
128         return getSSLContext().getSocketFactory().createSocket(
129             host,
130             port
131         );
132     }
133 
134     public Socket createSocket(
135         Socket socket,
136         String host,
137         int port,
138         boolean autoClose)
139         throws IOException, UnknownHostException
140     {
141         return getSSLContext().getSocketFactory().createSocket(
142             socket,
143             host,
144             port,
145             autoClose
146         );
147     }
148 }