Class HttpClientFactory

java.lang.Object
com.janeirodigital.sai.core.http.HttpClientFactory
All Implemented Interfaces:
com.janeirodigital.shapetrees.client.okhttp.OkHttpClientFactory

public class HttpClientFactory extends Object implements com.janeirodigital.shapetrees.client.okhttp.OkHttpClientFactory
Factory to get, cache, and clear OkHttp HTTP clients matching provided configurations.
Can enable / disable SSL validation and/or Shape Tree validation
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    Internal class to manage configuration options for http clients
  • Constructor Summary

    Constructors
    Constructor
    Description
    HttpClientFactory(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens)
     
    HttpClientFactory(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens, com.janeirodigital.sai.authentication.AuthorizedSessionAccessor sessionAccessor)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    okhttp3.OkHttpClient
    get()
     
    okhttp3.OkHttpClient
    get(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens)
    Factory to provide an OkHttpClient configured to enable or disable SSL and/or Shape Tree validation based on the provided options.
    okhttp3.OkHttpClient
    getClientForConfiguration(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens)
    Build and return an OkHttpClient based on the provided options for SSL Validation, Shape Tree Validation, and Token Refresh.
    okhttp3.OkHttpClient
    Implementation of the OkHttpClientFactory interface provided from shapetrees-java-client-okhttp.
    boolean
    Identifies whether the local OkHttp client cache is empty or not
    void
    Shuts down each initialized OkHttp client in the local cache, and then empties them from it.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpClientFactory

      public HttpClientFactory(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens, com.janeirodigital.sai.authentication.AuthorizedSessionAccessor sessionAccessor) throws SaiException
      Throws:
      SaiException
    • HttpClientFactory

      public HttpClientFactory(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens) throws SaiException
      Throws:
      SaiException
  • Method Details

    • get

      public okhttp3.OkHttpClient get() throws SaiException
      Throws:
      SaiException
    • get

      public okhttp3.OkHttpClient get(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens) throws SaiException
      Factory to provide an OkHttpClient configured to enable or disable SSL and/or Shape Tree validation based on the provided options. A small array of clients is maintained such that there is a given client for each unique configuration.
      SSL Validation should never be disabled in production!
      Parameters:
      validateSsl - Disables client/server SSL validation when false.
      validateShapeTrees - Disables client-side shape tree validation when false.
      refreshTokens - Disables automatic attempt to refresh access tokens on HTTP 401 responses when false
      Returns:
      Configured OkHttpClient
      Throws:
      SaiException
      See Also:
    • getClientForConfiguration

      public okhttp3.OkHttpClient getClientForConfiguration(boolean validateSsl, boolean validateShapeTrees, boolean refreshTokens) throws NoSuchAlgorithmException, KeyManagementException, SaiException
      Build and return an OkHttpClient based on the provided options for SSL Validation, Shape Tree Validation, and Token Refresh. SSL is disabled by installing an all-trusting certificate manager. Shape tree validation is enabled by injecting an OkHttp interceptor.
      Parameters:
      validateSsl - Disables client/server SSL validation when false.
      validateShapeTrees - Disables client-side shape tree validation when false.
      refreshTokens - Disables automatic attempt to refresh access tokens on HTTP 401 responses when false
      Returns:
      Configured OkHttpClient
      Throws:
      NoSuchAlgorithmException
      KeyManagementException
      SaiException
    • getOkHttpClient

      public okhttp3.OkHttpClient getOkHttpClient() throws com.janeirodigital.shapetrees.core.exceptions.ShapeTreeException
      Implementation of the OkHttpClientFactory interface provided from shapetrees-java-client-okhttp. Allows this factory to be used as the source of okhttp clients by that library.
      This call explicitly sets validateShapeTrees to false because it is the responsibility of OkHttpClientFactory enable validation, and would create an infinite loop if it were set to true.
      Specified by:
      getOkHttpClient in interface com.janeirodigital.shapetrees.client.okhttp.OkHttpClientFactory
      Returns:
      OkHttpClient
      Throws:
      com.janeirodigital.shapetrees.core.exceptions.ShapeTreeException
    • resetClients

      public void resetClients()
      Shuts down each initialized OkHttp client in the local cache, and then empties them from it.
    • isEmpty

      public boolean isEmpty()
      Identifies whether the local OkHttp client cache is empty or not
      Returns:
      true when the cache is empty