/*
 * Copyright (c) 2002-2009 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 *  the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util.net.ssl;

import com.jscape.util.SuppressObfuscation;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
@SuppressObfuscation
public class NullTrustSocketFactory
        extends SocketFactory {

    private final SSLSocketFactory base;

    public static SocketFactory getDefault() {
        try {
            SSLContext sslContext = SslContextFactory.systemContext();
            SSLSocketFactory base = sslContext.getSocketFactory();
            return new NullTrustSocketFactory(base);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public NullTrustSocketFactory()
            throws Exception {
        this(SslContextFactory.systemContext().getSocketFactory());
    }

    public NullTrustSocketFactory(SSLSocketFactory base) {
        assertNotNull(base);
        this.base = base;
    }

    @Override
    public Socket createSocket()
            throws IOException {
        return this.base.createSocket();
    }

    public Socket createSocket(String host, int port)
            throws IOException {
        return this.base.createSocket(host, port);
    }

    public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
            throws IOException {
        return this.base.createSocket(host, port, localHost, localPort);
    }

    public Socket createSocket(InetAddress host, int port)
            throws IOException {
        return this.base.createSocket(host, port);
    }

    public Socket createSocket(InetAddress host, int port, InetAddress localHost, int localPort)
            throws IOException {
        return this.base.createSocket(host, port, localHost, localPort);
    }


}
