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, Protocol paramProtocol) 

Source Link

Usage

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClientTest.java

@Test(expectedExceptions = HttpClientException.class)
public void testPostJsonStringThrowsException() throws Exception {
    ApacheHttpClient31BackedHttpClient client = new ApacheHttpClient31BackedHttpClient(httpClient,
            new HashMap<String, String>());

    final String path = "/path/to/endpoint";

    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost("foobar.com", 3456, Protocol.getProtocol("https"));
    when(httpClient.getHostConfiguration()).thenReturn(hostConfiguration);
    when(httpClient.executeMethod(Matchers.<HttpMethod>any())).thenThrow(new IOException());

    client.postJson(path, "body");
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClientTest.java

@Test(expectedExceptions = HttpClientException.class)
public void testPostJsonBytesThrowsException() throws Exception {
    ApacheHttpClient31BackedHttpClient client = new ApacheHttpClient31BackedHttpClient(httpClient,
            new HashMap<String, String>());

    final String path = "/path/to/endpoint";

    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost("foobar.com", 3456, Protocol.getProtocol("http"));
    when(httpClient.getHostConfiguration()).thenReturn(hostConfiguration);
    when(httpClient.executeMethod(Matchers.<HttpMethod>any())).thenThrow(new IOException());

    client.postBytes(path, "body".getBytes("UTF-8"));
}

From source file:ch.cyberduck.core.http.HTTP3Session.java

protected HostConfiguration getHostConfiguration(Host host) {
    int port = host.getPort();
    final HostConfiguration configuration = new StickyHostConfiguration();
    if ("https".equals(host.getProtocol().getScheme())) {
        if (-1 == port) {
            port = 443;/*from www  .j  ava  2 s  . c  o m*/
        }
        // Configuration with custom socket factory using the trust manager
        configuration.setHost(host.getHostname(), port,
                new org.apache.commons.httpclient.protocol.Protocol(host.getProtocol().getScheme(),
                        new SSLSocketFactory(this.getTrustManager(host.getHostname())), port));
        if (Preferences.instance().getBoolean("connection.proxy.enable")) {
            final Proxy proxy = ProxyFactory.instance();
            if (proxy.isHTTPSProxyEnabled(host)) {
                configuration.setProxy(proxy.getHTTPSProxyHost(host), proxy.getHTTPSProxyPort(host));
            }
        }
    } else if ("http".equals(host.getProtocol().getScheme())) {
        if (-1 == port) {
            port = 80;
        }
        configuration.setHost(host.getHostname(), port, new org.apache.commons.httpclient.protocol.Protocol(
                host.getProtocol().getScheme(), new DefaultProtocolSocketFactory(), port));
        if (Preferences.instance().getBoolean("connection.proxy.enable")) {
            final Proxy proxy = ProxyFactory.instance();
            if (proxy.isHTTPProxyEnabled(host)) {
                configuration.setProxy(proxy.getHTTPProxyHost(host), proxy.getHTTPProxyPort(host));
            }
        }
    }
    final HostParams parameters = configuration.getParams();
    parameters.setParameter(HttpMethodParams.USER_AGENT, this.getUserAgent());
    parameters.setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    parameters.setParameter(HttpMethodParams.SO_TIMEOUT, this.timeout());
    parameters.setParameter(HttpMethodParams.CREDENTIAL_CHARSET, "ISO-8859-1");
    parameters.setParameter(HttpClientParams.MAX_REDIRECTS, 10);
    return configuration;
}

From source file:com.legstar.http.client.CicsHttp.java

/**
 * Create an http host configuration using the protocol/host/port triple.
 * @param cicsHttpEndpoint the connection configuration
 * @return a valid host configuration// w w  w  . java 2  s .  c o m
 */
protected HostConfiguration createHostConfiguration(final CicsHttpEndpoint cicsHttpEndpoint) {
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(cicsHttpEndpoint.getHostIPAddress(), cicsHttpEndpoint.getHostIPPort(),
            cicsHttpEndpoint.getHostURLProtocol());
    /* TODO add proxy handling */
    return hostConfiguration;
}

From source file:de.juwimm.cms.common.http.HttpClientWrapper.java

public void setHostConfiguration(HttpClient client, URL targetURL) {
    int port = targetURL.getPort();
    String host = targetURL.getHost();
    HostConfiguration config = hostMap.get(host + ":" + port);
    if (config == null) {
        config = new HostConfiguration();
        if (port == -1) {
            if (targetURL.getProtocol().equalsIgnoreCase("https")) {
                port = 443;//from w w w. j av  a  2s  .  c  o m
            } else {
                port = 80;
            }
        }
        config.setHost(host, port, targetURL.getProtocol());
    }
    // in the meantime HttpProxyUser and HttpProxyPasword might have changed
    // (DlgUsernamePassword) or now trying NTLM instead of BASE
    // authentication
    if (getHttpProxyHost() != null && getHttpProxyPort() != null && getHttpProxyHost().length() > 0
            && getHttpProxyPort().length() > 0) {
        client.getParams().setAuthenticationPreemptive(true);
        int proxyPort = new Integer(getHttpProxyPort()).intValue();
        config.setProxy(getHttpProxyHost(), proxyPort);
        if (getHttpProxyUser() != null && getHttpProxyUser().length() > 0) {
            Credentials proxyCred = null;
            if (isUseNTproxy()) {
                proxyCred = new NTCredentials(getHttpProxyUser(), getHttpProxyPassword(), getHttpProxyHost(),
                        "");
            } else {
                proxyCred = new UsernamePasswordCredentials(getHttpProxyUser(), getHttpProxyPassword());
            }
            client.getState().setProxyCredentials(AUTHSCOPE_ANY, proxyCred);

        }
    }
    hostMap.put(host + ":" + port, config);
    client.setHostConfiguration(config);

}

From source file:flex.messaging.services.http.HTTPProxyAdapter.java

private void initHttpConnectionManagerParams(HTTPConnectionManagerSettings settings) {
    connectionParams = new HttpConnectionManagerParams();
    connectionParams.setMaxTotalConnections(settings.getMaxTotalConnections());
    connectionParams.setDefaultMaxConnectionsPerHost(settings.getDefaultMaxConnectionsPerHost());

    if (!settings.getCookiePolicy().equals(CookiePolicy.DEFAULT)) {
        HttpClientParams httpClientParams = (HttpClientParams) connectionParams.getDefaults();
        httpClientParams.setCookiePolicy(settings.getCookiePolicy());
    }//from  ww w . j a  v a  2s  .  co  m

    if (settings.getConnectionTimeout() >= 0)
        connectionParams.setConnectionTimeout(settings.getConnectionTimeout());

    if (settings.getSocketTimeout() >= 0)
        connectionParams.setSoTimeout(settings.getSocketTimeout());

    connectionParams.setStaleCheckingEnabled(settings.isStaleCheckingEnabled());

    if (settings.getSendBufferSize() > 0)
        connectionParams.setSendBufferSize(settings.getSendBufferSize());

    if (settings.getReceiveBufferSize() > 0)
        connectionParams.setReceiveBufferSize(settings.getReceiveBufferSize());

    connectionParams.setTcpNoDelay(settings.isTcpNoDelay());
    connectionParams.setLinger(settings.getLinger());

    if (settings.getMaxConnectionsPerHost() != null) {
        Iterator it = settings.getMaxConnectionsPerHost().iterator();
        while (it.hasNext()) {
            HostConfigurationSettings hcs = (HostConfigurationSettings) it.next();
            HostConfiguration hostConfig = new HostConfiguration();

            if (hcs.getProtocol() != null) {
                Protocol protocol = Protocol.getProtocol(hcs.getProtocol());
                hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
            } else if (hcs.getProtocolFactory() != null) {
                Protocol protocol = hcs.getProtocolFactory().getProtocol();
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
                else
                    hostConfig.setHost(hcs.getHost(), protocol.getDefaultPort(), protocol);
            } else {
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort());
                else
                    hostConfig.setHost(hcs.getHost());
            }

            if (hcs.getVirtualHost() != null) {
                HostParams params = hostConfig.getParams();
                if (params != null)
                    params.setVirtualHost(hcs.getVirtualHost());
            }

            if (hcs.getProxyHost() != null) {
                hostConfig.setProxy(hcs.getProxyHost(), hcs.getProxyPort());
            }

            try {
                InetAddress addr = InetAddress.getByName(hcs.getLocalAddress());
                hostConfig.setLocalAddress(addr);
            } catch (UnknownHostException ex) {
            }
            connectionParams.setMaxConnectionsPerHost(hostConfig, hcs.getMaximumConnections());
        }
    }
}

From source file:com.twinsoft.convertigo.engine.billing.GoogleAnalyticsTicketManager.java

private HttpClient prepareHttpClient(String[] url) throws EngineException, MalformedURLException {
    final Pattern scheme_host_pattern = Pattern.compile("https://(.*?)(?::([\\d]*))?(/.*|$)");

    HttpClient client = new HttpClient();
    HostConfiguration hostConfiguration = client.getHostConfiguration();
    HttpState httpState = new HttpState();
    client.setState(httpState);/* ww w  .  j  a  v  a  2  s . c  om*/
    if (proxyManager != null) {
        proxyManager.getEngineProperties();
        proxyManager.setProxy(hostConfiguration, httpState, new URL(url[0]));
    }

    Matcher matcher = scheme_host_pattern.matcher(url[0]);
    if (matcher.matches()) {
        String host = matcher.group(1);
        String sPort = matcher.group(2);
        int port = 443;

        try {
            port = Integer.parseInt(sPort);
        } catch (Exception e) {
        }

        try {
            Protocol myhttps = new Protocol("https",
                    MySSLSocketFactory.getSSLSocketFactory(null, null, null, null, true), port);

            hostConfiguration.setHost(host, port, myhttps);
            url[0] = matcher.group(3);
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
        }
    }
    return client;
}

From source file:com.twinsoft.convertigo.eclipse.wizards.setup.SetupWizard.java

private HttpClient prepareHttpClient(String[] url) throws EngineException, MalformedURLException {
    HttpClient client = new HttpClient();

    HostConfiguration hostConfiguration = client.getHostConfiguration();

    HttpState httpState = new HttpState();
    client.setState(httpState);//  w w w  .ja v a  2  s  . com

    if (proxyManager != null) {
        proxyManager.getEngineProperties();
        proxyManager.setProxy(hostConfiguration, httpState, new URL(url[0]));
    }

    Matcher matcher = scheme_host_pattern.matcher(url[0]);
    if (matcher.matches()) {
        String host = matcher.group(1);
        String sPort = matcher.group(2);
        int port = 443;

        try {
            port = Integer.parseInt(sPort);
        } catch (Exception e) {
        }

        try {
            Protocol myhttps = new Protocol("https",
                    MySSLSocketFactory.getSSLSocketFactory(null, null, null, null, true), port);
            hostConfiguration.setHost(host, port, myhttps);
            url[0] = matcher.group(3);
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
        }
    }

    return client;
}

From source file:com.polarion.alm.ws.client.internal.connection.CommonsHTTPSender.java

protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) {
    TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https
    int port = targetURL.getPort();
    boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts());

    HostConfiguration config = new HostConfiguration();

    if (port == -1) {
        if (targetURL.getProtocol().equalsIgnoreCase("https")) {
            port = 443; // default port for https being 443
        } else { // it must be http
            port = 80; // default port for http being 80
        }/*from  w w w  .  j  av  a 2  s. c o m*/
    }

    if (hostInNonProxyList) {
        config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
    } else {
        if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) {
            config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
        } else {
            if (tcp.getProxyUser().length() != 0) {
                Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(),
                        tcp.getProxyPassword());
                // if the username is in the form "user\domain"
                // then use NTCredentials instead.
                int domainIndex = tcp.getProxyUser().indexOf("\\");
                if (domainIndex > 0) {
                    String domain = tcp.getProxyUser().substring(0, domainIndex);
                    if (tcp.getProxyUser().length() > domainIndex + 1) {
                        String user = tcp.getProxyUser().substring(domainIndex + 1);
                        proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain);
                    }
                }
                client.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
            }
            int proxyPort = new Integer(tcp.getProxyPort()).intValue();
            config.setProxy(tcp.getProxyHost(), proxyPort);
        }
    }
    return config;
}

From source file:gov.va.med.imaging.proxy.ImageXChangeHttpCommonsSender.java

protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) {
    TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http
    // or//from ww w . java2s . c  o  m
    // https
    int port = targetURL.getPort();
    boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts());

    HostConfiguration config = new HostConfiguration();

    if (port == -1) {
        if (targetURL.getProtocol().equalsIgnoreCase("https")) {
            port = 443; // default port for https being 443
        } else { // it must be http
            port = 80; // default port for http being 80
        }
    }

    if (hostInNonProxyList) {
        config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
    } else {
        if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) {
            config.setHost(targetURL.getHost(), port, targetURL.getProtocol());
        } else {
            if (tcp.getProxyUser().length() != 0) {
                Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(),
                        tcp.getProxyPassword());
                // if the username is in the form "user\domain"
                // then use NTCredentials instead.
                int domainIndex = tcp.getProxyUser().indexOf("\\");
                if (domainIndex > 0) {
                    String domain = tcp.getProxyUser().substring(0, domainIndex);
                    if (tcp.getProxyUser().length() > domainIndex + 1) {
                        String user = tcp.getProxyUser().substring(domainIndex + 1);
                        proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain);
                    }
                }
                client.getState().setProxyCredentials(AuthScope.ANY, proxyCred);
            }
            int proxyPort = new Integer(tcp.getProxyPort()).intValue();
            config.setProxy(tcp.getProxyHost(), proxyPort);
        }
    }
    return config;
}