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:org.dmfs.android.authenticator.handlers.BasicHttpClientAuthenticationHandler.java

@Override
public void authenticate(AbstractHttpClient client) {
    // just set the credentials assuming that proper authentication schemes are registered with the AbstractHttpClient.
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, mAuthToken.getRealm()),
            new UsernamePasswordCredentials(mAuthToken.getUsername(), mAuthToken.getPassword()));

    client.setCredentialsProvider(credsProvider);
}

From source file:org.apache.olingo.client.core.http.NTLMAuthHttpClientFactory.java

@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {
    final DefaultHttpClient httpclient = super.create(method, uri);

    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(username, password, workstation, domain));

    httpclient.setCredentialsProvider(credsProvider);

    return httpclient;
}

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

public String scrape(String urlString, String username, String password)
        throws ClientProtocolException, IOException {
    URI uri = URI.create(urlString);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(username, password));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);/*from   w w w.  ja va2  s .co m*/
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpGet httpGet = new HttpGet(uri);
    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    HttpResponse response = httpClient.execute(host, httpGet, localContext);

    String resp = response.toString();

    return EntityUtils.toString(response.getEntity());
}

From source file:goofyhts.torrentkinesis.http.client.DefaultHttpClient.java

@Override
public void open() {
    client = HttpClients.createDefault();
    context = new HttpClientContext();
    CredentialsProvider credProv = new BasicCredentialsProvider();
    credProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    context.setCredentialsProvider(credProv);
}

From source file:org.mule.test.integration.security.HttpListenerAuthenticationTestCase.java

private CredentialsProvider getCredentialsProvider(String user, String password) {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    return credsProvider;
}

From source file:org.sonatype.spice.zapper.client.hc4.Hc4ClientPreemptiveAuthTest.java

@Override
protected Client getClient(Parameters parameters, String remoteUrl) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD));
    return new Hc4ClientBuilder(parameters, remoteUrl).withPreemptiveRealm(credentialsProvider).build();
}

From source file:fr.gael.dhus.util.http.BasicAuthHttpClientProducer.java

@Override
public CloseableHttpAsyncClient generateClient() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    if (timeout == 0) {
        requestConfigBuilder.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
                .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
                .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT);
    } else {/*from  w w  w .  j av  a 2 s  .com*/
        requestConfigBuilder.setSocketTimeout(timeout).setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout);
    }

    requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT);

    CloseableHttpAsyncClient res = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
            .setDefaultRequestConfig(requestConfigBuilder.build()).build();
    res.start();

    return res;
}

From source file:org.qucosa.migration.processors.PurgeFedoraObject.java

private HttpClient prepareHttpClient(String user, String password) {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));

    HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager)
            .setDefaultCredentialsProvider(credentialsProvider).build();
    return client;
}

From source file:org.mycontroller.standalone.restclient.RestFactory.java

public T createAPI(URI uri, String userName, String password) {
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(userName, password));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);//w  w w.java 2s.c om
    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpclient, context);

    ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
    client.register(JacksonJaxbJsonProvider.class);
    client.register(RequestLogger.class);
    client.register(ResponseLogger.class);
    ProxyBuilder<T> proxyBuilder = client.target(uri).proxyBuilder(proxyClazz);
    return proxyBuilder.build();
}

From source file:com.cognifide.qa.bb.provider.http.HttpClientProvider.java

private CredentialsProvider createCredentialsProvider(String url, String login, String password) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(getAuthScope(url), new UsernamePasswordCredentials(login, password));
    return credentialsProvider;
}