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:com.consol.citrus.http.client.BasicAuthClientRequestFactoryTest.java

@Test
public void testFactory() {
    Assert.assertNotNull(requestFactory);
    Assert.assertNotNull(requestFactory.getHttpClient());
    Assert.assertNotNull(requestFactory.getHttpClient().getParams());

    CredentialsProvider credentialsProvider = ((AbstractHttpClient) requestFactory.getHttpClient())
            .getCredentialsProvider();//w  ww  .  ja va 2 s.co m
    AuthScope authScope = new AuthScope("localhost", 8088, "", "basic");
    Assert.assertNotNull(credentialsProvider);
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope));
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope).getUserPrincipal().getName(),
            "someUsername");
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope).getPassword(), "somePassword");

    Assert.assertNotNull(requestFactory.getHttpClient().getParams().getParameter("http.socket.timeout"));
    Assert.assertEquals(requestFactory.getHttpClient().getParams().getIntParameter("http.socket.timeout", 0),
            10000);
}

From source file:com.controlj.experiment.bulktrend.trendclient.PreemptiveAuthRequestInterceptor.java

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 avaialble 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 creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/* w  ww.  j  a va 2s  .  c o m*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }
}

From source file:com.mgmtp.jfunk.web.JFunkHtmlUnitDriverImpl.java

@Override
public Credentials getCredentials(final AuthScope authscope) {
    String host = authscope.getHost();
    log.debug("Retrieving credentials for host " + host);

    CredentialsProvider credentialsProvider = credentialsProviderMap.get(host);
    checkState(credentialsProvider != null, "No credentials provider found for host " + host);

    return credentialsProvider.getCredentials(authscope);
}

From source file:com.mycompany.projecta.PreemptiveAuth.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    // Get the AuthState
    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 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 a  va  2s.  co m
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}

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");
            }//from  www  . j  a  va2 s.  c  om
            authState.update(authScheme, credentials);
        }
    }

}

From source file:de.escidoc.core.common.util.security.PreemptiveAuthInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    final AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        final AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        final CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        final HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            final Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication.");
            }/*w  ww  .j  a va 2s .c  om*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}

From source file:com.unboundid.scim.sdk.PreemptiveAuthInterceptor.java

/**
 * Method to update the AuthState in order to preemptively supply the
 * credentials to the server.//from   w  ww  .  ja v  a 2  s . c  o m
 *
 * @param host the HttpHost which we're authenticating to
 * @param authScheme the AuthScheme in use
 * @param authState the AuthState object from the HttpContext
 * @param credsProvider the CredentialsProvider which has the username and
 *                      password
 */
private void doPreemptiveAuth(final HttpHost host, final AuthScheme authScheme, final AuthState authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();

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

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(schemeName)) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    }
}

From source file:org.sonatype.nexus.apachehttpclient.Hc4ProviderImplTest.java

@Test
public void credentialsProviderReplaced() {
    testSubject = new Hc4ProviderImpl(applicationConfiguration, userAgentBuilder, eventBus, jmxInstaller, null);

    final Builder builder = testSubject
            .prepareHttpClient(applicationConfiguration.getGlobalRemoteStorageContext());

    final RemoteAuthenticationSettings remoteAuthenticationSettings = new UsernamePasswordRemoteAuthenticationSettings(
            "user", "pass");
    testSubject.applyAuthenticationConfig(builder, remoteAuthenticationSettings, null);

    final DefaultRemoteHttpProxySettings httpProxy = new DefaultRemoteHttpProxySettings();
    httpProxy.setHostname("http-proxy");
    httpProxy.setPort(8080);/*  ww w.ja v  a  2s. c o m*/
    httpProxy.setProxyAuthentication(
            new UsernamePasswordRemoteAuthenticationSettings("http-proxy", "http-pass"));
    final DefaultRemoteHttpProxySettings httpsProxy = new DefaultRemoteHttpProxySettings();
    httpsProxy.setHostname("https-proxy");
    httpsProxy.setPort(9090);
    httpsProxy.setProxyAuthentication(
            new UsernamePasswordRemoteAuthenticationSettings("https-proxy", "https-pass"));
    final DefaultRemoteProxySettings remoteProxySettings = new DefaultRemoteProxySettings();
    remoteProxySettings.setHttpProxySettings(httpProxy);
    remoteProxySettings.setHttpsProxySettings(httpsProxy);

    testSubject.applyProxyConfig(builder, remoteProxySettings);

    final CredentialsProvider credentialsProvider = builder.getCredentialsProvider();

    assertThat(credentialsProvider.getCredentials(AuthScope.ANY), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), equalTo("user"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), equalTo("pass"));

    final AuthScope httpProxyAuthScope = new AuthScope(new HttpHost("http-proxy", 8080));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope).getUserPrincipal().getName(),
            equalTo("http-proxy"));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope).getPassword(), equalTo("http-pass"));

    final AuthScope httpsProxyAuthScope = new AuthScope(new HttpHost("https-proxy", 9090));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope).getUserPrincipal().getName(),
            equalTo("https-proxy"));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope).getPassword(), equalTo("https-pass"));
}

From source file:org.sonatype.nexus.internal.httpclient.HttpClientFactoryImplTest.java

@Test
public void credentialsProviderReplaced() {
    testSubject = new HttpClientFactoryImpl(Providers.of(systemStatus),
            Providers.of(globalRemoteStorageContext), eventBus, jmxInstaller, null);

    RemoteStorageContextCustomizer customizer = new RemoteStorageContextCustomizer(globalRemoteStorageContext);
    final Builder builder = testSubject.prepare(customizer);

    final RemoteAuthenticationSettings remoteAuthenticationSettings = new UsernamePasswordRemoteAuthenticationSettings(
            "user", "pass");
    customizer.applyAuthenticationConfig(builder, remoteAuthenticationSettings, null);

    final DefaultRemoteHttpProxySettings httpProxy = new DefaultRemoteHttpProxySettings();
    httpProxy.setHostname("http-proxy");
    httpProxy.setPort(8080);/*from   w  w w .java  2s  . co m*/
    httpProxy.setProxyAuthentication(
            new UsernamePasswordRemoteAuthenticationSettings("http-proxy", "http-pass"));

    final DefaultRemoteHttpProxySettings httpsProxy = new DefaultRemoteHttpProxySettings();
    httpsProxy.setHostname("https-proxy");
    httpsProxy.setPort(9090);
    httpsProxy.setProxyAuthentication(
            new UsernamePasswordRemoteAuthenticationSettings("https-proxy", "https-pass"));

    final DefaultRemoteProxySettings remoteProxySettings = new DefaultRemoteProxySettings();
    remoteProxySettings.setHttpProxySettings(httpProxy);
    remoteProxySettings.setHttpsProxySettings(httpsProxy);

    customizer.applyProxyConfig(builder, remoteProxySettings);

    final CredentialsProvider credentialsProvider = builder.getCredentialsProvider();

    assertThat(credentialsProvider.getCredentials(AuthScope.ANY), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), equalTo("user"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), equalTo("pass"));

    final AuthScope httpProxyAuthScope = new AuthScope(new HttpHost("http-proxy", 8080));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope).getUserPrincipal().getName(),
            equalTo("http-proxy"));
    assertThat(credentialsProvider.getCredentials(httpProxyAuthScope).getPassword(), equalTo("http-pass"));

    final AuthScope httpsProxyAuthScope = new AuthScope(new HttpHost("https-proxy", 9090));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope), notNullValue(Credentials.class));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope).getUserPrincipal().getName(),
            equalTo("https-proxy"));
    assertThat(credentialsProvider.getCredentials(httpsProxyAuthScope).getPassword(), equalTo("https-pass"));
}

From source file:com.liferay.portal.search.solr.interceptor.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext httpContext) {
    AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() != null) {
        return;//w w  w  .  jav a2  s  .  c  o m
    }

    CredentialsProvider credentialsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);

    HttpHost targetHttpHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    AuthScope authScope = new AuthScope(targetHttpHost.getHostName(), targetHttpHost.getPort());

    Credentials credentials = credentialsProvider.getCredentials(authScope);

    if (credentials != null) {
        authState.update(new BasicScheme(), credentials);
    }
}