public class Client extends Object implements AutoCloseable
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()); } }
Modifier and Type | Class and Description |
---|---|
static class |
Client.Method
the HTTP methods supported by this client
|
Modifier and Type | Field and Description |
---|---|
protected org.apache.http.impl.client.HttpClientBuilder |
clientBuilder |
protected ClientConfiguration |
config
the configuration object for this client
|
protected static String |
DEFAULT_CHARSET
the default characters set to be used if not other is specified
|
protected org.apache.http.impl.client.CloseableHttpClient |
httpClient |
protected static org.slf4j.Logger |
logger |
protected String |
requestAsString |
Constructor and Description |
---|
Client()
creates a new client
|
Client(ClientConfiguration config)
creates a new client based on the passed configuration
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the client and releases all resources.
|
Response |
delete(String url,
Object payload,
Parameter... params)
Executes a DELETE request on the passed url using the passed payload and parameters
|
Response |
delete(String url,
Parameter... params)
Executes a DELETE request on the passed url using the passed parameters
|
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:
|
Response |
executeRequest(Client.Method method,
String url,
Map<String,String> headers,
Object payload,
Parameter... params)
Executes a request based on the specified parameters:
|
Response |
get(String url,
Parameter... params)
Executes a GET request on the passed url using the passed parameters
|
protected org.apache.http.client.methods.HttpRequestBase |
getClientForMethod(Client.Method method)
Returns the request implementation for the passed method
|
protected void |
init()
this method initializes the client based on the passed configuration object
|
Response |
post(String url,
Object payload,
Parameter... params)
Executes a POST request on the passed url using the passed payload and parameters
|
Response |
post(String url,
Parameter... params)
Executes a POST request on the passed url using the passed parameters
|
Response |
put(String url,
Object payload,
Parameter... params)
Executes a PUT request on the passed url using the passed payload and parameters
|
Response |
put(String url,
Parameter... params)
Executes a PUT request on the passed url using the passed parameters
|
String |
toString()
Returns a string representation of the request that should be executed by this client.
|
protected static org.slf4j.Logger logger
protected static final String DEFAULT_CHARSET
protected ClientConfiguration config
protected org.apache.http.impl.client.CloseableHttpClient httpClient
protected org.apache.http.impl.client.HttpClientBuilder clientBuilder
protected String requestAsString
public Client()
public Client(ClientConfiguration config)
config
- the configuration for this clientprotected void init()
public void close()
close
in interface AutoCloseable
public String toString()
public Response delete(String url, Parameter... params)
url
- the url of the requestparams
- the parameters for the requestpublic Response delete(String url, Object payload, Parameter... params)
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...)
)params
- the parameters for the requestpublic Response get(String url, Parameter... params)
url
- the url of the requestparams
- the parameters for the requestpublic Response post(String url, Parameter... params)
url
- the url of the requestparams
- the parameters for the request (sent as form parameters)public Response post(String url, Object payload, Parameter... params)
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...)
)params
- the parameters for the requestpublic Response put(String url, Parameter... params)
url
- the url of the requestparams
- the parameters for the requestpublic Response put(String url, Object payload, Parameter... params)
url
- the url of the requestpayload
- the payload for the request (e.g. a file, for more details see executeRequest(Method, String, Map, Object, Parameter...)
)params
- the parameters for the requestpublic Response executeRequest(Client.Method method, String url, Map<String,String> headers, Object payload, Parameter... params)
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.protected org.apache.http.client.methods.CloseableHttpResponse execute(Client.Method method, String url, Map<String,String> headers, Object payload, Parameter... params)
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.protected org.apache.http.client.methods.HttpRequestBase getClientForMethod(Client.Method method)
method
- the method of the request@enterprise 10.0.39049 Copyright © 2024 FREQUENTIS AG. All Rights Reserved.