<commit_before><commit_msg>Add NioChannelConfig to construct SSL/TLS configuration<commit_after>package influent.internal.nio;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
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.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

public class NioChannelConfig {

  private boolean sslEnabled = false;
  private String host;
  private int port;
  private String[] tlsVersions;
  private String[] ciphers;
  private SSLContext context;

  public NioChannelConfig() {
    sslEnabled = false;
    context = null;
    tlsVersions = null;
  }

  public NioChannelConfig(String host, int port, boolean sslEnabled, String[] tlsVersions, String[] ciphers,
                          String keystorePath, String keystorePassword, String keyPassword,
                          String truststroePath, String truststrorePassword) {
    this.host = host;
    this.port = port;
    this.sslEnabled = sslEnabled;
    this.tlsVersions = tlsVersions;
    this.ciphers = ciphers;
    try {
      if (isSslEnabled()) {
        context = SSLContext.getInstance("TLS");
        context.init(
            createKeyManagers(keystorePath, keystorePassword, keyPassword),
            createTrustManagers(truststroePath, truststrorePassword),
            new SecureRandom()
        );
      }
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    } catch (KeyManagementException e) {
      e.printStackTrace();
    }
  }

  public SSLEngine createSSLEngine() {
    SSLEngine engine = context.createSSLEngine(host, port);
    engine.setUseClientMode(false);
    engine.setEnabledProtocols(tlsVersions);
    if (ciphers != null) {
      engine.setEnabledCipherSuites(ciphers);
    }
    // TODO configure engine
    return engine;
  }

  private KeyManager[] createKeyManagers(String filepath, String keystorePassword, String keyPassword) {
    try {
      KeyStore keyStore = KeyStore.getInstance("JKS");
      InputStream keyStoreIS = new FileInputStream(filepath);
      try {
        keyStore.load(keyStoreIS, keystorePassword.toCharArray());
      } finally {
        if (keyStoreIS != null) {
          keyStoreIS.close();
        }
      }
      KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      kmf.init(keyStore, keyPassword.toCharArray());
      return kmf.getKeyManagers();
    } catch (final IOException e) {
      e.printStackTrace();
    } catch (final CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException e) {
      e.printStackTrace();
    }
    return null;
  }

  private TrustManager[] createTrustManagers(String filepath, String keystorePassword) {
    if (filepath == null) {
      return null;
    }
    try {
      KeyStore trustStore = KeyStore.getInstance("JKS");
      InputStream trustStoreIS = new FileInputStream(filepath);
      try {
        trustStore.load(trustStoreIS, keystorePassword.toCharArray());
      } finally {
        if (trustStoreIS != null) {
          trustStoreIS.close();
        }
      }
      TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      trustFactory.init(trustStore);
      return trustFactory.getTrustManagers();
    } catch (final IOException e) {
      e.printStackTrace();
    } catch (final CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    return null;
  }

  public boolean isSslEnabled() {
    return sslEnabled;
  }
}
{'fraction_non_alphanumeric': 0.09197700574856287, 'fraction_numerical': 0.0, 'mean_word_length': 3.431893687707641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4444141', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1096, 'n_words': 239}