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

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

Introduction

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

Prototype

public AuthScheme getAuthScheme() 

Source Link

Document

Returns actual AuthScheme .

Usage

From source file:com.microsoft.exchange.impl.http.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme preemptiveAuthScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        if (preemptiveAuthScheme != null) {
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*  w w w . j a  va 2s  . c o  m*/
            authState.setAuthScheme(preemptiveAuthScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + preemptiveAuthScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context; make sure you are using the CustomHttpComponentsMessageSender and the preemptiveAuthEnabled property is set to true");
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }
}

From source file:se.vgregion.pubsub.twitter.impl.PreemptiveBasicAuth.java

public void process(HttpRequest request, HttpContext context) {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        Credentials creds = new UsernamePasswordCredentials(username, password);
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(creds);
    }/*w w w  .  j  a  v a  2s .co m*/
}

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.
 *//*from ww w.  ja v  a  2 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:org.artifactory.util.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState authState = clientContext.getTargetAuthState();

    // If there's no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        HttpHost targetHost = clientContext.getTargetHost();
        Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds == null) {
            log.debug("No credentials found for host " + targetHost);
        } else {/*from w  w  w  .  ja  v a 2 s  .  co  m*/
            log.debug("Updating credentials for host " + targetHost);
            authState.update(new BasicScheme(), creds);
        }
    }
}

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.
 *//*from w ww  .  j av  a2  s.  co  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:com.soundcloud.playerapi.OAuth2HttpRequestInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    if (request == null)
        throw new IllegalArgumentException("HTTP request may not be null");
    if (context == null)
        throw new IllegalArgumentException("HTTP context may not be null");

    if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        if (authState != null) {
            AuthScheme authScheme = authState.getAuthScheme();
            if (authScheme != null && !authScheme.isConnectionBased()) {
                try {
                    request.setHeader(authScheme.authenticate(null, request));
                } catch (AuthenticationException ignored) {
                    // ignored
                }//from   ww  w  . j av a2  s  .  c  o  m
            }
        }
    }
}

From source file:org.apache.solr.client.solrj.impl.PreemptiveAuth.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    // If no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
        authState.update(authScheme, creds);
    }//from   w  w w.ja v  a2s  .  co  m
}

From source file:org.apache.camel.component.http4.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/* ww w.  j a  v  a 2s  . co m*/
            authState.update(authScheme, creds);
        }
    }

}

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.HttpPreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() != null) {
        return;/*from   www .j  a  va2 s  .  c  o  m*/
    }

    // fetch credentials
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

    // Obtain credentials matching the target host
    Credentials creds = credsProvider.getCredentials(authScope);

    if (creds == null) {
        log.warn("no credentials provided for " + authScope);
        return;
    }

    authState.setAuthScheme(authScheme);
    authState.setCredentials(creds);
}

From source file:com.controlj.experiment.bulktrend.trendclient.PreemptiveAuthRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }//from ww  w  . jav a 2 s.  c o  m
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }
}