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

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

Introduction

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

Prototype

public BasicCredentialsProvider() 

Source Link

Document

Default constructor.

Usage

From source file:org.hawk.http.HTTPManager.java

private CloseableHttpClient createClient() {
    final HttpClientBuilder builder = HttpClientBuilder.create();

    // Provide username and password if specified
    if (username != null) {
        final BasicCredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(new HttpHost(repositoryURL.getHost())),
                new UsernamePasswordCredentials(username, password));
        builder.setDefaultCredentialsProvider(credProvider);
    }/* w w  w .  ja  v a2  s  .  c  om*/

    // Follow redirects
    builder.setRedirectStrategy(new LaxRedirectStrategy());

    return builder.build();
}

From source file:app.android.auto.net.sampleapp.oauth.blackwork.EasyHttpClient.java

/**
 * Constructor which handles credentials for the connection (only if username and password are set)
 *
 * @param username/*from  w  w  w  . j  a va  2s  .co  m*/
 * @param password
 */
public EasyHttpClient(String username, String password) {
    if (username != null && password != null) {
        UsernamePasswordCredentials c = new UsernamePasswordCredentials(username, password);
        BasicCredentialsProvider cP = new BasicCredentialsProvider();
        cP.setCredentials(AuthScope.ANY, c);
        setCredentialsProvider(cP);
    }
}

From source file:securitytools.veracode.VeracodeClient.java

/**
 * Constructs a new VeracodeClient using the specified configuration.
 *     //  w  ww.ja  v  a2  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 VeracodeClient(VeracodeCredentials credentials, 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.build(clientConfiguration);
    } catch (NoSuchAlgorithmException nsae) {
        throw new VeracodeClientException(nsae.getMessage(), nsae);
    }
}

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.apache.geode.rest.internal.web.GeodeRestClient.java

public HttpResponse doRequest(HttpRequestBase request, String username, String password) throws Exception {
    HttpHost targetHost = new HttpHost(bindAddress, restPort, protocol);

    HttpClientBuilder clientBuilder = HttpClients.custom();
    HttpClientContext clientContext = HttpClientContext.create();

    // configures the clientBuilder and clientContext
    if (username != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials(username, password));
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
    }/*from w w w.ja v  a  2  s . co m*/

    if (useHttps) {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        clientBuilder.setSSLContext(ctx);
        clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }

    return clientBuilder.build().execute(targetHost, request, clientContext);
}

From source file:com.servoy.extensions.plugins.http.BaseRequest.java

private Response executeRequest(String userName, String password, String workstation, String domain,
        boolean windowsAuthentication) throws Exception {
    HttpEntity entity = buildEntity();/*from w  w  w  .ja v  a2 s  .  c om*/
    if (entity != null)
        ((HttpEntityEnclosingRequestBase) method).setEntity(entity);

    Iterator<String> it = headers.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        String[] values = headers.get(name);
        for (String value : values) {
            method.addHeader(name, value);
        }
    }

    if (!Utils.stringIsEmpty(userName)) {
        BasicCredentialsProvider bcp = new BasicCredentialsProvider();
        URL _url = HttpProvider.createURLFromString(url, access);
        Credentials cred = null;
        if (windowsAuthentication) {
            if (context == null) {
                context = new BasicHttpContext();
            }
            cred = new NTCredentials(userName, password, workstation, domain);
        } else {
            cred = new UsernamePasswordCredentials(userName, password);
        }
        bcp.setCredentials(new AuthScope(_url.getHost(), _url.getPort()), cred);
        client.setCredentialsProvider(bcp);
    }

    return new Response(client.execute(method, context));
}

From source file:cf.spring.servicebroker.CatalogTest.java

@Test
public void spelCatalogCredentials() throws Exception {
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(CONFIGURABLE_USERNAME, CONFIGURABLE_PASSWORD));
    final SpringApplication application = new SpringApplication(SpelServiceBrokerCatalog.class);
    try (ConfigurableApplicationContext context = application.run();
            CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider)
                    .build();) {/*from  ww  w  .  j a  va  2  s .  co  m*/
        final HttpUriRequest catalogRequest = RequestBuilder.get()
                .setUri("http://localhost:8080" + Constants.CATALOG_URI).build();
        final CloseableHttpResponse response = client.execute(catalogRequest);
        assertEquals(response.getStatusLine().getStatusCode(), 200);
    }
}

From source file:org.elasticsearch.xpack.security.transport.ssl.SslIntegrationTests.java

public void testThatConnectionToHTTPWorks() throws Exception {
    Settings.Builder builder = Settings.builder();
    addSSLSettingsForStore(builder,/*from   ww w  .ja  v  a 2  s  . co  m*/
            "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks", "testclient");
    SSLService service = new SSLService(builder.build(), null);

    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(nodeClientUsername(), new String(nodeClientPassword().getChars())));
    try (CloseableHttpClient client = HttpClients.custom()
            .setSSLSocketFactory(new SSLConnectionSocketFactory(service.sslSocketFactory(Settings.EMPTY),
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
            .setDefaultCredentialsProvider(provider).build();
            CloseableHttpResponse response = SocketAccess
                    .doPrivileged(() -> client.execute(new HttpGet(getNodeUrl())))) {
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        String data = Streams
                .copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
        assertThat(data, containsString("You Know, for Search"));
    }
}

From source file:jp.co.ctc_g.jse.core.rest.springmvc.client.ProxyClientHttpRequestFactory.java

/**
 * ???????HttpClient????????/* www .  ja  v  a2 s  .  com*/
 * {@inheritDoc}
 */
@Override
public void afterPropertiesSet() throws Exception {

    Assert.notNull(proxyHost, "(proxyHost)???");
    Assert.notNull(proxyPort, "??(proxyPort)???");

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(maxTotal);
    connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);

    HttpClientBuilder builder = HttpClients.custom();
    builder.setConnectionManager(connectionManager);
    if (authentication) {
        Assert.notNull(username,
                "??true???????(username)???");
        Assert.notNull(password,
                "??true?????(password)???");
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
                new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
        builder.setRoutePlanner(routePlanner);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)),
                new UsernamePasswordCredentials(username, password));
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(readTimeout).build());
    CloseableHttpClient client = builder.build();
    setHttpClient(client);
}