Example usage for org.apache.http.impl.auth BasicScheme BasicScheme

List of usage examples for org.apache.http.impl.auth BasicScheme BasicScheme

Introduction

In this page you can find the example usage for org.apache.http.impl.auth BasicScheme BasicScheme.

Prototype

public BasicScheme() 

Source Link

Usage

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   ww w  . ja v a2  s .c  o  m*/

    return context;
}

From source file:pl.wavesoftware.wfirma.api.simple.mapper.SimpleGateway.java

private String get(@Nonnull HttpRequest httpRequest) throws WFirmaException {
    httpRequest.setHeader("Accept", "text/xml");
    HttpHost target = getTargetHost();//from  w  ww. jav a2s .c  om
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(credentials.getConsumerKey(), credentials.getConsumerSecret()));
    try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build()) {

        // 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(target, basicAuth);

        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        try (CloseableHttpResponse response = httpclient.execute(target, httpRequest, localContext)) {
            return getContent(response);
        }
    } catch (IOException ioe) {
        throw new RemoteGatewayException(ioe);
    }
}

From source file:org.kaaproject.kaa.server.flume.sink.hdfs.AvroSchemaSource.java

private void initHttpRestClient() {
    httpClient = new DefaultHttpClient();
    restHost = new HttpHost(kaaRestHost, kaaRestPort, "http");

    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(restHost, basicAuth);/*www. j ava 2s.  c  om*/

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(kaaRestHost, kaaRestPort, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(kaaRestUser, kaaRestPassword));

    httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);

}

From source file:org.zenoss.metrics.reporter.HttpPoster.java

private final void postImpl(MetricBatch batch) throws IOException {
    int size = batch.getMetrics().size();
    MetricCollection metrics = new MetricCollection();
    metrics.setMetrics(batch.getMetrics());

    String json = asJson(metrics);

    // Add AuthCache to the execution context
    BasicHttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

    if (needsAuth && !authenticated) {
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        authCache.put(targetHost, basicAuth);

        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }//from  w ww  .ja v  a 2 s  .  c om

    post.setEntity(new StringEntity(json, APPLICATION_JSON));

    cookieJar.clearExpired(new Date());
    httpClient.execute(post, responseHandler, localContext);
}

From source file:com.example.aab119.restclientexample.RestClient.java

private HttpUriRequest addHeaderParams(HttpUriRequest request) throws Exception {
    for (NameValuePair h : headers) {
        request.addHeader(h.getName(), h.getValue());
    }//  w  ww  . j av  a 2  s.  c  o m

    if (authentication) {

        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
        request.addHeader(new BasicScheme().authenticate(creds, request));
    }

    return request;
}

From source file:com.amalto.workbench.utils.HttpClientUtil.java

private static HttpContext getPreemptiveContext(HttpHost targetHost) {
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    return localcontext;
}

From source file:org.muckebox.android.net.MuckeboxHttpClient.java

private void addCredentials() {
    String password = Preferences.getServerPassword();

    if (password != null && password.length() > 0) {
        mHttpClient.getCredentialsProvider().setCredentials(
                new AuthScope(Preferences.getServerAddress(), Preferences.getServerPort(), "muckebox"),
                new UsernamePasswordCredentials("muckebox", password));

        mContext = new BasicHttpContext();

        BasicScheme basicAuth = new BasicScheme();
        mContext.setAttribute("preemptive-auth", basicAuth);

        mHttpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
    }/*from w w w .  jav  a  2  s.  c  o m*/
}

From source file:be.cytomine.client.HttpClient.java

public void connect(String url, String username, String password) throws IOException {
    isAuthByPrivateKey = false;/*  w ww.  j av  a  2 s .c om*/
    log.info("Connection to " + url + " with login=" + username + " and pass=" + password);
    URL = new URL(url);
    targetHost = new HttpHost(URL.getHost(), URL.getPort());
    client = new DefaultHttpClient();
    // 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
    localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    // Set credentials
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
}