Class OkHttpClientHelper


  • public class OkHttpClientHelper
    extends Object
    Class which helps with the OkHttpClient and with the configuration of the transport layer security in particular.
    • Method Detail

      • initTLS

        public static void initTLS​(okhttp3.OkHttpClient.Builder builder,
                                   String clientCertPath,
                                   String clientCertPass,
                                   String serverCertPath,
                                   String serverCertPass)
                            throws GeneralSecurityException,
                                   IOException
        Initializes the transport layer security for a given OkHttpClient.Builder.
        Parameters:
        builder - OkHttpClient builder (not null, sslSocketFactory gets initialized)
        clientCertPath - Path to the PKCS12 client certificate (public and private key) or null (then client auth is disabled)
        clientCertPass - Password for PKCS12 client certificate (optional)
        serverCertPath - Path to the PKCS12 server certificate (CA or public key) or null (then the default truststore is used instead)
        serverCertPass - Password for PKCS12 client certificate (optional)
        Throws:
        GeneralSecurityException - if the client or server certificate is not valid
        IOException - if the client or server certificate is not readable
      • createKeyManagers

        public static KeyManager[] createKeyManagers​(String path,
                                                     String pass)
                                              throws GeneralSecurityException,
                                                     IOException
        Creates a list of key managers from a given client certificate.
        Parameters:
        path - Path to the PKCS12 client certificate which contains public and private key (optional)
        pass - Password for PKCS12 client certificate (optional)
        Returns:
        list of key managers or null if path is empty
        Throws:
        GeneralSecurityException - if the client certificate is not valid
        IOException - if the client certificate is not readable
      • createTrustManagers

        public static TrustManager[] createTrustManagers​(String path,
                                                         String pass)
                                                  throws GeneralSecurityException,
                                                         IOException
        Creates a list of trust managers from a given server certificate. Note: If the path is empty then TLS is enabled but the server certificate is not validated.
        Parameters:
        path - Path to the PKCS12 server certificate (usually a common authority or a public key or empty).
        pass - Password for PKCS12 client certificate (optional)
        Returns:
        list of trust managers (not null and not empty), when path is empty then the default truststore is returned
        Throws:
        GeneralSecurityException - if the server certificate is not valid
        IOException - if the server certificate is not readable