public class RestClient
extends java.lang.Object
The RestClient assists in using the DataProvider
with HTTP URLs as the data source. For instance, to read
an object from a sample URL that responds with json, you can use the following code:
RestClient restClient = RestClient.create() .host("http://myhost.com") .path("restservice/2/sample/19") .method("GET"); GluonObservableObject<Sample> sample = DataProvider.retrieveObject(restClient.createObjectDataReader(Sample.class));
Modifier and Type | Method and Description |
---|---|
RestClient |
connectTimeout(int connectTimeout)
Sets the connect timeout for the HTTP connection, in milliseconds.
|
RestClient |
consumerKey(java.lang.String consumerKey)
Sets the consumer key to use for creating the OAuth 1.0 signature that is sent along with the request, by setting
the
Authorization request header. |
RestClient |
consumerSecret(java.lang.String consumerSecret)
Sets the consumer secret to use for creating the OAuth 1.0 signature that is sent along with the request, by
setting the
Authorization request header. |
RestClient |
contentType(java.lang.String contentType)
Sets the Content-Type request header for the HTTP connection.
|
static RestClient |
create()
Create a RestClient builder for constructing a RestDataSource or one of the classes that are used in the methods
of the
DataProvider . |
<E> ListDataReader<E> |
createListDataReader(java.lang.Class<E> targetClass)
Creates an instance of
ListDataReader that can be passed directly in the
DataProvider.retrieveList(ListDataReader) method. |
<E> ListDataReader<E> |
createListDataReader(InputStreamIterableInputConverter<E> converter)
Creates an instance of
ListDataReader that can be passed directly in the
DataProvider.retrieveList(ListDataReader) method. |
<T> ObjectDataReader<T> |
createObjectDataReader(java.lang.Class<T> targetClass)
Creates an instance of
ObjectDataReader that can be passed directly in the
DataProvider.retrieveObject(ObjectDataReader) method. |
<T> ObjectDataReader<T> |
createObjectDataReader(InputStreamInputConverter<T> converter)
Creates an instance of
ObjectDataReader that can be passed directly in the
DataProvider.retrieveObject(ObjectDataReader) method. |
<T> ObjectDataRemover<T> |
createObjectDataRemover(java.lang.Class<T> targetClass)
Creates an instance of
ObjectDataRemover that can be passed directly in the
DataProvider.removeObject(GluonObservableObject, ObjectDataRemover) method. |
<T> ObjectDataRemover<T> |
createObjectDataRemover(OutputStreamOutputConverter<T> outputConverter,
InputStreamInputConverter<T> inputConverter)
Creates an instance of
ObjectDataRemover that can be passed directly in the
DataProvider.removeObject(GluonObservableObject, ObjectDataRemover) method. |
<T> ObjectDataWriter<T> |
createObjectDataWriter(java.lang.Class<T> targetClass)
Creates an instance of
ObjectDataWriter that can be passed directly in the
DataProvider.storeObject(Object, ObjectDataWriter) method. |
<T> ObjectDataWriter<T> |
createObjectDataWriter(OutputStreamOutputConverter<T> outputConverter,
InputStreamInputConverter<T> inputConverter)
Creates an instance of
ObjectDataWriter that can be passed directly in the
DataProvider.storeObject(Object, ObjectDataWriter) method. |
RestDataSource |
createRestDataSource()
Build a RestDataSource that can be used as an InputDataSource to read from or an OutputDataSource to write to the
HTTP connection that is created with the parameters specified by this rest client builder.
|
RestClient |
dataString(java.lang.String dataString)
Sets the entity to use for the HTTP connection.
|
RestClient |
formParam(java.lang.String key,
java.lang.String value)
Add a single form parameter to the request.
|
RestClient |
header(java.lang.String field,
java.lang.String value)
Adds a single HTTP header to the request.
|
RestClient |
host(java.lang.String host)
Sets the complete host address of the URL for the HTTP connection.
|
RestClient |
method(java.lang.String method)
Sets the request method to use for the HTTP connection.
|
RestClient |
multipartField(java.lang.String field,
byte[] value)
Adds a binary field to an HTTP Multipart Form-Data request.
|
RestClient |
multipartField(java.lang.String field,
java.lang.String value)
Adds a form field to an HTTP Multipart Form-Data request.
|
RestClient |
path(java.lang.String path)
Sets the entire path of the URL for the HTTP connection.
|
RestClient |
queryParam(java.lang.String key,
java.lang.String value)
Add a single query parameter to the request.
|
RestClient |
readTimeout(int readTimeout)
Sets the read timeout for the HTTP connection, in milliseconds.
|
public static RestClient create()
DataProvider
.DataProvider
public RestClient host(java.lang.String host)
host
- the complete host address of the URLRestDataSource.setHost(String)
public RestClient path(java.lang.String path)
path
if it is missing.path
- the entire path of the URLRestDataSource.setPath(String)
public RestClient method(java.lang.String method)
method
- the request method for the HTTP connectionRestDataSource.setMethod(String)
public RestClient readTimeout(int readTimeout)
readTimeout
- an int that specifies the timeout value to be used in millisecondsRestDataSource.setReadTimeout(int)
public RestClient connectTimeout(int connectTimeout)
connectTimeout
- an int that specifies the timeout value to be used in millisecondsRestDataSource.setConnectTimeout(int)
public RestClient dataString(java.lang.String dataString)
dataString
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.dataString
- the entity for the HTTP connectionRestDataSource.setDataString(String)
public RestClient consumerKey(java.lang.String consumerKey)
Authorization
request header. Setting the consumer secret
is
mandatory when setting the consumer key.consumerKey
- the consumer key used for calculating the OAuth 1.0 signatureRestDataSource.setConsumerKey(String)
public RestClient consumerSecret(java.lang.String consumerSecret)
Authorization
request header. Setting the consumer secret has no effect when the
consumer key
is not set.consumerSecret
- the consumer secret used for calculating the OAuth 1.0 signatureRestDataSource.setConsumerSecret(String)
public RestClient queryParam(java.lang.String key, java.lang.String value)
key
- the key of the query parametervalue
- the value of the query parameterRestDataSource.addQueryParam(String, String)
public RestClient formParam(java.lang.String key, java.lang.String value)
key
- the key of the form parametervalue
- the value of the form parameterRestDataSource.addFormParam(String, String)
public RestClient contentType(java.lang.String contentType)
data string
or form parameters
were set. In case
the content type header was not set, it will by default be set to application/x-www-form-urlencoded
.contentType
- the Content-Type request header for the HTTP connectionRestDataSource.setContentType(String)
public RestClient header(java.lang.String field, java.lang.String value)
contentType(String)
),
so to avoid complications you should only set headers here if you haven't set them via any other methodsfield
- The name of the HTTP header field (e.g. "Accept")value
- The header value to send with the requestpublic RestClient multipartField(java.lang.String field, java.lang.String value)
Adds a form field to an HTTP Multipart Form-Data request. The content type of the form field will be
text/plain
.
Note: the content type
needs to be set explicitly to
multipart/form-data
for the multipart form to be written to the request.
field
- the name of the form fieldvalue
- the value of the form fieldpublic RestClient multipartField(java.lang.String field, byte[] value)
Adds a binary field to an HTTP Multipart Form-Data request. The content type of the binary field will be
application/octet-stream
.
Note: the content type
needs to be set explicitly to
multipart/form-data
for the multipart form to be written to the request.
field
- the name of the form partvalue
- the bytes of the form partpublic RestDataSource createRestDataSource()
public <T> ObjectDataReader<T> createObjectDataReader(java.lang.Class<T> targetClass)
ObjectDataReader
that can be passed directly in the
DataProvider.retrieveObject(ObjectDataReader)
method. The object data reader will read the data from
the HTTP connection provided by the RestDataSource.
The object data reader will try to detect the converter to use based on the Content-Type response header that
is returned from the HTTP connection. The specified targetClass
will be passed in to the detected
converter where needed. A custom converter can be specified with createObjectDataReader(InputStreamInputConverter)
when no suitable converter could be detected.
T
- the type of the object to readtargetClass
- the class definition of the object to readpublic <T> ObjectDataReader<T> createObjectDataReader(InputStreamInputConverter<T> converter)
ObjectDataReader
that can be passed directly in the
DataProvider.retrieveObject(ObjectDataReader)
method. The object data reader will read the data from
the HTTP connection provided by the RestDataSource and convert it into an object by using the specified
converter
.T
- the type of the object to readconverter
- the converter to use to convert the data read from the HTTP connection into an objectpublic <T> ObjectDataWriter<T> createObjectDataWriter(java.lang.Class<T> targetClass)
ObjectDataWriter
that can be passed directly in the
DataProvider.storeObject(Object, ObjectDataWriter)
method.
The object data writer will try to detect the output converter to use based on the Content-Type
request header that was set. The specified targetClass
will be passed in to the detected converter
where needed. A custom output converter can be specified with createObjectDataWriter(OutputStreamOutputConverter, InputStreamInputConverter)
when no suitable converter could be detected. If an output converter could be found, the writer will convert an
object by using the detected converter and writes the converted data to the OutputStream of the HTTP connection
that is provided by the RestDataSource.
The ObjectDataWriter also returns an optional object. In case of the RestClient, the returned object will be
read from the response of the HTTP request. The object data reader will try to detect the input converter to use
based on the Content-Type response header that is returned from the HTTP connection. The specified
targetClass
will be passed in to the detected input converter as well where needed. The reader will
then use the input converter to convert the read bytes from the response of the HTTP request into an object.
T
- the type of the object to writetargetClass
- the class definition of the object to writepublic <T> ObjectDataWriter<T> createObjectDataWriter(OutputStreamOutputConverter<T> outputConverter, InputStreamInputConverter<T> inputConverter)
ObjectDataWriter
that can be passed directly in the
DataProvider.storeObject(Object, ObjectDataWriter)
method. The object data writer will convert an object
by using the specified output converter and writes the converted data to the OutputStream of the HTTP connection
that is provided by the RestDataSource.
The ObjectDataWriter also returns an optional object. In case of the RestClient, the returned object will be read from the response of the HTTP request. The object data reader will use the specified input converter to convert the read bytes from the response of the HTTP request into an object.
T
- the type of the object to writeoutputConverter
- the output converter to use to convert the object into data to write to the HTTP connectioninputConverter
- the input converter to use to convert the data read from the HTTP connection into an objectpublic <T> ObjectDataRemover<T> createObjectDataRemover(java.lang.Class<T> targetClass)
ObjectDataRemover
that can be passed directly in the
DataProvider.removeObject(GluonObservableObject, ObjectDataRemover)
method. See the documentation for the
createObjectDataWriter(Class)
method, as the writer works exactly the same as the remover.T
- the type of the object to removetargetClass
- the class definition of the object to removepublic <T> ObjectDataRemover<T> createObjectDataRemover(OutputStreamOutputConverter<T> outputConverter, InputStreamInputConverter<T> inputConverter)
ObjectDataRemover
that can be passed directly in the
DataProvider.removeObject(GluonObservableObject, ObjectDataRemover)
method. See the documentation for the
createObjectDataWriter(OutputStreamOutputConverter, InputStreamInputConverter)
method, as the
writer works exactly the same as the remover.T
- the type of the object to removeoutputConverter
- the output converter to use to convert the object into data to write to the HTTP connectioninputConverter
- the input converter to use to convert the data read from the HTTP connection into an objectpublic <E> ListDataReader<E> createListDataReader(java.lang.Class<E> targetClass)
ListDataReader
that can be passed directly in the
DataProvider.retrieveList(ListDataReader)
method. The list data reader will read the data from the HTTP
connection provided by the RestDataSource.
The list data reader will try to detect the converter to use based on the Content-Type response header that is
returned from the HTTP connection. The specified targetClass
will be passed in to the detected
converter where needed. A custom converter can be specified with createListDataReader(InputStreamIterableInputConverter)
when no suitable converter could be detected.
E
- the type of the objects contained in the list to readtargetClass
- the class definition of the objects contained in the listpublic <E> ListDataReader<E> createListDataReader(InputStreamIterableInputConverter<E> converter)
ListDataReader
that can be passed directly in the
DataProvider.retrieveList(ListDataReader)
method. The list data reader will read the data from the HTTP
connection provided by the RestDataSource and converts it into a list of objects by using the specified
converter
.E
- the type of the objects contained in the list to readconverter
- the converter to use to convert the data read from the HTTP connection into a list of objects