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.opentravel.schemacompiler.repository.impl.RemoteRepositoryClient.java

/**
 * Configures the 'Authorization' header on the given HTTP request using the current user ID and
 * encrypted password that is configured for this repository.
 *///from  w  ww . j  av  a2  s  .  c  o  m
private HttpClientContext createHttpContext() {
    HttpClientContext context = HttpClientContext.create();

    if ((userId != null) && (encryptedPassword != null)) {
        AuthState target = new AuthState();
        target.update(new BasicScheme(),
                new UsernamePasswordCredentials(userId, PasswordHelper.decrypt(encryptedPassword)));
        context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, target);
    }
    return context;
}

From source file:org.apache.http.client.protocol.RequestAuthCache.java

private void doPreemptiveAuth(final HttpHost host, final AuthScheme authScheme, final AuthState authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
    }// w w w  .ja va2  s.  co m

    final AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        this.log.debug("No credentials for preemptive authentication");
    }
}

From source file:org.apache.http.client.protocol.RequestAuthenticationBase.java

void process(final AuthState authState, final HttpRequest request, final HttpContext context) {
    AuthScheme authScheme = authState.getAuthScheme();
    Credentials creds = authState.getCredentials();
    switch (authState.getState()) {
    case FAILURE:
        return;// ww w .j  a  v  a2 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 = authenticate(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 = authenticate(authScheme, creds, request, context);
            request.addHeader(header);
        } catch (final AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error(authScheme + " authentication error: " + ex.getMessage());
            }
        }
    }
}

From source file:org.apache.http.impl.auth.HttpAuthenticator.java

public 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;/*w  w  w .  j  ava  2  s.  c o m*/
    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());
            }
        }
    }
}

From source file:org.opennms.core.web.HttpClientWrapper.java

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**/* www .  jav  a 2  s  .  co m*/
     * Add an HttpRequestInterceptor that will perform preemptive authentication
     * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
     */
    final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        @Override
        public void process(final HttpRequest request, final HttpContext context) throws IOException {
            if (context instanceof HttpClientContext) {
                final HttpClientContext clientContext = (HttpClientContext) context;
                final AuthState authState = clientContext.getTargetAuthState();
                final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
                final HttpHost targetHost = clientContext.getTargetHost();
                // If not authentication scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    final Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.update(new BasicScheme(), creds);
                    }
                }
            } else {
                throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
            }
        }

    };
    builder.addInterceptorFirst(preemptiveAuth);
}