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(URI paramURI) 

Source Link

Usage

From source file:org.scohen.juploadr.upload.HttpClientFactory.java

public static HttpClient getHttpClient(CommunityAccount account) {
    HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
    Protocol http;//w ww .j a va 2 s  .  c om
    if (account.isBandwidthLimited()) {
        http = new Protocol(HTTP, new BandwidthLimitingProtocolSocketFactory(account.getBandwidth()), 80);
    } else {
        http = new Protocol(HTTP, new DefaultProtocolSocketFactory(), 80);
    }

    Protocol.registerProtocol(HTTP, http);
    NetActivator activator = NetActivator.getDefault();
    IProxyService proxyService = activator.getProxyService();
    String home = ((IConfigurationElement) account.getConfiguration().getParent()).getAttribute("home"); //$NON-NLS-1$
    home = Core.furnishWebUrl(home);
    IProxyData proxyData = null;
    try {
        IProxyData[] select = proxyService.select(new URI(home));
        if (select.length > 0)
            proxyData = select[0];
    } catch (URISyntaxException e) {
        activator.logError(Messages.HttpClientFactory_bad_uri_for_proxy, e);
    } finally {
        activator.ungetProxyService(proxyService);
    }
    if (proxyData != null && proxyData.getHost() != null) {
        String proxyHost = proxyData.getHost();
        String proxyPassword = proxyData.getPassword();
        String proxyUsername = proxyData.getUserId();
        int proxyPort = proxyData.getPort();
        HostConfiguration hc = client.getHostConfiguration();
        if (proxyPort < 0)
            hc.setHost(proxyHost);
        else
            hc.setProxy(proxyHost, proxyPort);
        if (proxyData.isRequiresAuthentication() && proxyUsername.length() > 0) {
            Credentials creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
            client.getParams().setAuthenticationPreemptive(true);
            AuthScope scope = new AuthScope(proxyHost, proxyPort);
            client.getState().setProxyCredentials(scope, creds);
        }
    }
    client.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
    client.getHttpConnectionManager().getParams().setSoTimeout(60000);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    return client;
}

From source file:org.sonatype.nexus.proxy.storage.remote.commonshttpclient.HttpClientProxyUtil.java

public static void applyProxyToHttpClient(HttpClient httpClient, RemoteStorageContext ctx, Logger logger) {
    httpClient.setHttpConnectionManager(new CustomMultiThreadedHttpConnectionManager());

    // getting the timeout from RemoteStorageContext. The value we get depends on per-repo and global settings.
    // The value will "cascade" from repo level to global level, see imple of it.
    int timeout = ctx.getRemoteConnectionSettings().getConnectionTimeout();

    // getting the connection pool size, using a little trick to allow us "backdoor" to tune it using system
    // properties, but defaulting it to the same we had before (httpClient defaults)
    int connectionPoolSize = SystemPropertiesHelper.getInteger(CONNECTION_POOL_SIZE_KEY,
            MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
    // httpClient.getHttpConnectionManager().getParams().setTcpNoDelay( true );
    httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(connectionPoolSize);
    // NOTE: connPool is _per_ repo, hence all of those will connect to same host (unless mirrors are used)
    // so, we are violating intentionally the RFC and we let the whole pool size to chase same host
    httpClient.getHttpConnectionManager().getParams()
            .setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, connectionPoolSize);

    // Setting auth if needed
    HostConfiguration httpConfiguration = httpClient.getHostConfiguration();

    // BASIC and DIGEST auth only
    RemoteAuthenticationSettings ras = ctx.getRemoteAuthenticationSettings();

    boolean isSimpleAuthUsed = false;
    boolean isNtlmUsed = false;

    if (ras != null) {
        List<String> authPrefs = new ArrayList<String>(2);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.BASIC);

        if (ras instanceof ClientSSLRemoteAuthenticationSettings) {
            // ClientSSLRemoteAuthenticationSettings cras = (ClientSSLRemoteAuthenticationSettings) ras;

            // TODO - implement this
        } else if (ras instanceof NtlmRemoteAuthenticationSettings) {
            NtlmRemoteAuthenticationSettings nras = (NtlmRemoteAuthenticationSettings) ras;

            // Using NTLM auth, adding it as first in policies
            authPrefs.add(0, AuthPolicy.NTLM);

            logger(logger).info("... authentication setup for NTLM domain '{}'", nras.getNtlmDomain());

            httpConfiguration.setHost(nras.getNtlmHost());

            httpClient.getState().setCredentials(AuthScope.ANY, new NTCredentials(nras.getUsername(),
                    nras.getPassword(), nras.getNtlmHost(), nras.getNtlmDomain()));

            isNtlmUsed = true;//from  www  .j  av  a  2 s. c o  m
        } else if (ras instanceof UsernamePasswordRemoteAuthenticationSettings) {
            UsernamePasswordRemoteAuthenticationSettings uras = (UsernamePasswordRemoteAuthenticationSettings) ras;

            // Using Username/Pwd auth, will not add NTLM
            logger(logger).info("... authentication setup for remote storage with username '{}'",
                    uras.getUsername());

            httpClient.getState().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(uras.getUsername(), uras.getPassword()));

            isSimpleAuthUsed = true;
        }

        httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    }

    RemoteProxySettings rps = ctx.getRemoteProxySettings();

    boolean isProxyUsed = false;

    if (rps.isEnabled()) {
        isProxyUsed = true;

        logger(logger).info("... proxy setup with host '{}'", rps.getHostname());

        httpConfiguration.setProxy(rps.getHostname(), rps.getPort());

        // check if we have non-proxy hosts
        if (rps.getNonProxyHosts() != null && !rps.getNonProxyHosts().isEmpty()) {
            Set<Pattern> nonProxyHostPatterns = new HashSet<Pattern>(rps.getNonProxyHosts().size());
            for (String nonProxyHostRegex : rps.getNonProxyHosts()) {
                try {
                    nonProxyHostPatterns.add(Pattern.compile(nonProxyHostRegex, Pattern.CASE_INSENSITIVE));
                } catch (PatternSyntaxException e) {
                    logger(logger).warn("Invalid non proxy host regex: {}", nonProxyHostRegex, e);
                }
            }
            httpConfiguration.getParams().setParameter(
                    CustomMultiThreadedHttpConnectionManager.NON_PROXY_HOSTS_PATTERNS_KEY,
                    nonProxyHostPatterns);
        }

        if (rps.getProxyAuthentication() != null) {
            ras = rps.getProxyAuthentication();

            List<String> authPrefs = new ArrayList<String>(2);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.BASIC);

            if (ras instanceof ClientSSLRemoteAuthenticationSettings) {
                // ClientSSLRemoteAuthenticationSettings cras = (ClientSSLRemoteAuthenticationSettings) ras;

                // TODO - implement this
            } else if (ras instanceof NtlmRemoteAuthenticationSettings) {
                NtlmRemoteAuthenticationSettings nras = (NtlmRemoteAuthenticationSettings) ras;

                // Using NTLM auth, adding it as first in policies
                authPrefs.add(0, AuthPolicy.NTLM);

                if (ctx.getRemoteAuthenticationSettings() != null && (ctx
                        .getRemoteAuthenticationSettings() instanceof NtlmRemoteAuthenticationSettings)) {
                    logger(logger).warn("... Apache Commons HttpClient 3.x is unable to use NTLM auth scheme\n"
                            + " for BOTH server side and proxy side authentication!\n"
                            + " You MUST reconfigure server side auth and use BASIC/DIGEST scheme\n"
                            + " if you have to use NTLM proxy, otherwise it will not work!\n"
                            + " *** SERVER SIDE AUTH OVERRIDDEN");
                }

                logger(logger).info("... proxy authentication setup for NTLM domain '{}'",
                        nras.getNtlmDomain());

                httpConfiguration.setHost(nras.getNtlmHost());

                httpClient.getState().setProxyCredentials(AuthScope.ANY, new NTCredentials(nras.getUsername(),
                        nras.getPassword(), nras.getNtlmHost(), nras.getNtlmDomain()));

                isNtlmUsed = true;
            } else if (ras instanceof UsernamePasswordRemoteAuthenticationSettings) {
                UsernamePasswordRemoteAuthenticationSettings uras = (UsernamePasswordRemoteAuthenticationSettings) ras;

                // Using Username/Pwd auth, will not add NTLM
                logger(logger).info("... proxy authentication setup for remote storage with username '{}'",
                        uras.getUsername());

                httpClient.getState().setProxyCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(uras.getUsername(), uras.getPassword()));
            }

            httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }
    }

    // set preemptive only for simplest scenario:
    // no proxy and BASIC auth is used
    if (isSimpleAuthUsed && !isProxyUsed) {
        logger(logger)
                .info("... simple scenario: simple authentication used with no proxy in between target and us,"
                        + " will use preemptive authentication");

        // we have authentication, let's do it preemptive
        httpClient.getParams().setAuthenticationPreemptive(true);
    }

    // mark the fact that NTLM is in use
    // but ONLY IF IT CHANGED!
    // Otherwise, doing it always, actually marks the ctx itself as "changed", causing an avalanche of other
    // consequences, like resetting all the HTTP clients of all remote storages (coz they think there is a change
    // in proxy or remote connection settings, etc).
    final Boolean isNtlmUsedOldValue = (Boolean) ctx.getContextObject(NTLM_IS_IN_USE_KEY);
    if (isNtlmUsedOldValue == null || isNtlmUsedOldValue.booleanValue() != isNtlmUsed) {
        if (isNtlmUsed) {
            ctx.putContextObject(NTLM_IS_IN_USE_KEY, Boolean.TRUE);
        } else {
            ctx.putContextObject(NTLM_IS_IN_USE_KEY, Boolean.FALSE);
        }
    }
}

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

/**
 * Method is expected to determine hostConfiguration used to send request to the server by back-channel. Configuration
 * should contain URI of the host and used protocol including all security settings.
 * <p>/*from   w  ww .ja  v  a 2s .  c om*/
 * Default implementation uses either default http protocol for non-SSL requests or constructs a separate
 * TrustManager using trust engine specified in the SAMLMessageContext - based either on MetaIOP (certificates
 * obtained from Metadata and ExtendedMetadata are trusted) or PKIX (certificates from metadata and ExtendedMetadata
 * including specified trust anchors are trusted and verified using PKIX).
 * <p>
 * Used trust engine can be customized as part of the SAMLContextProvider used to process this request.
 * <p>
 * Default values for the HostConfiguration are cloned from the HTTPClient set in this instance, when there are
 * no defaults available a new object is created.
 *
 * @param uri uri the request should be sent to
 * @param context context including the peer address
 * @return host configuration
 * @throws MessageEncodingException in case peer URI can't be parsed
 */
protected HostConfiguration getHostConfiguration(URI uri, SAMLMessageContext context)
        throws MessageEncodingException {

    try {

        HostConfiguration hc = httpClient.getHostConfiguration();

        if (hc != null) {
            // Clone configuration from the HTTP Client object
            hc = new HostConfiguration(hc);
        } else {
            // Create brand new configuration when there are no defaults
            hc = new HostConfiguration();
        }

        if (uri.getScheme().equalsIgnoreCase("http")) {

            log.debug("Using HTTP configuration");
            hc.setHost(uri);

        } else {

            log.debug("Using HTTPS configuration");

            CriteriaSet criteriaSet = new CriteriaSet();
            criteriaSet.add(new EntityIDCriteria(context.getPeerEntityId()));
            criteriaSet
                    .add(new MetadataCriteria(IDPSSODescriptor.DEFAULT_ELEMENT_NAME, SAMLConstants.SAML20P_NS));
            criteriaSet.add(new UsageCriteria(UsageType.UNSPECIFIED));

            X509TrustManager trustManager = new X509TrustManager(criteriaSet, context.getLocalSSLTrustEngine());
            X509KeyManager manager = new X509KeyManager(context.getLocalSSLCredential());
            HostnameVerifier hostnameVerifier = context.getLocalSSLHostnameVerifier();

            ProtocolSocketFactory socketFactory = getSSLSocketFactory(context, manager, trustManager,
                    hostnameVerifier);
            Protocol protocol = new Protocol("https", socketFactory, 443);
            hc.setHost(uri.getHost(), uri.getPort(), protocol);

        }

        return hc;

    } catch (URIException e) {
        throw new MessageEncodingException("Error parsing remote location URI", e);
    }

}

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

/**
 * Method is expected to determine hostConfiguration used to send request to the server by back-channel. Configuration
 * should contain URI of the host and used protocol including all security settings.
 * <p/>/*from  w  ww .j ava  2 s .c  o  m*/
 * Default implementation uses either default http protocol for non-SSL requests or constructs a separate
 * TrustManager using trust engine specified in the SAMLMessageContext - based either on MetaIOP (certificates
 * obtained from Metadata and ExtendedMetadata are trusted) or PKIX (certificates from metadata and ExtendedMetadata
 * including specified trust anchors are trusted and verified using PKIX).
 * <p/>
 * Used trust engine can be customized as part of the SAMLContextProvider used to process this request.
 * <p/>
 * Default values for the HostConfiguration are cloned from the HTTPClient set in this instance, when there are
 * no defaults available a new object is created.
 *
 * @param uri uri the request should be sent to
 * @param context context including the peer address
 * @return host configuration
 * @throws MessageEncodingException in case peer URI can't be parsed
 */
protected HostConfiguration getHostConfiguration(URI uri, SAMLMessageContext context)
        throws MessageEncodingException {

    try {

        HostConfiguration hc = httpClient.getHostConfiguration();

        if (hc != null) {
            // Clone configuration from the HTTP Client object
            hc = new HostConfiguration(hc);
        } else {
            // Create brand new configuration when there are no defaults
            hc = new HostConfiguration();
        }

        if (uri.getScheme().equalsIgnoreCase("http")) {

            log.debug("Using HTTP configuration");
            hc.setHost(uri);

        } else {

            log.debug("Using HTTPS configuration");

            CriteriaSet criteriaSet = new CriteriaSet();
            criteriaSet.add(new EntityIDCriteria(context.getPeerEntityId()));
            criteriaSet
                    .add(new MetadataCriteria(IDPSSODescriptor.DEFAULT_ELEMENT_NAME, SAMLConstants.SAML20P_NS));
            criteriaSet.add(new UsageCriteria(UsageType.UNSPECIFIED));

            X509TrustManager trustManager = new X509TrustManager(criteriaSet, context.getLocalSSLTrustEngine());
            X509KeyManager manager = new X509KeyManager(context.getLocalSSLCredential());
            Protocol protocol = new Protocol("https",
                    (ProtocolSocketFactory) new TLSProtocolSocketFactory(manager, trustManager), 443);
            hc.setHost(uri.getHost(), uri.getPort(), protocol);

        }

        return hc;

    } catch (URIException e) {
        throw new MessageEncodingException("Error parsing remote location URI", e);
    }

}

From source file:org.springframework.ws.transport.http.CommonsHttpMessageSender.java

/**
 * Sets the maximum number of connections per host for the underlying HttpClient. The maximum number of connections
 * per host can be set in a form accepted by the {@code java.util.Properties} class, like as follows:
 * <pre>/*  w ww. j av a2 s . c  o  m*/
 * https://www.example.com=1
 * http://www.example.com:8080=7
 * www.springframework.org=10
 * *=5
 * </pre>
 * The host can be specified as hostname, or as URI (with scheme and port). The special host name {@code *} can be
 * used to specify {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
 *
 * @param maxConnectionsPerHost a properties object specifying the maximum number of connection
 * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setMaxConnectionsPerHost(org.apache.commons.httpclient.HostConfiguration,
 *      int)
 */
public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost) throws URIException {
    for (String host : maxConnectionsPerHost.keySet()) {
        HostConfiguration hostConfiguration = new HostConfiguration();
        if ("*".equals(host)) {
            hostConfiguration = HostConfiguration.ANY_HOST_CONFIGURATION;
        } else if (host.startsWith("http://")) {
            HttpURL httpURL = new HttpURL(host);
            hostConfiguration.setHost(httpURL);
        } else if (host.startsWith("https://")) {
            HttpsURL httpsURL = new HttpsURL(host);
            hostConfiguration.setHost(httpsURL);
        } else {
            hostConfiguration.setHost(host);
        }
        int maxHostConnections = Integer.parseInt(maxConnectionsPerHost.get(host));
        getHttpClient().getHttpConnectionManager().getParams().setMaxConnectionsPerHost(hostConfiguration,
                maxHostConnections);
    }
}

From source file:org.talend.mdm.bulkload.client.BulkloadClientUtil.java

public static void bulkload(String url, String cluster, String concept, String datamodel, boolean validate,
        boolean smartpk, boolean insertonly, InputStream itemdata, String username, String password,
        String transactionId, String sessionId, String universe, String tokenKey, String tokenValue)
        throws Exception {
    HostConfiguration config = new HostConfiguration();
    URI uri = new URI(url, false, "UTF-8"); //$NON-NLS-1$
    config.setHost(uri);

    NameValuePair[] parameters = { new NameValuePair("cluster", cluster), //$NON-NLS-1$
            new NameValuePair("concept", concept), //$NON-NLS-1$
            new NameValuePair("datamodel", datamodel), //$NON-NLS-1$
            new NameValuePair("validate", String.valueOf(validate)), //$NON-NLS-1$
            new NameValuePair("action", "load"), //$NON-NLS-1$ //$NON-NLS-2$
            new NameValuePair("smartpk", String.valueOf(smartpk)), //$NON-NLS-1$
            new NameValuePair("insertonly", String.valueOf(insertonly)) }; //$NON-NLS-1$

    HttpClient client = new HttpClient();
    String user = universe == null || universe.trim().length() == 0 ? username : universe + "/" + username; //$NON-NLS-1$
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    HttpClientParams clientParams = client.getParams();
    clientParams.setAuthenticationPreemptive(true);
    clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);

    PutMethod putMethod = new PutMethod();
    // This setPath call is *really* important (if not set, request will be sent to the JBoss root '/')
    putMethod.setPath(url);//from  www  .  j  av  a  2s  .  c  o  m
    String responseBody;
    try {
        // Configuration
        putMethod.setRequestHeader("Content-Type", "text/xml; charset=utf8"); //$NON-NLS-1$ //$NON-NLS-2$
        if (transactionId != null) {
            putMethod.setRequestHeader("transaction-id", transactionId); //$NON-NLS-1$
        }
        if (sessionId != null) {
            putMethod.setRequestHeader("Cookie", STICKY_SESSION + "=" + sessionId); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (tokenKey != null && tokenValue != null) {
            putMethod.setRequestHeader(tokenKey, tokenValue);
        }

        putMethod.setQueryString(parameters);
        putMethod.setContentChunked(true);
        // Set the content of the PUT request
        putMethod.setRequestEntity(new InputStreamRequestEntity(itemdata));

        client.executeMethod(config, putMethod);
        responseBody = putMethod.getResponseBodyAsString();
        if (itemdata instanceof InputStreamMerger) {
            ((InputStreamMerger) itemdata).setAlreadyProcessed(true);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        putMethod.releaseConnection();
    }

    int statusCode = putMethod.getStatusCode();
    if (statusCode >= 500) {
        throw new BulkloadException(responseBody);
    } else if (statusCode >= 400) {
        throw new BulkloadException("Could not send data to MDM (HTTP status code: " + statusCode + ")."); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:smartrics.rest.fitnesse.fixture.support.http.URIBuilder.java

@SuppressWarnings("deprecation")
public void setURI(org.apache.commons.httpclient.HttpMethodBase m, URI uri) throws URIException {
    HostConfiguration conf = m.getHostConfiguration();
    if (uri.isAbsoluteURI()) {
        conf.setHost(new HttpHost(uri));
        m.setHostConfiguration(conf);/*from   www.  j  a  va 2 s  . co  m*/
    }
    m.setPath(uri.getPath() != null ? uri.getEscapedPath() : "/");
    m.setQueryString(uri.getQuery());
}

From source file:voldemort.performance.HttpClientBench.java

private static HttpClient createClient() {
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams clientParams = httpClient.getParams();
    clientParams.setConnectionManagerTimeout(DEFAULT_CONNECTION_MANAGER_TIMEOUT);
    clientParams.setSoTimeout(500);/*w w w  .j av a2  s.  com*/
    clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
    clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    clientParams.setBooleanParameter("http.tcp.nodelay", false);
    clientParams.setIntParameter("http.socket.receivebuffer", 60000);
    clientParams.setParameter("http.useragent", VOLDEMORT_USER_AGENT);
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost("localhost");
    hostConfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    httpClient.setHostConfiguration(hostConfig);
    HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
    managerParams.setConnectionTimeout(DEFAULT_CONNECTION_MANAGER_TIMEOUT);
    managerParams.setMaxTotalConnections(DEFAULT_MAX_CONNECTIONS);
    managerParams.setMaxConnectionsPerHost(httpClient.getHostConfiguration(), DEFAULT_MAX_HOST_CONNECTIONS);
    managerParams.setStaleCheckingEnabled(false);

    return httpClient;
}

From source file:webdav.ManageWebDAV.java

public void connectWebDAVServer(String strUri, int intMaxConnections, String strUserId, String strPassword) {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(strUri);

    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxConnectionsPerHost(hostConfig, intMaxConnections);
    connectionManager.setParams(params);

    this.client = new HttpClient(connectionManager);
    client.setHostConfiguration(hostConfig);
    Credentials creds = new UsernamePasswordCredentials(strUserId, strPassword);
    client.getState().setCredentials(AuthScope.ANY, creds);
}