- java.lang.Object
-
- com.gluonhq.connect.source.RestDataSource
-
- All Implemented Interfaces:
InputDataSource
,IODataSource
,OutputDataSource
public class RestDataSource extends Object implements IODataSource
An implementation ofIODataSource
that can read from and write to an HTTP URL resource.Attention: it is advised not to use this class directly, but rather construct it by creating a
RestClient
and build the RestDataSource with theRestClient.createRestDataSource()
method.
-
-
Constructor Summary
Constructors Constructor Description RestDataSource()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFormParam(String key, String value)
Add a single form parameter to the request.void
addHeader(String field, String value)
Adds a single HTTP header to the request.void
addQueryParam(String key, String value)
Add a single query parameter to the request.int
getConnectTimeout()
Gets the connect timeout for the HTTP connection, in milliseconds.String
getConsumerKey()
Gets the consumer key to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.String
getConsumerSecret()
Gets the consumer secret to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.String
getContentType()
Gets the Content-Type request header that will be set on the HTTP connection.String
getDataString()
Gets the entity to use for the HTTP connection.MultiValuedMap<String,String>
getFormParams()
Returns a list of form parameters that will be sent along with the request.MultiValuedMap<String,String>
getHeaders()
Returns a list of HTTP headers which will be sent along with the request.String
getHost()
Returns the complete host address of the URL to use for the HTTP connection.InputStream
getInputStream()
Returns an InputStream that is able to read data from an HTTP URL that will be constructed with the settings defined on this data source.String
getMethod()
Returns the request method to use for the HTTP connection.MultiValuedMap<String,byte[]>
getMultipartByteFields()
Returns a list of multipart binary fields that will be sent along with the request as part of the multipart form data.MultiValuedMap<String,String>
getMultipartStringFields()
Returns a list of multipart form fields that will be sent along with the request as part of the multipart form data.OutputStream
getOutputStream()
Returns an OutputStream that is able to write data to an HTTP URL that will be constructed with the settings defined on this data source.String
getPath()
Returns the entire path of the URL to use for the HTTP connection.MultiValuedMap<String,String>
getQueryParams()
Returns a list of query parameters that will be sent along with the request.int
getReadTimeout()
Gets the read timeout for the HTTP connection, in milliseconds.int
getResponseCode()
Gets the status code from an HTTP response message.Map<String,List<String>>
getResponseHeaders()
Gets the response header fields from an HTTP response message.String
getResponseMessage()
Gets the HTTP response message, if any, returned along with the response code from a server.void
setConnectTimeout(int connectTimeout)
Sets the connect timeout for the HTTP connection, in milliseconds.void
setConsumerKey(String consumerKey)
Sets the consumer key to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.void
setConsumerSecret(String consumerSecret)
Sets the consumer secret to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.void
setContentType(String contentType)
Sets the Content-Type request header for the HTTP connection.void
setDataString(String dataString)
Sets the entity to use for the HTTP connection.void
setFormParams(MultiValuedMap<String,String> formParams)
Sets the list of form parameters to be sent along with the request.void
setHeaders(MultiValuedMap<String,String> headers)
Sets the list of HTTP headers to be sent along with the request.void
setHost(String host)
Sets the complete host address of the URL to use for the HTTP connection.void
setMethod(String method)
Sets the request method to use for the HTTP connection.void
setMultipartByteFields(MultiValuedMap<String,byte[]> multipartByteFields)
Sets the list of multipart binary fields that will be sent along with the request as part of the multipart form data.void
setMultipartStringFields(MultiValuedMap<String,String> multipartStringFields)
Sets the list of multipart form fields that will be sent along with the request as part of the multipart form data.void
setPath(String path)
Sets the entire path of the URL to use for the HTTP connection.void
setQueryParams(MultiValuedMap<String,String> queryParams)
Sets the list of query parameters to be sent along with the request.void
setReadTimeout(int readTimeout)
Sets the read timeout for the HTTP connection, in milliseconds.
-
-
-
Method Detail
-
getInputStream
public InputStream getInputStream() throws IOException
Returns an InputStream that is able to read data from an HTTP URL that will be constructed with the settings defined on this data source.- Specified by:
getInputStream
in interfaceInputDataSource
- Returns:
- an InputStream that is able to read from an HTTP URL
- Throws:
IOException
- when the HTTP connection could not be established or the InputStream could not be created
-
getOutputStream
public OutputStream getOutputStream() throws IOException
Returns an OutputStream that is able to write data to an HTTP URL that will be constructed with the settings defined on this data source.- Specified by:
getOutputStream
in interfaceOutputDataSource
- Returns:
- an OutputStream that is able to write data to an HTTP URL
- Throws:
IOException
- when the HTTP connection could not be established or the OutputStream could not be created
-
getHost
public String getHost()
Returns the complete host address of the URL to use for the HTTP connection. The host consists of the scheme, the remote host name and the port. If no port is specified, the default scheme port will be used.- Returns:
- the complete host address of the URL
-
setHost
public void setHost(String host)
Sets the complete host address of the URL to use for the HTTP connection. The host consists of the scheme, the remote host name and the port. If no port is specified, the default scheme port will be used.- Parameters:
host
- the complete host address of the URL
-
getPath
public String getPath()
Returns the entire path of the URL to use for the HTTP connection.- Returns:
- the entire path of the URL
-
setPath
public void setPath(String path)
Sets the entire path of the URL to use for the HTTP connection. A forward slash will automatically be added in front of the providedpath
if it is missing.- Parameters:
path
- the entire path of the URL
-
getMethod
public String getMethod()
Returns the request method to use for the HTTP connection.- Returns:
- the request method for the HTTP connection
- See Also:
HttpURLConnection.getRequestMethod()
-
setMethod
public void setMethod(String method)
Sets the request method to use for the HTTP connection. When the request method is not specified, POST will be used if any form parameters or a data string is set. Otherwise, the GET method will be used.- Parameters:
method
- the request method for the HTTP connection- See Also:
HttpURLConnection.setRequestMethod(String)
-
getReadTimeout
public int getReadTimeout()
Gets the read timeout for the HTTP connection, in milliseconds. A timeout of zero is interpreted as an infinite timeout.- Returns:
- an int that indicates the read timeout value in milliseconds
- See Also:
URLConnection.getReadTimeout()
-
setReadTimeout
public void setReadTimeout(int readTimeout)
Sets the read timeout for the HTTP connection, in milliseconds. A timeout of zero is interpreted as an infinite timeout.- Parameters:
readTimeout
- an int that specifies the timeout value to be used in milliseconds- See Also:
URLConnection.setReadTimeout(int)
-
getConnectTimeout
public int getConnectTimeout()
Gets the connect timeout for the HTTP connection, in milliseconds. A timeout of zero is interpreted as an infinite timeout.- Returns:
- an int that indicates the connect timeout value in milliseconds
- See Also:
URLConnection.getConnectTimeout()
-
setConnectTimeout
public void setConnectTimeout(int connectTimeout)
Sets the connect timeout for the HTTP connection, in milliseconds. A timeout of zero is interpreted as an infinite timeout.- Parameters:
connectTimeout
- an int that specifies the timeout value to be used in milliseconds- See Also:
URLConnection.setConnectTimeout(int)
-
getDataString
public String getDataString()
Gets the entity to use for the HTTP connection.- Returns:
- the entity of the HTTP connection
-
setDataString
public void setDataString(String dataString)
Sets the entity to use for the HTTP connection. ThedataString
will be written to the OutputStream of the HTTP connection. Please note, when specifying both a data string and form parameters, the data string will be appended with an ampersand, followed by the encoded list of form parameters.- Parameters:
dataString
- the entity for the HTTP connection
-
getConsumerKey
public String getConsumerKey()
Gets the consumer key to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.- Returns:
- the consumer key used for calculating the OAuth 1.0 signature
-
setConsumerKey
public void setConsumerKey(String consumerKey)
Sets the consumer key to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header. Setting theconsumer secret
is mandatory when setting the consumer key.- Parameters:
consumerKey
- the consumer key used for calculating the OAuth 1.0 signature
-
getConsumerSecret
public String getConsumerSecret()
Gets the consumer secret to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header.- Returns:
- the consumer secret used for calculating the OAuth 1.0 signature
-
setConsumerSecret
public void setConsumerSecret(String consumerSecret)
Sets the consumer secret to use for creating the OAuth 1.0 signature that is sent along with the request, by setting theAuthorization
request header. Setting the consumer secret has no effect when theconsumer key
is not set.- Parameters:
consumerSecret
- the consumer secret used for calculating the OAuth 1.0 signature
-
addQueryParam
public void addQueryParam(String key, String value)
Add a single query parameter to the request.- Parameters:
key
- the key of the query parametervalue
- the value of the query parameter
-
getQueryParams
public MultiValuedMap<String,String> getQueryParams()
Returns a list of query parameters that will be sent along with the request.- Returns:
- the list of query parameters for the request
-
setQueryParams
public void setQueryParams(MultiValuedMap<String,String> queryParams)
Sets the list of query parameters to be sent along with the request. The list is a multi valued map, hence there can be more than one value assigned to the same query parameter key.- Parameters:
queryParams
- the list of query parameters to be sent with the request
-
addFormParam
public void addFormParam(String key, String value)
Add a single form parameter to the request.- Parameters:
key
- the key of the form parametervalue
- the value of the form parameter
-
getFormParams
public MultiValuedMap<String,String> getFormParams()
Returns a list of form parameters that will be sent along with the request.- Returns:
- the list of form parameters for the request
-
setFormParams
public void setFormParams(MultiValuedMap<String,String> formParams)
Sets the list of form parameters to be sent along with the request. The list is a multi valued map, hence there can be more than one value assigned to the same form parameter key.- Parameters:
formParams
- the list of form parameters to be sent with the request
-
getContentType
public String getContentType()
Gets the Content-Type request header that will be set on the HTTP connection.- Returns:
- the Content-Type request header for the HTTP connection
-
setContentType
public void setContentType(String contentType)
Sets the Content-Type request header for the HTTP connection. The request header will only be set when either adata string
orform parameters
were set. In case the content type header was not set, it will by default be set toapplication/x-www-form-urlencoded
.- Parameters:
contentType
- the Content-Type request header for the HTTP connection
-
addHeader
public void addHeader(String field, String value)
Adds a single HTTP header to the request. NOTE: Headers set using this method will be included in addition to those set via other methods (e.g. bysetContentType(String)
), so to avoid complications you should only set headers here if you haven't set them via any other methods- Parameters:
field
- The name of the HTTP header field (e.g. "Accept")value
- The header value to send with the request
-
getHeaders
public MultiValuedMap<String,String> getHeaders()
Returns a list of HTTP headers which will be sent along with the request. Note: This only returns headers defined by eitheraddHeader(String, String)
orsetHeaders(MultiValuedMap)
, not those defined in any other way (e.g. bysetContentType(String)
)- Returns:
- the list of HTTP headers to be sent
-
setHeaders
public void setHeaders(MultiValuedMap<String,String> headers)
Sets the list of HTTP headers to be sent along with the request. The list is a multi valued map, hence there can be more than one header with the same field sent.- Parameters:
headers
- the list of headers to be sent with the request
-
getMultipartStringFields
public MultiValuedMap<String,String> getMultipartStringFields()
Returns a list of multipart form fields that will be sent along with the request as part of the multipart form data.- Returns:
- the list of multipart form fields to be sent
-
setMultipartStringFields
public void setMultipartStringFields(MultiValuedMap<String,String> multipartStringFields)
Sets the list of multipart form fields that will be sent along with the request as part of the multipart form data.- Parameters:
multipartStringFields
- the list of multipart form fields to be sent with the request
-
getMultipartByteFields
public MultiValuedMap<String,byte[]> getMultipartByteFields()
Returns a list of multipart binary fields that will be sent along with the request as part of the multipart form data.- Returns:
- the list of multipart binary fields to be sent
-
setMultipartByteFields
public void setMultipartByteFields(MultiValuedMap<String,byte[]> multipartByteFields)
Sets the list of multipart binary fields that will be sent along with the request as part of the multipart form data.- Parameters:
multipartByteFields
- the list of multipart binary fields to be sent with the request
-
getResponseHeaders
public Map<String,List<String>> getResponseHeaders()
Gets the response header fields from an HTTP response message.- Returns:
- a Map of header fields.
- See Also:
URLConnection.getHeaderFields()
-
getResponseCode
public int getResponseCode()
Gets the status code from an HTTP response message.- Returns:
- the HTTP Status-Code, or -1
- See Also:
HttpURLConnection.getResponseCode()
-
getResponseMessage
public String getResponseMessage()
Gets the HTTP response message, if any, returned along with the response code from a server.- Returns:
- the HTTP response message, or
null
- See Also:
HttpURLConnection.getResponseMessage()
-
-