KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > ssl > SimpleSSLTestProtocolSocketFactory


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: 514390 $
4  * $Date: 2007-03-04 12:37:15 +0000 (Sun, 04 Mar 2007) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30
31 package org.apache.commons.httpclient.ssl;
32
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.net.InetAddress JavaDoc;
36 import java.net.Socket JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.net.UnknownHostException JavaDoc;
39 import java.security.KeyStore JavaDoc;
40
41 import org.apache.commons.httpclient.ConnectTimeoutException;
42 import org.apache.commons.httpclient.params.HttpConnectionParams;
43 import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
44 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
45 import org.apache.commons.httpclient.server.SimpleSocketFactory;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 import com.sun.net.ssl.SSLContext;
50 import com.sun.net.ssl.TrustManager;
51 import com.sun.net.ssl.TrustManagerFactory;
52
53 public class SimpleSSLTestProtocolSocketFactory implements SecureProtocolSocketFactory {
54
55     private static final Log LOG = LogFactory.getLog(SimpleSSLTestProtocolSocketFactory.class);
56
57     private static SSLContext SSLCONTEXT = null;
58     
59     private static SSLContext createSSLContext() {
60         try {
61             ClassLoader JavaDoc cl = SimpleSocketFactory.class.getClassLoader();
62             URL JavaDoc url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
63             KeyStore JavaDoc keystore = KeyStore.getInstance("jks");
64             InputStream JavaDoc is = null;
65             try {
66                 is = url.openStream();
67                 keystore.load(is, "nopassword".toCharArray());
68             } finally {
69                 if (is != null) is.close();
70             }
71             TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
72                     TrustManagerFactory.getDefaultAlgorithm());
73             tmfactory.init(keystore);
74             TrustManager[] trustmanagers = tmfactory.getTrustManagers();
75             SSLContext sslcontext = SSLContext.getInstance("TLS");
76             sslcontext.init(null, trustmanagers, null);
77             return sslcontext;
78         } catch (Exception JavaDoc ex) {
79             // this is not the way a sane exception handling should be done
80
// but for our simple HTTP testing framework this will suffice
81
LOG.error(ex.getMessage(), ex);
82             throw new IllegalStateException JavaDoc(ex.getMessage());
83         }
84     
85     }
86     
87     private static SSLContext getSSLContext() {
88         if (SSLCONTEXT == null) {
89             SSLCONTEXT = createSSLContext();
90         }
91         return SSLCONTEXT;
92     }
93     
94     public SimpleSSLTestProtocolSocketFactory() {
95         super();
96     }
97     
98     public Socket JavaDoc createSocket(
99         final String JavaDoc host,
100         final int port,
101         final InetAddress JavaDoc localAddress,
102         final int localPort,
103         final HttpConnectionParams params
104     ) throws IOException JavaDoc, UnknownHostException JavaDoc, ConnectTimeoutException {
105         if (params == null) {
106             throw new IllegalArgumentException JavaDoc("Parameters may not be null");
107         }
108         int timeout = params.getConnectionTimeout();
109         if (timeout == 0) {
110             return createSocket(host, port, localAddress, localPort);
111         } else {
112             // To be eventually deprecated when migrated to Java 1.4 or above
113
return ControllerThreadSocketFactory.createSocket(
114                     this, host, port, localAddress, localPort, timeout);
115         }
116     }
117
118     public Socket JavaDoc createSocket(
119         String JavaDoc host,
120         int port,
121         InetAddress JavaDoc clientHost,
122         int clientPort)
123         throws IOException JavaDoc, UnknownHostException JavaDoc
124    {
125        return getSSLContext().getSocketFactory().createSocket(
126             host,
127             port,
128             clientHost,
129             clientPort
130         );
131     }
132
133     public Socket JavaDoc createSocket(String JavaDoc host, int port)
134         throws IOException JavaDoc, UnknownHostException JavaDoc
135     {
136         return getSSLContext().getSocketFactory().createSocket(
137             host,
138             port
139         );
140     }
141
142     public Socket JavaDoc createSocket(
143         Socket JavaDoc socket,
144         String JavaDoc host,
145         int port,
146         boolean autoClose)
147         throws IOException JavaDoc, UnknownHostException JavaDoc
148     {
149         return getSSLContext().getSocketFactory().createSocket(
150             socket,
151             host,
152             port,
153             autoClose
154         );
155     }
156 }
157
Popular Tags