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

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

Introduction

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

Prototype

public void setHost(String paramString, int paramInt)

Source Link

Usage

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testConnectionReuseWithoutParams() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());

    HttpConnection c1 = manager.getConnection(h1);

    // new connection
    assertTrue(!c1.isOpen());/*from   w  ww  . j  a va 2s  . c om*/

    c1.open();
    c1.releaseConnection();

    HostConfiguration h2 = new HostConfiguration();
    h2.setHost(address, server1.getLocalPort());

    HttpConnection c2 = manager.getConnection(h2);

    // connection should have been released
    // so c2 is c1
    assertTrue(h2.equals(h1));
    assertTrue(c2 == c1);
    assertTrue(c2.isOpen());

    HttpConnection c3 = manager.getConnection(h2);

    // connection c2 was not released so new connection
    // c2 != c3
    assertTrue(!c3.isOpen());
    assertTrue(c3 != c2);
    assertTrue(c3 != c1);

    c2.releaseConnection();
    c3.releaseConnection();

    Server server2 = new Server();

    // it's a new port
    HostConfiguration h4 = new HostConfiguration();
    h4.setHost(address, server2.getLocalPort());

    HttpConnection c4 = manager.getConnection(h4);

    assertTrue(!c4.isOpen());
    assertTrue(c4 != c1);
    assertTrue(c4 != c2);
    assertTrue(c4 != c3);

    server2.close();
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testConnectionReuseWithParams() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(PARAMS);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());
    h1.getParams().setParameter("A", "foo");
    h1.getParams().setParameter("B", "bar");
    h1.getParams().setParameter("C", "fff");

    HttpConnection c1 = manager.getConnection(h1);

    assertTrue(!c1.isOpen());//  w  w  w  . jav a2 s .  co  m
    c1.open();
    c1.releaseConnection();

    HostConfiguration h2 = new HostConfiguration();
    h2.setHost(address, server1.getLocalPort());
    h2.getParams().setParameter("A", "foo");
    h2.getParams().setParameter("B", "bar");
    // still should be reused since C is not checked param
    h2.getParams().setParameter("C", "ggg");

    HttpConnection c2 = manager.getConnection(h2);

    // connection should have been released
    // so c2 is c1
    assertTrue(h2.equals(h1));
    assertTrue(c2.isOpen());
    assertTrue(c2 == c1);

    HttpConnection c3 = manager.getConnection(h2);

    // new connection becuase it wasn't released
    assertTrue(c3 != c1);
    assertTrue(c3 != c2);
    assertTrue(!c3.isOpen());

    c2.releaseConnection();
    c3.releaseConnection();

    // this one does not have params
    HostConfiguration h4 = new HostConfiguration();
    h4.setHost(address, server1.getLocalPort());

    HttpConnection c4 = manager.getConnection(h4);

    // new connection
    assertTrue(c4 != c1);
    assertTrue(c4 != c2);
    assertTrue(c4 != c3);
    assertTrue(!c4.isOpen());

    c4.open();
    c4.releaseConnection();

    // this one only has B parameter
    HostConfiguration h5 = new HostConfiguration();
    h5.setHost(address, server1.getLocalPort());
    h5.getParams().setParameter("B", "bar");

    HttpConnection c5 = manager.getConnection(h5);

    // also a new connection
    assertTrue(c5 != c1);
    assertTrue(c5 != c2);
    assertTrue(c5 != c3);
    assertTrue(c5 != c4);
    assertTrue(!c5.isOpen());

    c5.open();
    c5.releaseConnection();

    // this one only has different B parameter
    HostConfiguration h6 = new HostConfiguration();
    h6.setHost(address, server1.getLocalPort());
    h6.getParams().setParameter("A", "fooo");
    h6.getParams().setParameter("B", "bar");

    HttpConnection c6 = manager.getConnection(h6);

    assertTrue(c6 != c1);
    assertTrue(c6 != c2);
    assertTrue(c6 != c3);
    assertTrue(c6 != c4);
    assertTrue(c6 != c5);
    assertTrue(!c6.isOpen());

    c6.open();
    c6.releaseConnection();
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testIdleConnectionSweeper() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null);
    manager.setConnectionIdleTime(1000 * 2);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());

    HttpConnection c1 = manager.getConnection(h1);

    // new connection
    assertTrue(!c1.isOpen());/*  w w w .  j a v a 2 s  . c  o  m*/
    c1.open();

    Thread.sleep(1000);

    c1.releaseConnection();

    assertTrue(c1 == manager.getConnection(h1));
    assertTrue(c1.isOpen());
    c1.releaseConnection();

    Thread.sleep(1000 * 4);

    HttpConnection c2 = manager.getConnection(h1);

    assertTrue(c1 != c2);
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testMultipleConnectionRelease() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());

    HttpConnection c1 = manager.getConnection(h1);

    assertTrue(!c1.isOpen());/*  w  w w. j  a  va 2 s  .  c o m*/
    c1.open();

    c1.releaseConnection();
    c1.releaseConnection();

    HttpConnection c2 = manager.getConnection(h1);

    assertTrue(c1 == c2);

    HttpConnection c3 = manager.getConnection(h1);

    assertTrue(c3 != c2);
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testHTTPContinue() throws Exception {
    HostConfiguration config = new HostConfiguration();
    config.setHost(address, server1.getLocalPort());

    HttpClient httpClient = new HttpClient();

    PostMethod method = new PostMethod("/foo/bar");
    method.setRequestBody("helloworld\r\n\r\n");

    int returnCode = httpClient.executeMethod(config, method, null);

    assertEquals(200, returnCode);/*from   www.  ja va  2 s  . c  om*/
}

From source file:org.globus.axis.transport.commons.tests.ExtendedHostConfigurationTest.java

private HostConfiguration getHostConfiguration(String[] params, String valueA, String valueB) {
    HostConfiguration h1 = new HostConfiguration();
    h1.setHost("foobar", 80);

    ExtendedHostConfiguration eh1 = new ExtendedHostConfiguration(h1, params);

    eh1.getParams().setParameter("A", valueA);
    eh1.getParams().setParameter("B", valueB);
    // even if C is different it's not included in the test
    eh1.getParams().setParameter("C", String.valueOf(counter++));

    return eh1;//from   w w  w.  j  a va2  s  .co  m
}

From source file:org.hydracache.client.transport.HttpTransport.java

@Override
public Transport establishConnection(String hostName, int port) {
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(hostName, port);

    this.httpClient.setHostConfiguration(hostConfiguration);
    return this;
}

From source file:org.kei.android.phone.cellhistory.towers.CellIdHelper.java

public static int tryToLocate(final Context context, final TowerInfo ti, int cfg_timeout, final String mode,
        final String apiKeyOpenCellID) {
    int timeout = cfg_timeout * 1000, ret = CellIdRequestEntity.OK;
    HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    connectionManager.getParams().setConnectionTimeout(timeout);
    connectionManager.getParams().setSoTimeout(timeout);
    // Create a connection to some 'hidden' Google-API
    String baseURL = null;// w  w  w.j av a  2 s.com
    if (mode.equals(OPEN_CELL_ID_API)) {
        ti.lock();
        try {
            baseURL = "http://opencellid.org/cell/get?key=" + apiKeyOpenCellID + "&mcc=" + ti.getMCC() + "&mnc="
                    + ti.getMNC() + "&cellid=" + ti.getCellId() + "&lac=" + ti.getLac() + "&format=json";
        } finally {
            ti.unlock();
        }
    } else
        baseURL = "http://www.google.com/glm/mmap";
    HttpConnection connection = null;
    ti.setCellLatitude(Double.NaN);
    ti.setCellLongitude(Double.NaN);
    try {
        // Setup the connection
        HttpURL httpURL = null;
        if (baseURL.startsWith("https"))
            httpURL = new HttpsURL(baseURL);
        else
            httpURL = new HttpURL(baseURL);
        final HostConfiguration host = new HostConfiguration();
        host.setHost(httpURL.getHost(), httpURL.getPort());
        connection = connectionManager.getConnection(host);
        // Open it
        connection.open();
        if (mode.equals(OPEN_CELL_ID_API))
            ret = new OpenCellIdRequestEntity(ti).decode(baseURL, connection, timeout);
        else
            ret = new GoogleHiddenRequestEntity(ti).decode(baseURL, connection, timeout);
    } catch (Exception e) {
        Log.e(CellIdHelper.class.getSimpleName(), "Exception: " + e.getMessage(), e);
        ret = CellIdRequestEntity.EXCEPTION;
        CellHistoryApp.addLog(context, "tryToLocate::Exception: " + e.getMessage());
    } finally {
        connection.close();
    }
    connectionManager.releaseConnection(connection);
    return ret;
}

From source file:org.mule.transport.http.MuleHostConfigurationTestCase.java

@Test
public void testSetHostViaHostAndPort() {
    HostConfiguration hostConfig = createHostConfiguration();

    hostConfig.setHost("www.mulesoft.org", 8080);

    assertMockSocketFactory(hostConfig);
    assertEquals("www.mulesoft.org", hostConfig.getHost());
    assertEquals(8080, hostConfig.getPort());
}

From source file:org.nuxeo.ecm.webdav.JackRabbitParallelBench.java

private HttpClient createClient() {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost("localhost", PORT);

    //HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(true);
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxConnectionsPerHost(hostConfig, 10);
    connectionManager.setParams(params);

    HttpClient client = new HttpClient(connectionManager);
    client.setHostConfiguration(hostConfig);

    Credentials creds = new UsernamePasswordCredentials(LOGIN, PASSWD);
    client.getState().setCredentials(AuthScope.ANY, creds);

    return client;
}