Example usage for org.apache.http.impl.client BasicCredentialsProvider setCredentials

List of usage examples for org.apache.http.impl.client BasicCredentialsProvider setCredentials

Introduction

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

Prototype

public void setCredentials(final AuthScope authscope, final Credentials credentials) 

Source Link

Usage

From source file:com.jimdo.graylog.net.Request.java

/**
 * Set Credentials for HTTPAuth//from  w w w . j a  v a2  s  .co m
 */
public void setHttpAuth(String username, String password) {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    httpClient.setCredentialsProvider(credentialsProvider);
}

From source file:org.superbiz.AuthBeanTest.java

private String get(final String user, final String password) {
    final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    final CloseableHttpClient client = HttpClients.custom()
            .setDefaultCredentialsProvider(basicCredentialsProvider).build();

    final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol());
    final AuthCache authCache = new BasicAuthCache();
    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(httpHost, basicAuth);/* ww  w  . j a v a 2  s  .c om*/
    final HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(authCache);

    final HttpGet get = new HttpGet(webapp.toExternalForm() + "servlet");
    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpHost, get, context);
        return response.getStatusLine().getStatusCode() + " " + EntityUtils.toString(response.getEntity());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } finally {
        try {
            IO.close(response);
        } catch (final IOException e) {
            // no-op
        }
    }
}

From source file:de.bytefish.fcmjava.client.tests.FakeFcmClientSettings.java

@Test
public void testFcmClientWithProxySettings() throws Exception {

    // Create Settings:
    IFcmClientSettings settings = new FakeFcmClientSettings();

    // Define the Credentials to be used:
    BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();

    // Set the Credentials (any auth scope used):
    basicCredentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("your_username", "your_password"));

    // Create the Apache HttpClientBuilder:
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
            // Set the Proxy Address:
            .setProxy(new HttpHost("your_hostname", 1234))
            // Set the Authentication Strategy:
            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
            // Set the Credentials Provider we built above:
            .setDefaultCredentialsProvider(basicCredentialsProvider);

    // Create the DefaultHttpClient:
    DefaultHttpClient httpClient = new DefaultHttpClient(settings, httpClientBuilder);

    // Finally build the FcmClient:
    try (IFcmClient client = new FcmClient(settings, httpClient)) {
        // TODO Work with the Proxy ...
    }/*from ww  w.  ja  v  a  2  s  .c o  m*/
}

From source file:org.everit.osgi.webconsole.tests.GetConfigurationTest.java

@Before
public void setUp() {
    BasicCredentialsProvider credProv = new BasicCredentialsProvider();
    credProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
    client = HttpClientBuilder.create().setDefaultCredentialsProvider(credProv).build();
}

From source file:org.sonatype.nexus.examples.url.UrlRealmTest.java

@Test(expected = UnknownAccountException.class)
public void testAuthcWrongCreds() throws Exception {
    // build client
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, "cake"));
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient);

    urlRealm.getAuthenticationInfo(new UsernamePasswordToken(username, "cake"));
}

From source file:org.sonatype.nexus.examples.url.UrlRealmTest.java

@Test(expected = UnknownAccountException.class)
public void testAuthcJunkCreds() throws Exception {
    // build client
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("fakeuser", "hack-me-in"));
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient);

    urlRealm.getAuthenticationInfo(new UsernamePasswordToken("fakeuser", "hack-me-in"));
}

From source file:org.springframework.mobile.urbanairship.impl.UrbanAirshipTemplate.java

private RestTemplate createRestTemplate(String key, String secret) {
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(key, secret));
    httpClient.setCredentialsProvider(credentialsProvider);
    requestFactory.setHttpClient(httpClient);
    return new RestTemplate(requestFactory);
}

From source file:org.sonatype.nexus.examples.url.UrlRealmTest.java

@Test
public void testAuthc() throws Exception {
    // build client
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient);

    final AuthenticationInfo info = urlRealm
            .getAuthenticationInfo(new UsernamePasswordToken(username, password));
    assertThat(info, notNullValue());//from ww w  .j a  v  a 2  s  .c o m
}

From source file:com.ccreanga.bitbucket.rest.client.http.BitBucketHttpExecutor.java

public BitBucketHttpExecutor(String baseUrl, BitBucketCredentials credentials) {
    this.baseUrl = baseUrl;

    HttpHost targetHost = HttpHost.create(baseUrl);
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(5);//from   w  w w. j  ava2  s  .c o  m
    connectionManager.setDefaultMaxPerRoute(4);

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(targetHost),
            new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));

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

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

    httpClient = HttpClients.custom().setConnectionManager(connectionManager)
            .setDefaultCredentialsProvider(credentialsProvider).build();

}

From source file:com.ericsson.gerrit.plugins.syncindex.HttpClientProvider.java

private BasicCredentialsProvider buildCredentials() {
    URI uri = URI.create(cfg.getUrl());
    BasicCredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(cfg.getUser(), cfg.getPassword()));
    return creds;
}