TrustEverythingSSLProtocolSocketFactory.java from Jameleon at Krugle
Show TrustEverythingSSLProtocolSocketFactory.java syntax highlighted
/*
Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit.util;
import net.sf.jameleon.util.X509TrustEverythingManager;
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 org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
/**
* An implementation of SecureProtocolSocketFactory that allows invalid certs through through
*/
public class TrustEverythingSSLProtocolSocketFactory implements SecureProtocolSocketFactory {
private SSLContext context = null;
private static SSLContext createTrustEverythingSSLContext() {
try {
SSLContext cntxt = SSLContext.getInstance("SSL");
cntxt.init(
null,
new TrustManager[] {new X509TrustEverythingManager(null)},
null);
return cntxt;
} catch (Exception e) {
throw new HttpClientError(e.toString());
}
}
private SSLContext getSSLContext() {
if (context == null) {
context = createTrustEverythingSSLContext();
}
return context;
}
public Socket createSocket( String host,
int port,
InetAddress clientHost,
int clientPort
) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
host,
port,
clientHost,
clientPort
);
}
public Socket createSocket( final String host,
final int port,
final InetAddress localAddress,
final int localPort,
final HttpConnectionParams params
) throws IOException,
UnknownHostException,
ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("params parameter must be set!");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
} else {
Socket socket = socketfactory.createSocket();
SocketAddress local = new InetSocketAddress(localAddress, localPort);
SocketAddress remote = new InetSocketAddress(host, port);
socket.bind(local);
socket.connect(remote, timeout);
return socket;
}
}
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port);
}
public Socket createSocket( Socket socket,
String host,
int port,
boolean autoClose
) throws
IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(socket,
host,
port,
autoClose);
}
public boolean equals(Object obj) {
boolean equals = ((obj != null) &&
obj.getClass().equals(TrustEverythingSSLProtocolSocketFactory.class));
return equals;
}
public int hashCode() {
return TrustEverythingSSLProtocolSocketFactory.class.hashCode();
}
}
See more files for this project here