Class JKServiceClient<T>

java.lang.Object
com.jk.services.client.JKServiceClient<T>
Type Parameters:
T - Specifies the type of data model that this service client works with.
Direct Known Subclasses:
JKMatureServiceClient

public class JKServiceClient<T> extends Object
This class is a generic utility for making HTTP service calls and processing responses.

It provides methods for making GET, POST, PUT, DELETE, and PATCH requests, as well as handling various types of responses.

Version:
1.0
Author:
Dr. Jalal H. Kiswani
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected JKLogger
    Represents this class logger.
  • Constructor Summary

    Constructors
    Constructor
    Description
    This method constructs a new JKServiceClient.
    This method constructs a new JKServiceClient with the specified base URL.
    JKServiceClient(String base, Class<T> clas)
    This method constructs a new JKServiceClient with the specified base URL and model class.
  • Method Summary

    Modifier and Type
    Method
    Description
    This method calls a service end-point with a POST request and expects a file download in response.
    This method calls a service end-point with a GET request and retrieves a String response.
    <T> T
    callJson(Class<T> clas, String url)
    This method makes an HTTP service call and retrieves the response as JSON, converting it to an object of the specified class.
    This method performs an HTTP GET request to the specified URL and returns the response as an object of type T.
    This method sends a GET request to the service and retrieves a JSON array of objects, then converts it into a list of objects of type T.
    <E> List<E>
    This method calls a service end-point with a GET request and retrieves a list of objects of the specified class from the JSON response.
    This method makes a GET request to the specified path and expects a JSON response representing an array of objects of type T.
    This method makes a GET request to the base URL and expects a JSON response as a String.
    This method makes a GET request to the specified path appended to the base URL and expects a JSON response as a String.
    This method calls the service with an HTTP DELETE request to the specified path.
    This method calls a service end-point with a PATCH request, sending a JSON pay-load, and retrieves a String response.
    This method calls an HTTP service with the HTTP PATCH method and sends a JSON pay-load.
    This method calls the service with an HTTP POST request and sends the provided object as the request pay-load.
    This method calls the service with an HTTP POST request to the specified path and sends the provided object as the request pay-load.
    This method calls the service with an HTTP PUT request to the base path and sends the provided object as the request pay-load.
    callJsonWithPut(String path, Object object)
    This method calls the service with an HTTP PUT request to the specified path and sends the provided object as the request pay-load.
    This method calls a service end-point with a PATCH request and retrieves a String response.
    This method calls a service end-point with a POST request and retrieves a String response.
    callPut(String path, Object object, String mediaType)
    This method calls the service with an HTTP PUT request to the specified path and sends the provided object as the request pay-load with the given media type.
    This method calls an HTTP service and handles the request and response using the provided service caller.
    <T> T
    callSingleJson(Class<T> clas, String url)
    This method performs an HTTP GET request to the specified URL and returns the response as an object of the specified class type T.
    This method performs an HTTP GET request to the specified URL and returns the response as an object of type T.
    protected jakarta.ws.rs.client.Client
    This method creates and configures an HTTP client for making requests to a remote service.
    protected SSLContext
    This method creates and initializes an SSLContext with the necessary settings to enable communication with HTTPS servers.
    This method retrieves the media type expected in the response from service calls.
    This method gets the base URL used for making service calls.
    protected int
    This method retrieves the connection timeout value for HTTP requests from the application's configuration.
    protected String
    This method constructs a full URL by appending the provided path to the base URL.
    Map<String,? extends Object>
    This method gets the map of HTTP headers to include in service requests.
    This method retrieves the log service client.
    This method gets the class type for the data model that this service client works with.
    protected int
    This method retrieves the read timeout value for HTTP requests from the application's configuration.
    This method retrieves the media type to use for the request body when making service calls.
    jakarta.ws.rs.core.MultivaluedMap<String,Object>
    This method gets the headers received in the response from service calls.
    This method retrieves the name of the service associated with this client.
    protected String
    This method is yet to be implemented (TBI).
    boolean
    This method checks whether remote logging is enabled for service calls made with this client or not.
    void
    setAcceptMediaType(String acceptMediaType)
    This method sets the media type expected in the response from service calls to the provided value.
    void
    This method sets the base URL for making service calls.
    void
    setEnableRemoteLogging(boolean enableRemoteLogging)
    This method sets whether remote logging is enabled for service calls made with this client or not.
    void
    setHeaders(Map<String,? extends Object> headers)
    This method sets the map of HTTP headers to include in service requests to the provided map.
    protected void
    setHeadersOnRequest(jakarta.ws.rs.client.Invocation.Builder builder)
    This method sets custom headers on the request builder before making a service call.
    void
    setModelClass(Class<T> modelClass)
    This method sets the class type for the data model that this service client works with.
    void
    setRequestMediaType(String requestMediaType)
    This method sets the media type to use for the request body when making service calls to the provided value.
    void
    setServiceName(String serviceName)
    This method sets the name of the service associated with this client to the provided value.
    <E> E
    toObject(Class<E> clas, String json)
    This method converts a JSON string to an object of the specified class.
    This method deserializes the provided JSON string into an object of type T.
    <E> List<E>
    toObjects(Class<E> clas, String json)
    This method deserializes a JSON string into a List of objects of the specified class.
    This method deserializes a JSON string into a list of objects of type T.
    uploadFile(String path, File file)
    This method uploads a file to a specified path on the service.
    protected void
    validateReponse(String url, jakarta.ws.rs.core.Response response)
    This method validates the HTTP response and checks if it represents a successful status code (2xx).

    Methods inherited from class java.lang.Object

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

    • logger

      protected JKLogger logger
      Represents this class logger.
  • Constructor Details

    • JKServiceClient

      public JKServiceClient()
      This method constructs a new JKServiceClient.
    • JKServiceClient

      public JKServiceClient(String base)
      This method constructs a new JKServiceClient with the specified base URL.
      Parameters:
      base - Specifies the base URL representing the root of the service's end-points.
    • JKServiceClient

      public JKServiceClient(String base, Class<T> clas)
      This method constructs a new JKServiceClient with the specified base URL and model class.
      Parameters:
      base - Specifies the base URL representing the root of the service's end-points.
      clas - Specifies the Class representing the model type associated with this client.
  • Method Details

    • callJson

      public T callJson(String url)
      This method performs an HTTP GET request to the specified URL and returns the response as an object of type T.
      Parameters:
      url - Specifies the URL to send the GET request to.
      Returns:
      an object of type T representing the response from the service.
    • callSingleJson

      public T callSingleJson(String url)
      This method performs an HTTP GET request to the specified URL and returns the response as an object of type T.
      Parameters:
      url - Specifies the URL to send the GET request to.
      Returns:
      an object of type T representing the response from the service.
    • callJson

      public <T> T callJson(Class<T> clas, String url)
      This method makes an HTTP service call and retrieves the response as JSON, converting it to an object of the specified class.
      Type Parameters:
      T - the generic type
      Parameters:
      clas - Specifies the class type to which the JSON response should be converted.
      url - Specifies the URL to make the service call to.
      Returns:
      an object of the specified class representing the JSON response data.
    • callSingleJson

      public <T> T callSingleJson(Class<T> clas, String url)
      This method performs an HTTP GET request to the specified URL and returns the response as an object of the specified class type T.
      Type Parameters:
      T - Specifies the response type.
      Parameters:
      clas - Specifies the class type T into which the response should be deserialized.
      url - Specifies the URL to send the GET request to.
      Returns:
      an object of type T representing the response from the service.
    • toObject

      public T toObject(String json)
      This method deserializes the provided JSON string into an object of type T.
      Parameters:
      json - Specifies the JSON string to deserialize into an object of type T.
      Returns:
      an object of type T representing the deserialized JSON data.
    • toObject

      public <E> E toObject(Class<E> clas, String json)
      This method converts a JSON string to an object of the specified class.
      Type Parameters:
      E - the element type
      Parameters:
      clas - Specifies the class type to which the JSON should be converted.
      json - Specifies the JSON string to be converted.
      Returns:
      an object of the specified class representing the JSON data.
    • callJsonAsListOfObjects

      public List<T> callJsonAsListOfObjects()
      This method sends a GET request to the service and retrieves a JSON array of objects, then converts it into a list of objects of type T.
      Returns:
      the list of objects of type T representing the deserialized JSON array.
    • toObjects

      public List<T> toObjects(String json)
      This method deserializes a JSON string into a list of objects of type T.
      Parameters:
      json - Specifies the JSON string representing an array of objects.
      Returns:
      the list of objects of type T representing the deserialized JSON array.
    • callJsonAsListOfObjects

      public List<T> callJsonAsListOfObjects(String path)
      This method makes a GET request to the specified path and expects a JSON response representing an array of objects of type T.
      Parameters:
      path - Specifies the path to the resource to retrieve.
      Returns:
      the list of objects of type T parsed from the JSON response.
    • callJsonAsString

      public String callJsonAsString()
      This method makes a GET request to the base URL and expects a JSON response as a String.
      Returns:
      the String representing the JSON response from the service.
    • callJsonAsString

      public String callJsonAsString(String path)
      This method makes a GET request to the specified path appended to the base URL and expects a JSON response as a String.
      Parameters:
      path - Specifies the path to append to the base URL for the GET request.
      Returns:
      the String representing the JSON response from the service.
    • validateReponse

      protected void validateReponse(String url, jakarta.ws.rs.core.Response response)
      This method validates the HTTP response and checks if it represents a successful status code (2xx).
      Parameters:
      url - Specifies the URL or path that was requested.
      response - Specifies the HTTP response to validate.
    • callJsonWithPost

      public String callJsonWithPost(Object object)
      This method calls the service with an HTTP POST request and sends the provided object as the request pay-load.
      Parameters:
      object - Specifies the object to be sent as the request pay-load.
      Returns:
      the response from the service as a JSON string.
    • callJsonWithPost

      public String callJsonWithPost(String path, Object object)
      This method calls the service with an HTTP POST request to the specified path and sends the provided object as the request pay-load.
      Parameters:
      path - Specifies the relative path to the service end-point.
      object - Specifies the object to be sent as the request pay-load.
      Returns:
      the response from the service as a JSON string.
    • callJsonWithPut

      public String callJsonWithPut(Object object)
      This method calls the service with an HTTP PUT request to the base path and sends the provided object as the request pay-load.
      Parameters:
      object - Specifies the object to be sent as the request pay-load.
      Returns:
      the response from the service as a JSON string.
    • callJsonWithPut

      public String callJsonWithPut(String path, Object object)
      This method calls the service with an HTTP PUT request to the specified path and sends the provided object as the request pay-load.
      Parameters:
      path - Specifies the path to which the PUT request should be sent.
      object - Specifies the object to be sent as the request pay-load.
      Returns:
      the response from the service as a JSON string.
    • callPut

      public String callPut(String path, Object object, String mediaType)
      This method calls the service with an HTTP PUT request to the specified path and sends the provided object as the request pay-load with the given media type.
      Parameters:
      path - Specifies the path to which the PUT request should be sent.
      object - Specifies the object to be sent as the request pay-load.
      mediaType - Specifies the media type of the request pay-load.
      Returns:
      the response from the service as a JSON string.
    • callJsonWithDelete

      public String callJsonWithDelete(String path)
      This method calls the service with an HTTP DELETE request to the specified path.
      Parameters:
      path - Specifies the path to which the DELETE request should be sent.
      Returns:
      the response from the service as a JSON string.
    • createClient

      protected jakarta.ws.rs.client.Client createClient()
      This method creates and configures an HTTP client for making requests to a remote service.
      Returns:
      an instance of the configured HTTP client.
    • getReadTimeout

      protected int getReadTimeout()
      This method retrieves the read timeout value for HTTP requests from the application's configuration.
      Returns:
      the read timeout value.
    • getConnectTimeout

      protected int getConnectTimeout()
      This method retrieves the connection timeout value for HTTP requests from the application's configuration.
      Returns:
      the connection timeout value.
    • createSslContext

      protected SSLContext createSslContext() throws NoSuchAlgorithmException, KeyManagementException
      This method creates and initializes an SSLContext with the necessary settings to enable communication with HTTPS servers.
      Returns:
      the initialized SSLContext instance.
      Throws:
      NoSuchAlgorithmException - if the SSL algorithm is not available.
      KeyManagementException - if there is an issue with key management during SSLContext initialization.
    • getFullUrl

      protected String getFullUrl(String path)
      This method constructs a full URL by appending the provided path to the base URL.
      Parameters:
      path - Specifies the path to be appended to the base URL.
      Returns:
      the full URL formed by concatenating the base URL and the provided path.
    • getBase

      public String getBase()
      This method gets the base URL used for making service calls.
      Returns:
      the base URL for service calls.
    • getServiceUrlPropertyName

      protected String getServiceUrlPropertyName()
      This method is yet to be implemented (TBI).
      Returns:
      null.
    • setBase

      public void setBase(String base)
      This method sets the base URL for making service calls.
      Parameters:
      base - Specifies the new base URL.
    • getModelClass

      public Class<T> getModelClass()
      This method gets the class type for the data model that this service client works with.
      Returns:
      the class type for the data model that this service client works with.
    • setModelClass

      public void setModelClass(Class<T> modelClass)
      This method sets the class type for the data model that this service client works with.
      Parameters:
      modelClass - Specifies the new class type for the data model.
    • callService

      public Object callService(String path, JKServiceCaller caller)
      This method calls an HTTP service and handles the request and response using the provided service caller.
      Parameters:
      path - Specifies the path to the service end-point.
      caller - Specifies the service caller responsible for handling the request and response.
      Returns:
      the processed response data.
    • isEnableRemoteLogging

      public boolean isEnableRemoteLogging()
      This method checks whether remote logging is enabled for service calls made with this client or not.
      Returns:
      true, if it is enabled, false otherwise.
    • setEnableRemoteLogging

      public void setEnableRemoteLogging(boolean enableRemoteLogging)
      This method sets whether remote logging is enabled for service calls made with this client or not.
      Parameters:
      enableRemoteLogging - Indicates whether remote logging is enabled for service calls made with this client (true) or not (false).
    • uploadFile

      public String uploadFile(String path, File file)
      This method uploads a file to a specified path on the service.
      Parameters:
      path - Specifies the path on the service where the file should be uploaded.
      file - Specifies the file to be uploaded.
      Returns:
      a response indicating the success or failure of the upload operation.
    • setHeadersOnRequest

      protected void setHeadersOnRequest(jakarta.ws.rs.client.Invocation.Builder builder)
      This method sets custom headers on the request builder before making a service call.
      Parameters:
      builder - Specifies the request builder to which custom headers should be added.
    • getServiceName

      public String getServiceName()
      This method retrieves the name of the service associated with this client.
      Returns:
      the name of the service associated with this client.
    • setServiceName

      public void setServiceName(String serviceName)
      This method sets the name of the service associated with this client to the provided value.
      Parameters:
      serviceName - Specifies the new name of the service associated with this client.
    • getHeaders

      public Map<String,? extends Object> getHeaders()
      This method gets the map of HTTP headers to include in service requests.
      Returns:
      the map of HTTP headers to include in service requests.
    • setHeaders

      public void setHeaders(Map<String,? extends Object> headers)
      This method sets the map of HTTP headers to include in service requests to the provided map.
      Parameters:
      headers - Specifies the new map of HTTP headers.
    • callDownloadFileWithPost

      public File callDownloadFileWithPost(String path, Object object)
      This method calls a service end-point with a POST request and expects a file download in response.
      Parameters:
      path - Specifies the path to the service end-point.
      object - Specifies the pay-load to be sent in the POST request.
      Returns:
      a File object representing the downloaded file.
    • callGet

      public String callGet(String path)
      This method calls a service end-point with a GET request and retrieves a String response.
      Parameters:
      path - Specifies the path to the service end-point.
      Returns:
      the String representing the response from the service.
    • callPost

      public String callPost(String path)
      This method calls a service end-point with a POST request and retrieves a String response.
      Parameters:
      path - Specifies the path to the service end-point.
      Returns:
      the String representing the response from the service.
    • callPatch

      public String callPatch(String path)
      This method calls a service end-point with a PATCH request and retrieves a String response.
      Parameters:
      path - Specifies the path to the service end-point.
      Returns:
      the String representing the response from the service.
    • callJsonWithPatch

      public String callJsonWithPatch(Object object)
      This method calls a service end-point with a PATCH request, sending a JSON pay-load, and retrieves a String response.
      Parameters:
      object - Specifies the JSON pay-load to send in the request.
      Returns:
      the String representing the response from the service.
    • callJsonWithPatch

      public String callJsonWithPatch(String path, Object object)
      This method calls an HTTP service with the HTTP PATCH method and sends a JSON pay-load.
      Parameters:
      path - Specifies the path to the service end-point.
      object - Specifies the JSON pay-load to send.
      Returns:
      the response from the service.
    • getLogServiceClient

      protected JKLogServiceClient getLogServiceClient()
      This method retrieves the log service client.
      Returns:
      the log service client.
    • getRequestMediaType

      public String getRequestMediaType()
      This method retrieves the media type to use for the request body when making service calls.
      Returns:
      the media type to use for the request body when making service calls.
    • setRequestMediaType

      public void setRequestMediaType(String requestMediaType)
      This method sets the media type to use for the request body when making service calls to the provided value.
      Parameters:
      requestMediaType - Specifies the new media type to use for the request body when making service calls.
    • getAcceptMediaType

      public String getAcceptMediaType()
      This method retrieves the media type expected in the response from service calls.
      Returns:
      the media type expected in the response from service calls.
    • setAcceptMediaType

      public void setAcceptMediaType(String acceptMediaType)
      This method sets the media type expected in the response from service calls to the provided value.
      Parameters:
      acceptMediaType - Specifies the new media type expected in the response from service calls.
    • callJsonAsListOfObjects

      public <E> List<E> callJsonAsListOfObjects(Class<E> clas, String path)
      This method calls a service end-point with a GET request and retrieves a list of objects of the specified class from the JSON response.
      Type Parameters:
      E - Specifies the type of objects to deserialize from the JSON response.
      Parameters:
      clas - Specifies the class of objects to deserialize from the JSON response.
      path - Specifies the path of the service end-point to call.
      Returns:
      the List of objects of the specified class.
    • toObjects

      public <E> List<E> toObjects(Class<E> clas, String json)
      This method deserializes a JSON string into a List of objects of the specified class.
      Type Parameters:
      E - Specifies the type of objects to deserialize from the JSON string.
      Parameters:
      clas - Specifies the class of objects to deserialize from the JSON string.
      json - Specifies the JSON string to deserialize.
      Returns:
      the List of objects of the specified class.
    • getResponseHeaders

      public jakarta.ws.rs.core.MultivaluedMap<String,Object> getResponseHeaders()
      This method gets the headers received in the response from service calls.
      Returns:
      the headers received in the response from service calls.