001: /*
002: * $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 $
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.InetAddress;
036: import java.net.Socket;
037: import java.net.URL;
038: import java.net.UnknownHostException;
039: import java.security.KeyStore;
040:
041: import org.apache.commons.httpclient.ConnectTimeoutException;
042: import org.apache.commons.httpclient.params.HttpConnectionParams;
043: import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
044: import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
045: import org.apache.commons.httpclient.server.SimpleSocketFactory;
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: import com.sun.net.ssl.SSLContext;
050: import com.sun.net.ssl.TrustManager;
051: import com.sun.net.ssl.TrustManagerFactory;
052:
053: public class SimpleSSLTestProtocolSocketFactory implements
054: SecureProtocolSocketFactory {
055:
056: private static final Log LOG = LogFactory
057: .getLog(SimpleSSLTestProtocolSocketFactory.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: TrustManagerFactory tmfactory = TrustManagerFactory
076: .getInstance(TrustManagerFactory
077: .getDefaultAlgorithm());
078: tmfactory.init(keystore);
079: TrustManager[] trustmanagers = tmfactory.getTrustManagers();
080: SSLContext sslcontext = SSLContext.getInstance("TLS");
081: sslcontext.init(null, trustmanagers, 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 SimpleSSLTestProtocolSocketFactory() {
100: super ();
101: }
102:
103: public Socket createSocket(final String host, final int port,
104: final InetAddress localAddress, final int localPort,
105: final HttpConnectionParams params) throws IOException,
106: UnknownHostException, ConnectTimeoutException {
107: if (params == null) {
108: throw new IllegalArgumentException(
109: "Parameters may not be null");
110: }
111: int timeout = params.getConnectionTimeout();
112: if (timeout == 0) {
113: return createSocket(host, port, localAddress, localPort);
114: } else {
115: // To be eventually deprecated when migrated to Java 1.4 or above
116: return ControllerThreadSocketFactory.createSocket(this ,
117: host, port, localAddress, localPort, timeout);
118: }
119: }
120:
121: public Socket createSocket(String host, int port,
122: InetAddress clientHost, int clientPort) throws IOException,
123: UnknownHostException {
124: return getSSLContext().getSocketFactory().createSocket(host,
125: port, clientHost, clientPort);
126: }
127:
128: public Socket createSocket(String host, int port)
129: throws IOException, UnknownHostException {
130: return getSSLContext().getSocketFactory().createSocket(host,
131: port);
132: }
133:
134: public Socket createSocket(Socket socket, String host, int port,
135: boolean autoClose) throws IOException, UnknownHostException {
136: return getSSLContext().getSocketFactory().createSocket(socket,
137: host, port, autoClose);
138: }
139: }
|