001: /*
002: * $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 $
003: * $Revision: 514390 $
004: * $Date: 2007-03-04 13:37:15 +0100 (Sun, 04 Mar 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.ssl;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.net.ServerSocket;
036: import java.net.URL;
037: import java.security.KeyStore;
038:
039: import javax.net.ServerSocketFactory;
040:
041: import org.apache.commons.httpclient.server.SimpleSocketFactory;
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: import com.sun.net.ssl.KeyManager;
046: import com.sun.net.ssl.KeyManagerFactory;
047: import com.sun.net.ssl.SSLContext;
048:
049: /**
050: * Defines a SSL socket factory
051: *
052: * @author Oleg Kalnichevski
053: */
054: public class SimpleSSLSocketFactory implements SimpleSocketFactory {
055:
056: private static final Log LOG = LogFactory
057: .getLog(SimpleSocketFactory.class);
058:
059: private static SSLContext SSLCONTEXT = null;
060:
061: private static SSLContext createSSLContext() {
062: try {
063: ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
064: URL url = cl
065: .getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
066: KeyStore keystore = KeyStore.getInstance("jks");
067: InputStream is = null;
068: try {
069: is = url.openStream();
070: keystore.load(is, "nopassword".toCharArray());
071: } finally {
072: if (is != null)
073: is.close();
074: }
075: KeyManagerFactory kmfactory = KeyManagerFactory
076: .getInstance(KeyManagerFactory
077: .getDefaultAlgorithm());
078: kmfactory.init(keystore, "nopassword".toCharArray());
079: KeyManager[] keymanagers = kmfactory.getKeyManagers();
080: SSLContext sslcontext = SSLContext.getInstance("TLS");
081: sslcontext.init(keymanagers, null, null);
082: return sslcontext;
083: } catch (Exception ex) {
084: // this is not the way a sane exception handling should be done
085: // but for our simple HTTP testing framework this will suffice
086: LOG.error(ex.getMessage(), ex);
087: throw new IllegalStateException(ex.getMessage());
088: }
089:
090: }
091:
092: private static SSLContext getSSLContext() {
093: if (SSLCONTEXT == null) {
094: SSLCONTEXT = createSSLContext();
095: }
096: return SSLCONTEXT;
097: }
098:
099: public SimpleSSLSocketFactory() {
100: super ();
101: }
102:
103: public ServerSocket createServerSocket(int port) throws IOException {
104: ServerSocketFactory socketfactory = getSSLContext()
105: .getServerSocketFactory();
106: return socketfactory.createServerSocket(port);
107: }
108:
109: }
|