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:org.apache.syncope.installer.utilities.HttpUtils.java

private HttpClientContext setAuth(final HttpHost targetHost, final AuthScheme authScheme) {
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(username, password));
    final HttpClientContext context = HttpClientContext.create();
    final AuthCache authCache = new BasicAuthCache();
    authCache.put(targetHost, authScheme);
    context.setAuthCache(authCache);/*from www .j a  va 2  s. c o  m*/
    context.setCredentialsProvider(credsProvider);
    return context;
}

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);//from w  w  w.ja  va  2 s .  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:ch.sbb.releasetrain.utils.http.HttpUtilImpl.java

/**
 * authenticates the context if user and password are set
 *//*from w w  w. jav a2s  .  c  o m*/
private HttpClientContext initAuthIfNeeded(String url) {

    HttpClientContext context = HttpClientContext.create();
    if (this.user.isEmpty() || this.password.isEmpty()) {
        log.debug(
                "http connection without autentication, because no user / password ist known to HttpUtilImpl ...");
        return context;
    }

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, password));
    URL url4Host;
    try {
        url4Host = new URL(url);
    } catch (MalformedURLException e) {
        log.error(e.getMessage(), e);
        return context;
    }

    HttpHost targetHost = new HttpHost(url4Host.getHost(), url4Host.getPort(), url4Host.getProtocol());
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    return context;
}

From source file:org.kaaproject.kaa.server.common.admin.HttpComponentsRequestFactoryBasicAuth.java

private HttpContext createHttpContext() {
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);/*from w  w w .  j  av  a  2s.c o  m*/
    return context;
}

From source file:org.trancecode.xproc.step.HttpRequestStepProcessor.java

private HttpClient prepareHttpClient(final XProcHttpRequest xProcRequest, final BasicHttpContext localContext) {
    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry);
    final DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
    final ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpClient.setRoutePlanner(routePlanner);

    if (xProcRequest.getCredentials() != null) {
        final List<String> authPref = Lists.newArrayList(AuthPolicy.BASIC, AuthPolicy.DIGEST);
        httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPref);
        httpClient.setCredentialsProvider(xProcRequest.getCredentials());
        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(xProcRequest.getHttpHost(), basicAuth);
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }/*from w  ww .j  a  va2  s  .c  o  m*/
    return httpClient;
}

From source file:org.etk.common.net.ETKHttpClient.java

public HttpEntity execute(String targetURL) throws ClientProtocolException, IOException {
    HttpHost targetHost = new HttpHost("127.0.0.1", 8080, "http");

    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials("demo", "gtn"));

    // 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
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpGet httpget = new HttpGet(targetURL);
    Header header = new BasicHeader("Content-Type", "application/json");
    httpget.setHeader(header);/*  ww w. j a va2s. c  om*/
    HttpResponse response = httpClient.execute(targetHost, httpget, localcontext);
    DumpHttpResponse.dumpHeader(response);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        entity = new BufferedHttpEntity(entity);
    }
    EntityUtils.consume(entity);

    return entity;
}

From source file:com.sulacosoft.bitcoindconnector4j.BitcoindApiHandler.java

public BitcoindApiHandler(String host, int port, String protocol, String uri, String username,
        String password) {// w  ww .  j  a va 2  s  .  c o  m
    this.uri = uri;

    httpClient = HttpClients.createDefault();
    targetHost = new HttpHost(host, port, protocol);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(username, password));

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

    context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
}

From source file:mobi.jenkinsci.ci.client.JenkinsHttpClient.java

public JenkinsHttpClient(final JenkinsConfig config) throws MalformedURLException {
    this.config = config;
    if (config.getUsername() == null) {
        httpClient = httpClientFactory.getHttpClient();
    } else {//from w  w w .  j a v  a 2 s  . c  o  m
        final URL url = new URL(config.getUrl());
        httpClient = httpClientFactory.getBasicAuthHttpClient(url, config.getUsername(), config.getPassword());

        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(new HttpHost(url.getHost(), url.getPort()), basicAuth);

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

From source file:com.gsma.mobileconnect.utils.RestClient.java

/**
 * Build a HttpClientContext that will preemptively authenticate using Basic Authentication
 *
 * @param username Username for credentials
 * @param password Password for credentials
 * @param uriForRealm uri used to determine the authentication realm
 * @return A HttpClientContext that will preemptively authenticate using Basic Authentication
 *//*w w w  .j  a va 2s .com*/
public HttpClientContext getHttpClientContext(String username, String password, URI uriForRealm) {
    String host = URIUtils.extractHost(uriForRealm).getHostName();
    int port = uriForRealm.getPort();
    if (port == -1) {
        if (Constants.HTTP_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) {
            port = Constants.HTTP_PORT;
        } else if (Constants.HTTPS_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) {
            port = Constants.HTTPS_PORT;
        }
    }

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));

    AuthCache authCache = new BasicAuthCache();
    authCache.put(new HttpHost(host, port, uriForRealm.getScheme()), new BasicScheme());

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

    return context;
}

From source file:com.garyclayburg.scimclient.authn.AuthHttpComponentsClientHttpRequestFactory.java

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    // 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);

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

    if (userName != null) {
        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(userName, password));
        localcontext.setCredentialsProvider(credsProvider);
    }/*  w ww. j av  a 2 s .  c om*/
    return localcontext;
}