Package com.groiss.http
Class Client
java.lang.Object
com.groiss.http.Client
- All Implemented Interfaces:
SilentCloseable
,AutoCloseable
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()); } }
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
the HTTP methods supported by this client -
Field Summary
Modifier and TypeFieldDescriptionprotected org.apache.http.impl.client.HttpClientBuilder
protected ClientConfiguration
the configuration object for this clientprotected static final String
the default characters set to be used if not other is specifiedprotected org.apache.http.impl.client.CloseableHttpClient
protected static org.slf4j.Logger
protected String
-
Constructor Summary
ConstructorDescriptionClient()
creates a new clientClient
(ClientConfiguration config) creates a new client based on the passed configuration -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the client and releases all resources.Executes a DELETE request on the passed url using the passed parametersExecutes a DELETE request on the passed url using the passed payload and parametersprotected 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:executeRequest
(Client.Method method, String url, Map<String, String> headers, Object payload, Parameter... params) Executes a request based on the specified parameters:Executes a GET request on the passed url using the passed parametersprotected org.apache.http.client.methods.HttpRequestBase
getClientForMethod
(Client.Method method) Returns the request implementation for the passed methodprotected void
init()
this method initializes the client based on the passed configuration objectExecutes a POST request on the passed url using the passed parametersExecutes a POST request on the passed url using the passed payload and parametersExecutes a PUT request on the passed url using the passed parametersExecutes a PUT request on the passed url using the passed payload and parameterstoString()
Returns a string representation of the request that should be executed by this client.
-
Field Details
-
logger
protected static org.slf4j.Logger logger -
DEFAULT_CHARSET
the default characters set to be used if not other is specified -
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
-
-
Constructor Details
-
Client
public Client()creates a new client -
Client
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 interfaceAutoCloseable
- Specified by:
close
in interfaceSilentCloseable
-
toString
Returns a string representation of the request that should be executed by this client. Will be null if request could not be created. -
delete
Executes a DELETE request on the passed url using the passed parameters- Parameters:
url
- the url of the requestparams
- the parameters for the request- Returns:
- the response of the request
-
delete
Executes a DELETE request on the passed url using the passed payload and parameters- Parameters:
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details seeexecuteRequest(Method, String, Map, Object, Parameter...)
)params
- the parameters for the request- Returns:
- the response of the request
-
get
Executes a GET request on the passed url using the passed parameters- Parameters:
url
- the url of the requestparams
- the parameters for the request- Returns:
- the response of the request
-
post
Executes a POST request on the passed url using the passed parameters- Parameters:
url
- the url of the requestparams
- the parameters for the request (sent as form parameters)- Returns:
- the response of the request
-
post
Executes a POST request on the passed url using the passed payload and parameters- Parameters:
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details seeexecuteRequest(Method, String, Map, Object, Parameter...)
)params
- the parameters for the request- Returns:
- the response of the request
-
put
Executes a PUT request on the passed url using the passed parameters- Parameters:
url
- the url of the requestparams
- the parameters for the request- Returns:
- the response of the request
-
put
Executes a PUT request on the passed url using the passed payload and parameters- Parameters:
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details seeexecuteRequest(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 requestheaders
- headers for the request. This is only needed if the default settings are not appropriate or do not contain the needed header propertypayload
- 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 requestheaders
- headers for the request. This is only needed if the default settings are not appropriate or do not contain the needed header propertypayload
- 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
Returns the request implementation for the passed method- Parameters:
method
- the method of the request- Returns:
- the appropriate request implementation
-