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.eviware.soapui.impl.wsdl.support.http.SoapUISSLSocketFactory.java

/**
 * @since 4.1// w  ww  .j  a  v a 2 s.  co m
 */
@Override
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");
    }
    Socket sock = socket != null ? socket : new Socket();
    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.getHostName() + "/" + remoteAddress.getAddress() + " timed out");
    }
    SSLSocket sslsock;
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        sslsock = (SSLSocket) sock;
    } else {
        sslsock = (SSLSocket) getSocketFactory().createSocket(sock, remoteAddress.getHostName(),
                remoteAddress.getPort(), true);
        sslsock = enableSocket(sslsock);
    }
    // do we need it? trust all hosts
    //      if( getHostnameVerifier() != null )
    //      {
    //         try
    //         {
    //            getHostnameVerifier().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.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 w ww  .  ja v a2  s .  c o m*/
 * 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:ucar.httpservices.CustomSSLProtocolSocketFactory.java

public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException {
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    SSLSocket sslsocket = (SSLSocket) ((sock != null) ? sock : createSocket(params));

    if (localAddress != null) {
        sslsocket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sslsocket.bind(localAddress);/*from ww w  . j a  va  2s.co m*/
    }

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

From source file:app.android.auto.net.sampleapp.oauth.blackwork.EasyHttpClient.java

/**
 * @see SocketFactory#connectSocket(Socket,
 *      String, int, InetAddress, int,//w  w w .ja va  2s. c  o  m
 *      HttpParams)
 */
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"
        }
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

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

From source file:groovyx.net.http.AsyncHTTPBuilder.java

/**
 * Get the timeout in for establishing an HTTP connection.
 * @return timeout in milliseconds.//  www.j av  a 2 s.  c o  m
 */
public int getTimeout() {
    return HttpConnectionParams.getConnectionTimeout(super.getClient().getParams());
}

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

/**
 * @since 4.1/*from w w w .j  a  va  2 s  .  c  o 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:com.dmbstream.android.service.RESTMusicService.java

private void increaseTimeouts(HttpParams requestParams) {
    if (requestParams != null) {
        int connectTimeout = HttpConnectionParams.getConnectionTimeout(requestParams);
        if (connectTimeout != 0) {
            HttpConnectionParams.setConnectionTimeout(requestParams, (int) (connectTimeout * 1.3F));
        }//from   w ww.  j  ava  2  s .c  o  m
        int readTimeout = HttpConnectionParams.getSoTimeout(requestParams);
        if (readTimeout != 0) {
            HttpConnectionParams.setSoTimeout(requestParams, (int) (readTimeout * 1.5F));
        }
    }
}

From source file:com.mgmtp.perfload.core.client.web.ssl.LtSSLSocketFactory.java

@Override
public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params) throws IOException {
    checkArgument(remoteAddress != null, "Remote address may not be null");
    checkArgument(params != null, "HTTP parameters may not be null");

    Socket socket = sock != null ? sock : new Socket();
    if (localAddress != null) {
        socket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        socket.bind(localAddress);//from   w  w  w .  java  2s .c  om
    }

    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    socket.connect(remoteAddress, HttpConnectionParams.getConnectionTimeout(params));

    if (socket instanceof SSLSocket) {
        return socket;
    }

    return getSSLContext().getSocketFactory().createSocket(socket, remoteAddress.getHostName(),
            remoteAddress.getPort(), true);
}

From source file:pl.openrnd.connection.rest.ConnectionHandler.java

private HttpResponse execute(HttpUriRequest request, Integer connectionTimeout, Integer readTimeout)
        throws ClientProtocolException, IOException {
    HttpResponse response = null;/*from  ww w  . jav  a2  s  . c  om*/
    HttpClient httpClient = getHttpClient();
    HttpParams params = httpClient.getParams();

    Integer connectionTimeoutOriginal = null;
    Integer readTimeoutOriginal = null;

    if (connectionTimeout != null) {
        connectionTimeoutOriginal = HttpConnectionParams.getConnectionTimeout(params);
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    }

    if (readTimeout != null) {
        readTimeoutOriginal = HttpConnectionParams.getSoTimeout(params);
        HttpConnectionParams.setSoTimeout(params, readTimeout);
    }

    try {
        if (mConnectionConfig.isUsingCookies()) {
            createCookieIfNotSet();
            response = httpClient.execute(request, getHttpContext());
        } else {
            response = httpClient.execute(request);
        }
    } finally {
        if (connectionTimeoutOriginal != null) {
            HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        }

        if (readTimeoutOriginal != null) {
            HttpConnectionParams.setSoTimeout(params, readTimeoutOriginal);
        }
    }

    return response;
}

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  w  ww.ja  va  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 (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;
}