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.behaviours.SqlDatasourceSupport.java

private Credentials getCredential(String url) throws OpenRDFException, IOException {
    Matcher m = HOST_POST_REGEX.matcher(url);
    if (!m.find())
        return null;
    String host = m.group(1);/*from   www  .j  av a2s  . c  o m*/
    int port = Integer.parseInt(m.group(2));
    String uri = this.getResource().stringValue();
    DetachedRealm realm = this.getCalliRepository().getRealm(uri);
    CredentialsProvider creds = realm.getCredentialsProvider();
    return creds.getCredentials(new AuthScope(host, port));
}

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

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**//from   w  w w . j  ava2  s.c om
     * 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);
}