Example usage for org.apache.commons.httpclient HostConfiguration getLocalAddress

List of usage examples for org.apache.commons.httpclient HostConfiguration getLocalAddress

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HostConfiguration getLocalAddress.

Prototype

public InetAddress getLocalAddress()

Source Link

Usage

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

/**
 * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
 * /*from   w ww . ja  va 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:org.lockss.util.urlconn.TestHttpClientUrlConnection.java

public void testExecuteProxy() throws Exception {
    client.setRes(202);//from w  w  w.j a  v  a2  s.com
    conn.setProxy("phost", 9009);
    conn.execute();
    assertTrue(conn.isExecuted());
    assertEquals(202, conn.getResponseCode());
    Header hdr;
    hdr = method.getRequestHeader("connection");
    assertEquals("keep-alive", hdr.getValue());
    hdr = method.getRequestHeader("accept");
    assertEquals(HttpClientUrlConnection.DEFAULT_ACCEPT_HEADER, hdr.getValue());
    HostConfiguration hc = client.getHostConfiguration();
    assertEquals("phost", hc.getProxyHost());
    assertEquals(9009, hc.getProxyPort());
    assertEquals(null, hc.getLocalAddress());

    conn = newConn(urlString + "foo");
    client.setRes(202);
    conn.execute();
    assertTrue(conn.isExecuted());
    assertEquals(202, conn.getResponseCode());
    hdr = method.getRequestHeader("connection");
    assertEquals("keep-alive", hdr.getValue());
    hdr = method.getRequestHeader("accept");
    assertEquals(HttpClientUrlConnection.DEFAULT_ACCEPT_HEADER, hdr.getValue());
    hc = client.getHostConfiguration();
    assertEquals(null, hc.getProxyHost());
    assertEquals(-1, hc.getProxyPort());
    assertEquals(null, hc.getLocalAddress());
}

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

public void testBindLocalAddress() throws Exception {
    String local = "127.3.42.6";
    client.setRes(202);/*from w w  w . j a v  a  2  s.co  m*/
    conn.setLocalAddress(IPAddr.getByName(local));
    conn.execute();
    assertTrue(conn.isExecuted());
    assertEquals(202, conn.getResponseCode());
    Header hdr;
    hdr = method.getRequestHeader("connection");
    assertEquals("keep-alive", hdr.getValue());
    hdr = method.getRequestHeader("accept");
    assertEquals(HttpClientUrlConnection.DEFAULT_ACCEPT_HEADER, hdr.getValue());
    HostConfiguration hc = client.getHostConfiguration();
    assertEquals(InetAddress.getByName(local), hc.getLocalAddress());

    conn = newConn(urlString + "foo");
    client.setRes(202);
    conn.execute();
    assertTrue(conn.isExecuted());
    assertEquals(202, conn.getResponseCode());
    hdr = method.getRequestHeader("connection");
    assertEquals("keep-alive", hdr.getValue());
    hdr = method.getRequestHeader("accept");
    assertEquals(HttpClientUrlConnection.DEFAULT_ACCEPT_HEADER, hdr.getValue());
    hc = client.getHostConfiguration();
    assertEquals(null, hc.getLocalAddress());
}