1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/ssl/SimpleSSLSocketFactory.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 1999-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.ServerSocket;
34  import java.net.URL;
35  import java.security.KeyStore;
36  
37  import javax.net.ServerSocketFactory;
38  
39  import org.apache.commons.httpclient.server.SimpleSocketFactory;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import com.sun.net.ssl.KeyManager;
44  import com.sun.net.ssl.KeyManagerFactory;
45  import com.sun.net.ssl.SSLContext;
46  
47  /***
48   * Defines a SSL socket factory
49   * 
50   * @author Oleg Kalnichevski
51   */
52  public class SimpleSSLSocketFactory implements SimpleSocketFactory {
53      
54      private static final Log LOG = LogFactory.getLog(SimpleSocketFactory.class);
55  
56      private static SSLContext SSLCONTEXT = null;
57      
58      private static SSLContext createSSLContext() {
59          try {
60              ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
61              URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
62              KeyStore keystore  = KeyStore.getInstance("jks");
63              keystore.load(url.openStream(), "nopassword".toCharArray());
64              KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
65                      KeyManagerFactory.getDefaultAlgorithm());
66              kmfactory.init(keystore, "nopassword".toCharArray());
67              KeyManager[] keymanagers = kmfactory.getKeyManagers(); 
68              SSLContext sslcontext = SSLContext.getInstance("TLS");
69              sslcontext.init(keymanagers, null, null);
70              return sslcontext;
71          } catch (Exception ex) {
72          	// this is not the way a sane exception handling should be done
73              // but for our simple HTTP testing framework this will suffice
74              LOG.error(ex.getMessage(), ex);
75              throw new IllegalStateException(ex.getMessage());
76          }
77      
78      }
79      
80      private static SSLContext getSSLContext() {
81      	if (SSLCONTEXT == null) {
82      		SSLCONTEXT = createSSLContext();
83          }
84          return SSLCONTEXT;
85      }
86      
87      public SimpleSSLSocketFactory() {
88          super();
89      }
90      
91      public ServerSocket createServerSocket(int port) throws IOException {
92      	ServerSocketFactory socketfactory = getSSLContext().getServerSocketFactory();
93          return socketfactory.createServerSocket(port);
94      }
95      
96  }