package com.kytkemo.preemptiveauthenticationresttemplate.web.client;

import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

public class HttpComponentsClientHttpRequestFactoryPreemptiveAuthentication extends HttpComponentsClientHttpRequestFactory {

    private BasicHttpContext context;
    private AuthCache cache;

    public HttpComponentsClientHttpRequestFactoryPreemptiveAuthentication() {

        super();

        context = new BasicHttpContext();
        cache = new BasicAuthCache();

        context.setAttribute(ClientContext.AUTH_CACHE, cache);
    }

    public void setCredentials(HttpHost host,
                               PreemptiveAuthenticationScheme authenticationScheme,
                               String username,
                               String password) {

        AuthScheme scheme = null;

        if (authenticationScheme == PreemptiveAuthenticationScheme.BASIC_AUTHENTICATION) {
            scheme = new BasicScheme();
        } else if (authenticationScheme == PreemptiveAuthenticationScheme.DIGEST_AUTHENTICATION) {
            scheme = new DigestScheme();
        }

        cache.put(host, scheme);

        final DefaultHttpClient client = (DefaultHttpClient) getHttpClient();
        final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        client.getCredentialsProvider().setCredentials(new AuthScope(host), credentials);
    }

    @Override
    protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {

        return context;
    }
}

@@ -286,32 +286,83 @@
 ient.AuthCache;%0A
+import org.apache.http.client.CredentialsProvider;%0A
 import org.apach
@@ -384,16 +384,20 @@
 rotocol.
+Http
 ClientCo
@@ -582,23 +582,77 @@
 .client.
-Default
+BasicCredentialsProvider;%0Aimport org.apache.http.impl.client.
 HttpClie
@@ -653,16 +653,17 @@
 tpClient
+s
 ;%0Aimport
@@ -1276,16 +1276,20 @@
 tribute(
+Http
 ClientCo
@@ -1905,86 +1905,8 @@
 );%0A%0A
-        final DefaultHttpClient client = (DefaultHttpClient) getHttpClient();%0A
     
@@ -2023,87 +2023,268 @@
     
-client.getCredentialsProvider().setCredentials(new AuthScope(host), credentials
+final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();%0A        credentialsProvider.setCredentials(new AuthScope(host), credentials);%0A%0A        setHttpClient(HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()
 );%0A 

Use new HttpClient APIs