Package org.silverpeas.core.chat.servers
Class HttpRequester
- java.lang.Object
-
- org.silverpeas.core.chat.servers.HttpRequester
-
- All Implemented Interfaces:
AutoCloseable
public class HttpRequester extends Object implements AutoCloseable
An HTTP requester. It wraps the HTTP mechanism to communicate with a REST resource.HttpRequester
instances are heavy-weight objects. Initialization as well as disposal may be a rather expensive operation. It is therefore advised to construct only a small number ofHttpRequester
instances in the application. Thoses instances must beproperly closed
before being disposed to avoid leaking resources.- Author:
- mmoquillon
-
-
Field Summary
Fields Modifier and Type Field Description static int
STATUS_CONFLICT
The status code for a request resulting to a conflict in the server.static int
STATUS_CREATED
The status code for a request resulting to the successful creation of a resource in the server.static int
STATUS_FORBIDDEN
The status code for a forbidden request.static int
STATUS_OK
The status code for a successful request.static int
STATUS_UNAUTHORIZED
The status code for an unauthorized request.
-
Constructor Summary
Constructors Constructor Description HttpRequester(String authenticationToken)
Constructs a newHttpRequester
with the specified authentication token that will used to identify and to authorize the requester to perform some tasks in the remote resource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpRequester
at(String url, String... path)
Sets the specified URL of the resource to request.void
close()
Closes thisHttpRequester
.javax.ws.rs.core.Response
delete()
Deletes the resource referred by the set URL.javax.ws.rs.core.Response
head()
Sends a HEAD HTTP request to the set URL in order to obtain some information about the remote Web resource.HttpRequester
header(String name, String value)
Sets an HTTP header.javax.ws.rs.core.Response
post(UnaryOperator<JSONCodec.JSONObject> entityBuilder)
Posts to resource referred by the set URL the data that are generated by the specified builder.
-
-
-
Field Detail
-
STATUS_OK
public static final int STATUS_OK
The status code for a successful request.- See Also:
- Constant Field Values
-
STATUS_FORBIDDEN
public static final int STATUS_FORBIDDEN
The status code for a forbidden request.- See Also:
- Constant Field Values
-
STATUS_UNAUTHORIZED
public static final int STATUS_UNAUTHORIZED
The status code for an unauthorized request.- See Also:
- Constant Field Values
-
STATUS_CONFLICT
public static final int STATUS_CONFLICT
The status code for a request resulting to a conflict in the server. For example, creating a resource that already exists.- See Also:
- Constant Field Values
-
STATUS_CREATED
public static final int STATUS_CREATED
The status code for a request resulting to the successful creation of a resource in the server.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HttpRequester
public HttpRequester(String authenticationToken)
Constructs a newHttpRequester
with the specified authentication token that will used to identify and to authorize the requester to perform some tasks in the remote resource.- Parameters:
authenticationToken
- an authentication token that was provided by the REST service to access its API.
-
-
Method Detail
-
at
public HttpRequester at(String url, String... path)
Sets the specified URL of the resource to request. This method must be invoked before any call to other HTTP methods.- Parameters:
url
- the URL of a REST API.path
- optionally the path from the previous URL of the part of the API to request.- Returns:
- itself.
-
header
public HttpRequester header(String name, String value)
Sets an HTTP header.- Parameters:
name
- the HTTP header name.value
- the value of the HTTP header.- Returns:
- itself.
-
close
public void close() throws Exception
Closes thisHttpRequester
. It frees all the resources used to perform HTTP requests. So, once this method is called, this instance cannot be used again to send HTTP requests.- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
- if theHttpRequester
cannot be closed.
-
post
public javax.ws.rs.core.Response post(UnaryOperator<JSONCodec.JSONObject> entityBuilder)
Posts to resource referred by the set URL the data that are generated by the specified builder.- Parameters:
entityBuilder
- a builder of data encoded in JSON.- Returns:
- the response to the POST HTTP request.
-
delete
public javax.ws.rs.core.Response delete()
Deletes the resource referred by the set URL.- Returns:
- the response to the DELETE HTTP request.
-
head
public javax.ws.rs.core.Response head()
Sends a HEAD HTTP request to the set URL in order to obtain some information about the remote Web resource.- Returns:
- the response to the HEAD HTTP request.
-
-