<commit_before>package com.bolyartech.forge.http.ssl;

import com.bolyartech.forge.http.ForgeCloseableHttpClient;
import forge.apache.http.HttpHost;
import forge.apache.http.HttpRequest;
import forge.apache.http.client.ResponseHandler;
import forge.apache.http.client.methods.CloseableHttpResponse;
import forge.apache.http.client.methods.HttpUriRequest;
import forge.apache.http.config.Registry;
import forge.apache.http.config.RegistryBuilder;
import forge.apache.http.conn.ClientConnectionManager;
import forge.apache.http.conn.socket.ConnectionSocketFactory;
import forge.apache.http.conn.ssl.SSLConnectionSocketFactory;
import forge.apache.http.conn.ssl.SSLContextBuilder;
import forge.apache.http.impl.client.CloseableHttpClient;
import forge.apache.http.impl.client.HttpClients;
import forge.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import forge.apache.http.params.HttpParams;
import forge.apache.http.protocol.HttpContext;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;


/**
 * Created by ogre on 2015-10-10
 */
public class DefaultSslHttpClient implements ForgeCloseableHttpClient {
    private final CloseableHttpClient mHttpClient;

    @SuppressWarnings("FieldCanBeLocal")
    private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(this.getClass().getSimpleName());


    //TODO make it to use inject parameter
    public DefaultSslHttpClient(InputStream keyStore, String keyStorePassword) {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        if (keyStore == null || keyStorePassword == null) {
            throw new NullPointerException();
        }

        KeyStore ks;
        try {
            ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(keyStore, keyStorePassword.toCharArray());
        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
            mLogger.error("Cannot create the keystore", e);
            throw new IllegalStateException("Cannot create the keystore");
        } finally {
            try {
                keyStore.close();
            } catch (IOException e) {
                mLogger.error("Cannot create the keystore", e);
            }
        }


        try {
            sslContextBuilder.loadTrustMaterial(ks);
        } catch (NoSuchAlgorithmException | KeyStoreException e) {
            mLogger.error("Cannot load trust material", e);
        }

        try {
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContextBuilder.build());
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                    .<ConnectionSocketFactory>create().register("https", sslsf)
                    .build();


            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
                    socketFactoryRegistry);
            mHttpClient = HttpClients.custom().setConnectionManager(cm).build();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            mLogger.error("Cannot load trust material", e);
            throw new IllegalStateException("Unable to create http client");
        }
    }


    @Override<commit_msg>DefaultSslHttpClient now uses externally initilized key store<commit_after>package com.bolyartech.forge.http.ssl;

import com.bolyartech.forge.http.ForgeCloseableHttpClient;
import forge.apache.http.HttpHost;
import forge.apache.http.HttpRequest;
import forge.apache.http.client.ResponseHandler;
import forge.apache.http.client.methods.CloseableHttpResponse;
import forge.apache.http.client.methods.HttpUriRequest;
import forge.apache.http.config.Registry;
import forge.apache.http.config.RegistryBuilder;
import forge.apache.http.conn.ClientConnectionManager;
import forge.apache.http.conn.socket.ConnectionSocketFactory;
import forge.apache.http.conn.ssl.SSLConnectionSocketFactory;
import forge.apache.http.conn.ssl.SSLContextBuilder;
import forge.apache.http.impl.client.CloseableHttpClient;
import forge.apache.http.impl.client.HttpClients;
import forge.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import forge.apache.http.params.HttpParams;
import forge.apache.http.protocol.HttpContext;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;


/**
 * Created by ogre on 2015-10-10
 */
public class DefaultSslHttpClient implements ForgeCloseableHttpClient {
    private final CloseableHttpClient mHttpClient;

    @SuppressWarnings("FieldCanBeLocal")
    private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(this.getClass().getSimpleName());


    //TODO make it to use inject parameter
    public DefaultSslHttpClient(KeyStore keyStore) {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        if (keyStore == null) {
            throw new NullPointerException();
        }

        try {
            sslContextBuilder.loadTrustMaterial(keyStore);
        } catch (NoSuchAlgorithmException | KeyStoreException e) {
            mLogger.error("Cannot load trust material", e);
        }

        try {
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContextBuilder.build());
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                    .<ConnectionSocketFactory>create().register("https", sslsf)
                    .build();


            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
                    socketFactoryRegistry);
            mHttpClient = HttpClients.custom().setConnectionManager(cm).build();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            mLogger.error("Cannot load trust material", e);
            throw new IllegalStateException("Unable to create http client");
        }
    }


    @Override
{'fraction_non_alphanumeric': 0.08842570661613769, 'fraction_numerical': 0.0031580609505763462, 'mean_word_length': 3.688378978534419, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1892681', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1626, 'n_words': 320}