package com.jivesoftware.os.routing.bird.http.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.ssl.SSLContexts;

/**
 * @author jonathan.colt
 */
public class HttpRequestHelperUtils {

    private static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 10_000;

    public static HttpRequestHelper buildRequestHelper(boolean sslEnable,
        boolean allowSelfSigendCerts,
        OAuthSigner signer,
        String host,
        int port) throws Exception {
        return buildRequestHelper(sslEnable, allowSelfSigendCerts, signer, host, port, DEFAULT_SOCKET_TIMEOUT_MILLIS);
    }

    public static HttpRequestHelper buildRequestHelper(boolean sslEnable,
        boolean allowSelfSigendCerts,
        OAuthSigner signer,
        String host,
        int port,
        int socketTimeoutInMillis) throws Exception {

        List<HttpClientConfiguration> configs = new ArrayList<>();
        HttpClientConfig httpClientConfig = HttpClientConfig.newBuilder().setSocketTimeoutInMillis(socketTimeoutInMillis).build();
        configs.add(httpClientConfig);
        if (sslEnable) {

            HttpClientSSLConfig.Builder builder = HttpClientSSLConfig.newBuilder();
            builder.setUseSSL(true);
            if (allowSelfSigendCerts) {
                SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
                // Allow TLSv1 protocol only, use NoopHostnameVerifier to trust self-singed cert
                builder.setUseSslWithCustomSSLSocketFactory(new SSLConnectionSocketFactory(sslcontext,
                    new String[] { "TLSv1" }, null, new NoopHostnameVerifier()));
            }
            configs.add(builder.build());
        }

        HttpClientFactory httpClientFactory = new HttpClientFactoryProvider().createHttpClientFactory(configs, false);
        HttpClient httpClient = httpClientFactory.createClient(signer, host, port);
        HttpRequestHelper requestHelper = new HttpRequestHelper(httpClient, new ObjectMapper());
        return requestHelper;
    }

}

@@ -96,24 +96,156 @@
 jectMapper;%0A
+import java.security.KeyManagementException;%0Aimport java.security.KeyStoreException;%0Aimport java.security.NoSuchAlgorithmException;%0A
 import java.
@@ -256,24 +256,24 @@
 .ArrayList;%0A
-
 import java.
@@ -1224,24 +1224,507 @@
 xception %7B%0A%0A
+        HttpClient httpClient = buildHttpClient(sslEnable, allowSelfSigendCerts, signer, host, port, socketTimeoutInMillis);%0A        return new HttpRequestHelper(httpClient, new ObjectMapper());%0A    %7D%0A%0A    public static HttpClient buildHttpClient(boolean sslEnable,%0A        boolean allowSelfSigendCerts,%0A        OAuthSigner signer,%0A        String host,%0A        int port,%0A        int socketTimeoutInMillis) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException %7B%0A
         List
@@ -2736,39 +2736,22 @@
         
-HttpClient httpClient =
+return
  httpCli
@@ -2799,135 +2799,8 @@
 t);%0A
-        HttpRequestHelper requestHelper = new HttpRequestHelper(httpClient, new ObjectMapper());%0A        return requestHelper;%0A
     

add request helper buildHttpClient