Package com.jk.services.client
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
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 -
Constructor Summary
ConstructorsConstructorDescriptionThis method constructs a newJKServiceClient
.JKServiceClient
(String base) This method constructs a newJKServiceClient
with the specified base URL.JKServiceClient
(String base, Class<T> clas) This method constructs a newJKServiceClient
with the specified base URL and model class. -
Method Summary
Modifier and TypeMethodDescriptioncallDownloadFileWithPost
(String path, Object object) 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
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>
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.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.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.callJsonWithDelete
(String path) This method calls the service with an HTTP DELETE request to the specified path.callJsonWithPatch
(Object object) This method calls a service end-point with a PATCH request, sending a JSON pay-load, and retrieves a String response.callJsonWithPatch
(String path, Object object) This method calls an HTTP service with the HTTP PATCH method and sends a JSON pay-load.callJsonWithPost
(Object object) This method calls the service with an HTTP POST request and sends the provided object as the request pay-load.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.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.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.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.callService
(String path, JKServiceCaller caller) 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.callSingleJson
(String url) 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.getBase()
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
getFullUrl
(String path) This method constructs a full URL by appending the provided path to the base URL.This method gets the map of HTTP headers to include in service requests.protected JKLogServiceClient
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.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
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>
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).
-
Field Details
-
logger
Represents this class logger.
-
-
Constructor Details
-
JKServiceClient
public JKServiceClient()This method constructs a newJKServiceClient
. -
JKServiceClient
This method constructs a newJKServiceClient
with the specified base URL.- Parameters:
base
- Specifies the base URL representing the root of the service's end-points.
-
JKServiceClient
This method constructs a newJKServiceClient
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
This method gets the base URL used for making service calls.- Returns:
- the base URL for service calls.
-
getServiceUrlPropertyName
This method is yet to be implemented (TBI).- Returns:
- null.
-
setBase
This method sets the base URL for making service calls.- Parameters:
base
- Specifies the new base URL.
-
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
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
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
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
This method retrieves the name of the service associated with this client.- Returns:
- the name of the service associated with this client.
-
setServiceName
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
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
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
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
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
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
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
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
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
This method retrieves the log service client.- Returns:
- the log service client.
-
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
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
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
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
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
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
This method gets the headers received in the response from service calls.- Returns:
- the headers received in the response from service calls.
-