package com.github.kubernetes.java.client.v2;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.jboss.resteasy.client.jaxrs.ProxyBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;

public class RestFactory {

    private final ClassLoader classLoader;

    public RestFactory() {
        classLoader = null;
    }

    public RestFactory(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public KubernetesAPI createAPI(URI uri, String userName, String password) {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
                new UsernamePasswordCredentials(userName, password));

        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        client.register(JacksonJaxbJsonProvider.class);
        ProxyBuilder<KubernetesAPI> proxyBuilder = client.target(uri).proxyBuilder(KubernetesAPI.class);
        if (classLoader != null) {
            proxyBuilder = proxyBuilder.classloader(classLoader);
        }
        return proxyBuilder.build();
    }

    public KubernetesAPI createAPI(String url, String userName, String password) throws URISyntaxException {
        URI uri = new URI(url);
        return createAPI(uri, userName, password);
    }
}

@@ -98,16 +98,49 @@
 ption;%0A%0A
+import org.apache.http.HttpHost;%0A
 import o
@@ -227,16 +227,208 @@
 ntials;%0A
+import org.apache.http.client.AuthCache;%0Aimport org.apache.http.client.protocol.ClientContext;%0Aimport org.apache.http.impl.auth.BasicScheme;%0Aimport org.apache.http.impl.client.BasicAuthCache;%0A
 import o
@@ -465,32 +465,82 @@
 aultHttpClient;%0A
+import org.apache.http.protocol.BasicHttpContext;%0A
 import org.codeh
@@ -1132,24 +1132,461 @@
 password) %7B%0A
+%0A        // Configure HttpClient to authenticate preemptively%0A        // by prepopulating the authentication data cache.%0A        // http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html/RESTEasy_Client_Framework.html#transport_layer%0A        // http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/authentication.html#d5e1032%0A%0A        HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());%0A%0A
         Defa
@@ -1599,25 +1599,25 @@
 pClient http
-C
+c
 lient = new 
@@ -1633,24 +1633,25 @@
 tpClient();%0A
+%0A
         http
@@ -1646,25 +1646,25 @@
         http
-C
+c
 lient.getCre
@@ -1701,40 +1701,75 @@
 als(
-new AuthScope(uri.getHost(), uri
+%0A                new AuthScope(targetHost.getHostName(), targetHost
 .get
@@ -1844,24 +1844,520 @@
 assword));%0A%0A
+        // Create AuthCache instance%0A        AuthCache authCache = new BasicAuthCache();%0A        // Generate BASIC scheme object and add it to the local auth cache%0A        BasicScheme basicAuth = new BasicScheme();%0A        authCache.put(targetHost, basicAuth);%0A%0A        // Add AuthCache to the execution context%0A        BasicHttpContext localcontext = new BasicHttpContext();%0A        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);%0A%0A        // 4. Create client executor and proxy%0A
         Apac
@@ -2417,22 +2417,36 @@
 ine(http
-C
+c
 lient
+, localcontext
 );%0A     
@@ -2524,24 +2524,25 @@
 e).build();%0A
+%0A
         clie

Use preemtive authentication to avoid calling API twice for each request