Class 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 of HttpRequester instances in the application. Thoses instances must be properly closed before being disposed to avoid leaking resources.
    Author:
    mmoquillon
    • 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 new HttpRequester 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 this HttpRequester. 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 interface AutoCloseable
        Throws:
        Exception - if the HttpRequester 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.