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

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

Introduction

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

Prototype

public String getProxyHost() 

Source Link

Usage

From source file:com.worldline.easycukes.commons.Configuration.java

/**
 * Allows to deal with the proxy configuration. It creates a
 * {@link HostConfiguration} object that can be used by the HTTPClient, and
 * also sets the proxy in system variables.
 *
 * @return a {@link HostConfiguration} containing information about the
 *         proxy, along with the fact that system properties have been set.
 *         Null if the proxy is not specified
 *///from  ww  w . jav  a  2  s  .  c o m
public static HostConfiguration configureProxy() {
    if (!isLoaded)
        load();
    if (isProxyNeeded()) {
        LOGGER.info("Proxy is needed => configuring the http client");
        final HostConfiguration hostConfiguration = new HostConfiguration();
        hostConfiguration.setProxy(
                environment.get("proxy").get("host") != null ? environment.get("proxy").get("host").toString()
                        : "",
                environment.get("proxy").get("port") != null
                        ? Integer.parseInt(environment.get("proxy").get("port").toString())
                        : 0);

        LOGGER.info("Setting the proxy... Using " + hostConfiguration.getProxyHost() + ":"
                + hostConfiguration.getProxyPort());

        // Set system properties too
        System.setProperty("https.proxyHost", environment.get("proxy").get("host").toString());
        System.setProperty("https.proxyPort", environment.get("proxy").get("port").toString());
        return hostConfiguration;
    }
    return null;
}

From source file:com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor.java

private HostConfiguration getHostConfiguration(HttpClient client, Map<String, Object> props) {
    Object proxy = props.get(ApacheHttpClientConfig.PROPERTY_PROXY_URI);
    if (proxy != null) {
        URI proxyUri = getProxyUri(proxy);

        String proxyHost = proxyUri.getHost();
        if (proxyHost == null) {
            proxyHost = "localhost";
        }//from   w ww  .  ja  v a 2  s  .com

        int proxyPort = proxyUri.getPort();
        if (proxyPort == -1) {
            proxyPort = 8080;
        }

        HostConfiguration hostConfig = new HostConfiguration(client.getHostConfiguration());
        String setHost = hostConfig.getProxyHost();
        int setPort = hostConfig.getProxyPort();

        if ((setHost == null) || (!setHost.equals(proxyHost)) || (setPort == -1) || (setPort != proxyPort)) {
            hostConfig.setProxyHost(new ProxyHost(proxyHost, proxyPort));
        }
        return hostConfig;
    } else {
        return null;
    }
}

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

/**
 * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
 * /* www. j a v a2  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  av  a2  s. c om*/
    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.n52.oxf.util.TestIOHelper.java

/**
 * Test method for {@link org.n52.oxf.util.IOHelper#sendPostMessage(java.lang.String, java.lang.String)}.
 *//* w  w w. j  a v a  2 s.c  o  m*/
public void testGetHostConfiguration1() {
    try {
        URL serviceURL = new URL("http://my.sos.de?REQUEST=GetObservation");

        System.setProperty("http.proxyHost", "proxy");
        System.setProperty("http.proxyPort", "8080");

        HostConfiguration hostConfig_0 = IOHelper.getHostConfiguration(serviceURL);
        LOGGER.info("selected proxy is: " + hostConfig_0.getProxyHost());
        assertEquals("proxy", hostConfig_0.getProxyHost());

        System.setProperty("http.nonProxyHosts", "localhost|www.test.de");
        HostConfiguration hostConfig_1 = IOHelper.getHostConfiguration(serviceURL);
        LOGGER.info("selected proxy is: " + hostConfig_1.getProxyHost());
        assertEquals("proxy", hostConfig_1.getProxyHost());

        System.setProperty("http.nonProxyHosts", "my.sos.de");
        HostConfiguration hostConfig_2 = IOHelper.getHostConfiguration(serviceURL);
        LOGGER.info("selected proxy is: " + hostConfig_2.getProxyHost());
        assertEquals(null, hostConfig_2.getProxyHost());

        System.setProperty("http.nonProxyHosts", "localhost|my.sos.de");
        HostConfiguration hostConfig_3 = IOHelper.getHostConfiguration(serviceURL);
        LOGGER.info("selected proxy is: " + hostConfig_3.getProxyHost());
        assertEquals(null, hostConfig_3.getProxyHost());

        System.setProperty("http.nonProxyHosts", "");
        HostConfiguration hostConfig_4 = IOHelper.getHostConfiguration(serviceURL);
        LOGGER.info("selected proxy is: " + hostConfig_4.getProxyHost());
        assertEquals("proxy", hostConfig_4.getProxyHost());

    } catch (Exception e) {
        LOGGER.error(e, e);
        fail();
    }
}

From source file:org.springframework.security.saml.websso.ArtifactResolutionProfileImplTest.java

/**
 * Verifies that hostConfiguration is correctly cloned when HttpClient contains defaults.
 *///from   w  w  w . j av  a2 s. c  om
@Test
public void testHostConfigurationWithDefaults() throws Exception {

    // Client object with default settings
    HttpClient client = new HttpClient();
    HostConfiguration defaultConfiguration = new HostConfiguration();
    defaultConfiguration.setProxy("testProxy", 8000);
    defaultConfiguration.getParams().setParameter("testParam", "testValue");
    client.setHostConfiguration(defaultConfiguration);

    ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(client);
    URI uri = new URI("http", "test", "/artifact", null);
    HostConfiguration hostConfiguration = artifactResolutionProfile.getHostConfiguration(uri, null);

    // Verify that settings were cloned
    assertNotNull(hostConfiguration);
    assertEquals("test", hostConfiguration.getHost());
    assertEquals("testProxy", hostConfiguration.getProxyHost());
    assertEquals(8000, hostConfiguration.getProxyPort());
    assertEquals("testValue", hostConfiguration.getParams().getParameter("testParam"));

    // Make sure default object and newly created configuration are independent
    defaultConfiguration.setProxyHost(null);
    assertEquals("testProxy", hostConfiguration.getProxyHost());
    assertEquals(8000, hostConfiguration.getProxyPort());

}