Class GXRequest

java.lang.Object
org.gcube.common.gxhttp.request.GXRequest

public class GXRequest extends Object
An HTTP request to a remote service, sent with the JDK HttpClient. Every method, PATCH included, takes the same path, and the body, when set, is sent with any of them.
 GXResponse response = GXRequest.newRequest(address).path("instances").path(uuid)
                .header("Content-Type", GXRequest.APPLICATION_JSON_CHARSET_UTF_8).withBody(json).patch();
 

A request that never reached the server (refused connection, unknown host, connect timeout) fails with the original exception. A request sent but left without a response (read timeout, connection closed while waiting) fails with GXResponseException: the server may have processed it.

Author:
Luca Frosini (ISTI-CNR)
  • Field Details

  • Method Details

    • newRequest

      public static GXRequest newRequest(String address)
      Creates a request.
      Parameters:
      address - the address of the remote service
      Returns:
      the request
    • getDefaultConnectTimeout

      public static int getDefaultConnectTimeout()
      Gets the default connect timeout: "gxhttp.connectTimeout" when set, otherwise 30000.
      Returns:
      the timeout in milliseconds; 0 means infinite
    • getDefaultReadTimeout

      public static int getDefaultReadTimeout()
      Gets the default read timeout: "gxhttp.readTimeout" when set, otherwise 300000.
      Returns:
      the timeout in milliseconds; 0 means infinite
    • from

      public GXRequest from(String agent)
      Sets the agent sent as User-Agent.
      Parameters:
      agent - the agent
      Returns:
      this request
    • path

      public GXRequest path(String pathPart)
      Appends a part to the path; each segment is encoded.
      Parameters:
      pathPart - the path part, possibly with several segments separated by "/"
      Returns:
      this request
    • queryParams

      public GXRequest queryParams(Map<String,String> parameters)
      Sets the query parameters; names and values are encoded.
      Parameters:
      parameters - the parameters
      Returns:
      this request
    • pathParams

      public GXRequest pathParams(String pathParameters)
      Sets path parameters, appended as they are after the path (e.g. ;name=value).
      Parameters:
      pathParameters - the already encoded path parameters
      Returns:
      this request
    • queryString

      public GXRequest queryString(String queryParameters)
      Sets the query string as it is.
      Parameters:
      queryParameters - the already encoded query string, without "?"
      Returns:
      this request
    • header

      public GXRequest header(String name, String value)
      Sets a header. Connection, Content-Length, Expect, Host and Upgrade are set by the client itself: they are ignored with a warning.
      Parameters:
      name - the header name
      value - the header value
      Returns:
      this request
    • withBody

      public GXRequest withBody(String body)
      Sets the body as text, sent in UTF-8.
      Parameters:
      body - the body
      Returns:
      this request
    • withBody

      public GXRequest withBody(InputStream body)
      Sets the body as a stream, read while the request is sent.
      Parameters:
      body - the body
      Returns:
      this request
    • connectTimeout

      public GXRequest connectTimeout(int connectTimeout)
      Sets the connect timeout of this request.
      Parameters:
      connectTimeout - the timeout in milliseconds; 0 means infinite
      Returns:
      this request
    • readTimeout

      public GXRequest readTimeout(int readTimeout)
      Sets the read timeout of this request: the time allowed, once connected, to receive the response status and headers. The body is then read without a time limit.
      Parameters:
      readTimeout - the timeout in milliseconds; 0 means infinite
      Returns:
      this request
    • clear

      public void clear()
      Clears path, path parameters, query parameters and body, so that the request can be reused with the same address, headers and timeouts.
    • getURI

      public URI getURI()
      Returns:
      the URI the request is sent to
    • get

      Sends a GET request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • head

      Sends a HEAD request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • post

      Sends a POST request, with the body if set.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • put

      Sends a PUT request, with the body if set.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • patch

      Sends a PATCH request, with the body if set.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • delete

      public GXResponse delete() throws IOException, InterruptedException
      Sends a DELETE request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • purge

      Sends a PURGE request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • options

      public GXResponse options() throws IOException, InterruptedException
      Sends an OPTIONS request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • trace

      Sends a TRACE request.
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response
    • send

      public GXResponse send(String method) throws IOException, InterruptedException
      Sends the request with the given method. A 301, 302 or 303 response is followed with the same method.
      Parameters:
      method - the HTTP method
      Returns:
      the response
      Throws:
      IOException - if the request fails, see GXRequest
      InterruptedException - if the thread is interrupted while waiting for the response