Example usage for org.apache.http.params HttpConnectionParams getConnectionTimeout

List of usage examples for org.apache.http.params HttpConnectionParams getConnectionTimeout

Introduction

In this page you can find the example usage for org.apache.http.params HttpConnectionParams getConnectionTimeout.

Prototype

public static int getConnectionTimeout(HttpParams httpParams) 

Source Link

Usage

From source file:com.example.xiadan.core.EasySSLSocketFactory.java

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 .  jav  a  2 s . c om*/
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

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

}

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 av a  2  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;
    remoteAddress = new InetSocketAddress(host, port);

    sslsock.connect(remoteAddress, connTimeout);

    sslsock.setSoTimeout(soTimeout);

    return sslsock;
}

From source file:org.pluroid.pluroium.HttpClientFactory.java

public Socket connectSocket(Socket socket, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (socket == null) {
        socket = createSocket();/*from   w  ww. j av  a2s.co  m*/
    }
    if ((localAddress != null) || (localPort > 0)) {
        if (localPort < 0) {
            localPort = 0; // indicates "any"
        }
        socket.bind(new InetSocketAddress(localAddress, localPort));
    }
    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    socket.connect(new InetSocketAddress(host, port), HttpConnectionParams.getConnectionTimeout(params));
    return socket;
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient4.ECFHttpClientSecureProtocolSocketFactory.java

private void performConnection(final Socket socket, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params) throws SocketException, IOException {
    try {/*  www .  j ava 2  s  . co  m*/
        socket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        socket.bind(localAddress);

        int connectionTimeout = HttpConnectionParams.getConnectionTimeout(params);
        int socketTimeout = HttpConnectionParams.getSoTimeout(params);

        socket.connect(remoteAddress, connectionTimeout);
        socket.setSoTimeout(socketTimeout);
    } catch (SocketException e) {
        Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING,
                ECFHttpClientSecureProtocolSocketFactory.class, "performConnection", e); //$NON-NLS-1$
        fireEvent(this.socketConnectListener, new SocketClosedEvent(source, socket, socket));
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING,
                ECFHttpClientSecureProtocolSocketFactory.class, "performConnection", e); //$NON-NLS-1$
        throw e;
    } catch (IOException e) {
        Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING,
                ECFHttpClientSecureProtocolSocketFactory.class, "performConnection", e); //$NON-NLS-1$
        fireEvent(this.socketConnectListener, new SocketClosedEvent(source, socket, socket));
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING,
                ECFHttpClientSecureProtocolSocketFactory.class, "performConnection", e); //$NON-NLS-1$
        throw e;
    }
}

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;//ww  w. j a  va 2 s. com
        }

        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.sos.VirtualFileSystem.HTTP.EasySSLSocketFactory.java

@Override
public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);
    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket(params));
    if (localAddress != null) {
        // we need to bind explicitly 
        sslsock.bind(localAddress);//from  w  w  w.j  ava  2 s  .co m
    }

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

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.");
    }// ww w .  j  av a 2 s  .  c o m
    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: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.");
    }//  w  ww .  j a v a 2  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:org.apache.chemistry.opencmis.client.bindings.spi.http.ApacheClientHttpInvoker.java

/**
 * Builds a SSL Socket Factory for the Apache HTTP Client.
 *///from   ww  w  .  j a  v  a2  s. c o  m
private SchemeLayeredSocketFactory getSSLSocketFactory(final UrlBuilder url, final BindingSession session) {
    // get authentication provider
    AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);

    // check SSL Socket Factory
    final SSLSocketFactory sf = authProvider.getSSLSocketFactory();
    if (sf == null) {
        // no custom factory -> return default factory
        return org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
    }

    // check hostame verifier and use default if not set
    final HostnameVerifier hv = (authProvider.getHostnameVerifier() == null
            ? new BrowserCompatHostnameVerifier()
            : authProvider.getHostnameVerifier());

    if (hv instanceof X509HostnameVerifier) {
        return new org.apache.http.conn.ssl.SSLSocketFactory(sf, (X509HostnameVerifier) hv);
    }

    // build new socket factory
    return new SchemeLayeredSocketFactory() {

        @Override
        public boolean isSecure(Socket sock) {
            return true;
        }

        @Override
        public Socket createSocket(HttpParams params) throws IOException {
            return sf.createSocket();
        }

        @Override
        public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress,
                final InetSocketAddress localAddress, final HttpParams params) throws IOException {

            Socket sock = socket != null ? socket : createSocket(params);
            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) {
                closeSocket(sock);
                throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out!");
            }

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

            SSLSocket sslSocket;
            if (sock instanceof SSLSocket) {
                sslSocket = (SSLSocket) sock;
            } else {
                int port = remoteAddress.getPort();
                sslSocket = (SSLSocket) sf.createSocket(sock, host, port, true);
            }
            verify(hv, host, sslSocket);

            return sslSocket;
        }

        @Override
        public Socket createLayeredSocket(final Socket socket, final String host, final int port,
                final HttpParams params) throws IOException {
            SSLSocket sslSocket = (SSLSocket) sf.createSocket(socket, host, port, true);
            verify(hv, host, sslSocket);

            return sslSocket;
        }
    };
}

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

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>// w w  w.j  a  v  a 2 s  .c  o  m
 * To circumvent the limitations of older JREs that do not support connect timeout 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);
    ;
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}