diff options
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/util/EasySSLSocketFactory.java')
-rwxr-xr-x | src/main/java/io/trygvis/esper/testing/util/EasySSLSocketFactory.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/util/EasySSLSocketFactory.java b/src/main/java/io/trygvis/esper/testing/util/EasySSLSocketFactory.java new file mode 100755 index 0000000..c1f1a7e --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/EasySSLSocketFactory.java @@ -0,0 +1,104 @@ +package io.trygvis.esper.testing.util; + +/* + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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/>. + * + */ + +import java.io.*; +import java.net.*; +import javax.net.ssl.*; +import org.apache.http.conn.*; +import org.apache.http.conn.scheme.*; +import org.apache.http.params.*; + +public class EasySSLSocketFactory implements SocketFactory, LayeredSocketFactory { + + private SSLContext sslcontext = null; + + private static SSLContext createEasySSLContext() throws IOException { + try { + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, new TrustManager[]{new EasyX509TrustManager(null)}, null); + return context; + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + private SSLContext getSSLContext() throws IOException { + if (this.sslcontext == null) { + this.sslcontext = createEasySSLContext(); + } + return this.sslcontext; + } + + public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, + HttpParams params) throws IOException, ConnectTimeoutException { + int connTimeout = HttpConnectionParams.getConnectionTimeout(params); + int soTimeout = HttpConnectionParams.getSoTimeout(params); + InetSocketAddress remoteAddress = new InetSocketAddress(host, port); + SSLSocket sslSocket = (SSLSocket) ((sock != null) ? sock : createSocket()); + + if ((localAddress != null) || (localPort > 0)) { + // we need to bind explicitly + if (localPort < 0) { + localPort = 0; // indicates "any" + } + InetSocketAddress isa = new InetSocketAddress(localAddress, localPort); + sslSocket.bind(isa); + } + + sslSocket.connect(remoteAddress, connTimeout); + sslSocket.setSoTimeout(soTimeout); + + return sslSocket; + } + + public Socket createSocket() throws IOException { + return getSSLContext().getSocketFactory().createSocket(); + } + + public boolean isSecure(Socket socket) throws IllegalArgumentException { + return true; + } + + public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { + return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose); + } + + // ------------------------------------------------------------------- + // javadoc in org.apache.http.conn.scheme.SocketFactory says : + // Both Object.equals() and Object.hashCode() must be overridden + // for the correct operation of some connection managers + // ------------------------------------------------------------------- + + public boolean equals(Object obj) { + return ((obj != null) && obj.getClass().equals(EasySSLSocketFactory.class)); + } + + public int hashCode() { + return EasySSLSocketFactory.class.hashCode(); + } +} |