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

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

Introduction

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

Prototype

void setCredentials(AuthScope authscope, Credentials credentials);

Source Link

Document

Sets the Credentials credentials for the given authentication scope.

Usage

From source file:com.googlesource.gerrit.plugins.github.oauth.PooledHttpClientProvider.java

@Override
public HttpClient get() {
    HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnPerRoute(maxConnectionPerRoute)
            .setMaxConnTotal(maxTotalConnection);

    if (proxy.getProxyUrl() != null) {
        URL url = proxy.getProxyUrl();
        builder.setProxy(new HttpHost(url.getHost(), url.getPort()));
        if (!Strings.isNullOrEmpty(proxy.getUsername()) && !Strings.isNullOrEmpty(proxy.getPassword())) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials(proxy.getUsername(),
                    proxy.getPassword());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), creds);
            builder.setDefaultCredentialsProvider(credsProvider);
            builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
        }//from  www .j a  va2s. com
    }

    return builder.build();
}

From source file:com.icoin.trading.bitcoin.client.BitcoinClientDefaultConfig.java

private CredentialsProvider credentialsProvicer() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(host, Integer.valueOf(port)),
            new UsernamePasswordCredentials(user, password));
    return credsProvider;
}

From source file:com.rometools.test.HttpClientFeedFetcherTest.java

/**
 * @see com.rometools.rome.fetcher.impl.AbstractJettyTest#getAuthenticatedFeedFetcher()
 *//*  w w w  .j  ava2  s  .  c om*/
@Override
public FeedFetcher getAuthenticatedFeedFetcher() {
    HttpClientFeedFetcher fetcher = new HttpClientFeedFetcher();
    fetcher.setContextSupplier(new ContextSupplier() {
        @Override
        public HttpClientContext getContext(HttpHost host) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("username:password"));
            HttpClientContext context = HttpClientContext.create();
            context.setCredentialsProvider(credentialsProvider);

            return context;
        }
    });

    return fetcher;
}

From source file:com.liferay.portal.search.solr.http.BasicAuthPoolingHttpClientFactory.java

@Override
protected void configure(DefaultHttpClient defaultHttpClient) {
    if (Validator.isBlank(_username)) {
        return;/*from  www . ja v a2 s .  c  o m*/
    }

    if (_authScope == null) {
        _authScope = AuthScope.ANY;
    }

    if (Validator.isNull(_password)) {
        _password = StringPool.BLANK;
    }

    CredentialsProvider credentialsProvider = defaultHttpClient.getCredentialsProvider();

    credentialsProvider.setCredentials(_authScope, new UsernamePasswordCredentials(_username, _password));
}

From source file:com.smartling.api.sdk.util.HttpProxyUtils.java

/**
 * Get an HttpClient given a proxy config if any
 * @param proxyConfiguration configuration of proxy to use
 * @return org.apache.http.impl.client.CloseableHttpClient
 *///from  ww  w  . jav  a2 s.c  om
public CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder httpClientBuilder = getHttpClientBuilder();

    if (proxyAuthenticationRequired(proxyConfiguration)) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()),
                new UsernamePasswordCredentials(proxyConfiguration.getUsername(),
                        proxyConfiguration.getPassword()));

        httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return httpClientBuilder.build();
}

From source file:mobi.jenkinsci.server.core.net.PooledHttpClientFactory.java

private CredentialsProvider getCredentialsProvider(final URL url, final String user, final String password) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM, "BASIC"),
            new UsernamePasswordCredentials(user, password));
    return provider;
}

From source file:com.webbfontaine.valuewebb.startup.ProxyConfiguration.java

public void addProxy(AbstractHttpClient httpClient) {
    if (ApplicationProperties.isAllowProxy()) {
        LOGGER.debug("Adding PROXY for HTTP Client");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(host, port),
                new UsernamePasswordCredentials(userName, password));
        httpClient.setCredentialsProvider(credsProvider);

        HttpHost proxy = new HttpHost(host, port, protocol);

        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    } else {//  w w w. j  a v  a2s  .co m
        LOGGER.warn("HTTP Client has requested for PROXY but it is disabled.");
    }
}

From source file:org.opennms.minion.core.impl.ScvEnabledRestClientImpl.java

@Override
public void ping() throws Exception {
    // Setup a client with pre-emptive authentication
    HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(username, password));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*  w ww  .  j  a  v  a 2  s .  c  om*/
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);

        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        // Issue a simple GET against the Info endpoint
        HttpGet httpget = new HttpGet(url.toExternalForm() + "/rest/info");
        CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
        response.close();
    } finally {
        httpclient.close();
    }
}

From source file:securitytools.veracode.VeracodeAsyncClient.java

/**
 * Constructs a new asynchronous VeracodeClient using the specified
 * configuration.//from   w w w  .  j  a va 2  s  . co m
 *
 * @param credentials Credentials used to access Veracode services.   
 * @param clientConfiguration Client configuration for options such as proxy
 * settings, user-agent header, and network timeouts.
 */
public VeracodeAsyncClient(VeracodeCredentials credentials, ClientConfiguration clientConfiguration) {
    this.clientConfiguration = clientConfiguration;
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(TARGET), credentials);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(TARGET, new BasicScheme());

    context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);

    try {
        client = HttpClientFactory.buildAsync(clientConfiguration);
    } catch (NoSuchAlgorithmException nsae) {
        throw new VeracodeClientException(nsae.getMessage(), nsae);
    }
}

From source file:org.apache.jena.atlas.web.auth.AbstractCredentialsAuthenticator.java

@Override
public void apply(AbstractHttpClient client, HttpContext context, URI target) {
    // At least a user name is required or no authentication will be done
    if (!this.hasUserName(target))
        return;//from   w ww  .ja  v a2  s  .  co m

    // Be careful to scope credentials to the specific URI so that
    // HttpClient won't try and send them to other servers
    HttpHost host = new HttpHost(target.getHost(), target.getPort());
    CredentialsProvider provider = new BasicCredentialsProvider();

    provider.setCredentials(new AuthScope(host), this.createCredentials(target));

    client.setCredentialsProvider(provider);
}