Package org.appng.api.rest
Class RestClient
- java.lang.Object
-
- org.appng.api.rest.RestClient
-
public class RestClient extends Object
A simple client for the appNG REST API, open for extension. Note that this class is NOT thread-safe!
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RestClient.Pageable
Wraps paging and sorting
-
Constructor Summary
Constructors Constructor Description RestClient(String url)
Creates a newRestClient
.RestClient(String url, Map<String,String> cookies)
Creates a newRestClient
, using an existing cookie.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RestResponseEntity<Datasource>
datasource(String application, String id)
Retrieves theDatasource
.RestResponseEntity<Datasource>
datasource(String application, String id, RestClient.Pageable pageable)
Retrieves theDatasource
.RestResponseEntity<Datasource>
datasource(String application, String id, RestClient.Pageable pageable, org.springframework.util.MultiValueMap<String,String> parameters)
Retrieves theDatasource
.RestResponseEntity<Datasource>
datasource(String application, String id, org.springframework.util.MultiValueMap<String,String> parameters)
Retrieves theDatasource
.<OUT,IN>
RestResponseEntity<IN>exchange(String path, OUT body, Class<IN> returnType, org.springframework.http.HttpMethod method)
Retrieves a REST-Resource with a given HTTP method.
Example:protected <IN,OUT>
RestResponseEntity<IN>exchange(URI uri, OUT body, org.springframework.http.HttpMethod method, Class<IN> returnType)
protected <IN,OUT>
RestResponseEntity<IN>exchange(URI uri, OUT body, org.springframework.http.HttpMethod method, Class<IN> returnType, boolean acceptAnyType)
RestResponseEntity<Action>
getAction(String application, String eventId, String actionId, String... pathVariables)
Retrieves theAction
.RestResponseEntity<Action>
getAction(Link link)
protected URI
getActionURL(String application, String eventId, String actionId, String[] pathVariables)
RestResponseEntity<byte[]>
getBinaryData(String relativePath)
Returns the resource represented by the relative pathRestResponseEntity<byte[]>
getBinaryData(Link link)
Returns the resource represented by the link as binary dataMap<String,String>
getCookies()
Returns the current cookies or this clientprotected org.springframework.http.HttpHeaders
getHeaders(boolean acceptAnyType)
<IN> RestResponseEntity<IN>
getResource(String path, Class<IN> returnType)
Retrieves a REST-Resource with HTTP GET.
Example:RestResponseEntity<Action>
performAction(String application, Action data, String... pathVariables)
Performs anAction
.RestResponseEntity<Action>
performAction(Action data, Link link)
protected void
setCookies(org.springframework.http.ResponseEntity<?> entity)
-
-
-
Constructor Detail
-
RestClient
public RestClient(String url)
Creates a newRestClient
.Note that you probably need to perform a login action before you can use this client.
- Parameters:
url
- the URL pointing to a site's service URL (/service/<site-name>
)
-
RestClient
public RestClient(String url, Map<String,String> cookies)
Creates a newRestClient
, using an existing cookie. This cookie should be retrieved from another client that performed a login action.- Parameters:
url
- the URL pointing to a site's service URL (/service/<site-name>
)cookies
- the cookie to use- See Also:
getCookies()
-
-
Method Detail
-
datasource
public RestResponseEntity<Datasource> datasource(String application, String id) throws URISyntaxException
Retrieves theDatasource
.- Parameters:
application
- the name of the applicationid
- the ID of theDatasource
- Returns:
- the
Datasource
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
datasource
public RestResponseEntity<Datasource> datasource(String application, String id, RestClient.Pageable pageable) throws URISyntaxException
Retrieves theDatasource
.- Parameters:
application
- the name of the applicationid
- the ID of theDatasource
pageable
- aRestClient.Pageable
(optional)- Returns:
- the
Datasource
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
datasource
public RestResponseEntity<Datasource> datasource(String application, String id, org.springframework.util.MultiValueMap<String,String> parameters) throws URISyntaxException
Retrieves theDatasource
.- Parameters:
application
- the name of the applicationid
- the ID of theDatasource
parameters
- some additional parameters- Returns:
- the
Datasource
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
datasource
public RestResponseEntity<Datasource> datasource(String application, String id, RestClient.Pageable pageable, org.springframework.util.MultiValueMap<String,String> parameters) throws URISyntaxException
Retrieves theDatasource
.- Parameters:
application
- the name of the applicationid
- the ID of theDatasource
pageable
- aRestClient.Pageable
(optional)parameters
- some additional parameters- Returns:
- the
Datasource
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
getAction
public RestResponseEntity<Action> getAction(String application, String eventId, String actionId, String... pathVariables) throws URISyntaxException
Retrieves theAction
.- Parameters:
application
- the name of the applicationeventId
- the event-ID of theAction
actionId
- the ID of theAction
pathVariables
- some additional path variables- Returns:
- the (unprocessed)
Action
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
getAction
public RestResponseEntity<Action> getAction(Link link) throws URISyntaxException
- Parameters:
link
- theLink
representing theAction
's URI- Returns:
- the (unprocessed)
Action
wrapped in aRestResponseEntity
- Throws:
URISyntaxException
- if something is wrong with the URI
-
performAction
public RestResponseEntity<Action> performAction(Action data, Link link) throws URISyntaxException
- Parameters:
data
- theAction
-data to sendlink
- theLink
- Returns:
- a
RestResponseEntity
wrapping the resultingAction
- Throws:
URISyntaxException
- if something is wrong with the URI
-
setCookies
protected void setCookies(org.springframework.http.ResponseEntity<?> entity)
-
getActionURL
protected URI getActionURL(String application, String eventId, String actionId, String[] pathVariables) throws URISyntaxException
- Throws:
URISyntaxException
-
performAction
public RestResponseEntity<Action> performAction(String application, Action data, String... pathVariables) throws URISyntaxException
Performs anAction
.- Parameters:
application
- the name of the applicationdata
- theAction
-data to sendpathVariables
- some additional path variables- Returns:
- a
RestResponseEntity
wrapping the resultingAction
- Throws:
URISyntaxException
- if something is wrong with the URI
-
getHeaders
protected org.springframework.http.HttpHeaders getHeaders(boolean acceptAnyType)
-
getCookies
public Map<String,String> getCookies()
Returns the current cookies or this client- Returns:
- the cookie's map
-
getBinaryData
public RestResponseEntity<byte[]> getBinaryData(Link link) throws URISyntaxException
Returns the resource represented by the link as binary data- Parameters:
link
- the ink to there resource- Returns:
- the resource
- Throws:
URISyntaxException
- if something is wrong with the link
-
getBinaryData
public RestResponseEntity<byte[]> getBinaryData(String relativePath) throws URISyntaxException
Returns the resource represented by the relative path- Parameters:
relativePath
- the relative path, e.g./application/rest/downloads/4711
- Returns:
- the resource
- Throws:
URISyntaxException
- if something is wrong with the path
-
exchange
public <OUT,IN> RestResponseEntity<IN> exchange(String path, OUT body, Class<IN> returnType, org.springframework.http.HttpMethod method) throws URISyntaxException
Retrieves a REST-Resource with a given HTTP method.
Example:ResponseEntity
result = restClient.retrieveResource("/application/rest/calculator/add/47/11", null, Integer.class, HttpMethod.GET) - Parameters:
path
- the relative path to the resource, starting with the application's namebody
- the request body (optional)returnType
- the type of the responsemethod
- theHttpMethod
to use- Returns:
- the
RestResponseEntity
- Throws:
URISyntaxException
-
exchange
protected <IN,OUT> RestResponseEntity<IN> exchange(URI uri, OUT body, org.springframework.http.HttpMethod method, Class<IN> returnType)
-
exchange
protected <IN,OUT> RestResponseEntity<IN> exchange(URI uri, OUT body, org.springframework.http.HttpMethod method, Class<IN> returnType, boolean acceptAnyType)
-
getResource
public <IN> RestResponseEntity<IN> getResource(String path, Class<IN> returnType) throws URISyntaxException
Retrieves a REST-Resource with HTTP GET.
Example:ResponseEntity
result = restClient.retrieveResource("/application/rest/calculator/add/47/11", null, Integer.class, HttpMethod.GET) - Parameters:
path
- the relative path to the resource, starting with the application's namereturnType
- the type of the response- Returns:
- the
RestResponseEntity
- Throws:
URISyntaxException
-
-