Class JsonHttpClientSupport

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
All Implemented Interfaces:
net.solarnetwork.service.Identifiable
Direct Known Subclasses:
UserMetadataService

public abstract class JsonHttpClientSupport extends HttpClientSupport
An abstract class to support HTTP based services that use JSON.

The configurable properties of this class are:

compress
Flag to compress the HTTP body content, defaults to false.
objectMapper
The ObjectMapper to marshall/unmarshall objects to/from JSON with.
Version:
1.3
Author:
matt
  • Field Details

  • Constructor Details

    • JsonHttpClientSupport

      public JsonHttpClientSupport()
      Default constructor.
  • Method Details

    • doJson

      protected final InputStream doJson(String url, String method, Object data) throws IOException
      Perform a JSON HTTP request.
      Parameters:
      url - the URL to make the request to
      method - the HTTP method, e.g. HttpClientSupport.HTTP_METHOD_GET
      data - the optional data to marshall to JSON and upload as the request content
      Returns:
      the InputStream for the HTTP response
      Throws:
      IOException - if any IO error occurs
    • doJson

      protected final InputStream doJson(String url, String method, Object data, Consumer<URLConnection> connectionCustomizer) throws IOException
      Perform a JSON HTTP request.
      Parameters:
      url - the URL to make the request to
      method - the HTTP method, e.g. HttpClientSupport.HTTP_METHOD_GET
      data - the optional data to marshall to JSON and upload as the request content
      connectionCustomizer - an optional consumer to customize the created URL connection, before it is opened
      Returns:
      the InputStream for the HTTP response
      Throws:
      IOException - if any IO error occurs
      Since:
      1.3
    • jsonGET

      protected final InputStream jsonGET(String url) throws IOException
      Perform a JSON GET HTTP request.
      Parameters:
      url - the URL to GET
      Returns:
      the HTTP response InputStream
      Throws:
      IOException - if any IO error occurs
    • jsonGET

      protected final InputStream jsonGET(String url, Consumer<URLConnection> connectionCustomizer) throws IOException
      Perform a JSON GET HTTP request.
      Parameters:
      url - the URL to GET
      connectionCustomizer - an optional consumer to customize the created URL connection, before it is opened
      Returns:
      the HTTP response InputStream
      Throws:
      IOException - if any IO error occurs
      Since:
      1.3
    • jsonPOST

      protected final InputStream jsonPOST(String url, Object data) throws IOException
      Perform a JSON POST HTTP request.
      Parameters:
      url - the URL to POST
      data - the object to marshall into JSON
      Returns:
      the HTTP response InputStream
      Throws:
      IOException - if any IO error occurs
    • jsonPOST

      protected final InputStream jsonPOST(String url, Object data, Consumer<URLConnection> connectionCustomizer) throws IOException
      Perform a JSON POST HTTP request.
      Parameters:
      url - the URL to POST
      data - the object to marshall into JSON
      connectionCustomizer - an optional consumer to customize the created URL connection, before it is opened
      Returns:
      the HTTP response InputStream
      Throws:
      IOException - if any IO error occurs
      Since:
      1.3
    • extractResponseData

      protected <T> T extractResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException
      Parse a standard Response HTTP response and return the data object as the provided type.
      Type Parameters:
      T - the element type
      Parameters:
      in - the InputStream to read, which will be closed before returning from this method
      dataType - the type of object to extract from the response
      Returns:
      the extracted object, or null
      Throws:
      net.solarnetwork.service.RemoteServiceException - if the response does not include the success flag
      IOException - if any IO error occurs
    • extractCollectionResponseData

      protected <T> Collection<T> extractCollectionResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException
      Parse a standard Response HTTP response and return the data array as a collection of objects of the provided type.
      Type Parameters:
      T - the collection element type
      Parameters:
      in - the InputStream to read, which will be closed before returning from this method
      dataType - the type of object to extract from the response
      Returns:
      the extracted object, or null
      Throws:
      net.solarnetwork.service.RemoteServiceException - if the response does not include the success flag
      IOException - if any IO error occurs
      Since:
      1.1
    • extractFilterResultsCollectionResponseData

      protected <T> Collection<T> extractFilterResultsCollectionResponseData(InputStream in, Class<T> dataType) throws net.solarnetwork.service.RemoteServiceException, IOException
      Parse a standard Response HTTP response and return the data.results array as objects of the provided type.
      Type Parameters:
      T - the collection element type
      Parameters:
      in - the InputStream to read, which will be closed before returning from this method
      dataType - the type of object to extract from the response
      Returns:
      the extracted objects, or null
      Throws:
      net.solarnetwork.service.RemoteServiceException - if the response does not include the success flag
      IOException - if any IO error occurs
      Since:
      1.2
    • verifyResponseSuccess

      protected void verifyResponseSuccess(InputStream in) throws net.solarnetwork.service.RemoteServiceException, IOException
      Test for a successful standard Response HTTP response.
      Parameters:
      in - the InputStream to read, which will be closed before returning from this method
      Throws:
      net.solarnetwork.service.RemoteServiceException - if the response does not include the success flag
      IOException - if any IO error occurs
    • getObjectMapper

      public final com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Get the JSON mapper.
      Returns:
      the mapper
    • setObjectMapper

      public final void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Set the JSON mapper.
      Parameters:
      objectMapper - the mapper
    • isCompress

      public final boolean isCompress()
      Get the compress flag.
      Returns:
      true to compress HTTP body content with gzip
    • setCompress

      public final void setCompress(boolean compress)
      Set the compress flag.
      Parameters:
      compress - true to compress HTTP body content with gzip