Example usage for javax.net.ssl SSLSocket close

List of usage examples for javax.net.ssl SSLSocket close

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket close.

Prototype

public synchronized void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:orca.ektorp.client.ContextualSSLSocketFactory.java

/**
 * @since 4.1//from w  w  w  .j a  v  a  2s .com
 * @param socket socket
 * @param remoteAddress remote address
 * @param localAddress local address
 * @param params HTTP params
 * @throws IOException in case of IO error
 * @throws UnknownHostException in case of unknonwn host
 * @throws ConnectTimeoutException in case of connect timeout
 * @return returns the socket
 */
public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (remoteAddress == null) {
        throw new IllegalArgumentException("Remote address may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    //Here!
    Socket sock = socket != null ? socket : this.socketfactory.createSocket();
    if (localAddress != null) {
        sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sock.bind(localAddress);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        sock.setSoTimeout(soTimeout);
        sock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }

    String hostname;
    if (remoteAddress instanceof HttpInetSocketAddress) {
        hostname = ((HttpInetSocketAddress) remoteAddress).getHttpHost().getHostName();
    } else {
        hostname = remoteAddress.getHostName();
    }

    SSLSocket sslsock;
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        sslsock = (SSLSocket) sock;
    } else {
        int port = remoteAddress.getPort();
        sslsock = (SSLSocket) this.socketfactory.createSocket(sock, hostname, port, true);
        prepareSocket(sslsock);
    }
    if (this.hostnameVerifier != null) {
        try {
            this.hostnameVerifier.verify(hostname, sslsock);
            // verifyHostName() didn't blowup - good!
        } catch (IOException iox) {
            // close the socket before re-throwing the exception
            try {
                sslsock.close();
            } catch (Exception x) {
                /*ignore*/ }
            throw iox;
        }
    }
    return sslsock;
}

From source file:com.budrotech.jukebox.service.ssl.SSLSocketFactory.java

/**
 * @since 4.1/*from w w w  .  j a va  2 s.com*/
 */
public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params) throws IOException {
    if (remoteAddress == null) {
        throw new IllegalArgumentException("Remote address may not be null");
    }

    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }

    SSLSocket sslSocket = (SSLSocket) (sock != null ? sock : createSocket());

    if (localAddress != null) {
        //            sslSocket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sslSocket.bind(localAddress);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        sslSocket.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException(String.format("Connect to %s/%s timed out",
                remoteAddress.getHostName(), remoteAddress.getAddress()));
    }

    sslSocket.setSoTimeout(soTimeout);

    if (this.hostnameVerifier != null) {
        try {
            this.hostnameVerifier.verify(remoteAddress.getHostName(), sslSocket);
            // verifyHostName() didn't blowup - good!
        } catch (IOException iox) {
            // close the socket before re-throwing the exception
            try {
                sslSocket.close();
            } catch (Exception x) {
                /*ignore*/ }
            throw iox;
        }
    }

    return sslSocket;
}

From source file:info.guardianproject.net.http.ModSSLSocketFactory.java

public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*  ww  w  .j ava  2 s. c  o  m*/
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    //Socket underlying = (Socket)
    //    ((sock != null) ? sock : createSocket());
    //Socket underlying = sock;
    //if (underlying == null) underlying = new Socket(); 

    //  Socket underlying = SocksSocketFactory.getSocketFactory("localhost", 9050).connectSocket(sock, host, port, localAddress, localPort, params);

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    Socket underlying = null;

    if (proxy != null) {
        //underlying = new Socket(proxy);
        //   InetSocketAddress sAddress = InetSocketAddress.createUnresolved(host,port);
        //    InetSocketAddress sAddress = new InetSocketAddress(host,port);
        //underlying.connect(sAddress,Socks soTimeout);

        SocksSocketFactory ssf = SocksSocketFactory.getSocketFactory(proxyAddr.getHostName(),
                proxyAddr.getPort());
        underlying = ssf.createSocket(null, null, -1, params);
        //   underlying.connect(sAddress);
    }

    SSLSocket sslsock = (SSLSocket) this.socketfactory.createSocket(underlying, host, port, true);

    if ((localAddress != null) || (localPort > 0)) {

        // we need to bind explicitly
        if (localPort < 0)
            localPort = 0; // indicates "any"

        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }

    sslsock.connect(remoteAddress, connTimeout);
    sslsock.setSoTimeout(soTimeout);

    try {

        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:com.android.beyondemail.SSLSocketFactory.java

public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }//from www.jav  a  2s . com
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (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);
        sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (nameResolver != null) {
        remoteAddress = new InetSocketAddress(nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);
    try {
        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:com.chen.emailcommon.utility.SSLSocketFactory.java

@Override
public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }//from w  ww  .j  av  a 2  s.co  m
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (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);
        sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (nameResolver != null) {
        remoteAddress = new InetSocketAddress(nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);
    try {
        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:com.android.exchange.SSLSocketFactory.java

public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }// w  ww  .  j a  va 2s.c  o  m
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (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);
        sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);
    try {
        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:com.thejoshwa.ultrasonic.androidapp.service.ssl.SSLSocketFactory.java

/**
 * @since 4.1//from   w  w  w  .j  a v a  2 s  . c om
 */
public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (remoteAddress == null) {
        throw new IllegalArgumentException("Remote address may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    SSLSocket sslsock = (SSLSocket) (sock != null ? sock : createSocket());
    if (localAddress != null) {
        //            sslsock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sslsock.bind(localAddress);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        sslsock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException(
                "Connect to " + remoteAddress.getHostName() + "/" + remoteAddress.getAddress() + " timed out");
    }
    sslsock.setSoTimeout(soTimeout);
    if (this.hostnameVerifier != null) {
        try {
            this.hostnameVerifier.verify(remoteAddress.getHostName(), sslsock);
            // verifyHostName() didn't blowup - good!
        } catch (IOException iox) {
            // close the socket before re-throwing the exception
            try {
                sslsock.close();
            } catch (Exception x) {
                /*ignore*/ }
            throw iox;
        }
    }
    return sslsock;
}

From source file:com.example.mp_master.helper.UntrustedSSLSocketFactory.java

public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from w  ww . jav a2 s  . c o  m*/
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (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);
        sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }
    try {
        sslsock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }
    sslsock.setSoTimeout(soTimeout);
    try {
        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:github.popeen.dsub.service.ssl.SSLSocketFactory.java

/**
 * @since 4.1/*  w  w w  .  j a  va  2s. c o m*/
 */
public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (remoteAddress == null) {
        throw new IllegalArgumentException("Remote address may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    SSLSocket sslsock = (SSLSocket) (sock != null ? sock : createSocket());
    if (localAddress != null) {
        //            sslsock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sslsock.bind(localAddress);
    }

    setHostName(sslsock, remoteAddress.getHostName());
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        sslsock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException(
                "Connect to " + remoteAddress.getHostName() + "/" + remoteAddress.getAddress() + " timed out");
    }
    sslsock.setSoTimeout(soTimeout);
    if (this.hostnameVerifier != null) {
        try {
            this.hostnameVerifier.verify(remoteAddress.getHostName(), sslsock);
            // verifyHostName() didn't blowup - good!
        } catch (IOException iox) {
            // close the socket before re-throwing the exception
            try {
                sslsock.close();
            } catch (Exception x) {
                /*ignore*/ }
            throw iox;
        }
    }
    return sslsock;
}

From source file:org.globus.myproxy.MyProxy.java

/**
 * Bootstraps trustroot information from the MyProxy server.
 *
 * @exception MyProxyException//w  w w  .  j  av  a  2 s.  co m
 *         If an error occurred during the operation.
 */
public void bootstrapTrust() throws MyProxyException {
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        MyTrustManager myTrustManager = new MyTrustManager();
        TrustManager[] trustAllCerts = new TrustManager[] { myTrustManager };
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory sf = sc.getSocketFactory();
        SSLSocket socket = (SSLSocket) sf.createSocket(this.host, this.port);
        socket.setEnabledProtocols(new String[] { "SSLv3" });
        socket.startHandshake();
        socket.close();

        X509Certificate[] acceptedIssuers = myTrustManager.getAcceptedIssuers();
        if (acceptedIssuers == null) {
            throw new MyProxyException("Failed to determine MyProxy server trust roots in bootstrapTrust.");
        }
        for (int idx = 0; idx < acceptedIssuers.length; idx++) {
            File x509Dir = new File(org.globus.myproxy.MyProxy.getTrustRootPath());
            if (!x509Dir.exists()) {
                StringBuffer newSubject = new StringBuffer();
                String[] subjArr = acceptedIssuers[idx].getSubjectDN().getName().split(", ");
                for (int i = (subjArr.length - 1); i > -1; i--) {
                    newSubject.append("/");
                    newSubject.append(subjArr[i]);
                }
                String subject = newSubject.toString();

                File tmpDir = new File(getTrustRootPath() + "-" + System.currentTimeMillis());
                if (tmpDir.mkdir() == true) {
                    String hash = opensslHash(acceptedIssuers[idx]);
                    String filename = tmpDir.getPath() + tmpDir.separator + hash + ".0";

                    FileOutputStream os = new FileOutputStream(new File(filename));
                    CertificateIOUtil.writeCertificate(os, acceptedIssuers[idx]);

                    os.close();
                    if (logger.isDebugEnabled()) {
                        logger.debug("wrote trusted certificate to " + filename);
                    }

                    filename = tmpDir.getPath() + tmpDir.separator + hash + ".signing_policy";

                    os = new FileOutputStream(new File(filename));
                    Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8"));
                    wr.write("access_id_CA X509 '");
                    wr.write(subject);
                    wr.write("'\npos_rights globus CA:sign\ncond_subjects globus \"*\"\n");
                    wr.flush();
                    wr.close();
                    os.close();

                    if (logger.isDebugEnabled()) {
                        logger.debug("wrote trusted certificate policy to " + filename);
                    }

                    // success.  commit the bootstrapped directory.
                    if (tmpDir.renameTo(x509Dir) == true) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("renamed " + tmpDir.getPath() + " to " + x509Dir.getPath());
                        }
                    } else {
                        throw new MyProxyException(
                                "Unable to rename " + tmpDir.getPath() + " to " + x509Dir.getPath());
                    }
                } else {
                    throw new MyProxyException("Cannot create temporary directory: " + tmpDir.getName());
                }
            }
        }
    } catch (Exception e) {
        throw new MyProxyException("MyProxy bootstrapTrust failed.", e);
    }
}