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

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

Introduction

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

Prototype

String AUTH_CACHE

To view the source code for org.apache.http.client.protocol HttpClientContext 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.springframework.cloud.dataflow.shell.command.support.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java

private HttpContext createHttpContext() {
    final AuthCache authCache = new BasicAuthCache();

    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);/*from w w  w  . ja va2s  . c o m*/

    final BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}

From source file:com.github.avarabyeu.restendpoint.http.PreemptiveAuthInterceptor.java

/**
 * Adds provided auth scheme to the client if there are no another provided
 * auth schemes//from   w ww .jav a  2s .c  om
 */
@Override
public final void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {

        HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        AuthCache authCache = new BasicAuthCache();
        authCache.put(targetHost, new BasicScheme());
        context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    }
}

From source file:org.springframework.cloud.dataflow.rest.util.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    final AuthCache authCache = new BasicAuthCache();

    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);/*  www. ja  v a2  s. co  m*/

    final BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}

From source file:com.nestorledon.employeedirectory.http.HttpComponentsClientHttpRequestFactoryBasicAuth.java

private HttpContext createHttpContext(HttpHost host) {
    // 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(host, basicAuth);/*from  w  w  w  .  j a  v  a 2  s .c  o  m*/
    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}

From source file:org.aludratest.service.gui.web.selenium.TAFMSSeleniumResourceService.java

@Override
public String acquire() {
    // prepare a JSON query to the given TAFMS server
    JSONObject query = new JSONObject();

    try {/*from w  w w. j  av a 2s. c o  m*/
        query.put("resourceType", "selenium");
        query.put("niceLevel", configuration.getIntValue("tafms.niceLevel", 0));
        String jobName = configuration.getStringValue("tafms.jobName");
        if (jobName != null && !"".equals(jobName)) {
            query.put("jobName", jobName);
        }
    } catch (JSONException e) {
    }

    // prepare authentication
    BasicCredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
            configuration.getStringValue("tafms.user"), configuration.getStringValue("tafms.password")));

    CloseableHttpClient client = HttpClientBuilder.create()
            .setConnectionReuseStrategy(new NoConnectionReuseStrategy()).disableConnectionState()
            .disableAutomaticRetries().setDefaultCredentialsProvider(provider).build();

    String message = null;
    try {
        boolean wait;

        // use preemptive authentication to avoid double connection count
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        URL url = new URL(getTafmsUrl());
        HttpHost host = new HttpHost(url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort(),
                url.getProtocol());
        authCache.put(host, basicAuth);

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

        do {
            // send a POST request to resource URL
            HttpPost request = new HttpPost(getTafmsUrl() + "resource");

            // attach query as JSON string data
            request.setEntity(new StringEntity(query.toString(), ContentType.APPLICATION_JSON));

            CloseableHttpResponse response = null;

            // fire request
            response = client.execute(request, localcontext);

            try {
                if (response.getStatusLine() == null) {
                    throw new ClientProtocolException("No HTTP status line transmitted");
                }

                message = extractMessage(response);
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    LOG.error("Exception when querying TAFMS server for resource. HTTP Status: "
                            + response.getStatusLine().getStatusCode() + ", message: " + message);
                    return null;
                }

                JSONObject object = new JSONObject(message);
                if (object.has("errorMessage")) {
                    LOG.error("TAFMS server reported an error: " + object.get("errorMessage"));
                    return null;
                }

                // continue wait?
                if (object.has("waiting") && object.getBoolean("waiting")) {
                    wait = true;
                    query.put("requestId", object.getString("requestId"));
                } else {
                    JSONObject resource = object.optJSONObject("resource");
                    if (resource == null) {
                        LOG.error("TAFMS server response did not provide a resource. Message was: " + message);
                        return null;
                    }

                    String sUrl = resource.getString("url");
                    hostResourceIds.put(sUrl, object.getString("requestId"));

                    return sUrl;
                }
            } finally {
                IOUtils.closeQuietly(response);
            }
        } while (wait);

        // should never come here
        return null;
    } catch (ClientProtocolException e) {
        LOG.error("Exception in HTTP transmission", e);
        return null;
    } catch (IOException e) {
        LOG.error("Exception in communication with TAFMS server", e);
        return null;
    } catch (JSONException e) {
        LOG.error("Invalid JSON received from TAFMS server. JSON message was: " + message, e);
        return null;
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:com.github.sardine.impl.SardineImpl.java

@Override
public void enablePreemptiveAuthentication(String hostname) {
    AuthCache cache = new BasicAuthCache();
    // Generate Basic preemptive scheme object and stick it to the local execution context
    BasicScheme basicAuth = new BasicScheme();
    // Configure HttpClient to authenticate preemptively by prepopulating the authentication data cache.
    for (String scheme : Arrays.asList("http", "https")) {
        cache.put(new HttpHost(hostname, -1, scheme), basicAuth);
    }/*from w w  w.j a v a2s .co m*/
    // Add AuthCache to the execution context
    this.context.setAttribute(HttpClientContext.AUTH_CACHE, cache);
}

From source file:com.github.sardine.impl.SardineImpl.java

@Override
public void disablePreemptiveAuthentication() {
    this.context.removeAttribute(HttpClientContext.AUTH_CACHE);
}

From source file:com.sangupta.jerry.http.HttpExecutor.java

/**
 * Execute the given web request and return the obtained raw web response.
 * /* w  w w.  j a  v  a 2 s  .c om*/
 * @param webRequest
 *            the {@link WebRequest} to be executed
 * 
 * @return the {@link WebRawResponse} obtained after execution
 * 
 * @throws IOException
 *             if something fails
 * 
 * @throws ClientProtocolException
 *             if something fails
 */
public WebRawResponse execute(WebRequest webRequest) throws ClientProtocolException, IOException {
    // sharing the context may lead to circular redirects in case
    // of redirections from two request objects towards a single
    // URI - like hitting http://google.com twice leads to circular
    // redirects in the second request
    HttpContext localHttpContext = new BasicHttpContext();

    localHttpContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
    localHttpContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
    localHttpContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);

    // localHttpContext.removeAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);

    HttpRequestBase httpRequest = webRequest.getHttpRequest();
    httpRequest.reset();
    return new WebRawResponse(this.client.execute(httpRequest, localHttpContext), localHttpContext);
}