securitytools.common.http.TrustingSSLConnectionSocketFactory.java Source code

Java tutorial

Introduction

Here is the source code for securitytools.common.http.TrustingSSLConnectionSocketFactory.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 Security Tools SDK for Java.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package securitytools.common.http;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;

/**
 *
 *
 * @author Adam Parsons
 * @version 0.0.1, 05/13/14
 * @since 1.0.0
 */
public final class TrustingSSLConnectionSocketFactory
        implements ConnectionSocketFactory, LayeredConnectionSocketFactory {

    private SSLContext sslContext = null;

    @Override
    public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress,
            InetSocketAddress localAddress, HttpContext context) throws IOException {
        if (sock == null) {
            sock = createSocket(context);
        }
        SSLSocket sslSocket = null;
        if (sock instanceof SSLSocket) {
            sslSocket = (SSLSocket) sock;
        } else {
            throw new AssertionError("Unexpected type: " + sock);
        }

        if (localAddress != null) {
            sslSocket.bind(localAddress);
        }

        sslSocket.connect(remoteAddress, connectTimeout);
        return sslSocket;
    }

    @Override
    public Socket createLayeredSocket(Socket socket, String host, int port, HttpContext context)
            throws IOException, UnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(socket, host, port, true);
    }

    @Override
    public Socket createSocket(HttpContext context) throws IOException {
        return getSSLContext().getSocketFactory().createSocket();
    }

    private SSLContext getSSLContext() throws IOException {
        if (sslContext == null) {
            try {
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, new TrustManager[] { new TrustingX509TrustManager() }, null);
            } catch (NoSuchAlgorithmException nsae) {
                throw new IOException(nsae.getMessage(), nsae);
            } catch (KeyManagementException kme) {
                throw new IOException(kme.getMessage(), kme);
            }
        }
        return sslContext;
    }

}