org.glite.slcs.httpclient.ssl
Class ExtendedProtocolSocketFactory

java.lang.Object
  extended by org.glite.slcs.httpclient.ssl.ExtendedProtocolSocketFactory
All Implemented Interfaces:
org.apache.commons.httpclient.protocol.ProtocolSocketFactory

public class ExtendedProtocolSocketFactory
extends java.lang.Object
implements org.apache.commons.httpclient.protocol.ProtocolSocketFactory

ExtendedProtocolSocketFactory extends the default system TrustManager with your custom truststore (KeyStore of trusted CA).

Use JDK keytool utility to import a trusted certificate and generate a truststore file:

                 keytool -import -alias "my server cert" -file server.crt -keystore my.truststore
 

Example of using custom protocol socket factory for a specific host:

 Protocol https = new Protocol("https", new ExtendedProtocolSocketFactory(
      "my.truststore.jks"), 443);
 
 HttpClient client = new HttpClient();
 client.getHostConfiguration().setHost("localhost", 443, https);
 // use relative url only
 GetMethod httpget = new GetMethod("/");
 client.executeMethod(httpget);
 

Example of using custom protocol socket factory per default instead of the standard one:

 Protocol https = new Protocol("https", new ExtendedProtocolSocketFactory(
      "my.truststore.jks"), 443);
 Protocol.registerProtocol("https", https);
 
 HttpClient client = new HttpClient();
 GetMethod httpget = new GetMethod("https://localhost/");
 client.executeMethod(httpget);
 

Version:
$Revision: 1.5 $
Author:
Valery Tschopp

Constructor Summary
ExtendedProtocolSocketFactory(java.security.KeyStore keystore, java.lang.String keystorePassword, java.security.KeyStore truststore)
          Creates a ExtendedProtocolSocketFactory with the given keystore and keystore password, and the truststore.
ExtendedProtocolSocketFactory(java.lang.String truststorePath)
          Extends the trust capabilities of the ProtocolSocketFactory with the given TrustStore.
ExtendedProtocolSocketFactory(java.lang.String keystorePath, java.lang.String keystorePassword, java.lang.String truststorePath)
          Extends the trust and client authentication of the ProtocolSocketFactory with the given KeyStore and TrustStore.
 
Method Summary
 java.net.Socket createSocket(java.net.Socket socket, java.lang.String host, int port, boolean autoClose)
           
 java.net.Socket createSocket(java.lang.String host, int port)
           
 java.net.Socket createSocket(java.lang.String host, int port, java.net.InetAddress clientHost, int clientPort)
           
 java.net.Socket createSocket(java.lang.String host, int port, java.net.InetAddress localAddress, int localPort, org.apache.commons.httpclient.params.HttpConnectionParams params)
          Attempts to get a new socket connection to the given host within the given time limit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtendedProtocolSocketFactory

public ExtendedProtocolSocketFactory(java.lang.String truststorePath)
                              throws java.io.IOException,
                                     java.security.GeneralSecurityException
Extends the trust capabilities of the ProtocolSocketFactory with the given TrustStore.

Parameters:
truststorePath - Path of the truststore file (JKS).
Throws:
java.io.IOException - If an error occurs while loading the truststore.
java.security.GeneralSecurityException - If an error occurs while initializing the SSLContext.

ExtendedProtocolSocketFactory

public ExtendedProtocolSocketFactory(java.lang.String keystorePath,
                                     java.lang.String keystorePassword,
                                     java.lang.String truststorePath)
                              throws java.io.IOException,
                                     java.security.GeneralSecurityException
Extends the trust and client authentication of the ProtocolSocketFactory with the given KeyStore and TrustStore.

Parameters:
keystorePath - Path of the keystore file (JKS).
keystorePassword - Password of the keystore.
truststorePath - Path of the truststore file (JKS).
Throws:
java.io.IOException - If an error occurs while loading the keystore or the truststore.
java.security.GeneralSecurityException - If an error occurs while initializing the SSLContext.

ExtendedProtocolSocketFactory

public ExtendedProtocolSocketFactory(java.security.KeyStore keystore,
                                     java.lang.String keystorePassword,
                                     java.security.KeyStore truststore)
                              throws java.io.IOException,
                                     java.security.GeneralSecurityException
Creates a ExtendedProtocolSocketFactory with the given keystore and keystore password, and the truststore.

Parameters:
keystore - The already loaded keystore object
keystorePassword - The password of the keystore.
truststore - The already loaded truststore object
Throws:
java.io.IOException - Should never occurs in this context
java.security.GeneralSecurityException - If an error occurs while initializing the SSLContext.
Method Detail

createSocket

public java.net.Socket createSocket(java.lang.String host,
                                    int port,
                                    java.net.InetAddress localAddress,
                                    int localPort,
                                    org.apache.commons.httpclient.params.HttpConnectionParams params)
                             throws java.io.IOException,
                                    java.net.UnknownHostException,
                                    org.apache.commons.httpclient.ConnectTimeoutException
Attempts to get a new socket connection to the given host within the given time limit.

To circumvent the limitations of older JREs that do not support connect timeout a controller thread is executed. The controller thread attempts to create a new socket within the given limit of time. If socket constructor does not return until the timeout expires, the controller terminates and throws an ConnectTimeoutException

Specified by:
createSocket in interface org.apache.commons.httpclient.protocol.ProtocolSocketFactory
Parameters:
host - the host name/IP
port - the port on the host
clientHost - the local host name/IP to bind the socket to
clientPort - the port on the local machine
params - Http connection parameters
Returns:
Socket a new socket
Throws:
java.io.IOException - if an I/O error occurs while creating the socket
java.net.UnknownHostException - if the IP address of the host cannot be determined
org.apache.commons.httpclient.ConnectTimeoutException

createSocket

public java.net.Socket createSocket(java.lang.String host,
                                    int port,
                                    java.net.InetAddress clientHost,
                                    int clientPort)
                             throws java.io.IOException,
                                    java.net.UnknownHostException
Specified by:
createSocket in interface org.apache.commons.httpclient.protocol.ProtocolSocketFactory
Throws:
java.io.IOException
java.net.UnknownHostException
See Also:
ProtocolSocketFactory.createSocket(java.lang.String,int,java.net.InetAddress,int)

createSocket

public java.net.Socket createSocket(java.lang.String host,
                                    int port)
                             throws java.io.IOException,
                                    java.net.UnknownHostException
Specified by:
createSocket in interface org.apache.commons.httpclient.protocol.ProtocolSocketFactory
Throws:
java.io.IOException
java.net.UnknownHostException
See Also:
ProtocolSocketFactory.createSocket(java.lang.String,int)

createSocket

public java.net.Socket createSocket(java.net.Socket socket,
                                    java.lang.String host,
                                    int port,
                                    boolean autoClose)
                             throws java.io.IOException,
                                    java.net.UnknownHostException
Throws:
java.io.IOException
java.net.UnknownHostException
See Also:
ProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)