Example usage for org.apache.http.client AuthCache put

List of usage examples for org.apache.http.client AuthCache put

Introduction

In this page you can find the example usage for org.apache.http.client AuthCache put.

Prototype

void put(HttpHost host, AuthScheme authScheme);

Source Link

Usage

From source file:securitytools.veracode.VeracodeAsyncClient.java

/**
 * Constructs a new asynchronous VeracodeClient using the specified
 * configuration.//w  ww  .  j  a va2s. c  o  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.datagator.api.client.backend.DataGatorService.java

public DataGatorService(UsernamePasswordCredentials auth) {
    this();/*ww  w . j  a  v a2 s  .  c  o  m*/
    log.info(String.format("Initializing service with authorization: %s", auth.getUserName()));
    // attach credentials to context
    HttpHost host = new HttpHost(environ.DATAGATOR_API_HOST, environ.DATAGATOR_API_PORT,
            environ.DATAGATOR_API_SCHEME);
    AuthScope scope = new AuthScope(host.getHostName(), host.getPort());
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(scope, auth);
    this.context.setCredentialsProvider(provider);
    // enable preemptive (pro-active) basic authentication
    AuthCache cache = new BasicAuthCache();
    cache.put(host, new BasicScheme());
    this.context.setAuthCache(cache);
}

From source file:org.springframework.cloud.dataflow.shell.command.support.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java

private HttpContext createHttpContext() {
    final AuthCache authCache = new BasicAuthCache();

    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);

    final BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}

From source file:com.garethahealy.resteastpathparamescape.utils.RestFactory.java

protected HttpClientContext getBasicAuthContext(URI uri, String userName, String password) {
    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
    // Generate BASIC scheme object and add it to the local auth cache
    AuthCache authCache = new BasicAuthCache();
    authCache.put(targetHost, new BasicScheme());

    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);/*from  w  w w .jav  a2  s  .c o  m*/

    return context;
}

From source file:org.springframework.cloud.dataflow.rest.util.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    final AuthCache authCache = new BasicAuthCache();

    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);

    final BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}

From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java

public Properties sendLogEvent(SplunkConnectionData connData, byte[] thePayload, Properties properties)
        throws ConnectorException {
    HttpClient myhClient = HttpClients.custom().setConnectionManager(this.cm).build();

    HttpClientContext myhContext = HttpClientContext.create();

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(connData.getUser(), connData.getPass()));
    AuthCache authCache = new BasicAuthCache();
    authCache.put(new HttpHost(connData.getHost(), Integer.parseInt(connData.getPort()), connData.getScheme()),
            new BasicScheme());

    myhContext.setCredentialsProvider(credsProvider);
    myhContext.setAuthCache(authCache);/*from w  w  w . j  ava2s  .  c o m*/

    HttpPost myhPost = new HttpPost(connData.getSplunkURI());

    ByteArrayEntity payload = new ByteArrayEntity(thePayload);
    try {
        myhPost.setEntity(payload);
        HttpResponse response = myhClient.execute(myhPost, myhContext);
        Integer statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200 && !connData.getIgnoreSplunkErrors()) {
            throw new ConnectorException(
                    "Error posting log event to Splunk: " + response.getStatusLine().toString());
        }
        System.out.println(response.getStatusLine().toString());
        properties.setProperty("status", String.valueOf(statusCode));
        Integer leasedConns = Integer
                .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getLeased());
        properties.setProperty("conns_leased", leasedConns.toString());
        Integer availConns = Integer
                .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getAvailable());
        properties.setProperty("conns_available", availConns.toString());
    } catch (IOException e) {
        e.fillInStackTrace();
        throw new ConnectorException(e.toString());
    }
    return properties;
}

From source file:com.github.restdriver.clientdriver.integration.BasicAuthTest.java

@Test
public void basicAuthWorks() throws Exception {

    clientDriver.addExpectation(onRequestTo("/").withBasicAuth("Aladdin", "open sesame"),
            giveEmptyResponse().withStatus(418)).anyTimes();

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("Aladdin", "open sesame"));

    HttpHost host = new HttpHost("localhost", 12345);

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

    BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(ClientContext.AUTH_CACHE, authCache);

    List<String> authPrefs = new ArrayList<String>();
    authPrefs.add(AuthPolicy.BASIC);//from w w w  .j  a va2s . c o  m
    client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPrefs);

    HttpGet get = new HttpGet(clientDriver.getBaseUrl() + "/");

    HttpResponse response = client.execute(host, get, context);

    assertThat(response.getStatusLine().getStatusCode(), is(418));

}

From source file:org.openlmis.UiUtils.HttpClient.java

public ResponseEntity SendJSON(String json, String url, String commMethod, String username, String password) {

    HttpHost targetHost = new HttpHost(HOST, PORT, PROTOCOL);
    AuthScope localhost = new AuthScope(HOST, PORT);

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    httpClient.getCredentialsProvider().setCredentials(localhost, credentials);

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

    httpContext.setAttribute(AUTH_CACHE, authCache);

    try {//from  w  ww .j a  v  a2 s  . c om
        return handleRequest(commMethod, json, url, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.openlmis.UiUtils.HttpClient.java

public ResponseEntity SendJSONWithoutHeaders(String json, String url, String commMethod, String username,
        String password) {//from  w  w w.j a  va  2  s  . c o  m
    HttpHost targetHost = new HttpHost(HOST, PORT, PROTOCOL);
    AuthScope localhost = new AuthScope(HOST, PORT);

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    httpClient.getCredentialsProvider().setCredentials(localhost, credentials);

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

    httpContext.setAttribute(AUTH_CACHE, authCache);

    try {
        return handleRequest(commMethod, json, url, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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 ava  2  s  .c o  m
        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();
    }
}