Package org.gcube.common.gxhttp.request
Class GXRequest
java.lang.Object
org.gcube.common.gxhttp.request.GXRequest
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe application/json;charset=UTF-8 content type.static final StringSystem property holding the default connect timeout, in milliseconds (0means infinite).static final intConnect timeout used when "gxhttp.connectTimeout" is not set, in milliseconds.static final intRead timeout used when "gxhttp.readTimeout" is not set, in milliseconds.static final StringThe parameter equals.static final StringThe parameter separator.static final StringThe parameter starter.static final StringThe path separator.static final StringSystem property holding the default read timeout, in milliseconds (0means infinite).static final StringThe UTF-8 charset. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears path, path parameters, query parameters and body, so that the request can be reused with the same address, headers and timeouts.connectTimeout(int connectTimeout) Sets the connect timeout of this request.delete()Sends a DELETE request.Sets the agent sent asUser-Agent.get()Sends a GET request.static intGets the default connect timeout: "gxhttp.connectTimeout" when set, otherwise 30000.static intGets the default read timeout: "gxhttp.readTimeout" when set, otherwise 300000.getURI()head()Sends a HEAD request.Sets a header.static GXRequestnewRequest(String address) Creates a request.options()Sends an OPTIONS request.patch()Sends a PATCH request, with the body if set.Appends a part to the path; each segment is encoded.pathParams(String pathParameters) Sets path parameters, appended as they are after the path (e.g.post()Sends a POST request, with the body if set.purge()Sends a PURGE request.put()Sends a PUT request, with the body if set.queryParams(Map<String, String> parameters) Sets the query parameters; names and values are encoded.queryString(String queryParameters) Sets the query string as it is.readTimeout(int readTimeout) Sets the read timeout of this request: the time allowed, once connected, to receive the response status and headers.Sends the request with the given method.trace()Sends a TRACE request.withBody(InputStream body) Sets the body as a stream, read while the request is sent.Sets the body as text, sent in UTF-8.
-
Field Details
-
APPLICATION_JSON_CHARSET_UTF_8
The application/json;charset=UTF-8 content type.- See Also:
-
PATH_SEPARATOR
The path separator.- See Also:
-
PARAM_STARTER
The parameter starter.- See Also:
-
PARAM_EQUALS
The parameter equals.- See Also:
-
PARAM_SEPARATOR
The parameter separator.- See Also:
-
UTF8
The UTF-8 charset.- See Also:
-
CONNECT_TIMEOUT_PROPERTY
System property holding the default connect timeout, in milliseconds (0means infinite).- See Also:
-
READ_TIMEOUT_PROPERTY
System property holding the default read timeout, in milliseconds (0means infinite).- See Also:
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUTConnect timeout used when "gxhttp.connectTimeout" is not set, in milliseconds.- See Also:
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUTRead timeout used when "gxhttp.readTimeout" is not set, in milliseconds.- See Also:
-
-
Method Details
-
newRequest
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;
0means infinite
-
getDefaultReadTimeout
public static int getDefaultReadTimeout()Gets the default read timeout: "gxhttp.readTimeout" when set, otherwise 300000.- Returns:
- the timeout in milliseconds;
0means infinite
-
from
Sets the agent sent asUser-Agent.- Parameters:
agent- the agent- Returns:
- this request
-
path
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
Sets the query parameters; names and values are encoded.- Parameters:
parameters- the parameters- Returns:
- this request
-
pathParams
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
Sets the query string as it is.- Parameters:
queryParameters- the already encoded query string, without "?"- Returns:
- this request
-
header
Sets a header.Connection,Content-Length,Expect,HostandUpgradeare set by the client itself: they are ignored with a warning.- Parameters:
name- the header namevalue- the header value- Returns:
- this request
-
withBody
Sets the body as text, sent in UTF-8.- Parameters:
body- the body- Returns:
- this request
-
withBody
Sets the body as a stream, read while the request is sent.- Parameters:
body- the body- Returns:
- this request
-
connectTimeout
Sets the connect timeout of this request.- Parameters:
connectTimeout- the timeout in milliseconds;0means infinite- Returns:
- this request
-
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;0means 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
- Returns:
- the URI the request is sent to
-
get
Sends a GET request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
head
Sends a HEAD request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- 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, seeGXRequestInterruptedException- 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, seeGXRequestInterruptedException- 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, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
delete
Sends a DELETE request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
purge
Sends a PURGE request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
options
Sends an OPTIONS request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
trace
Sends a TRACE request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-
send
Sends the request with the given method. A301,302or303response is followed with the same method.- Parameters:
method- the HTTP method- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestInterruptedException- if the thread is interrupted while waiting for the response
-