Rewrite rest-client to use Jersey 2.x API
[aai/rest-client.git] / src / main / java / org / onap / aai / restclient / client / RestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.restclient.client;
22
23 import java.io.ByteArrayOutputStream;
24 import java.text.SimpleDateFormat;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.UUID;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentMap;
33 import java.util.stream.Collectors;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.Invocation.Builder;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.MultivaluedHashMap;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import org.onap.aai.cl.api.LogFields;
43 import org.onap.aai.cl.api.LogLine;
44 import org.onap.aai.cl.api.Logger;
45 import org.onap.aai.cl.eelf.LoggerFactory;
46 import org.onap.aai.cl.mdc.MdcContext;
47 import org.onap.aai.cl.mdc.MdcOverride;
48 import org.onap.aai.restclient.enums.RestAuthenticationMode;
49 import org.onap.aai.restclient.logging.RestClientMsgs;
50 import org.onap.aai.restclient.rest.RestClientBuilder;
51
52
53
54 /**
55  * This class provides a general client implementation that micro services can use for communicating with the endpoints
56  * via their exposed REST interfaces.
57  * 
58  */
59
60 public class RestClient {
61
62     /**
63      * This is a generic builder that is used for constructing the REST client that we will use to communicate with the
64      * REST endpoint.
65      */
66     private RestClientBuilder clientBuilder;
67
68     private final ConcurrentMap<String, InitializedClient> CLIENT_CACHE = new ConcurrentHashMap<>();
69     private static final String REST_CLIENT_INSTANCE = "REST_CLIENT_INSTANCE";
70
71     /** Standard logger for producing log statements. */
72     private Logger logger = LoggerFactory.getInstance().getLogger("AAIRESTClient");
73
74     /** Standard logger for producing metric statements. */
75     private Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger("AAIRESTClient");
76
77     private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
78
79     /** Reusable function call for GET REST operations. */
80     private final RestOperation getOp = new GetRestOperation();
81
82     /** Reusable function call for PUT REST operations. */
83     private final RestOperation putOp = new PutRestOperation();
84
85     /** Reusable function call for POST REST operations. */
86     private final RestOperation postOp = new PostRestOperation();
87
88     /** Reusable function call for DELETE REST operations. */
89     private final RestOperation deleteOp = new DeleteRestOperation();
90
91     /** Reusable function call for HEAD REST operations. */
92     private final RestOperation headOp = new HeadRestOperation();
93
94     /** Reusable function call for PATCH REST operations. */
95     private final RestOperation patchOp = new PatchRestOperation();
96
97     /**
98      * Creates a new instance of the {@link RestClient}.
99      */
100     public RestClient() {
101         clientBuilder = new RestClientBuilder();
102     }
103
104     /**
105      * Creates a new instance of the {@link RestClient} using the supplied {@link RestClientBuilder}.
106      *
107      * @param rcBuilder - The REST client builder that this instance of the {@link RestClient} should use.
108      */
109     public RestClient(RestClientBuilder rcBuilder) {
110         clientBuilder = rcBuilder;
111     }
112
113     public RestClient authenticationMode(RestAuthenticationMode mode) {
114         logger.debug("Set rest authentication mode= " + mode);
115         clientBuilder.setAuthenticationMode(mode);
116         return this;
117     }
118
119     public RestClient basicAuthUsername(String username) {
120         logger.debug("Set SSL BasicAuth username = " + username);
121         clientBuilder.setBasicAuthUsername(username);
122         return this;
123     }
124
125     public RestClient basicAuthPassword(String password) {
126         /*
127          * purposely not logging out the password, I guess we could obfuscate it if we really want to see it in the logs
128          */
129         clientBuilder.setBasicAuthPassword(password);
130         return this;
131     }
132
133     /**
134      * Sets the flag to indicate whether or not validation should be performed against the host name of the server we
135      * are trying to communicate with.
136      *
137      * @parameter validate - Set to true to enable validation, false to disable
138      *
139      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
140      */
141     public RestClient validateServerHostname(boolean validate) {
142         logger.debug("Set validate server hostname = " + validate);
143         clientBuilder.setValidateServerHostname(validate);
144         return this;
145     }
146
147     /**
148      * Sets the flag to indicate whether or not validation should be performed against the certificate chain.
149      *
150      * @parameter validate - Set to true to enable validation, false to disable.
151      *
152      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
153      */
154     public RestClient validateServerCertChain(boolean validate) {
155         logger.debug("Set validate server certificate chain = " + validate);
156         clientBuilder.setValidateServerCertChain(validate);
157         return this;
158     }
159
160     /**
161      * Assigns the client certificate file to use.
162      *
163      * @param filename - The name of the certificate file.
164      *
165      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
166      */
167     public RestClient clientCertFile(String filename) {
168         logger.debug("Set client certificate filename = " + filename);
169         clientBuilder.setClientCertFileName(filename);
170         return this;
171     }
172
173     /**
174      * Assigns the client certificate password to use.
175      *
176      * @param password - The certificate password.
177      *
178      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
179      */
180     public RestClient clientCertPassword(String password) {
181         clientBuilder.setClientCertPassword(password);
182         return this;
183     }
184
185     /**
186      * Assigns the name of the trust store file to use.
187      *
188      * @param filename - the name of the trust store file.
189      *
190      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
191      */
192     public RestClient trustStore(String filename) {
193         logger.debug("Set trust store filename = " + filename);
194         clientBuilder.setTruststoreFilename(filename);
195         return this;
196     }
197
198     /**
199      * Assigns the connection timeout (in ms) to use when connecting to the target server.
200      *
201      * @param timeout - The length of time to wait in ms before timing out.
202      *
203      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
204      */
205     public RestClient connectTimeoutMs(int timeout) {
206         logger.debug("Set connection timeout = " + timeout + " ms");
207         clientBuilder.setConnectTimeoutInMs(timeout);
208         return this;
209     }
210
211     /**
212      * Assigns the read timeout (in ms) to use when communicating with the target server.
213      *
214      * @param timeout The read timeout in milliseconds.
215      *
216      * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
217      */
218     public RestClient readTimeoutMs(int timeout) {
219         logger.debug("Set read timeout = " + timeout + " ms");
220         clientBuilder.setReadTimeoutInMs(timeout);
221         return this;
222     }
223
224     /**
225      * Configures the client for a specific SSL protocol
226      *
227      * @param sslProtocol - protocol string constant such as TLS, TLSv1, TLSv1.1, TLSv1.2
228      *
229      * @return The AAIRESTClient instance.
230      */
231     public RestClient sslProtocol(String sslProtocol) {
232         logger.debug("Set sslProtocol = " + sslProtocol);
233         clientBuilder.setSslProtocol(sslProtocol);
234         return this;
235     }
236
237     private boolean shouldRetry(OperationResult operationResult) {
238
239         if (operationResult == null) {
240             return true;
241         }
242
243         int resultCode = operationResult.getResultCode();
244
245         if (resultCode == 200) {
246             return false;
247         }
248
249         if (resultCode == 404) {
250             return false;
251         }
252
253         return true;
254
255     }
256
257     /**
258      * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL. This
259      * variant of the method will perform a requested number of retries in the event that the first request is
260      * unsuccessful.
261      *
262      * @param operation - the REST operation type to send to the url
263      * @param url - The REST endpoint to submit the REST request to.
264      * @param payload - They payload to provide in the REST request, if applicable
265      * @param headers - The headers that should be passed in the request
266      * @param contentType - The content type of the payload
267      * @param responseType - The expected format of the response.
268      * 
269      * @return The result of the REST request.
270      */
271     protected OperationResult processRequest(RestOperation operation, String url, String payload,
272             Map<String, List<String>> headers, MediaType contentType, MediaType responseType, int numRetries) {
273
274
275         OperationResult result = null;
276
277         long startTimeInMs = System.currentTimeMillis();
278         for (int retryCount = 0; retryCount < numRetries; retryCount++) {
279
280             logger.info(RestClientMsgs.HTTP_REQUEST_WITH_RETRIES, operation.getRequestType().toString(), url,
281                     Integer.toString(retryCount + 1));
282
283             // Submit our query to the AAI.
284             result = processRequest(operation, url, payload, headers, contentType, responseType);
285
286             // If the submission was successful then we're done.
287
288             if (!shouldRetry(result)) {
289
290                 logger.info(RestClientMsgs.HTTP_REQUEST_TIME_WITH_RETRIES, operation.getRequestType().toString(), url,
291                         Long.toString(System.currentTimeMillis() - startTimeInMs), Integer.toString(retryCount));
292
293                 result.setNumRetries(retryCount);
294
295                 return result;
296             }
297
298             // Our submission was unsuccessful...
299             try {
300                 // Sleep between re-tries to be nice to the target system.
301                 Thread.sleep(50);
302
303             } catch (InterruptedException e) {
304                 logger.error(RestClientMsgs.HTTP_REQUEST_INTERRUPTED, url, e.getLocalizedMessage());
305                 Thread.currentThread().interrupt();
306                 break;
307             }
308         }
309
310         // If we've gotten this far, then we failed all of our retries.
311         if (result == null) {
312             result = new OperationResult();
313         }
314
315         result.setNumRetries(numRetries);
316         result.setResultCode(504);
317         result.setFailureCause("Failed to get a successful result after multiple retries to target server.");
318
319
320         return result;
321     }
322
323     /**
324      * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL.
325      *
326      * @param operation - the REST operation type to send to the url
327      * @param url - The REST endpoint to submit the REST request to.
328      * @param payload - They payload to provide in the REST request, if applicable
329      * @param headers - The headers that should be passed in the request
330      * @param contentType - The content type of the payload
331      * @param responseType - The expected format of the response.
332      *
333      * @return The result of the REST request.
334      */
335     protected OperationResult processRequest(RestOperation operation, String url, String payload,
336             Map<String, List<String>> headers, MediaType contentType, MediaType responseType) {
337
338         Response clientResponse = null;
339         OperationResult operationResult = new OperationResult();
340         ByteArrayOutputStream baos = new ByteArrayOutputStream();
341
342         String requestType = operation.getRequestType().name();
343
344         // Grab the current time so that we can log how long the
345         // query took once we are done.
346         long startTimeInMs = System.currentTimeMillis();
347         MdcOverride override = new MdcOverride();
348         override.addAttribute(MdcContext.MDC_START_TIME, formatter.format(startTimeInMs));
349
350         logger.info(RestClientMsgs.HTTP_REQUEST, requestType, url);
351
352         try {
353
354             // Get a REST client instance for our request.
355             Client client = getClient();
356
357             // Debug log the request
358             debugRequest(url, payload, headers, responseType);
359
360             // Get a client request builder, and submit our GET request.
361             Builder builder = getClientBuilder(client, url, headers, responseType);
362             clientResponse = operation.processOperation(builder, payload, contentType);
363
364             populateOperationResult(clientResponse, operationResult);
365
366             // Debug log the response
367             if (clientResponse != null) {
368                 debugResponse(operationResult, clientResponse.getHeaders());
369             }
370
371         } catch (Exception ex) {
372
373             logger.error(RestClientMsgs.HTTP_REQUEST_ERROR, requestType, url, ex.getLocalizedMessage());
374             operationResult.setResultCode(500);
375             operationResult
376                     .setFailureCause("Error during GET operation to AAI with message = " + ex.getLocalizedMessage());
377
378         } finally {
379
380             if (logger.isDebugEnabled()) {
381                 logger.debug(baos.toString());
382             }
383
384             // Not every valid response code is actually represented by the Response.Status
385             // object, so we need to guard against missing codes, otherwise we throw null
386             // pointer exceptions when we try to generate our metrics logs...
387             Response.Status responseStatus = Response.Status.fromStatusCode(operationResult.getResultCode());
388             String responseStatusCodeString = "";
389             if (responseStatus != null) {
390                 responseStatusCodeString = responseStatus.toString();
391             }
392
393             metricsLogger.info(RestClientMsgs.HTTP_REQUEST_TIME,
394                     new LogFields().setField(LogLine.DefinedFields.STATUS_CODE, responseStatusCodeString)
395                             .setField(LogLine.DefinedFields.RESPONSE_CODE, operationResult.getResultCode())
396                             .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, operationResult.getResult()),
397                     override, requestType, Long.toString(System.currentTimeMillis() - startTimeInMs), url);
398             logger.info(RestClientMsgs.HTTP_REQUEST_TIME, requestType,
399                     Long.toString(System.currentTimeMillis() - startTimeInMs), url);
400             logger.info(RestClientMsgs.HTTP_RESPONSE, url,
401                     operationResult.getResultCode() + " " + responseStatusCodeString);
402         }
403
404         return operationResult;
405     }
406
407     /**
408      * This method submits an HTTP PUT request against the supplied URL.
409      *
410      * @param url - The REST endpoint to submit the PUT request to.
411      * @param payload - the payload to send to the supplied URL
412      * @param headers - The headers that should be passed in the request
413      * @param contentType - The content type of the payload
414      * @param responseType - The expected format of the response.
415      *
416      * @return The result of the PUT request.
417      */
418     public OperationResult put(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
419             MediaType responseType) {
420         return processRequest(putOp, url, payload, headers, contentType, responseType);
421     }
422
423     /**
424      * This method submits an HTTP POST request against the supplied URL.
425      *
426      * @param url - The REST endpoint to submit the POST request to.
427      * @param payload - the payload to send to the supplied URL
428      * @param headers - The headers that should be passed in the request
429      * @param contentType - The content type of the payload
430      * @param responseType - The expected format of the response.
431      *
432      * @return The result of the POST request.
433      */
434     public OperationResult post(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
435             MediaType responseType) {
436         return processRequest(postOp, url, payload, headers, contentType, responseType);
437     }
438
439     /**
440      * This method submits an HTTP POST request against the supplied URL, and emulates a PATCH operation by setting a
441      * special header value
442      *
443      * @param url - The REST endpoint to submit the POST request to.
444      * @param payload - the payload to send to the supplied URL
445      * @param headers - The headers that should be passed in the request
446      * @param contentType - The content type of the payload
447      * @param responseType - The expected format of the response.
448      *
449      * @return The result of the PATCH request.
450      */
451     public OperationResult patch(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
452             MediaType responseType) {
453         return processRequest(patchOp, url, payload, headers, contentType, responseType);
454     }
455
456     /**
457      * This method submits an HTTP HEAD request against the supplied URL
458      *
459      * @param url - The REST endpoint to submit the POST request to.
460      * @param headers - The headers that should be passed in the request
461      * @param responseType - The expected format of the response.
462      *
463      * @return The result of the HEAD request.
464      */
465     public OperationResult head(String url, Map<String, List<String>> headers, MediaType responseType) {
466         return processRequest(headOp, url, null, headers, null, responseType);
467     }
468
469     /**
470      * This method submits an HTTP GET request against the supplied URL.
471      *
472      * @param url - The REST endpoint to submit the GET request to.
473      * @param headers - The headers that should be passed in the request
474      * @param responseType - The expected format of the response.
475      *
476      * @return The result of the GET request.
477      */
478     public OperationResult get(String url, Map<String, List<String>> headers, MediaType responseType) {
479         return processRequest(getOp, url, null, headers, null, responseType);
480     }
481
482     /**
483      * This method submits an HTTP GET request against the supplied URL. This variant of the method will perform a
484      * requested number of retries in the event that the first request is unsuccessful.
485      * 
486      * @param url - The REST endpoint to submit the GET request to.
487      * @param headers - The headers that should be passed in the request
488      * @param responseType - The expected format of the response.
489      * @param numRetries - The number of times to try resubmitting the request in the event of a failure.
490      * 
491      * @return The result of the GET request.
492      */
493     public OperationResult get(String url, Map<String, List<String>> headers, MediaType responseType, int numRetries) {
494         return processRequest(getOp, url, null, headers, null, responseType, numRetries);
495     }
496
497     /**
498      * This method submits an HTTP DELETE request against the supplied URL.
499      *
500      * @param url - The REST endpoint to submit the DELETE request to.
501      * @param headers - The headers that should be passed in the request
502      * @param responseType - The expected format of the response.
503      *
504      * @return The result of the DELETE request.
505      */
506     public OperationResult delete(String url, Map<String, List<String>> headers, MediaType responseType) {
507         return processRequest(deleteOp, url, null, headers, null, responseType);
508     }
509
510     /**
511      * This method does a health check ("ping") against the supplied URL.
512      *
513      * @param url - The REST endpoint to attempt a health check.
514      * @param srcAppName - The name of the application using this client.
515      * @param destAppName - The name of the destination app.
516      *
517      * @return A boolean value. True if connection attempt was successful, false otherwise.
518      *
519      */
520     public boolean healthCheck(String url, String srcAppName, String destAppName) {
521         return healthCheck(url, srcAppName, destAppName, MediaType.TEXT_PLAIN_TYPE);
522
523     }
524
525     /**
526      * This method does a health check ("ping") against the supplied URL.
527      *
528      * @param url - The REST endpoint to attempt a health check.
529      * @param srcAppName - The name of the application using this client.
530      * @param destAppName - The name of the destination app.
531      * @param responseType - The response type.
532      *
533      * @return A boolean value. True if connection attempt was successful, false otherwise.
534      *
535      */
536     public boolean healthCheck(String url, String srcAppName, String destAppName, MediaType responseType) {
537         Map<String, List<String>> headers = new HashMap<>();
538         headers.put(Headers.FROM_APP_ID, Arrays.asList(new String[] {srcAppName}));
539         headers.put(Headers.TRANSACTION_ID, Arrays.asList(new String[] {UUID.randomUUID().toString()}));
540
541         try {
542             logger.info(RestClientMsgs.HEALTH_CHECK_ATTEMPT, destAppName, url);
543             OperationResult result = get(url, headers, responseType);
544
545             if (result != null && result.getFailureCause() == null) {
546                 logger.info(RestClientMsgs.HEALTH_CHECK_SUCCESS, destAppName, url);
547                 return true;
548             } else {
549                 logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url,
550                         result != null ? result.getFailureCause() : null);
551                 return false;
552             }
553         } catch (Exception e) {
554             logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url, e.getMessage());
555             return false;
556         }
557     }
558
559     /**
560      * This method constructs a client request builder that can be used for submitting REST requests to the supplied URL
561      * endpoint.
562      *
563      * @param client - The REST client we will be using to talk to the server.
564      * @param url - The URL endpoint that our request will be submitted to.
565      * @param headers - The headers that should be passed in the request
566      * @param responseType - The expected format of the response.
567      *
568      * @return A client request builder.
569      */
570     private Builder getClientBuilder(Client client, String url, Map<String, List<String>> headers,
571             MediaType responseType) {
572
573         WebTarget target = client.target(url);
574
575         Builder builder = target.request().accept(responseType);
576
577         if (headers != null) {
578             for (Entry<String, List<String>> header : headers.entrySet()) {
579                 builder.header(header.getKey(), String.join(";", header.getValue()));
580             }
581
582             // Added additional check to prevent adding duplicate authorization header if client is already sending the
583             // authorization header
584             // AAI-1097 - For AAI calls when Rest authentication mode is selected as SSL_BASIC getting 403 error
585             if (clientBuilder.getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC
586                     && headers.get(Headers.AUTHORIZATION) == null) {
587                 builder = builder.header(Headers.AUTHORIZATION, clientBuilder.getBasicAuthenticationCredentials());
588             }
589
590         }
591
592         return builder;
593     }
594
595     private void debugRequest(String url, String payload, Map<String, List<String>> headers, MediaType responseType) {
596         if (!logger.isDebugEnabled()) {
597             return;
598         }
599
600         StringBuilder debugRequest = new StringBuilder("REQUEST:\n");
601         debugRequest.append("URL: ").append(url).append("\n");
602         debugRequest.append("Payload: ").append(payload).append("\n");
603         debugRequest.append("Response Type: ").append(responseType).append("\n");
604
605         if (headers == null) {
606             logger.debug(debugRequest.toString());
607             return;
608         }
609
610         debugRequest.append("Headers: ");
611         for (Entry<String, List<String>> header : headers.entrySet()) {
612             debugRequest.append("\n\t").append(header.getKey()).append(":");
613             for (String headerEntry : header.getValue()) {
614                 debugRequest.append("\"").append(headerEntry).append("\" ");
615             }
616         }
617
618         logger.debug(debugRequest.toString());
619
620     }
621
622     private void debugResponse(OperationResult operationResult, MultivaluedMap<String, Object> headers) {
623
624         if (!logger.isDebugEnabled()) {
625             return;
626         }
627
628         StringBuilder debugResponse = new StringBuilder("RESPONSE:\n");
629         debugResponse.append("Result: ").append(operationResult.getResultCode()).append("\n");
630         debugResponse.append("Failure Cause: ").append(operationResult.getFailureCause()).append("\n");
631         debugResponse.append("Payload: ").append(operationResult.getResult()).append("\n");
632
633         if (headers == null) {
634             logger.debug(debugResponse.toString());
635             return;
636         }
637
638         debugResponse.append("Headers: ");
639         for (Entry<String, List<Object>> header : headers.entrySet()) {
640             debugResponse.append("\n\t").append(header.getKey()).append(":");
641             for (Object headerEntry : header.getValue()) {
642                 debugResponse.append("\"").append(headerEntry).append("\" ");
643             }
644         }
645
646         logger.debug(debugResponse.toString());
647     }
648
649     /**
650      * This method creates an instance of the low level REST client to use for communicating with the AAI, if one has
651      * not already been created, otherwise it returns the already created instance.
652      *
653      * @return A {@link Client} instance.
654      */
655     protected Client getClient() throws Exception {
656
657         /*
658          * Attempting a new way of doing non-blocking thread-safe lazy-initialization by using Java 1.8 computeIfAbsent
659          * functionality. A null value will not be stored, but once a valid mapping has been established, then the same
660          * value will be returned.
661          * 
662          * One awkwardness of the computeIfAbsent is the lack of support for thrown exceptions, which required a bit of
663          * hoop jumping to preserve the original exception for the purpose of maintaining the pre-existing this API
664          * signature.
665          */
666
667         final InitializedClient clientInstance =
668                 CLIENT_CACHE.computeIfAbsent(REST_CLIENT_INSTANCE, k -> loggedClientInitialization());
669
670         if (clientInstance.getCaughtException() != null) {
671             throw new InstantiationException(clientInstance.getCaughtException().getMessage());
672         }
673
674         return clientInstance.getClient();
675
676     }
677
678     /**
679      * This method will only be called if computerIfAbsent is true. The return value is null, then the result is not
680      * stored in the map.
681      * 
682      * @return a new client instance or null
683      */
684     private InitializedClient loggedClientInitialization() {
685
686         if (logger.isDebugEnabled()) {
687             logger.debug("Instantiating REST client with following parameters:");
688             logger.debug(clientBuilder.toString());
689         }
690
691         InitializedClient initClient = new InitializedClient();
692
693         try {
694             initClient.setClient(clientBuilder.getClient());
695         } catch (Exception error) {
696             initClient.setCaughtException(error);
697         }
698
699         return initClient;
700
701     }
702
703
704     /**
705      * This method populates the fields of an {@link OperationResult} instance based on the contents of a
706      * {@link Response} received in response to a REST request.
707      */
708     private void populateOperationResult(Response response, OperationResult opResult) {
709
710         // If we got back a NULL response, then just produce a generic
711         // error code and result indicating this.
712         if (response == null) {
713             opResult.setResultCode(500);
714             opResult.setFailureCause("Client response was null");
715             return;
716         }
717
718         int statusCode = response.getStatus();
719         opResult.setResultCode(statusCode);
720
721         if (opResult.wasSuccessful()) {
722             if (statusCode != Response.Status.NO_CONTENT.getStatusCode()) {
723                 opResult.setResult(response.readEntity(String.class));
724             }
725         } else {
726             opResult.setFailureCause(response.readEntity(String.class));
727         }
728
729         opResult.setHeaders(convertHeaderObjectsToString(response.getHeaders()));
730     }
731
732     private MultivaluedMap<String, String> convertHeaderObjectsToString(MultivaluedMap<String, Object> headers) {
733         MultivaluedMap<String, String> result = new MultivaluedHashMap<>();
734         headers.forEach((k, v) -> result.addAll(k, v.stream().map(Object::toString).collect(Collectors.toList())));
735         return result;
736     }
737
738     private class GetRestOperation implements RestOperation {
739         @Override
740         public Response processOperation(Builder builder, String payload, MediaType contentType) {
741             return builder.get();
742         }
743
744         @Override
745         public RequestType getRequestType() {
746             return RequestType.GET;
747         }
748     }
749
750     private class PutRestOperation implements RestOperation {
751         @Override
752         public Response processOperation(Builder builder, String payload, MediaType contentType) {
753             return builder.put(Entity.entity(payload, contentType));
754         }
755
756         @Override
757         public RequestType getRequestType() {
758             return RequestType.PUT;
759         }
760     }
761
762     private class PostRestOperation implements RestOperation {
763         @Override
764         public Response processOperation(Builder builder, String payload, MediaType contentType) {
765             return builder.post(Entity.entity(payload, contentType));
766         }
767
768         @Override
769         public RequestType getRequestType() {
770             return RequestType.POST;
771         }
772     }
773
774     private class DeleteRestOperation implements RestOperation {
775         @Override
776         public Response processOperation(Builder builder, String payload, MediaType contentType) {
777             return builder.delete();
778         }
779
780         @Override
781         public RequestType getRequestType() {
782             return RequestType.DELETE;
783         }
784     }
785
786     private class HeadRestOperation implements RestOperation {
787         @Override
788         public Response processOperation(Builder builder, String payload, MediaType contentType) {
789             return builder.head();
790         }
791
792         @Override
793         public RequestType getRequestType() {
794             return RequestType.HEAD;
795         }
796     }
797
798     private class PatchRestOperation implements RestOperation {
799
800         /**
801          * Technically there is no standarized PATCH operation for the jersey client, but we can use the method-override
802          * approach instead.
803          */
804         @Override
805         public Response processOperation(Builder builder, String payload, MediaType contentType) {
806             builder = builder.header("X-HTTP-Method-Override", "PATCH");
807             return builder.post(Entity.entity(payload, contentType));
808         }
809
810         @Override
811         public RequestType getRequestType() {
812             return RequestType.PATCH;
813         }
814     }
815
816
817     /**
818      * Interface used wrap a Jersey REST call using a functional interface.
819      */
820     private interface RestOperation {
821
822         /**
823          * Method used to wrap the functionality of making a REST call out to the endpoint.
824          * 
825          * @param builder the Jersey builder used to make the request
826          * @param payload the request payload
827          * @param contentType the content type of the payload
828          * @return the response from the REST endpoint
829          */
830         public Response processOperation(Builder builder, String payload, MediaType contentType);
831
832         /**
833          * Returns the REST request type.
834          */
835         public RequestType getRequestType();
836
837         /**
838          * The supported REST request types.
839          */
840         public enum RequestType {
841             GET, PUT, POST, DELETE, PATCH, HEAD
842         }
843     }
844
845     /*
846      * An entity to encapsulate an expected result and a potential failure cause when returning from a functional
847      * interface during the computeIfAbsent call.
848      */
849     private class InitializedClient {
850         private Client client;
851         private Throwable caughtException;
852
853         public InitializedClient() {
854             client = null;
855             caughtException = null;
856         }
857
858         public Client getClient() {
859             return client;
860         }
861
862         public void setClient(Client client) {
863             this.client = client;
864         }
865
866         public Throwable getCaughtException() {
867             return caughtException;
868         }
869
870         public void setCaughtException(Throwable caughtException) {
871             this.caughtException = caughtException;
872         }
873
874     }
875
876 }