Example usage for org.apache.commons.httpclient.params HttpClientParams setAuthenticationPreemptive

List of usage examples for org.apache.commons.httpclient.params HttpClientParams setAuthenticationPreemptive

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpClientParams setAuthenticationPreemptive.

Prototype

public void setAuthenticationPreemptive(boolean paramBoolean) 

Source Link

Usage

From source file:org.jivesoftware.openfire.crowd.CrowdManager.java

private CrowdManager() {
    try {//from w ww . j a  v a 2  s.com
        // loading crowd.properties file
        CrowdProperties crowdProps = new CrowdProperties();

        MultiThreadedHttpConnectionManager threadedConnectionManager = new MultiThreadedHttpConnectionManager();
        HttpClient hc = new HttpClient(threadedConnectionManager);

        HttpClientParams hcParams = hc.getParams();
        hcParams.setAuthenticationPreemptive(true);

        HttpConnectionManagerParams hcConnectionParams = hc.getHttpConnectionManager().getParams();
        hcConnectionParams.setDefaultMaxConnectionsPerHost(crowdProps.getHttpMaxConnections());
        hcConnectionParams.setMaxTotalConnections(crowdProps.getHttpMaxConnections());
        hcConnectionParams.setConnectionTimeout(crowdProps.getHttpConnectionTimeout());
        hcConnectionParams.setSoTimeout(crowdProps.getHttpSocketTimeout());

        crowdServer = new URI(crowdProps.getCrowdServerUrl()).resolve("rest/usermanagement/latest/");

        // setting BASIC authentication in place for connection with Crowd
        HttpState httpState = hc.getState();
        Credentials crowdCreds = new UsernamePasswordCredentials(crowdProps.getApplicationName(),
                crowdProps.getApplicationPassword());
        httpState.setCredentials(new AuthScope(crowdServer.getHost(), crowdServer.getPort()), crowdCreds);

        // setting Proxy config in place if needed
        if (StringUtils.isNotBlank(crowdProps.getHttpProxyHost()) && crowdProps.getHttpProxyPort() > 0) {
            hc.getHostConfiguration().setProxy(crowdProps.getHttpProxyHost(), crowdProps.getHttpProxyPort());

            if (StringUtils.isNotBlank(crowdProps.getHttpProxyUsername())
                    || StringUtils.isNotBlank(crowdProps.getHttpProxyPassword())) {
                Credentials proxyCreds = new UsernamePasswordCredentials(crowdProps.getHttpProxyUsername(),
                        crowdProps.getHttpProxyPassword());
                httpState.setProxyCredentials(
                        new AuthScope(crowdProps.getHttpProxyHost(), crowdProps.getHttpProxyPort()),
                        proxyCreds);
            }
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("HTTP Client config");
            LOG.debug(crowdServer.toString());
            LOG.debug("Max connections:" + hcConnectionParams.getMaxTotalConnections());
            LOG.debug("Socket timeout:" + hcConnectionParams.getSoTimeout());
            LOG.debug("Connect timeout:" + hcConnectionParams.getConnectionTimeout());
            LOG.debug("Proxy host:" + crowdProps.getHttpProxyHost() + ":" + crowdProps.getHttpProxyPort());
            LOG.debug("Crowd application name:" + crowdProps.getApplicationName());
        }

        client = hc;
    } catch (Exception e) {
        LOG.error("Failure to load the Crowd manager", e);
    }
}

From source file:org.lockss.plugin.silverchair.PostHttpClientUrlConnection.java

public void setCredentials(String username, String password) {
    assertNotExecuted();//from  w  w w .  j  a v  a 2  s  .  c om
    Credentials credentials = new UsernamePasswordCredentials(username, password);
    HttpState state = client.getState();
    state.setCredentials(AuthScope.ANY, credentials);
    if (CurrentConfig.getBooleanParam(PARAM_USE_PREEMPTIVE_AUTH, DEFAULT_USE_PREEMPTIVE_AUTH)) {
        HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);
    }
}

From source file:org.opensaml.ws.soap.client.http.HttpClientBuilder.java

/**
 * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has
 * been created./*from   www .  j av a  2 s . c o  m*/
 * 
 * @return the created client.
 */
public HttpClient buildClient() {
    if (httpsProtocolSocketFactory != null) {
        Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
    clientParams.setContentCharset(getContentCharSet());
    clientParams.setParameter(HttpClientParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(connectionRetryAttempts, false));

    HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
    connMgrParams.setConnectionTimeout(getConnectionTimeout());
    connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
    connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
    connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
    connMgrParams.setSendBufferSize(getSendBufferSize());
    connMgrParams.setTcpNoDelay(isTcpNoDelay());

    MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
    connMgr.setParams(connMgrParams);

    HttpClient httpClient = new HttpClient(clientParams, connMgr);

    if (proxyHost != null) {
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
        httpClient.setHostConfiguration(hostConfig);

        if (proxyUsername != null) {
            AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);
            httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

    return httpClient;
}

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);/*from  w w w .j  av a  2s  .  c  o m*/

    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);
    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$
    }
}