1 # Common REST Client Library
3 This library provides a single client implementation to be used by micro services for communicating via its REST API.
8 In order to make the _REST Client_ library available to your microservice, include the following dependency in your service's pom.xml:
10 <!-- Common REST Client Library -->
12 <groupId>org.openecomp.aai</groupId>
13 <artifactId>rest-client</artifactId>
14 <version>1.2.0-SNAPSHOT</version>
19 ### Creating and Configuring a Client Instance
20 In order to start talking to a service, you need to create a client instance and configure it. The _RestClient_ uses a fluent interface which allows it to be both instantiated and configured as in the following examples:
22 i) A client using an SSL Client Certificate:
24 // Create an instance of the Rest Client and configure it.
25 RestClient myClient = new RestClient()
26 .authenticationMode(RestAuthenticationMode.SSL_CERT)
27 .validateServerHostname(false)
28 .validateServerCertChain(true)
29 .clientCertFile("certificate_filename")
30 .trustStroe("trust_store_filename")
31 .connectTimeoutMs(1000)
34 ii) A client using SSL Basic-Auth:
36 // Create an instance of the Rest Client and configure it.
37 RestClient myClient = new RestClient()
38 .authenticationMode(RestAuthenticationMode.SSL_BASIC)
39 .basicAuthUsername("username")
40 .basicAuthPassword("password")
41 .connectTimeoutMs(1000)
44 iii) A client using non-authentication HTTP:
46 // Create an instance of the Rest Client and configure it.
47 RestClient myClient = new RestClient()
48 .authenticationMode(RestAuthenticationMode.HTTP_NOAUTH)
49 .connectTimeoutMs(1000)
52 Note, that all of the above configuration parameters are optional and will be set to default values if they are not specified.
55 Once your service has a client instance, it can query the _Active & Available Inventory_ by specifying an HTTP endpoint, headers, and the expected response format:
57 MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
58 headers.put("Accept", Arrays.asList(new String[]{"application/json"}));
59 headers.put("X-FromAppId", Arrays.asList(new String[]{"APP-ID"}));
60 headers.put("X-TransactionId", Arrays.asList(new String[]{UUID.randomUUID().toString()}));
62 OperationResult result = myClient.queryActiveInventory("http://some/endpoint", headers, RestClient.RESPONSE_MIME_TYPE.JSON);
64 // You can also specify number of re-tries:
66 OperationResult result = myClient.queryActiveInventory("http://some/endpoint", headers, RestClient.RESPONSE_MIME_TYPE.JSON, retries);
69 The result of the query is returned as an _OperationResult_ object, which can be unpacked in the following manner:
71 The standard HTTP result code received back from the _A&AI_ is accessible as follows:
73 int resultCode = getResultCode()
75 The actual result payload is accessible in the following manner:
77 String resultPayload = result.getResult()
79 Finally, in the event of a failure, a failure cause message will be populated and can be accessed as follows:
81 String failureCause = result.getFailureCause()