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

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

Introduction

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

Prototype

@Deprecated
public void setCredentials(final Credentials credentials) 

Source Link

Document

Sets user Credentials to be used for authentication

Usage

From source file:dtu.ds.warnme.app.ws.client.https.ssl.PreemptiveAuthenticationRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//w w w .  j  a  v a2 s  .com
    }
}

From source file:cn.com.loopj.android.http.PreemptiveAuthorizationHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }/*from   w w  w.  jav  a2  s  .co  m*/
    }
}

From source file:org.jasig.schedassist.impl.caldav.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final 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);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(caldavAdminAuthScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*from ww  w  .ja  v a 2s .  co m*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + authScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context, failed to set scheme and credentials for " + request);
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }

}

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 v a2s. 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:pl.llp.aircasting.util.http.HttpBuilder.java

private HttpRequestInterceptor preemptiveAuth() {
    return new HttpRequestInterceptor() {
        @Override/*from w w  w .j av  a 2s .  c o m*/
        public void process(HttpRequest httpRequest, HttpContext httpContext)
                throws HttpException, IOException {
            AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);

            Credentials credentials;
            if (useLogin) {
                credentials = new UsernamePasswordCredentials(login, password);
            } else {
                credentials = new UsernamePasswordCredentials(settingsHelper.getAuthToken(), "X");
            }

            authState.setAuthScope(AuthScope.ANY);
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(credentials);
        }
    };
}

From source file:de.escidoc.core.http.PreemtiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest httpRequest, final HttpContext httpContext)
        throws HttpException, IOException {
    final AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    final CredentialsProvider credsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);
    final HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth 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.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from w w  w. j  ava 2s.  c o  m
    }
}

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  w  w w.  j  av a2s  .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.rightscale.provider.rest.DashboardSession.java

private HttpRequestInterceptor createBasicAuthInterceptor() {
    return new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }/*from   www. j  a  v  a  2  s .  co m*/
            }
        }
    };
}

From source file:org.gbif.utils.PreemptiveAuthenticationInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    LOG.debug(request.getRequestLine().toString());

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null && credsProvider != null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            LOG.debug("Authentication used for scope " + authScope.getHost());
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from  ww  w .  j a v a2  s  .  co m
    }
}

From source file:org.orbeon.oxf.resources.handler.PreemptiveAuthHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credentialsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials credentials = credentialsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (credentials != null) {
            authState.setAuthScheme(credentials instanceof NTCredentials ? new NTLMScheme(new JCIFSEngine())
                    : new BasicScheme());
            authState.setCredentials(credentials);
        }//from ww  w.  j a  va  2  s.  c o m
    }
}