Example usage for org.apache.http.impl.auth BasicScheme getSchemeName

List of usage examples for org.apache.http.impl.auth BasicScheme getSchemeName

Introduction

In this page you can find the example usage for org.apache.http.impl.auth BasicScheme getSchemeName.

Prototype

public String getSchemeName() 

Source Link

Document

Returns textual designation of the basic authentication scheme.

Usage

From source file:io.kahu.hawaii.util.call.http.HttpRequestBuilder.java

private void addAuthentication(AbortableHttpRequest<T> request, URI uri) {
    if (credentials != null) {
        CredentialsProvider credentialsProvider = null;

        switch (credentials.getAuthenticationType()) {
        case BASIC:
            // fall-through to default
        default://from w ww.j av  a 2s .  c om
            BasicScheme authenticationScheme = new BasicScheme();

            credentialsProvider = new BasicCredentialsProvider();
            // Explicitly set the AuthScope to ANY_REALM in order to get
            // 'preemptive authentication' to work
            credentialsProvider.setCredentials(
                    new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM,
                            authenticationScheme.getSchemeName()),
                    new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));

            // Create an auth cache for preemptive authentication.
            AuthCache authCache = new BasicAuthCache();
            authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), authenticationScheme);
            request.getHttpClientContext().setAuthCache(authCache);

            break;
        }

        request.getHttpClientContext().setCredentialsProvider(credentialsProvider);
    }
}

From source file:org.apache.abdera2.common.protocol.Session.java

public void usePreemptiveAuthentication(String target, String realm) throws URISyntaxException {
    AuthCache cache = (AuthCache) localContext.getAttribute(ClientContext.AUTH_CACHE);
    if (cache == null) {
        String host = AuthScope.ANY_HOST;
        int port = AuthScope.ANY_PORT;
        if (target != null) {
            URI uri = new URI(target);
            host = uri.getHost();//from w  w w  .jav  a  2s. c  o m
            port = uri.getPort();
        }
        BasicScheme basicAuth = new BasicScheme();
        HttpHost targetHost = new HttpHost(host, port, basicAuth.getSchemeName());
        cache = new BasicAuthCache();
        cache.put(targetHost, basicAuth);
        localContext.setAttribute(ClientContext.AUTH_CACHE, cache);
    }
}