Class Client

java.lang.Object
com.groiss.http.Client
All Implemented Interfaces:
SilentCloseable, AutoCloseable

public class Client extends Object implements SilentCloseable
This class is designed to be used in any case you need to communicate with another server using the HTTP protocol. It provides easy-to-use methods for those use cases we identified as being the most common ones (e.g. a GET or POST request with some parameters). For all other methods a more generic (and therefore not so handy) method is provided.

Additionally it hides the need for caring about how data (parameters or other payload) will be transfered to that server, how it needs to be encoded, etc.

For aspects common to (almost) all of your client-server-calls you can use a ClientConfiguration object, e.g. for setting a base url for that server or for setting an authorization token that needs always to be passed of for setting an url prefix.

A typical usage of this class could be like that:

 private static ClientConfiguration config = new ClientConfiguration().setBaseUrl("http://mytestserver/myrestbase/");

 protected JSONObject myGet(String myRestId) throws Exception {
    try (Client client = new Client(config); Response res = client.get("myrestmethod/" + myRestId)) {
       if (res.getStatusCode() == 200) {
          return res.getJSONObjectResult();
       }
       throw new ApplicationException("Unsupported status code: " + res.getStatusCode() + " - " + res.getStatusMessage());
    }
 }

 protected JSONObject myPost(String id) throws Exception {
    try (Client client = new Client(config);
       Response res = client.post("myrestmethod/", new Parameter("id", id), new Parameter("name", name))) {
       if (res.getStatusCode() == 200) {
          return res.getJSONObjectResult();
       }
       throw new ApplicationException("Unsupported status code: " + res.getStatusCode() + " - " + res.getStatusMessage());
    }
 }
 
  • Field Details

    • logger

      protected static org.slf4j.Logger logger
    • DEFAULT_CHARSET

      protected static final String DEFAULT_CHARSET
      the default characters set to be used if not other is specified
    • config

      protected ClientConfiguration config
      the configuration object for this client
    • httpClient

      protected org.apache.http.impl.client.CloseableHttpClient httpClient
    • clientBuilder

      protected org.apache.http.impl.client.HttpClientBuilder clientBuilder
    • requestAsString

      protected String requestAsString
  • Constructor Details

    • Client

      public Client()
      creates a new client
    • Client

      public Client(ClientConfiguration config)
      creates a new client based on the passed configuration
      Parameters:
      config - the configuration for this client
  • Method Details

    • init

      protected void init()
      this method initializes the client based on the passed configuration object
    • close

      public void close()
      Closes the client and releases all resources. If the client is already closed then invoking this method has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface SilentCloseable
    • toString

      public String toString()
      Returns a string representation of the request that should be executed by this client. Will be null if request could not be created.
      Overrides:
      toString in class Object
    • delete

      public Response delete(String url, Parameter... params)
      Executes a DELETE request on the passed url using the passed parameters
      Parameters:
      url - the url of the request
      params - the parameters for the request
      Returns:
      the response of the request
    • delete

      public Response delete(String url, Object payload, Parameter... params)
      Executes a DELETE request on the passed url using the passed payload and parameters
      Parameters:
      url - the url of the request
      payload - the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...))
      params - the parameters for the request
      Returns:
      the response of the request
    • get

      public Response get(String url, Parameter... params)
      Executes a GET request on the passed url using the passed parameters
      Parameters:
      url - the url of the request
      params - the parameters for the request
      Returns:
      the response of the request
    • post

      public Response post(String url, Parameter... params)
      Executes a POST request on the passed url using the passed parameters
      Parameters:
      url - the url of the request
      params - the parameters for the request (sent as form parameters)
      Returns:
      the response of the request
    • post

      public Response post(String url, Object payload, Parameter... params)
      Executes a POST request on the passed url using the passed payload and parameters
      Parameters:
      url - the url of the request
      payload - the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...))
      params - the parameters for the request
      Returns:
      the response of the request
    • put

      public Response put(String url, Parameter... params)
      Executes a PUT request on the passed url using the passed parameters
      Parameters:
      url - the url of the request
      params - the parameters for the request
      Returns:
      the response of the request
    • put

      public Response put(String url, Object payload, Parameter... params)
      Executes a PUT request on the passed url using the passed payload and parameters
      Parameters:
      url - the url of the request
      payload - the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...))
      params - the parameters for the request
      Returns:
      the response of the request
    • executeRequest

      public Response executeRequest(Client.Method method, String url, Map<String,String> headers, Object payload, Parameter... params)
      Executes a request based on the specified parameters:
      Parameters:
      method - the method of the request (e.g. GET or HEAD)
      url - the url of the request
      headers - headers for the request. This is only needed if the default settings are not appropriate or do not contain the needed header property
      payload - the payload of the request. By now we provide support for a File, a JSONObject or a JSONArray. For every other type of payload you need to specify the 'Content-Type' on your own and the toString()-result of your payload must match with that content type.
      params - the parameters for the request. In case of POST, PATCH and PUT the parameters will be sent as form parameters, otherwise they will be sent as request parameters.
      Returns:
      the response of the request
    • execute

      protected org.apache.http.client.methods.CloseableHttpResponse execute(Client.Method method, String url, Map<String,String> headers, Object payload, Parameter... params)
      Executes a request based on the specified parameters:
      Parameters:
      method - the method of the request (e.g. GET or HEAD)
      url - the url of the request
      headers - headers for the request. This is only needed if the default settings are not appropriate or do not contain the needed header property
      payload - the payload of the request. By now we support a File, a JSONObject or a JSONArray. For every other payload you need to specify the 'Content-Type' on your own and the toString()-result of your payload must match with that content type.
      params - the parameters for the request. In case of POST, PATCH and PUT the parameters will be sent as form parameters, otherwise they will be sent as request parameters.
      Returns:
      the response of the request
    • getClientForMethod

      protected org.apache.http.client.methods.HttpRequestBase getClientForMethod(Client.Method method)
      Returns the request implementation for the passed method
      Parameters:
      method - the method of the request
      Returns:
      the appropriate request implementation