Class DefaultCommunicator

java.lang.Object
com.onlinepayments.DefaultCommunicator
All Implemented Interfaces:
Communicator, LoggingCapable, ObfuscationCapable, Closeable, AutoCloseable

public class DefaultCommunicator extends Object implements Communicator
Used to communicate with the Online Payments platform web services.

It contains all the logic to transform a request object to a HTTP request and a HTTP response to a response object.

Thread-safe.

  • Constructor Details

  • Method Details

    • close

      public void close() throws IOException
      Releases any system resources associated with this object.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • get

      public <O> O get(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context)
      Corresponds to the HTTP GET method.
      Specified by:
      get in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      responseType - The type of response to return.
      context - The optional call context to use.
    • get

      public void get(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context)
      Corresponds to the HTTP GET method.
      Specified by:
      get in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      bodyHandler - The handler for the response body.
      context - The optional call context to use.
    • delete

      public <O> O delete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context)
      Corresponds to the HTTP DELETE method.
      Specified by:
      delete in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      responseType - The type of response to return.
      context - The optional call context to use.
    • delete

      public void delete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context)
      Corresponds to the HTTP DELETE method.
      Specified by:
      delete in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      bodyHandler - The handler for the response body.
      context - The optional call context to use.
    • post

      public <O> O post(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context)
      Corresponds to the HTTP POST method.
      Specified by:
      post in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      requestBody - The optional request body to send.
      responseType - The type of response to return.
      context - The optional call context to use.
    • post

      public void post(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context)
      Corresponds to the HTTP POST method.
      Specified by:
      post in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      requestBody - The optional request body to send.
      bodyHandler - The handler for the response body.
      context - The optional call context to use.
    • put

      public <O> O put(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context)
      Corresponds to the HTTP PUT method.
      Specified by:
      put in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      requestBody - The optional request body to send.
      responseType - The type of response to return.
      context - The optional call context to use.
    • put

      public void put(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context)
      Corresponds to the HTTP PUT method.
      Specified by:
      put in interface Communicator
      Parameters:
      relativePath - The path to call, relative to the base URI.
      requestHeaders - An optional list of request headers.
      requestParameters - An optional set of request parameters.
      requestBody - The optional request body to send.
      bodyHandler - The handler for the response body.
      context - The optional call context to use.
    • getMarshaller

      public Marshaller getMarshaller()
      Specified by:
      getMarshaller in interface Communicator
      Returns:
      The Marshaller object associated with this communicator. Never null.
    • toAbsoluteURI

      protected URI toAbsoluteURI(String relativePath, List<RequestParam> requestParameters)
    • addGenericHeaders

      protected void addGenericHeaders(String httpMethod, URI uri, List<RequestHeader> requestHeaders, CallContext context)
      Adds the necessary headers to the given list of headers. This includes the authorization header, which uses other headers, so when you need to override this method, make sure to call super.addGenericHeaders at the end of your overridden method.
    • getHeaderDateString

      protected static String getHeaderDateString()
      Returns the date in the preferred format for the HTTP date header (RFC1123).
    • processResponse

      protected <O> O processResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, Class<O> responseType, String requestPath, CallContext context)
    • processResponse

      protected void processResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, BodyHandler bodyHandler, String requestPath, CallContext context)
    • updateContext

      protected static void updateContext(List<ResponseHeader> headers, CallContext context)
      Updates the given call context based on the contents of the given response.
    • throwExceptionIfNecessary

      protected static void throwExceptionIfNecessary(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, String requestPath)
      Checks the status code and headers for errors and throws an exception if necessary.
    • closeIdleConnections

      public void closeIdleConnections(long idleTime, TimeUnit timeUnit)
      Utility method that delegates the call to this communicator's connection if that's an instance of PooledConnection. If not this method does nothing.
      Specified by:
      closeIdleConnections in interface Communicator
      See Also:
    • closeExpiredConnections

      public void closeExpiredConnections()
      Utility method that delegates the call to this communicator's connection if that's an instance of PooledConnection. If not this method does nothing.
      Specified by:
      closeExpiredConnections in interface Communicator
      See Also:
    • setBodyObfuscator

      public void setBodyObfuscator(BodyObfuscator bodyObfuscator)
      Sets the current non-null body obfuscator to use.
      Specified by:
      setBodyObfuscator in interface ObfuscationCapable
    • setHeaderObfuscator

      public void setHeaderObfuscator(HeaderObfuscator headerObfuscator)
      Sets the current non-null header obfuscator to use.
      Specified by:
      setHeaderObfuscator in interface ObfuscationCapable
    • enableLogging

      public void enableLogging(CommunicatorLogger communicatorLogger)
      Turns on logging using the given communicator logger.
      Specified by:
      enableLogging in interface LoggingCapable
    • disableLogging

      public void disableLogging()
      Turns off logging.
      Specified by:
      disableLogging in interface LoggingCapable