/**
   * @return
   * @throws NoSuchAlgorithmException
   * @throws KeyStoreException
   * @throws UnrecoverableKeyException
   * @throws IOException
   * @throws CertificateException
   */
  private KeyManager[] initKeyManagers()
      throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, IOException,
          CertificateException {
    // Initialize trust manager factory and set trusted CA list using keystore
    if (StringUtils.isEmpty(this.keyStorePath)) {
      log.info("Unset [keyStorePath] parameter, disable local private and certificate.");
      return null;
    } else {
      log.info(
          String.format("Loading private key and certificate from store: [%s]", this.keyStorePath));
      // Load key store
      KeyStore keystore = KeyStore.getInstance("JKS");
      InputStream in = Helper.getResourceAsStream(this.getClass(), this.keyStorePath);
      if (in == null) {
        throw new IOException(
            String.format("Could not reading from : [%s]", this.trustCertsStorePath));
      }
      keystore.load(in, this.keyStorePassword);

      KeyManagerFactory kmf = KeyManagerFactory.getInstance(this.getKeyManagerAlgorithm());
      kmf.init(keystore, this.keyStoreKeyPassword);
      log.info(String.format("Initialized key store: [%s]", this.keyStorePath));

      KeyManager[] keyManagers = kmf.getKeyManagers();
      return keyManagers;
    }
  }
  /**
   * @return
   * @throws KeyStoreException
   * @throws IOException
   * @throws NoSuchAlgorithmException
   * @throws CertificateException
   */
  private TrustManager[] initTrustManagers()
      throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
    // Initialize trust manager factory and set trusted CA list using keystore
    if (StringUtils.isEmpty(this.trustCertsStorePath)) {
      // Unset trustCertsStorePath, disable trust manager
      log.info("Unset [trustCertsStorePath] parameter, disable TrustManager");
      TrustManager[] trustAllCerts =
          new TrustManager[] {
            new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
              }

              public void checkClientTrusted(X509Certificate[] certs, String authType) {
                log.debug(String.format("Client certs;[%s], authType: [%s]", certs, authType));
              }

              public void checkServerTrusted(X509Certificate[] certs, String authType) {
                log.debug(String.format("Client certs;[%s], authType: [%s]", certs, authType));
              }
            }
          };
      return trustAllCerts;
    } else {
      log.info(String.format("Loading trust certs from store: [%s]", this.trustCertsStorePath));
      // Load key store
      KeyStore keystore = KeyStore.getInstance("JKS");
      InputStream in = Helper.getResourceAsStream(this.getClass(), this.trustCertsStorePath);
      if (in == null) {
        throw new IOException(
            String.format("Could not reading from : [%s]", this.trustCertsStorePath));
      }
      keystore.load(in, this.trustCertsStorePassword);

      TrustManagerFactory tmf = TrustManagerFactory.getInstance(this.getKeyManagerAlgorithm());
      tmf.init(keystore);
      TrustManager[] trustManagers = tmf.getTrustManagers();
      return trustManagers;
    }
  }