Example usage for org.apache.http.client.protocol ClientContext AUTH_CACHE

List of usage examples for org.apache.http.client.protocol ClientContext AUTH_CACHE

Introduction

In this page you can find the example usage for org.apache.http.client.protocol ClientContext AUTH_CACHE.

Prototype

String AUTH_CACHE

To view the source code for org.apache.http.client.protocol ClientContext AUTH_CACHE.

Click Source Link

Document

Attribute name of a org.apache.http.client.AuthCache object that represents the auth scheme cache.

Usage

From source file:org.etk.common.net.ETKHttpClient.java

public HttpEntity execute(String targetURL) throws ClientProtocolException, IOException {
    HttpHost targetHost = new HttpHost("127.0.0.1", 8080, "http");

    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials("demo", "gtn"));

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpGet httpget = new HttpGet(targetURL);
    Header header = new BasicHeader("Content-Type", "application/json");
    httpget.setHeader(header);/*from w ww .  j a  v  a2 s .  c o  m*/
    HttpResponse response = httpClient.execute(targetHost, httpget, localcontext);
    DumpHttpResponse.dumpHeader(response);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        entity = new BufferedHttpEntity(entity);
    }
    EntityUtils.consume(entity);

    return entity;
}

From source file:mobi.jenkinsci.ci.client.JenkinsHttpClient.java

public JenkinsHttpClient(final JenkinsConfig config) throws MalformedURLException {
    this.config = config;
    if (config.getUsername() == null) {
        httpClient = httpClientFactory.getHttpClient();
    } else {/*www .jav  a2  s .  c om*/
        final URL url = new URL(config.getUrl());
        httpClient = httpClientFactory.getBasicAuthHttpClient(url, config.getUsername(), config.getPassword());

        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(new HttpHost(url.getHost(), url.getPort()), basicAuth);

        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
}

From source file:org.syncope.client.http.PreemptiveAuthHttpRequestFactory.java

@Override
protected HttpContext createHttpContext(final HttpMethod httpMethod, final URI uri) {

    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    return localcontext;
}

From source file:magicware.scm.redmine.tools.RedmineClient.java

public void fillBasicAuth(String userName, String base64Pwd) {

    // Basic?/*from   w  w w.  j a v  a2  s  . c om*/
    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(userName,
                    StringUtils.isEmpty(base64Pwd) ? UUID.randomUUID().toString()
                            : new String(Base64.decodeBase64(base64Pwd))));
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}

From source file:cz.zcu.kiv.eegdatabase.logic.util.BasicAuthHttpClient.java

@Override
protected HttpContext createHttpContext() {
    HttpContext context = super.createHttpContext();

    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    authCache.put(targetHost, basicAuth);

    context.setAttribute(ClientContext.AUTH_CACHE, authCache);

    return context;
}

From source file:com.unispezi.cpanelremotebackup.http.HTTPClient.java

public HTTPClient(String hostName, int port, boolean secure, String userName, String password) {
    this.httpClient = new DefaultHttpClient();
    host = new HttpHost(hostName, port, secure ? "https" : null);
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostName, port),
            new UsernamePasswordCredentials(userName, password));

    //        HttpHost proxy = new HttpHost("localhost", 8888);
    //        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    // Set up HTTP basic auth (without challenge) or "preemptive auth"
    // Taken from http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e1031

    // Create AuthCache instance
    BasicAuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);/*from   w  ww.j a v a  2 s  .c o  m*/

    // Add AuthCache to the execution context
    localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}

From source file:com.netdimensions.client.Client.java

private static BasicHttpContext context(final URI url) {
    final BasicHttpContext result = new BasicHttpContext();
    result.setAttribute(ClientContext.AUTH_CACHE, authCache(url));
    return result;
}

From source file:com.consol.citrus.http.client.BasicAuthClientHttpRequestFactory.java

/**
 * Construct the client factory bean with user credentials.
 *//*  w w  w  .  j  a v a  2s. c  o  m*/
public HttpComponentsClientHttpRequestFactory getObject() throws Exception {
    Assert.notNull(credentials, "User credentials not set properly!");

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient) {
        @Override
        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
            // we have to use preemptive authentication
            // therefore add some basic auth cache to the local context
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();

            authCache.put(new HttpHost(authScope.getHost(), authScope.getPort(), "http"), basicAuth);
            authCache.put(new HttpHost(authScope.getHost(), authScope.getPort(), "https"), basicAuth);

            BasicHttpContext localcontext = new BasicHttpContext();
            localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

            return localcontext;
        }
    };

    if (httpClient instanceof AbstractHttpClient) {
        ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(authScope, credentials);
    } else {
        log.warn("Unable to set username password credentials for basic authentication, "
                + "because nested HttpClient implementation does not support a credentials provider!");
    }

    return requestFactory;
}

From source file:org.apache.jena.atlas.web.auth.PreemptiveBasicAuthenticator.java

@Override
public void apply(AbstractHttpClient client, HttpContext httpContext, URI target) {
    this.authenticator.apply(client, httpContext, target);

    // Enable preemptive basic authentication
    // For nice layering we need to respect existing auth cache if present
    AuthCache authCache = (AuthCache) httpContext.getAttribute(ClientContext.AUTH_CACHE);
    if (authCache == null)
        authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme(this.isProxy ? ChallengeState.PROXY : ChallengeState.TARGET);
    // TODO It is possible that this overwrites existing cached credentials
    // so potentially not ideal.
    authCache.put(new HttpHost(target.getHost(), target.getPort()), basicAuth);
    httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}

From source file:org.geosdi.geoplatform.connector.server.security.PreemptiveSecurityConnector.java

protected void preparePreemptiveParameters(HttpHost targetHost) {
    if (this.authCache == null) {
        this.authCache = new BasicAuthCache();
        this.authCache.put(targetHost, createScheme());
        this.localcontext.setAttribute(ClientContext.AUTH_CACHE, this.authCache);
    }/*from  www  .jav  a2  s. c o m*/
}