Example usage for org.apache.commons.httpclient.params HttpConnectionParams getBooleanParameter

List of usage examples for org.apache.commons.httpclient.params HttpConnectionParams getBooleanParameter

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpConnectionParams getBooleanParameter.

Prototype

public boolean getBooleanParameter(String paramString, boolean paramBoolean) 

Source Link

Usage

From source file:org.lockss.util.urlconn.LockssDefaultProtocolSocketFactory.java

/**
 * This is the only factory method that handles params, thus the only one
 * we need to override./*from  w w  w.j  a v a2s  .c  o m*/
 * @param host the host name/IP
 * @param port the port on the host
 * @param localAddress the local host name/IP to bind the socket to
 * @param localPort the local port to bing the socket to
 * @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
 * @throws ConnectTimeoutException if socket cannot be connected within
 * the given time limit
 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    Socket sock = new Socket();
    sock.bind(new InetSocketAddress(localAddress, localPort));
    sock.setKeepAlive(params.getBooleanParameter(HttpClientUrlConnection.SO_KEEPALIVE, false));
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        sock.connect(new InetSocketAddress(host, port));
    } else {
        try {
            sock.connect(new InetSocketAddress(host, port), timeout);
        } catch (SocketTimeoutException e) {
            // Reproduce httpclient behavior - distinguish connect timeout from
            // data timeout
            String msg = "The host did not accept the connection within timeout of " + timeout + " ms";
            throw new ConnectTimeoutException(msg, e);
        }
    }
    return sock;
}