Example usage for org.apache.http.client CredentialsProvider getCredentials

List of usage examples for org.apache.http.client CredentialsProvider getCredentials

Introduction

In this page you can find the example usage for org.apache.http.client CredentialsProvider getCredentials.

Prototype

Credentials getCredentials(AuthScope authscope);

Source Link

Document

Get the Credentials credentials for the given authentication scope.

Usage

From source file:org.callimachusproject.repository.SparqlServiceCredentialManager.java

private synchronized Credentials getCredentials(String serviceUrl, String base)
        throws OpenRDFException, IOException {
    String origin = getOrigin(base);
    CalliRepository repository = repositories.get(origin);
    if (repository == null)
        return null;
    CredentialsProvider provider = repository.getRealm(base).getCredentialsProvider();
    if (provider == null)
        return null;
    HttpHost authority = URIUtils.extractHost(URI.create(serviceUrl));
    Credentials credential = provider.getCredentials(new AuthScope(authority));
    if (credential != null) {
        credentials.get(repository).add(credential);
    }/* w  w  w  .j av a2 s . c o m*/
    return credential;
}

From source file:at.bitfire.davdroid.webdav.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, 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 .ja v a 2  s.  c  om*/
    }
}

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);
        }//from  ww  w .  ja v  a 2s.  c o  m
    }
}

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);
        }// w w  w . j  a va  2s .  c  om
    }
}

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.  java2 s.  co  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:org.callimachusproject.xproc.SparqlStep.java

private RepositoryConnection createConnection(String endpoint) throws OpenRDFException, IOException {
    final SPARQLRepository repository = new SPARQLRepository(endpoint);
    HttpClient client = runtime.getHttpClient();
    if (client instanceof HttpRepositoryClient) {
        HttpRepositoryClient rclient = (HttpRepositoryClient) client;
        CredentialsProvider provider = rclient.getCredentialsProvider();
        HttpHost authority = URIUtils.extractHost(URI.create(endpoint));
        AuthScope scope = new AuthScope(authority);
        if (provider != null && provider.getCredentials(scope) != null) {
            Credentials cred = provider.getCredentials(scope);
            String username = cred.getUserPrincipal().getName();
            String password = cred.getPassword();
            repository.setUsernameAndPassword(username, password);
        }/*from w w w . j a v  a 2 s .  co  m*/
    } else {
        logger.warn("Repository credentials could not be read");
    }
    repository.initialize();
    RepositoryConnection con = repository.getConnection();
    return con;
}

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  .  jav  a 2 s. c om
    }
}

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 ww.j av a 2 s. c  om
    }
}

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   w  ww  .j  a  v  a 2  s  .  com*/
    }
}

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;
        }// w  w  w  .j  ava  2 s  .co 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);
    }
}