You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2004/12/11 23:35:26 UTC
cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ssl SimpleSSLSocketFactory.java SimpleSSLTestProtocolSocketFactory.java simpleserver.keystore
olegk 2004/12/11 14:35:26
Modified: httpclient/src/test/org/apache/commons/httpclient
TestProxy.java
httpclient/src/test/org/apache/commons/httpclient/server
ProxyRequestHandler.java SimpleHttpServer.java
SimpleHttpServerConnection.java SimpleProxy.java
StreamProxy.java
TransparentProxyRequestHandler.java
Added: httpclient/src/test/org/apache/commons/httpclient/server
SimplePlainSocketFactory.java
SimpleSocketFactory.java
httpclient/src/test/org/apache/commons/httpclient/ssl
SimpleSSLSocketFactory.java
SimpleSSLTestProtocolSocketFactory.java
simpleserver.keystore
Removed: httpclient/src/test/org/apache/commons/httpclient/server
BidiStreamProxy.java
Log:
* SSL support for the SimpleHttpServer and SimpleProxy
* SSL tunneling test cases
Contributed by Oleg Kalnichevski
Revision Changes Path
1.11 +69 -10 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java
Index: TestProxy.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- TestProxy.java 28 Nov 2004 15:44:39 -0000 1.10
+++ TestProxy.java 11 Dec 2004 22:35:26 -0000 1.11
@@ -27,6 +27,8 @@
*/
package org.apache.commons.httpclient;
+import java.util.Enumeration;
+
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
@@ -35,12 +37,18 @@
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.server.AuthRequestHandler;
import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
import org.apache.commons.httpclient.server.HttpServiceHandler;
import org.apache.commons.httpclient.server.SimpleHttpServer;
+import org.apache.commons.httpclient.server.SimplePlainSocketFactory;
import org.apache.commons.httpclient.server.SimpleProxy;
+import org.apache.commons.httpclient.server.SimpleSocketFactory;
+import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory;
+import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory;
+import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -56,27 +64,70 @@
private SimpleProxy proxy = null;
private SimpleHttpServer httpserver = null;
private HttpClient httpclient = null;
+ private boolean usessl = false;
public TestProxy(String testName) {
super(testName);
}
+ static class SSLDecorator extends TestSetup {
+
+ public static void addTests(TestSuite suite) {
+ TestSuite ts2 = new TestSuite();
+ addTest(ts2, suite);
+ suite.addTest(ts2);
+ }
+
+ private static void addTest(TestSuite suite, Test t) {
+ if (t instanceof TestProxy) {
+ suite.addTest(new SSLDecorator((TestProxy) t));
+ } else if (t instanceof TestSuite) {
+ Enumeration en = ((TestSuite) t).tests();
+ while (en.hasMoreElements()) {
+ addTest(suite, (Test) en.nextElement());
+ }
+ }
+ }
+
+ public SSLDecorator(TestProxy test) {
+ super(test);
+ }
+
+ protected void setUp() throws Exception {
+ TestProxy base = (TestProxy)getTest();
+ base.setUseSSL(true);
+ }
+ }
+
public static Test suite() {
- return new TestSuite(TestProxy.class);
+ TestSuite suite = new TestSuite(TestProxy.class);
+ SSLDecorator.addTests(suite);
+ return suite;
}
protected void setUp() throws Exception {
super.setUp();
- this.proxy = new SimpleProxy();
- this.httpserver = new SimpleHttpServer();
this.httpclient = new HttpClient();
+ this.proxy = new SimpleProxy();
+ this.httpclient.getHostConfiguration().setProxy(
+ this.proxy.getLocalAddress(),
+ this.proxy.getLocalPort());
+
+ SimpleSocketFactory serversocketfactory = null;
+ Protocol testhttp = null;
+ if (this.usessl) {
+ serversocketfactory = new SimpleSSLSocketFactory();
+ testhttp = new Protocol("https",
+ (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
+ } else {
+ serversocketfactory = new SimplePlainSocketFactory();
+ testhttp = Protocol.getProtocol("http");
+ }
+ this.httpserver = new SimpleHttpServer(serversocketfactory, 0);
this.httpclient.getHostConfiguration().setHost(
this.httpserver.getLocalAddress(),
this.httpserver.getLocalPort(),
- Protocol.getProtocol("http"));
- this.httpclient.getHostConfiguration().setProxy(
- this.proxy.getLocalAddress(),
- this.proxy.getLocalPort());
+ testhttp);
}
protected void tearDown() throws Exception {
@@ -88,6 +139,14 @@
super.tearDown();
}
+ public void setUseSSL(boolean b) {
+ this.usessl = b;
+ }
+
+ public boolean isUseSSL() {
+ return this.usessl;
+ }
+
class GetItWrongThenGetItRight implements CredentialsProvider {
private int hostcount = 0;
1.11 +4 -3 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java
Index: ProxyRequestHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ProxyRequestHandler.java 13 Nov 2004 22:38:27 -0000 1.10
+++ ProxyRequestHandler.java 11 Dec 2004 22:35:26 -0000 1.11
@@ -76,6 +76,7 @@
URI uri = new URI(oldreqline.getUri(), true);
SimpleHost host = new SimpleHost(uri.getHost(), uri.getPort());
SimpleHttpServerConnection proxyconn = this.connmanager.openConnection(host);
+ proxyconn.setSocketTimeout(0);
try {
1.15 +25 -11 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
Index: SimpleHttpServer.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- SimpleHttpServer.java 13 Nov 2004 12:21:28 -0000 1.14
+++ SimpleHttpServer.java 11 Dec 2004 22:35:26 -0000 1.15
@@ -63,17 +63,22 @@
* @throws IOException if anything goes wrong during initialization
*/
public SimpleHttpServer() throws IOException {
- this(0);
+ this(null, 0);
}
/**
- * Creates a new HTTP server instance, using the specified TCP port
+ * Creates a new HTTP server instance, using the specified socket
+ * factory and the TCP port
*
* @param port Desired TCP port
* @throws IOException if anything goes wrong during initialization
*/
- public SimpleHttpServer(int port) throws IOException {
- listener = new ServerSocket(port);
+ public SimpleHttpServer(SimpleSocketFactory socketfactory, int port)
+ throws IOException {
+ if (socketfactory == null) {
+ socketfactory = new SimplePlainSocketFactory();
+ }
+ listener = socketfactory.createServerSocket(port);
if(LOG.isDebugEnabled()) {
LOG.debug("Starting test HTTP server on port " + getLocalPort());
}
@@ -83,6 +88,16 @@
t.start();
}
+ /**
+ * Creates a new HTTP server instance, using the specified TCP port
+ *
+ * @param port Desired TCP port
+ * @throws IOException if anything goes wrong during initialization
+ */
+ public SimpleHttpServer(int port) throws IOException {
+ this(null, port);
+ }
+
public String getTestname() {
return this.testname;
}
@@ -133,16 +148,15 @@
/**
* Stops this HTTP server instance.
*/
- public void destroy() {
+ public synchronized void destroy() {
if (stopped) {
return;
}
- stopped = true;
+ this.stopped = true;
if(LOG.isDebugEnabled()) {
LOG.debug("Stopping test HTTP server on port " + getLocalPort());
}
-
tg.interrupt();
if (listener != null) {
@@ -179,7 +193,7 @@
public void run() {
try {
- while (!Thread.interrupted()) {
+ while (!this.stopped && !Thread.interrupted()) {
Socket socket = listener.accept();
try {
if (this.requestHandler == null) {
1.21 +12 -3 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
Index: SimpleHttpServerConnection.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- SimpleHttpServerConnection.java 13 Nov 2004 22:38:27 -0000 1.20
+++ SimpleHttpServerConnection.java 11 Dec 2004 22:35:26 -0000 1.21
@@ -34,6 +34,7 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
+import java.net.SocketException;
import java.util.Iterator;
import org.apache.commons.httpclient.ChunkedOutputStream;
@@ -232,5 +233,13 @@
outsream.flush();
}
+ public int getSocketTimeout() throws SocketException {
+ return this.socket.getSoTimeout();
+ }
+
+ public void setSocketTimeout(int timeout) throws SocketException {
+ this.socket.setSoTimeout(timeout);
+ }
+
}
1.8 +4 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java
Index: SimpleProxy.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SimpleProxy.java 28 Nov 2004 15:44:39 -0000 1.7
+++ SimpleProxy.java 11 Dec 2004 22:35:26 -0000 1.8
@@ -78,8 +78,8 @@
}
public void destroy() {
- this.connmanager.shutdown();
super.destroy();
+ this.connmanager.shutdown();
}
}
1.4 +5 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/StreamProxy.java
Index: StreamProxy.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/StreamProxy.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- StreamProxy.java 22 Feb 2004 18:08:52 -0000 1.3
+++ StreamProxy.java 11 Dec 2004 22:35:26 -0000 1.4
@@ -25,8 +25,6 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
package org.apache.commons.httpclient.server;
@@ -56,6 +54,7 @@
public synchronized void start() {
if (state != 0) throw new IllegalStateException("Can not start again.");
state = 1;
+ pumpThread.setDaemon(true);
pumpThread.start();
}
@@ -89,7 +88,7 @@
private class Pump implements Runnable {
public void run() {
- byte[] buffer = new byte[10000];
+ byte[] buffer = new byte[1024];
try {
while (!Thread.interrupted()) {
int len;
1.7 +82 -39 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java
Index: TransparentProxyRequestHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TransparentProxyRequestHandler.java 27 Feb 2004 19:01:34 -0000 1.6
+++ TransparentProxyRequestHandler.java 11 Dec 2004 22:35:26 -0000 1.7
@@ -25,23 +25,19 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
package org.apache.commons.httpclient.server;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InterruptedIOException;
import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
import java.net.Socket;
import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpURL;
-import org.apache.commons.httpclient.URI;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.HttpVersion;
/**
* This request handler can handle the CONNECT method. It does nothing for any
@@ -60,44 +56,91 @@
final SimpleHttpServerConnection conn,
final SimpleRequest request) throws IOException
{
+
RequestLine line = request.getRequestLine();
String method = line.getMethod();
- if (!"CONNECT".equalsIgnoreCase(method))
+ if (!"CONNECT".equalsIgnoreCase(method)) {
return false;
- URI url = new HttpURL(line.getUri());
- handshake(conn, url);
+ }
+ Socket targetSocket = null;
+ try {
+ targetSocket = connect(line.getUri());
+ } catch (IOException e) {
+ SimpleResponse response = new SimpleResponse();
+ response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND);
+ response.setHeader(new Header("Server", "test proxy"));
+ response.setBodyString("Cannot connect to " + line.getUri());
+ conn.writeResponse(response);
+ return true;
+ }
+ SimpleResponse response = new SimpleResponse();
+ response.setHeader(new Header("Server", "test proxy"));
+ response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "Connection established");
+ conn.writeResponse(response);
+
+ SimpleHttpServerConnection target = new SimpleHttpServerConnection(targetSocket);
+ pump(conn, target);
return true;
}
- private void handshake(SimpleHttpServerConnection conn, URI url) throws IOException {
- Socket targetSocket = new Socket(url.getHost(), url.getPort());
- InputStream sourceIn = conn.getInputStream();
- OutputStream sourceOut = conn.getOutputStream();
- InputStream targetIn = targetSocket.getInputStream();
- OutputStream targetOut = targetSocket.getOutputStream();
-
- ResponseWriter out = conn.getWriter();
- out.println("HTTP/1.1 200 Connection established");
- out.flush();
+ private void pump(final SimpleHttpServerConnection source, final SimpleHttpServerConnection target)
+ throws IOException {
- BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut, targetIn, targetOut);
- bdsp.start();
- try {
- bdsp.block();
- } catch (InterruptedException e) {
- throw new IOException(e.toString());
- }
- }
+ source.setSocketTimeout(100);
+ target.setSocketTimeout(100);
- private void sendHeaders(Header[] headers, OutputStream os) throws IOException {
- Writer out;
- try {
- out = new OutputStreamWriter(os, "US-ASCII");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e.toString());
+ InputStream sourceIn = source.getInputStream();
+ OutputStream sourceOut = source.getOutputStream();
+ InputStream targetIn = target.getInputStream();
+ OutputStream targetOut = target.getOutputStream();
+
+ byte[] tmp = new byte[1024];
+ int l;
+ for (;;) {
+ if (!source.isOpen() || !target.isOpen()) {
+ break;
+ }
+ try {
+ l = sourceIn.read(tmp);
+ if (l == -1) {
+ break;
+ }
+ targetOut.write(tmp, 0, l);
+ } catch (InterruptedIOException ignore) {
+ if (Thread.interrupted()) {
+ break;
+ }
+ }
+ try {
+ l = targetIn.read(tmp);
+ if (l == -1) {
+ break;
+ }
+ sourceOut.write(tmp, 0, l);
+ } catch (InterruptedIOException ignore) {
+ if (Thread.interrupted()) {
+ break;
+ }
+ }
}
- for (int i = 0; i < headers.length; i++) {
- out.write(headers[i].toExternalForm());
+ }
+
+ private static Socket connect(final String host) throws IOException {
+ String hostname = null;
+ int port;
+ int i = host.indexOf(':');
+ if (i != -1) {
+ hostname = host.substring(0, i);
+ try {
+ port = Integer.parseInt(host.substring(i + 1));
+ } catch (NumberFormatException ex) {
+ throw new IOException("Invalid host address: " + host);
+ }
+ } else {
+ hostname = host;
+ port = 80;
}
+ return new Socket(hostname, port);
}
+
}
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimplePlainSocketFactory.java
Index: SimplePlainSocketFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimplePlainSocketFactory.java,v 1.1 2004/12/11 22:35:26 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/12/11 22:35:26 $
*
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.server;
import java.io.IOException;
import java.net.ServerSocket;
/**
* Defines a plain socket factory
*
* @author Oleg Kalnichevski
*/
public class SimplePlainSocketFactory implements SimpleSocketFactory {
public SimplePlainSocketFactory() {
super();
}
public ServerSocket createServerSocket(int port) throws IOException {
return new ServerSocket(port);
}
}
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleSocketFactory.java
Index: SimpleSocketFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleSocketFactory.java,v 1.1 2004/12/11 22:35:26 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/12/11 22:35:26 $
*
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.server;
import java.io.IOException;
import java.net.ServerSocket;
/**
* Defines a socket factory interface
*
* @author Oleg Kalnichevski
*/
public interface SimpleSocketFactory {
ServerSocket createServerSocket(int port) throws IOException;
}
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ssl/SimpleSSLSocketFactory.java
Index: SimpleSSLSocketFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ssl/SimpleSSLSocketFactory.java,v 1.1 2004/12/11 22:35:26 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/12/11 22:35:26 $
*
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.ssl;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import java.security.KeyStore;
import javax.net.ServerSocketFactory;
import org.apache.commons.httpclient.server.SimpleSocketFactory;
import com.sun.net.ssl.KeyManager;
import com.sun.net.ssl.KeyManagerFactory;
import com.sun.net.ssl.SSLContext;
/**
* Defines a SSL socket factory
*
* @author Oleg Kalnichevski
*/
public class SimpleSSLSocketFactory implements SimpleSocketFactory {
private static SSLContext SSLCONTEXT = null;
private static SSLContext createSSLContext() {
try {
ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
KeyStore keystore = KeyStore.getInstance("jks");
keystore.load(url.openStream(), "nopassword".toCharArray());
KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(keystore, "nopassword".toCharArray());
KeyManager[] keymanagers = kmfactory.getKeyManagers();
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(keymanagers, null, null);
return sslcontext;
} catch (Exception ex) {
// this is not the way a sane exception handling should be done
// but for our simple HTTP testing framework this will suffice
throw new IllegalStateException(ex.getMessage());
}
}
private static SSLContext getSSLContext() {
if (SSLCONTEXT == null) {
SSLCONTEXT = createSSLContext();
}
return SSLCONTEXT;
}
public SimpleSSLSocketFactory() {
super();
}
public ServerSocket createServerSocket(int port) throws IOException {
ServerSocketFactory socketfactory = getSSLContext().getServerSocketFactory();
return socketfactory.createServerSocket(port);
}
}
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ssl/SimpleSSLTestProtocolSocketFactory.java
Index: SimpleSSLTestProtocolSocketFactory.java
===================================================================
/*
* $Header: /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 $
* $Revision: 1.1 $
* $Date: 2004/12/11 22:35:26 $
*
* ====================================================================
*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.ssl;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyStore;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import org.apache.commons.httpclient.server.SimpleSocketFactory;
import com.sun.net.ssl.SSLContext;
import com.sun.net.ssl.TrustManager;
import com.sun.net.ssl.TrustManagerFactory;
public class SimpleSSLTestProtocolSocketFactory implements SecureProtocolSocketFactory {
private static SSLContext SSLCONTEXT = null;
private static SSLContext createSSLContext() {
try {
ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
KeyStore keystore = KeyStore.getInstance("jks");
keystore.load(url.openStream(), "nopassword".toCharArray());
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init(keystore);
TrustManager[] trustmanagers = tmfactory.getTrustManagers();
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, trustmanagers, null);
return sslcontext;
} catch (Exception ex) {
// this is not the way a sane exception handling should be done
// but for our simple HTTP testing framework this will suffice
throw new IllegalStateException(ex.getMessage());
}
}
private static SSLContext getSSLContext() {
if (SSLCONTEXT == null) {
SSLCONTEXT = createSSLContext();
}
return SSLCONTEXT;
}
public SimpleSSLTestProtocolSocketFactory() {
super();
}
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("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
if (timeout == 0) {
return createSocket(host, port, localAddress, localPort);
} else {
// To be eventually deprecated when migrated to Java 1.4 or above
return ControllerThreadSocketFactory.createSocket(
this, host, port, localAddress, localPort, timeout);
}
}
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(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
);
}
}
1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/ssl/simpleserver.keystore
<<Binary file>>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org