Class GXRequest
- java.lang.Object
-
- org.gcube.common.gxhttp.request.GXRequest
-
public class GXRequest extends Object
An HTTP request to a remote service, sent withHttpURLConnection. Every method 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).put();The request carries the credentials of the current gCube session, the UMA token held by
AccessTokenProviderasAuthorization: Bearerand the legacy gCube token held bySecurityTokenProviderin its own header, unless the caller sets one of those headers itself or declares the callexternal. A gCube request without any credential fails withIllegalStateException.PATCHandPURGEare not available:HttpURLConnectionrefuses them, sopatch()andpurge()throwUnsupportedOperationException.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
Fields Modifier and Type Field Description static StringAPPLICATION_JSON_CHARSET_UTF_8The application/json;charset=UTF-8 content type.static StringCONNECT_TIMEOUT_PROPERTYSystem property holding the default connect timeout, in milliseconds (0means infinite).static intDEFAULT_CONNECT_TIMEOUTConnect timeout used when "gxhttp.connectTimeout" is not set, in milliseconds.static intDEFAULT_READ_TIMEOUTRead timeout used when "gxhttp.readTimeout" is not set, in milliseconds.static StringPARAM_EQUALSThe parameter equals.static StringPARAM_SEPARATORThe parameter separator.static StringPARAM_STARTERThe parameter starter.static StringPATH_SEPARATORThe path separator.static StringREAD_TIMEOUT_PROPERTYSystem property holding the default read timeout, in milliseconds (0means infinite).static StringUTF8The UTF-8 charset.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears path, path parameters, query parameters and body, so that the request can be reused with the same address, headers and timeouts.GXRequestconnectTimeout(int connectTimeout)Sets the connect timeout of this request.GXResponsedelete()Sends a DELETE request.GXRequestexternal(boolean external)Declares whether the call goes to a service outside gCube: an external call carries no credential of the current session.GXRequestfrom(String agent)Sets the agent sent asUser-Agent.GXResponseget()Sends a GET request.static intgetDefaultConnectTimeout()Gets the default connect timeout: "gxhttp.connectTimeout" when set, otherwise 30000.static intgetDefaultReadTimeout()Gets the default read timeout: "gxhttp.readTimeout" when set, otherwise 300000.URIgetURI()GXResponsehead()Sends a HEAD request.GXRequestheader(String name, String value)Sets a header.static GXRequestnewRequest(String address)Creates a request.GXResponseoptions()Sends an OPTIONS request.GXResponsepatch()Not available:HttpURLConnectionrefuses the PATCH method.GXRequestpath(String pathPart)Appends a part to the path; each segment is encoded.GXRequestpathParams(String pathParameters)Sets path parameters, appended as they are after the path (e.g.GXResponsepost()Sends a POST request, with the body if set.GXResponsepurge()Not available:HttpURLConnectionrefuses the PURGE method.GXResponseput()Sends a PUT request, with the body if set.GXRequestqueryParams(Map<String,String> parameters)Sets the query parameters; names and values are encoded.GXRequestqueryString(String queryParameters)Sets the query string as it is.GXRequestreadTimeout(int readTimeout)Sets the read timeout of this request: the time allowed, once connected, between two reads from the server, the wait for the response status and headers included.GXResponsesend(String method)Sends the request with the given method.GXResponsetrace()Sends a TRACE request.GXRequestwithBody(InputStream body)Sets the body as a stream, read while the request is sent.GXRequestwithBody(String body)Sets the body as text, sent in UTF-8.
-
-
-
Field Detail
-
APPLICATION_JSON_CHARSET_UTF_8
public static final String APPLICATION_JSON_CHARSET_UTF_8
The application/json;charset=UTF-8 content type.- See Also:
- Constant Field Values
-
PATH_SEPARATOR
public static final String PATH_SEPARATOR
The path separator.- See Also:
- Constant Field Values
-
PARAM_STARTER
public static final String PARAM_STARTER
The parameter starter.- See Also:
- Constant Field Values
-
PARAM_EQUALS
public static final String PARAM_EQUALS
The parameter equals.- See Also:
- Constant Field Values
-
PARAM_SEPARATOR
public static final String PARAM_SEPARATOR
The parameter separator.- See Also:
- Constant Field Values
-
UTF8
public static final String UTF8
The UTF-8 charset.- See Also:
- Constant Field Values
-
CONNECT_TIMEOUT_PROPERTY
public static final String CONNECT_TIMEOUT_PROPERTY
System property holding the default connect timeout, in milliseconds (0means infinite).- See Also:
- Constant Field Values
-
READ_TIMEOUT_PROPERTY
public static final String READ_TIMEOUT_PROPERTY
System property holding the default read timeout, in milliseconds (0means infinite).- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT
Connect timeout used when "gxhttp.connectTimeout" is not set, in milliseconds.- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT
Read timeout used when "gxhttp.readTimeout" is not set, in milliseconds.- See Also:
- Constant Field Values
-
-
Method Detail
-
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;
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
public GXRequest from(String agent)
Sets the agent sent asUser-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. AnAuthorizationor gCube token header set here replaces the one the request would take from the current session.- Parameters:
name- the header namevalue- the header value- Returns:
- this request
-
external
public GXRequest external(boolean external)
Declares whether the call goes to a service outside gCube: an external call carries no credential of the current session.- Parameters:
external-truefor a service outside gCube- 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;0means infinite- Returns:
- this request
-
readTimeout
public GXRequest readTimeout(int readTimeout)
Sets the read timeout of this request: the time allowed, once connected, between two reads from the server, the wait for the response status and headers included.- 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
public URI getURI()
- Returns:
- the URI the request is sent to
-
get
public GXResponse get() throws IOException
Sends a GET request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
head
public GXResponse head() throws IOException
Sends a HEAD request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
post
public GXResponse post() throws IOException
Sends a POST request, with the body if set.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
put
public GXResponse put() throws IOException
Sends a PUT request, with the body if set.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
patch
public GXResponse patch() throws IOException
Not available:HttpURLConnectionrefuses the PATCH method.- Returns:
- never returns
- Throws:
IOException- never thrownUnsupportedOperationException- always
-
delete
public GXResponse delete() throws IOException
Sends a DELETE request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
purge
public GXResponse purge() throws IOException
Not available:HttpURLConnectionrefuses the PURGE method.- Returns:
- never returns
- Throws:
IOException- never thrownUnsupportedOperationException- always
-
options
public GXResponse options() throws IOException
Sends an OPTIONS request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
trace
public GXResponse trace() throws IOException
Sends a TRACE request.- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequest
-
send
public GXResponse send(String method) throws IOException
Sends the request with the given method. A301,302or303response is followed with the same method.- Parameters:
method- the HTTP method, one of thoseHttpURLConnectionsupports- Returns:
- the response
- Throws:
IOException- if the request fails, seeGXRequestUnsupportedOperationException- forPATCHandPURGE
-
-