Example usage for org.apache.http.auth AuthSchemeRegistry getAuthScheme

List of usage examples for org.apache.http.auth AuthSchemeRegistry getAuthScheme

Introduction

In this page you can find the example usage for org.apache.http.auth AuthSchemeRegistry getAuthScheme.

Prototype

public AuthScheme getAuthScheme(final String name, final HttpParams params) throws IllegalStateException 

Source Link

Document

Gets the AuthScheme authentication scheme with the given name.

Usage

From source file:net.oauth.client.httpclient4.PreemptiveAuthorizer.java

/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 *///w ww  .ja v a2 s.  c o  m
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(),
                    scheme.getSchemeName());
            Credentials cred = creds.getCredentials(targetScope);
            if (cred != null) {
                authState.setAuthScheme(scheme);
                authState.setCredentials(cred);
                return;
            }
        }
    }
}

From source file:glaze.oauth.PreemptiveAuthorizer.java

/**
 * If no auth scheme has been selected for the given context, consider each
 * of the preferred auth schemes and select the first one for which an
 * AuthScheme and matching Credentials are available.
 *//* w  ww . j a  va2 s. c  o m*/
@SuppressWarnings("unchecked")
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    if (authState != null && authState.getAuthScheme() != null) {
        return;
    }

    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider provider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    HttpParams params = request.getParams();

    for (String schemeName : (Iterable<String>) params.getParameter(AuthPNames.PROXY_AUTH_PREF)) {
        AuthScheme scheme = schemes.getAuthScheme(schemeName, params);
        if (scheme != null) {
            AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(),
                    scheme.getSchemeName());
            Credentials creds = provider.getCredentials(targetScope);

            if (creds != null) {
                authState.update(scheme, creds);
                return;
            }
        }
    }
}

From source file:org.apache.http.impl.client.AbstractAuthenticationHandler.java

public AuthScheme selectScheme(final Map<String, Header> challenges, final HttpResponse response,
        final HttpContext context) throws AuthenticationException {

    final AuthSchemeRegistry registry = (AuthSchemeRegistry) context
            .getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    Asserts.notNull(registry, "AuthScheme registry");
    Collection<String> authPrefs = getAuthPreferences(response, context);
    if (authPrefs == null) {
        authPrefs = DEFAULT_SCHEME_PRIORITY;
    }/*from w w w . j  a v  a 2 s.c  om*/

    if (this.log.isDebugEnabled()) {
        this.log.debug("Authentication schemes in the order of preference: " + authPrefs);
    }

    AuthScheme authScheme = null;
    for (final String id : authPrefs) {
        final Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));

        if (challenge != null) {
            if (this.log.isDebugEnabled()) {
                this.log.debug(id + " authentication scheme selected");
            }
            try {
                authScheme = registry.getAuthScheme(id, response.getParams());
                break;
            } catch (final IllegalStateException e) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication scheme " + id + " not supported");
                    // Try again
                }
            }
        } else {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Challenge for " + id + " authentication scheme not available");
                // Try again
            }
        }
    }
    if (authScheme == null) {
        // If none selected, something is wrong
        throw new AuthenticationException("Unable to respond to any of these challenges: " + challenges);
    }
    return authScheme;
}