Example usage for javax.net.ssl SSLSocket connect

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

Introduction

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

Prototype

public void connect(SocketAddress endpoint, int timeout) throws IOException 

Source Link

Document

Connects this socket to the server with a specified timeout value.

Usage

From source file:com.alphabetbloc.accessmrs.utilities.MySSLSocketFactory.java

@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    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.");
    }

    if (App.DEBUG)
        Log.e(TAG + "delete", "ConnectSocket with " + "\n\t host=" + host + "\n\t port=" + port
                + "\n\t localport=" + localPort);

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

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

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

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

    InetSocketAddress remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    try {
        hostnameVerifier.verify(host, sslsock);
    } catch (IOException iox) {
        try {
            sslsock.close();
        } catch (Exception x) {
        }

        throw iox;
    }

    return sslsock;
}

From source file:org.akita.io._FakeSSLSocketFactory.java

@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from  www  . ja  v  a2  s.c om*/
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

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

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

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

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

    InetSocketAddress remoteAddress;
    remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    return sslsock;
}

From source file:ti.modules.titanium.network.NonValidatingSSLSocketFactory.java

@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from  w w  w .  ja v  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 = new InetSocketAddress(host, port);
    sslsock.connect(remoteAddress, connTimeout);
    sslsock.setSoTimeout(soTimeout);

    return sslsock;
}

From source file:org.thoughtcrime.ssl.pinning.PinningSSLSocketFactory.java

@Override
public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {
    final SSLSocket sslSock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
        if (localPort < 0) {
            localPort = 0;//  w w w  .  ja  v  a2  s .c om
        }

        sslSock.bind(new InetSocketAddress(localAddress, localPort));
    }

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

    final InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
    sslSock.connect(remoteAddress, connTimeout);
    sslSock.setSoTimeout(soTimeout);

    try {
        SSLSocketFactory.STRICT_HOSTNAME_VERIFIER.verify(host, sslSock);
    } catch (IOException iox) {
        try {
            sslSock.close();
        } catch (Exception ignored) {
        }
        throw iox;
    }

    return sslSock;
}

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

/**
 * @since 4.1//from  w  ww .  java 2 s. co m
 */
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:org.gege.caldavsyncadapter.caldav.EasySSLSocketFactory.java

@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from w  ww .j a v  a 2s  .  c  om*/
    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;
    remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    return sslsock;
}

From source file:org.apache.commons.httpclient.contrib.ssl.StrictSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>//from  ww  w.j av a2 s  .  c om
 * This method employs several techniques to circumvent the limitations of older JREs that 
 * do not support connect timeout. When running in JRE 1.4 or above reflection is used to 
 * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older 
 * JREs a controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the timeout 
 * expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = HttpConnectionParams.getConnectionTimeout(params);
    SSLSocket socket = null;

    SSLSocketFactory socketfactory = SSLSocketFactory.getSystemSocketFactory();
    if (timeout == 0) {
        socket = (SSLSocket) socketfactory.createSocket(params);
    } else {
        socket = (SSLSocket) socketfactory.createSocket(params);
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }
    verifyHostname(socket);
    return socket;
}

From source file:net.vexelon.myglob.utils.TrustAllSocketFactory.java

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

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from w  w  w.  ja  v  a2 s.c om*/
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    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);
    }

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

    InetSocketAddress remoteAddress;
    remoteAddress = new InetSocketAddress(host, port);

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

    return sslSocket;
}

From source file:cn.org.eshow.framwork.http.ssl.AuthSSLProtocolSocketFactory.java

@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
        // we need to bind explicitly
        if (localPort < 0) {
            localPort = 0; // indicates "any"
        }//from   w w w .j  a va2s  . com
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

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

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

/**
 * @since 4.1//from   www . j a  v a 2 s  . co 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);
    }

    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;
}