I thought maybe my attachment would come through...it did not. Here is my
source code. (I am putting this in using Outlook's HTML option so it
doesn't get the line breaks.)
package (your package)
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
public class SocketFactoryWrapper implements SecureProtocolSocketFactory {
private SSLSocketFactory socketFactory;
public SocketFactoryWrapper(SSLSocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
public Socket createSocket(String host, int port) throws
IOException, UnknownHostException {
return socketFactory.createSocket(host,port);
}
public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort) throws IOException, UnknownHostException {
return
socketFactory.createSocket(host,port,localAddress,localPort);
}
public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort, HttpConnectionParams params) throws
IOException, UnknownHostException, ConnectTimeoutException {
Socket rval;
if (params == null) {
throw new IllegalArgumentException("Parameters may
not be null");
}
int timeout = params.getConnectionTimeout();
if (timeout == 0) {
rval = socketFactory.createSocket(host, port,
localAddress, localPort);
}
else {
rval = socketFactory.createSocket();
SocketAddress localaddr = new
InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new
InetSocketAddress(host, port);
rval.bind(localaddr);
rval.connect(remoteaddr, timeout);
}
return rval;
}
public Socket createSocket( Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return
socketFactory.createSocket(socket,host,port,autoClose);
}
}
-----Original Message-----
From: Mark Claassen [ <mailto:[EMAIL PROTECTED]> mailto:[EMAIL PROTECTED]
Sent: Monday, September 18, 2006 10:58 AM
To: 'HttpClient Project'
Subject: SocketFactoryWrapper
I asked a question on this or the user list some time ago that didn't get
resolved. I let it go for a while and when I came back to it I found the
answer immediately. However, since no one was able to answer my question
before, I though I should share it with you in case it has any value for
anyone else.
Java WebStart is able to use the browser's ssl keystore and give that
information to the HttpsURLConnection. So, say if a company is using
smartcards for SSL authentication, code using HttpsURLConnection will just
work out of the box. HttpClient will not.
The socket factories implemented in HttpClient do not use webstart's. This
was causing a roadblock for me in adopting HttpClient in our product
(although everything else about it was far superior).
I was struggling with this for a while and then the obvious answer came to
me, just get the SocketFactory from
HttpsURLConnection.getDefaultSocketFactory() and wrap it in
SecureProtocolSocketFactory. My initial tests shows this work perfectly.
(And much appreciation to whoever designed the ProtocolSocketFactory
interfaces to be just like the java.net stuff!)
I didn't know if this is something that others are struggling with, or even
if something like this simple class should be included in the API. With
smartcards and such getting more prevelant, I see this becoming more and
more of an issue as time goes by.
(One of these method implementations comes from
EasySSLProtocolSocketFactory.java)
Mark