Search in sources :

Example 1 with SystemDefaultCredentialsProvider

use of org.apache.http.impl.client.SystemDefaultCredentialsProvider in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnection method createHttpClient.

private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
    HttpRoutePlanner routePlanner;
    CredentialsProvider credentialsProvider;
    if (proxyConfiguration != null) {
        HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
        routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
        credentialsProvider = new BasicCredentialsProvider();
        if (proxyConfiguration.getUsername() != null) {
            AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
            final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            credentialsProvider.setCredentials(authscope, credentials);
            // enable preemptive authentication
            HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
                    if (header == null) {
                        header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
                        if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
                            header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
                        }
                        request.setHeader(header);
                    }
                }
            };
            builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
        }
    } else {
        // add support for system properties
        routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
        credentialsProvider = new SystemDefaultCredentialsProvider();
    }
    // add logging - last for requests, first for responses
    LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
    builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
    builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
    return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) BasicHeader(org.apache.http.message.BasicHeader) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicHeader(org.apache.http.message.BasicHeader) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 2 with SystemDefaultCredentialsProvider

use of org.apache.http.impl.client.SystemDefaultCredentialsProvider in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesACustomCredentialsProvider.

@Test
void usesACustomCredentialsProvider() {
    CredentialsProvider customCredentialsProvider = new SystemDefaultCredentialsProvider();
    builder.using(customCredentialsProvider);
    verify(apacheHttpClientBuilder).using(customCredentialsProvider);
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Test(org.junit.jupiter.api.Test)

Example 3 with SystemDefaultCredentialsProvider

use of org.apache.http.impl.client.SystemDefaultCredentialsProvider in project gradle by gradle.

the class HttpClientConfigurer method configure.

public void configure(HttpClientBuilder builder) {
    SystemDefaultCredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
    configureSslSocketConnectionFactory(builder, httpSettings.getSslContextFactory(), httpSettings.getHostnameVerifier());
    configureAuthSchemeRegistry(builder);
    configureCredentials(builder, credentialsProvider, httpSettings.getAuthenticationSettings());
    configureProxy(builder, credentialsProvider, httpSettings);
    configureUserAgent(builder);
    configureCookieSpecRegistry(builder);
    configureRequestConfig(builder);
    configureSocketConfig(builder);
    configureRedirectStrategy(builder);
    builder.setDefaultCredentialsProvider(credentialsProvider);
    builder.setMaxConnTotal(MAX_HTTP_CONNECTIONS);
    builder.setMaxConnPerRoute(MAX_HTTP_CONNECTIONS);
    builder.setConnectionTimeToLive(httpSettings.getTimeoutSettings().getIdleConnectionTimeoutMs(), TimeUnit.MILLISECONDS);
}
Also used : SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider)

Aggregations

SystemDefaultCredentialsProvider (org.apache.http.impl.client.SystemDefaultCredentialsProvider)3 CredentialsProvider (org.apache.http.client.CredentialsProvider)2 RequestHeader (com.ingenico.connect.gateway.sdk.java.RequestHeader)1 ResponseHeader (com.ingenico.connect.gateway.sdk.java.ResponseHeader)1 Header (org.apache.http.Header)1 HttpHost (org.apache.http.HttpHost)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)1 AuthScope (org.apache.http.auth.AuthScope)1 Credentials (org.apache.http.auth.Credentials)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 HttpRoutePlanner (org.apache.http.conn.routing.HttpRoutePlanner)1 BasicScheme (org.apache.http.impl.auth.BasicScheme)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 DefaultProxyRoutePlanner (org.apache.http.impl.conn.DefaultProxyRoutePlanner)1 SystemDefaultRoutePlanner (org.apache.http.impl.conn.SystemDefaultRoutePlanner)1 BasicHeader (org.apache.http.message.BasicHeader)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1