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

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

Introduction

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

Prototype

public void update(final AuthScheme authScheme, final Credentials credentials) 

Source Link

Document

Updates the auth state with AuthScheme and Credentials .

Usage

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 {/* w w  w  .j  a v  a2s  . co m*/
            log.debug("Updating credentials for host " + targetHost);
            authState.update(new BasicScheme(), creds);
        }
    }
}

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");
            }//from  ww  w  . j  a v a  2 s. co m
            authState.update(authScheme, creds);
        }
    }

}

From source file:kuona.client.PreemptiveAuth.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 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  w  w  w .  j  av  a 2  s .  c  o m*/
            authState.update(authScheme, creds);
        }
    }
}

From source file:com.github.pascalgn.jiracli.web.HttpClient.java

private void setCredentials() {
    AuthState authState = httpClientContext.getTargetAuthState();
    if (authState != null) {
        CredentialsProvider credentialsProvider = httpClientContext.getCredentialsProvider();
        AuthScope authScope = new AuthScope(HttpHost.create(getBaseUrl()));
        org.apache.http.auth.Credentials credentials = credentialsProvider.getCredentials(authScope);
        if (credentials != null) {
            authState.update(new BasicScheme(), credentials);
            authState.setState(AuthProtocolState.CHALLENGED);
        }/*w ww.  j a  v a2 s.  c  o m*/
    }
}

From source file:org.xwiki.extension.repository.http.internal.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) {
        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 credentials = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (credentials == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*w  w  w. j  a v  a2 s  .  c  o m*/
            authState.update(authScheme, credentials);
        }
    }

}

From source file:org.artifactory.util.ProxyPreemptiveAuthInterceptor.java

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

    // If there's no auth scheme available yet, try to initialize it preemptively
    if (proxyAuthState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        RouteInfo route = clientContext.getHttpRoute();
        if (route == null) {
            log.debug("No route found for {}", clientContext.getTargetHost());
            return;
        }/*from   w w  w.ja va2 s .  c o m*/

        HttpHost proxyHost = route.getProxyHost();
        if (proxyHost == null) {
            log.warn("No proxy host found in route {} for host {}", route, clientContext.getTargetHost());
            return;
        }

        Credentials creds = credsProvider
                .getCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()));
        if (creds == null) {
            log.info("No credentials found for proxy: " + proxyHost);
            return;
        }
        proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), 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.
 *//* w  w  w  . j  av a 2  s.  c  om*/
@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.callimachusproject.client.HttpAuthenticator.java

private void generateAuthResponse(final HttpRequest request, final AuthState authState,
        final HttpContext context) throws HttpException, IOException {
    AuthScheme authScheme = authState.getAuthScheme();
    Credentials creds = authState.getCredentials();
    switch (authState.getState()) {
    case FAILURE:
        return;/*from ww  w. j  a  va 2  s  .  com*/
    case SUCCESS:
        ensureAuthScheme(authScheme);
        if (authScheme.isConnectionBased()) {
            return;
        }
        break;
    case CHALLENGED:
        final Queue<AuthOption> authOptions = authState.getAuthOptions();
        if (authOptions != null) {
            while (!authOptions.isEmpty()) {
                final AuthOption authOption = authOptions.remove();
                authScheme = authOption.getAuthScheme();
                creds = authOption.getCredentials();
                authState.update(authScheme, creds);
                if (this.log.isDebugEnabled()) {
                    this.log.debug("Generating response to an authentication challenge using "
                            + authScheme.getSchemeName() + " scheme");
                }
                try {
                    final Header header = doAuth(authScheme, creds, request, context);
                    request.addHeader(header);
                    break;
                } catch (final AuthenticationException ex) {
                    if (this.log.isWarnEnabled()) {
                        this.log.warn(authScheme + " authentication error: " + ex.getMessage());
                    }
                }
            }
            return;
        } else {
            ensureAuthScheme(authScheme);
        }
    }
    if (authScheme != null) {
        try {
            final Header header = doAuth(authScheme, creds, request, context);
            request.addHeader(header);
        } catch (final AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error(authScheme + " authentication error: " + ex.getMessage());
            }
        }
    }
}