Example usage for org.apache.http.impl.client AbstractHttpClient getCredentialsProvider

List of usage examples for org.apache.http.impl.client AbstractHttpClient getCredentialsProvider

Introduction

In this page you can find the example usage for org.apache.http.impl.client AbstractHttpClient getCredentialsProvider.

Prototype

public synchronized final CredentialsProvider getCredentialsProvider() 

Source Link

Usage

From source file:nl.esciencecenter.octopus.webservice.JobLauncherService.java

/**
 * Adds MAC Access Authentication scheme to http client and registers list of MAC credentials with http client.
 *
 * Http client will use MAC Access Authentication when url is in scope of given MAC credentials.
 *
 * @param httpClient/*ww w.j  a v  a  2 s . c  o  m*/
 * @param macs
 * @return httpClient with MAC access authentication and credentials injected.
 */
public static AbstractHttpClient macifyHttpClient(AbstractHttpClient httpClient,
        ImmutableList<MacCredential> macs) {

    // Add MAC scheme
    httpClient.getAuthSchemes().register(MacScheme.SCHEME_NAME, new MacSchemeFactory());

    // Add configured MAC id/key pairs.
    CredentialsProvider credentialProvider = httpClient.getCredentialsProvider();
    for (MacCredential mac : macs) {
        credentialProvider.setCredentials(mac.getAuthScope(), mac);
    }

    // Add MAC scheme to ordered list of supported authentication schemes
    // See HTTP authentication parameters chapter on
    // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
    List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME,
            AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC }));
    httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authSchemes);

    return httpClient;
}

From source file:net.adamcin.httpsig.http.apache4.Http4Util.java

public static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId) {
    if (client == null) {
        throw new NullPointerException("client");
    }/*from  w  w  w  . j av  a 2 s  .  c o  m*/

    if (keychain == null) {
        throw new NullPointerException("keychain");
    }

    client.getAuthSchemes().register(Constants.SCHEME, new AuthSchemeFactory() {
        public AuthScheme newInstance(HttpParams params) {
            return new Http4SignatureAuthScheme();
        }
    });

    Signer signer = new Signer(keychain, keyId);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, new SignerCredentials(signer));
    client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(Constants.SCHEME));

    HttpClientParams.setAuthenticating(client.getParams(), true);
}

From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java

public static void configureAuthentication(AbstractHttpClient client, RepositoryLocation location,
        UserCredentials credentials) {//from ww  w .j a va2 s.co  m
    Assert.isNotNull(client);
    Assert.isNotNull(location);
    Assert.isNotNull(credentials);
    String url = location.getUrl();
    Assert.isNotNull(url, "The location url must not be null"); //$NON-NLS-1$

    String host = NetUtil.getHost(url);
    int port = NetUtil.getPort(url);

    NTCredentials ntlmCredentials = getNtCredentials(credentials, ""); //$NON-NLS-1$
    if (ntlmCredentials != null) {
        AuthScope authScopeNtlm = new AuthScope(host, port, AuthScope.ANY_REALM, AuthPolicy.NTLM);
        client.getCredentialsProvider().setCredentials(authScopeNtlm, ntlmCredentials);
    }

    UsernamePasswordCredentials usernamePasswordCredentials = getUserNamePasswordCredentials(credentials);
    AuthScope authScopeAny = new AuthScope(host, port, AuthScope.ANY_REALM);
    client.getCredentialsProvider().setCredentials(authScopeAny, usernamePasswordCredentials);
}

From source file:com.twitter.hbc.httpclient.auth.BasicAuth.java

public void setupConnection(AbstractHttpClient client) {
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));
}

From source file:bad.robot.http.apache.matchers.CredentialsMatcher.java

@Override
protected boolean matchesSafely(HttpClient actual, Description mismatch) {
    AbstractHttpClient client = (AbstractHttpClient) actual;
    Credentials credentials = client.getCredentialsProvider().getCredentials(scope);
    if (credentials == null) {
        mismatch.appendText("No credentials found for ").appendValue(scope);
        return false;
    }/*from  w  w  w. java 2s.  co  m*/
    if (!username.equals(credentials.getUserPrincipal().getName())) {
        mismatch.appendText("found username ").appendValue(credentials.getUserPrincipal().getName());
        return false;
    }
    if (!password.equals(credentials.getPassword())) {
        mismatch.appendText("found password ").appendValue(credentials.getPassword());
        return false;
    }
    return true;
}

From source file:org.springframework.data.solr.server.support.HttpSolrServerFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrServer solrServer) {
    if (isHttpSolrServer(solrServer)) {
        HttpSolrServer httpSolrServer = (HttpSolrServer) solrServer;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrServer.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrServer.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }//from   w  w  w .  j a va2 s.c  om
    }
}

From source file:com.frank.search.solr.server.support.HttpSolrClientFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrClient solrClient) {
    if (isHttpSolrClient(solrClient)) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrClient.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrClient.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }/*ww  w  .  j  a  va 2 s. c o m*/
    }
}

From source file:at.pagu.soldockr.core.HttpSolrServerFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrServer solrServer) {
    if (assertSolrServerInstance(solrServer)) {
        HttpSolrServer httpSolrServer = (HttpSolrServer) solrServer;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrServer.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrServer.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }/*from  www .ja  v  a2  s  .  co  m*/
    }
}

From source file:at.pagu.soldockr.core.HttpSolrServerFactoryTest.java

@Test
public void testInitFactoryWithAuthentication() {
    HttpSolrServerFactory factory = new HttpSolrServerFactory(solrServer, "core",
            new UsernamePasswordCredentials("username", "password"), "BASIC");

    AbstractHttpClient solrHttpClient = (AbstractHttpClient) ((HttpSolrServer) factory.getSolrServer())
            .getHttpClient();/*from  w w w  .  j  a  va2 s .  co  m*/
    Assert.assertNotNull(solrHttpClient.getCredentialsProvider().getCredentials(AuthScope.ANY));
    Assert.assertNotNull(solrHttpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
    Assert.assertEquals("username", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getUserName());
    Assert.assertEquals("password", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getPassword());
}

From source file:org.springframework.data.solr.HttpSolrClientFactoryTests.java

@Test
public void testInitFactoryWithAuthentication() {
    HttpSolrClientFactory factory = new HttpSolrClientFactory(solrClient, "core",
            new UsernamePasswordCredentials("username", "password"), "BASIC");

    AbstractHttpClient solrHttpClient = (AbstractHttpClient) ((HttpSolrClient) factory.getSolrClient())
            .getHttpClient();/*from   w w  w. ja  v  a2  s  .  co  m*/
    Assert.assertNotNull(solrHttpClient.getCredentialsProvider().getCredentials(AuthScope.ANY));
    Assert.assertNotNull(solrHttpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
    Assert.assertEquals("username", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getUserName());
    Assert.assertEquals("password", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getPassword());
}