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:com.esri.geoportal.commons.utils.HttpClientContextBuilder.java

/**
 * Creates client context./*from  ww w. j  a  v a  2s .co  m*/
 * @param url url
 * @param cred credentials
 * @return client context
 */
public static HttpClientContext createHttpClientContext(URL url, SimpleCredentials cred) {
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(cred.getUserName(), cred.getPassword()));

    // 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);

    return context;
}

From source file:org.hibernate.ogm.datastore.couchdb.test.dialect.authenticated.AuthenticatedAccessTest.java

private static ResteasyClient getClientWithServerAdminCredentials() {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port),
            new UsernamePasswordCredentials(serverAdminUser, serverAdminPassword));

    AuthCache authCache = new BasicAuthCache();
    authCache.put(new HttpHost(host, port, "http"), new BasicScheme());

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

    return new ResteasyClientBuilder().httpEngine(new ApacheHttpClient4Engine(httpClient, localContext))
            .build();// w  w w .j  a  va  2  s  . c o  m
}

From source file:org.deegree.maven.utils.HttpUtils.java

public static HttpClient getAuthenticatedHttpClient(ServiceIntegrationTestHelper helper) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 30000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 1200000);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("deegree", "deegree"));
    // preemptive authentication used to be easier in pre-4.x httpclient
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    HttpHost host = new HttpHost("localhost", Integer.parseInt(helper.getPort()));
    authCache.put(host, basicAuth);
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(AUTH_CACHE, authCache);
    return client;
}

From source file:com.impetus.client.couchdb.CouchDBUtils.java

/**
 * Gets the context.//w  w  w .  ja va2  s .  c o m
 * 
 * @param httpHost
 *            the http host
 * @return the context
 */
public static HttpContext getContext(HttpHost httpHost) {
    AuthCache authCache = new BasicAuthCache();
    authCache.put(httpHost, new BasicScheme());

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

From source file:com.ksc.http.apache.utils.ApacheUtils.java

private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
        HttpClientSettings settings) {//from   w  ww. j  a va2  s. com

    if (settings.isPreemptiveBasicProxyAuth()) {
        HttpHost targetHost = new HttpHost(settings.getProxyHost(), settings.getProxyPort());
        final CredentialsProvider credsProvider = newProxyCredentialsProvider(settings);
        // 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);

        clientContext.setCredentialsProvider(credsProvider);
        clientContext.setAuthCache(authCache);
    }
}

From source file:org.pentaho.di.core.util.HttpClientUtil.java

/**
 * Returns context with AuthCache or null in case of any exception was thrown.
 *
 * @param host//from  w  w w  . j a va 2 s  .  c om
 * @param port
 * @param user
 * @param password
 * @param schema
 * @return {@link org.apache.http.client.protocol.HttpClientContext HttpClientContext}
 */
public static HttpClientContext createPreemptiveBasicAuthentication(String host, int port, String user,
        String password, String schema) {
    HttpClientContext localContext = null;
    try {
        HttpHost target = new HttpHost(host, port, schema);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(user, 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(target, basicAuth);

        // Add AuthCache to the execution context
        localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
    } catch (Exception e) {
        return null;
    }
    return localContext;
}

From source file:org.apache.metron.dataloads.taxii.TaxiiHandler.java

private static HttpClientContext createContext(URL endpoint, String username, String password, int port) {
    HttpClientContext context = null;/*  w  w w  .jav  a 2s. c om*/
    HttpHost target = new HttpHost(endpoint.getHost(), port, endpoint.getProtocol());
    if (username != null && password != null) {

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username, password));

        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
        AuthCache authCache = new BasicAuthCache();
        authCache.put(target, new BasicScheme());

        // Add AuthCache to the execution context
        context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);
    } else {
        context = null;
    }
    return context;
}

From source file:org.nuxeo.connect.registration.RegistrationHelper.java

protected static HttpClientContext getHttpClientContext(String url, String login, String password) {
    HttpClientContext context = HttpClientContext.create();

    // Set credentials provider
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    if (login != null) {
        Credentials ba = new UsernamePasswordCredentials(login, password);
        credentialsProvider.setCredentials(AuthScope.ANY, ba);
    }//from   w w  w .j a  va  2s .c  om
    context.setCredentialsProvider(credentialsProvider);

    // Create AuthCache instance for preemptive authentication
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    try {
        authCache.put(URIUtils.extractHost(new URI(url)), basicAuth);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    context.setAuthCache(authCache);

    // Create request configuration
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setConnectTimeout(10000);

    // Configure the http proxy if needed
    ProxyHelper.configureProxyIfNeeded(requestConfigBuilder, credentialsProvider, url);

    context.setRequestConfig(requestConfigBuilder.build());
    return context;
}

From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitManagementUtils.java

public static RestTemplate buildRestTemplate(String adminUri, String user, String password) {
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(user, password));
    HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    // Set up pre-emptive basic Auth because the rabbit plugin doesn't currently support challenge/response for PUT
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local; from the apache docs...
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    URI uri;//from   w w  w. j  a  v a  2s.co m
    try {
        uri = new URI(adminUri);
    } catch (URISyntaxException e) {
        throw new RabbitAdminException("Invalid URI", e);
    }
    authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), basicAuth);
    // Add AuthCache to the execution context
    final HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient) {

        @Override
        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
            return localContext;
        }

    });
    restTemplate.setMessageConverters(
            Collections.<HttpMessageConverter<?>>singletonList(new MappingJackson2HttpMessageConverter()));
    return restTemplate;
}

From source file:lucee.commons.net.http.httpclient4.HTTPEngine4Impl.java

public static BasicHttpContext setCredentials(DefaultHttpClient client, HttpHost httpHost, String username,
        String password, boolean preAuth) {
    // set Username and Password
    if (!StringUtil.isEmpty(username, true)) {
        if (password == null)
            password = "";
        CredentialsProvider cp = client.getCredentialsProvider();
        cp.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(username, password));

        BasicHttpContext httpContext = new BasicHttpContext();
        if (preAuth) {
            AuthCache authCache = new BasicAuthCache();
            authCache.put(httpHost, new BasicScheme());
            httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        }/*from   w  w  w.ja v a  2  s .c o m*/
        return httpContext;
    }
    return null;
}