REST Client
This library is for integration scenario to programmatically invoke the REST API (Restful Objects Viewer) of an Apache Causeway application, using the restfulobjects protocol.
A common use case would be to consume information from some external system, for example uploading a scanned PDF or a representation of an incoming invoice.
Initializing
We create a RestfulClient
using a static factory method that accepts a RestfulClientConfig
object.
For example:
-
to create a
RestfulClient
that authenticates itself using basic auth:RestfulClient restfulClient = RestfulClient.ofConfig( RestfulClientConfig.builder() .restfulBaseUrl("localhost:8080") .authenticationMode(AuthenticationMode.BASIC) .basicAuthUser("xxxx") .basicAuthPassword("xxxx") .build());
-
to create a
RestfulClient
that authenticates itself using OAuth2 (Azure):RestfulClient restfulClient = RestfulClient.ofConfig( RestfulClientConfig.builder() .restfulBaseUrl("localhost:8080") .authenticationMode(AuthenticationMode.OAUTH2_AZURE) .oauthTenantId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") (1) .oauthClientId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") (2) .oauthClientSecret("xxXXx~XXxxXx-XxXX.XxXX.XxxXX-XxxXX~XxxXX") (3) .build());
For the Causeway app being connected to, this will need to be configured appropriately; see Spring Oauth2 extension for more details. -
It’s also possible to create a
RestfulClient
that uses some other arbitrary authentication mechanism:RestfulClient restfulClient = RestfulClient.ofConfig( RestfulClientConfig.builder() (1) .restfulBaseUrl("localhost:8080") .build(), new AuthorizationHeaderFactory() { @Override public String create() { return "some authorization header"; (2) } }); );
1 No need to provide any authentication details in the RestfulClientConfig
2 Used as the value of the Authorization
HTTP header.
Making a Request
Invocation.Builder request = (1)
restfulClient.request("services/customers.CustomerApi/actions/findAll/invoke"); (2)
Response response = request.get(); (3)
// Response response = request.put(...); (3)
// Response response = request.post(...); (3)
// Response response = request.delete(); (3)
1 | instance of javax.ws.rs.client.Invocation.Builder |
2 | specify the endpoint |
3 | call appropriately using either HTTP GET , PUT , POST or DELETE .
The PUT and POST methods will require an entity body, and all might require additional headers to be set. |
Refining the Client
The RestfulClient
delegates to an instance of javax.ws.rs.client.Client
to actually perform the invocation.
The configuration of this underlying Client
can be refined by providing a "refining" operator of ClientBuilder
, when the RestfulClient
is created.
For example, to specify a connection and read timeout:
RestfulClient restfulClient = RestfulClient.ofConfig(
RestfulClientConfig.builder()
.restfulBaseUrl("localhost:8080")
//... (1)
.build(),
clientBuilder -> (2)
clientBuilder.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(2, TimeUnit.SECONDS)
);
1 | authentication details omitted |
2 | instance of javax.ws.rs.client.ClientBuilder |