1 30 31 package org.apache.commons.httpclient.ssl; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.net.InetAddress ; 36 import java.net.Socket ; 37 import java.net.URL ; 38 import java.net.UnknownHostException ; 39 import java.security.KeyStore ; 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 cl = SimpleSocketFactory.class.getClassLoader(); 62 URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore"); 63 KeyStore keystore = KeyStore.getInstance("jks"); 64 InputStream 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 ex) { 79 LOG.error(ex.getMessage(), ex); 82 throw new IllegalStateException (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 createSocket( 99 final String host, 100 final int port, 101 final InetAddress localAddress, 102 final int localPort, 103 final HttpConnectionParams params 104 ) throws IOException , UnknownHostException , ConnectTimeoutException { 105 if (params == null) { 106 throw new IllegalArgumentException ("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 return ControllerThreadSocketFactory.createSocket( 114 this, host, port, localAddress, localPort, timeout); 115 } 116 } 117 118 public Socket createSocket( 119 String host, 120 int port, 121 InetAddress clientHost, 122 int clientPort) 123 throws IOException , UnknownHostException 124 { 125 return getSSLContext().getSocketFactory().createSocket( 126 host, 127 port, 128 clientHost, 129 clientPort 130 ); 131 } 132 133 public Socket createSocket(String host, int port) 134 throws IOException , UnknownHostException 135 { 136 return getSSLContext().getSocketFactory().createSocket( 137 host, 138 port 139 ); 140 } 141 142 public Socket createSocket( 143 Socket socket, 144 String host, 145 int port, 146 boolean autoClose) 147 throws IOException , UnknownHostException 148 { 149 return getSSLContext().getSocketFactory().createSocket( 150 socket, 151 host, 152 port, 153 autoClose 154 ); 155 } 156 } 157 | Popular Tags |