Example usage for org.apache.commons.httpclient HttpConnection setHttpConnectionManager

List of usage examples for org.apache.commons.httpclient HttpConnection setHttpConnectionManager

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnection setHttpConnectionManager.

Prototype

public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) 

Source Link

Document

Sets the httpConnectionManager.

Usage

From source file:com.carrotsearch.util.httpclient.SingleHttpConnectionManager.java

/** */
@Override/*ww w.j a  v a 2s  . co  m*/
public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, long timeout) {
    final HttpConnection conn = new HttpConnection(hostConfiguration);
    conn.setHttpConnectionManager(this);
    conn.getParams().setDefaults(this.getParams());
    conn.getParams().setSoTimeout((int) timeout);
    return conn;
}

From source file:com.cyberway.issue.httpclient.SingleHttpConnectionManager.java

public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, long timeout) {

    HttpConnection conn = new HttpConnection(hostConfiguration);
    conn.setHttpConnectionManager(this);
    conn.getParams().setDefaults(this.getParams());
    return conn;//ww  w.  ja v  a2  s.  co m
}

From source file:com.cyberway.issue.httpclient.ThreadLocalHttpConnectionManager.java

/**
 * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
 * // w  w  w.  jav a 2 s  . c  o m
 * @since 3.0
 */
public HttpConnection getConnectionWithTimeout(final HostConfiguration hostConfiguration, final long timeout) {

    final ConnectionInfo ci = getConnectionInfo();
    HttpConnection httpConnection = ci.conn;

    // make sure the host and proxy are correct for this connection
    // close it and set the values if they are not
    if (httpConnection == null || !finishLastResponse(httpConnection)
            || !hostConfiguration.hostEquals(httpConnection)
            || !hostConfiguration.proxyEquals(httpConnection)) {

        if (httpConnection != null && httpConnection.isOpen()) {
            closer.closeConnection(httpConnection);
        }

        httpConnection = new HttpConnection(hostConfiguration);
        httpConnection.setHttpConnectionManager(this);
        httpConnection.getParams().setDefaults(this.params);
        ci.conn = httpConnection;

        httpConnection.setHost(hostConfiguration.getHost());
        httpConnection.setPort(hostConfiguration.getPort());
        httpConnection.setProtocol(hostConfiguration.getProtocol());
        httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());

        httpConnection.setProxyHost(hostConfiguration.getProxyHost());
        httpConnection.setProxyPort(hostConfiguration.getProxyPort());
    }

    // remove the connection from the timeout handler
    ci.idleStartTime = Long.MAX_VALUE;

    return httpConnection;
}

From source file:com.exalead.io.failover.MonitoredHttpConnectionManager.java

/**
 * Try to create a connection by looping on all hosts of the cluster.
 * @throws PoolAcquireException if all hosts in the cluster are down
 *//*w  w  w.  ja va2 s  . c o m*/
private HttpConnection acquireConnectionOnAnyHost() throws PoolAcquireException {
    HttpConnection connection = null;
    for (int i = 0; i < hosts.size(); i++) {
        HostState hs = null;
        try {
            synchronized (this) {
                hs = getNextRoundRobinHost();
            }
        } catch (IOException e) {
            /* Crap, we already know that all hosts are down, break */
            break;
        }
        try {
            connection = acquireConnection(hs);
            break;
        } catch (PoolAcquireException e) {
            logger.info("This host (" + hs + ") is down, goto next");
            // This host is down, goto next
            continue;
        }
    }
    if (connection == null) {
        throw new PoolAcquireException("All hosts are down");
    }
    connection.setHttpConnectionManager(this);
    return connection;
}

From source file:org.zaproxy.zap.ZapGetMethod.java

/**
 * Allow response code 101, that is Switching Protocols.
 * //from   w w w .jav  a  2 s  .c om
  * @see GetMethod#readResponse(HttpState, HttpConnection)
  */
@Override
protected void readResponse(HttpState state, HttpConnection conn) throws IOException, HttpException {
    LOG.trace("enter HttpMethodBase.readResponse(HttpState, HttpConnection)");

    boolean isUpgrade = false;

    while (getStatusLine() == null) {
        readStatusLine(state, conn);
        processStatusLine(state, conn);
        readResponseHeaders(state, conn);
        processResponseHeaders(state, conn);

        int status = this.statusLine.getStatusCode();
        if (status == 101) {
            LOG.debug("Retrieved HTTP status code '101 Switching Protocols'. Keep connection open!");

            // This means the requester has asked the server to switch protocols
            // and the server is acknowledging that it will do so
            // e.g.: upgrade to websocket

            if (conn instanceof ZapHttpConnection) {
                isUpgrade = true;
                // avoid connection release of HttpClient library 
                conn.setHttpConnectionManager(null);
            }
        } else if ((status >= 100) && (status < 200)) {
            if (LOG.isInfoEnabled()) {
                LOG.info("Discarding unexpected response: " + this.statusLine.toString());
            }
            this.statusLine = null;
        }
    }

    // get socket and input stream out of HttpClient
    if (conn instanceof ZapHttpConnection) {
        ZapHttpConnection zapConn = (ZapHttpConnection) conn;
        upgradedSocket = zapConn.getSocket();
        inputStream = zapConn.getResponseInputStream();
    }

    if (!isUpgrade) {
        // read & process rest of response
        // only if connection should not be kept
        readResponseBody(state, conn);
        processResponseBody(state, conn);
    }
}