aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/restclient/client/RestClient.java
blob: 9639c9d25b3125feb6bc4def2626e127e001dc09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.restclient.client;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.restclient.enums.RestAuthenticationMode;
import org.onap.aai.restclient.logging.RestClientMsgs;
import org.onap.aai.restclient.rest.RestClientBuilder;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;



/**
 * This class provides a general client implementation that micro services can use for communicating with the endpoints
 * via their exposed REST interfaces.
 * 
 */

public class RestClient {

    /**
     * This is a generic builder that is used for constructing the REST client that we will use to communicate with the
     * REST endpoint.
     */
    private RestClientBuilder clientBuilder;

    private final ConcurrentMap<String, InitializedClient> CLIENT_CACHE = new ConcurrentHashMap<>();
    private static final String REST_CLIENT_INSTANCE = "REST_CLIENT_INSTANCE";

    /** Standard logger for producing log statements. */
    private static EELFLogger logger = EELFManager.getLogger(RestClient.class.getName());

    /** Standard logger for producing metric statements. */
    private static EELFLogger metricsLogger = EELFManager.getMetricsLogger();
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

    /** Reusable function call for GET REST operations. */
    private final RestOperation getOp = new GetRestOperation();

    /** Reusable function call for PUT REST operations. */
    private final RestOperation putOp = new PutRestOperation();

    /** Reusable function call for POST REST operations. */
    private final RestOperation postOp = new PostRestOperation();

    /** Reusable function call for DELETE REST operations. */
    private final RestOperation deleteOp = new DeleteRestOperation();

    /** Reusable function call for HEAD REST operations. */
    private final RestOperation headOp = new HeadRestOperation();

    /** Reusable function call for PATCH REST operations. */
    private final RestOperation patchOp = new PatchRestOperation();

    /**
     * Creates a new instance of the {@link RestClient}.
     */
    public RestClient() {
        clientBuilder = new RestClientBuilder();
    }

    /**
     * Creates a new instance of the {@link RestClient} using the supplied {@link RestClientBuilder}.
     *
     * @param rcBuilder - The REST client builder that this instance of the {@link RestClient} should use.
     */
    public RestClient(RestClientBuilder rcBuilder) {
        clientBuilder = rcBuilder;
    }

    public RestClient authenticationMode(RestAuthenticationMode mode) {
        logger.debug("Set rest authentication mode= " + mode);
        clientBuilder.setAuthenticationMode(mode);
        return this;
    }

    public RestClient basicAuthUsername(String username) {
        logger.debug("Set SSL BasicAuth username = " + username);
        clientBuilder.setBasicAuthUsername(username);
        return this;
    }

    public RestClient basicAuthPassword(String password) {
        /*
         * purposely not logging out the password, I guess we could obfuscate it if we really want to see it in the logs
         */
        clientBuilder.setBasicAuthPassword(password);
        return this;
    }

    /**
     * Sets the flag to indicate whether or not validation should be performed against the host name of the server we
     * are trying to communicate with.
     *
     * @parameter validate - Set to true to enable validation, false to disable
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient validateServerHostname(boolean validate) {
        logger.debug("Set validate server hostname = " + validate);
        clientBuilder.setValidateServerHostname(validate);
        return this;
    }

    /**
     * Sets the flag to indicate whether or not validation should be performed against the certificate chain.
     *
     * @parameter validate - Set to true to enable validation, false to disable.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient validateServerCertChain(boolean validate) {
        logger.debug("Set validate server certificate chain = " + validate);
        clientBuilder.setValidateServerCertChain(validate);
        return this;
    }

    /**
     * Assigns the client certificate file to use.
     *
     * @param filename - The name of the certificate file.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient clientCertFile(String filename) {
        logger.debug("Set client certificate filename = " + filename);
        clientBuilder.setClientCertFileName(filename);
        return this;
    }

    /**
     * Assigns the client certificate password to use.
     *
     * @param password - The certificate password.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient clientCertPassword(String password) {
        clientBuilder.setClientCertPassword(password);
        return this;
    }

    /**
     * Assigns the name of the trust store file to use.
     *
     * @param filename - the name of the trust store file.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient trustStore(String filename) {
        logger.debug("Set trust store filename = " + filename);
        clientBuilder.setTruststoreFilename(filename);
        return this;
    }

    /**
     * Assigns the connection timeout (in ms) to use when connecting to the target server.
     *
     * @param timeout - The length of time to wait in ms before timing out.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient connectTimeoutMs(int timeout) {
        logger.debug("Set connection timeout = " + timeout + " ms");
        clientBuilder.setConnectTimeoutInMs(timeout);
        return this;
    }

    /**
     * Assigns the read timeout (in ms) to use when communicating with the target server.
     *
     * @param timeout The read timeout in milliseconds.
     *
     * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
     */
    public RestClient readTimeoutMs(int timeout) {
        logger.debug("Set read timeout = " + timeout + " ms");
        clientBuilder.setReadTimeoutInMs(timeout);
        return this;
    }

    /**
     * Configures the client for a specific SSL protocol
     *
     * @param sslProtocol - protocol string constant such as TLS, TLSv1, TLSv1.1, TLSv1.2
     *
     * @return The AAIRESTClient instance.
     */
    public RestClient sslProtocol(String sslProtocol) {
        logger.debug("Set sslProtocol = " + sslProtocol);
        clientBuilder.setSslProtocol(sslProtocol);
        return this;
    }

    private boolean shouldRetry(OperationResult operationResult) {

        if (operationResult == null) {
            return true;
        }

        int resultCode = operationResult.getResultCode();

        if (resultCode == 200) {
            return false;
        }

        if (resultCode == 404) {
            return false;
        }

        return true;

    }

    /**
     * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL. This
     * variant of the method will perform a requested number of retries in the event that the first request is
     * unsuccessful.
     *
     * @param operation - the REST operation type to send to the url
     * @param url - The REST endpoint to submit the REST request to.
     * @param payload - They payload to provide in the REST request, if applicable
     * @param headers - The headers that should be passed in the request
     * @param contentType - The content type of the payload
     * @param responseType - The expected format of the response.
     * 
     * @return The result of the REST request.
     */
    protected OperationResult processRequest(RestOperation operation, String url, String payload,
            Map<String, List<String>> headers, MediaType contentType, MediaType responseType, int numRetries) {


        OperationResult result = null;

        long startTimeInMs = System.currentTimeMillis();
        for (int retryCount = 0; retryCount < numRetries; retryCount++) {

            logger.info(RestClientMsgs.HTTP_REQUEST_WITH_RETRIES, operation.getRequestType().toString(), url,
                    Integer.toString(retryCount + 1));

            // Submit our query to the AAI.
            result = processRequest(operation, url, payload, headers, contentType, responseType);

            // If the submission was successful then we're done.

            if (!shouldRetry(result)) {

                logger.info(RestClientMsgs.HTTP_REQUEST_TIME_WITH_RETRIES, operation.getRequestType().toString(), url,
                        Long.toString(System.currentTimeMillis() - startTimeInMs), Integer.toString(retryCount));

                result.setNumRetries(retryCount);

                return result;
            }

            // Our submission was unsuccessful...
            try {
                // Sleep between re-tries to be nice to the target system.
                Thread.sleep(50);

            } catch (InterruptedException e) {
                logger.error(RestClientMsgs.HTTP_REQUEST_INTERRUPTED, url, e.getLocalizedMessage());
                Thread.currentThread().interrupt();
                break;
            }
        }

        // If we've gotten this far, then we failed all of our retries.
        if (result == null) {
            result = new OperationResult();
        }

        result.setNumRetries(numRetries);
        result.setResultCode(504);
        result.setFailureCause("Failed to get a successful result after multiple retries to target server.");


        return result;
    }

    /**
     * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL.
     *
     * @param operation - the REST operation type to send to the url
     * @param url - The REST endpoint to submit the REST request to.
     * @param payload - They payload to provide in the REST request, if applicable
     * @param headers - The headers that should be passed in the request
     * @param contentType - The content type of the payload
     * @param responseType - The expected format of the response.
     *
     * @return The result of the REST request.
     */
    protected OperationResult processRequest(RestOperation operation, String url, String payload,
            Map<String, List<String>> headers, MediaType contentType, MediaType responseType) {

        Response clientResponse = null;
        OperationResult operationResult = new OperationResult();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String requestType = operation.getRequestType().name();

        // Grab the current time so that we can log how long the
        // query took once we are done.
        long startTimeInMs = System.currentTimeMillis();
        MdcOverride override = new MdcOverride();
        override.addAttribute(MdcContext.MDC_START_TIME, formatter.format(startTimeInMs));

        logger.info(RestClientMsgs.HTTP_REQUEST, requestType, url);

        try {

            // Get a REST client instance for our request.
            Client client = getClient();

            // Debug log the request
            debugRequest(url, payload, headers, responseType);

            // Get a client request builder, and submit our GET request.
            Builder builder = getClientBuilder(client, url, headers, responseType);
            clientResponse = operation.processOperation(builder, payload, contentType);

            populateOperationResult(clientResponse, operationResult);

            // Debug log the response
            if (clientResponse != null) {
                debugResponse(operationResult, clientResponse.getHeaders());
            }

        } catch (Exception ex) {

            logger.error(RestClientMsgs.HTTP_REQUEST_ERROR, requestType, url, ex.getLocalizedMessage());
            operationResult.setResultCode(500);
            operationResult
                    .setFailureCause("Error during GET operation to AAI with message = " + ex.getLocalizedMessage());

        } finally {

            if (logger.isDebugEnabled()) {
                logger.debug(baos.toString());
            }

            // Not every valid response code is actually represented by the Response.Status
            // object, so we need to guard against missing codes, otherwise we throw null
            // pointer exceptions when we try to generate our metrics logs...
            Response.Status responseStatus = Response.Status.fromStatusCode(operationResult.getResultCode());
            String responseStatusCodeString = "";
            if (responseStatus != null) {
                responseStatusCodeString = responseStatus.toString();
            }
            Map<String, String> logFields = new HashMap<String, String>();
            logFields.put(LogLine.DefinedFields.STATUS_CODE.name(), responseStatusCodeString);
            logFields.put(LogLine.DefinedFields.RESPONSE_CODE.name(), String.valueOf(operationResult.getResultCode()));
            logFields.put(LogLine.DefinedFields.RESPONSE_DESCRIPTION.name(), operationResult.getResult());
            metricsLogger.info(RestClientMsgs.HTTP_REQUEST_TIME, logFields.toString(), requestType, Long.toString(System.currentTimeMillis() - startTimeInMs), url);
            logger.info(RestClientMsgs.HTTP_REQUEST_TIME, requestType,
                    Long.toString(System.currentTimeMillis() - startTimeInMs), url);
            logger.info(RestClientMsgs.HTTP_RESPONSE, url,
                    operationResult.getResultCode() + " " + responseStatusCodeString);
        }

        return operationResult;
    }

    /**
     * This method submits an HTTP PUT request against the supplied URL.
     *
     * @param url - The REST endpoint to submit the PUT request to.
     * @param payload - the payload to send to the supplied URL
     * @param headers - The headers that should be passed in the request
     * @param contentType - The content type of the payload
     * @param responseType - The expected format of the response.
     *
     * @return The result of the PUT request.
     */
    public OperationResult put(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
            MediaType responseType) {
        return processRequest(putOp, url, payload, headers, contentType, responseType);
    }

    /**
     * This method submits an HTTP POST request against the supplied URL.
     *
     * @param url - The REST endpoint to submit the POST request to.
     * @param payload - the payload to send to the supplied URL
     * @param headers - The headers that should be passed in the request
     * @param contentType - The content type of the payload
     * @param responseType - The expected format of the response.
     *
     * @return The result of the POST request.
     */
    public OperationResult post(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
            MediaType responseType) {
        return processRequest(postOp, url, payload, headers, contentType, responseType);
    }

    /**
     * This method submits an HTTP POST request against the supplied URL, and emulates a PATCH operation by setting a
     * special header value
     *
     * @param url - The REST endpoint to submit the POST request to.
     * @param payload - the payload to send to the supplied URL
     * @param headers - The headers that should be passed in the request
     * @param contentType - The content type of the payload
     * @param responseType - The expected format of the response.
     *
     * @return The result of the PATCH request.
     */
    public OperationResult patch(String url, String payload, Map<String, List<String>> headers, MediaType contentType,
            MediaType responseType) {
        return processRequest(patchOp, url, payload, headers, contentType, responseType);
    }

    /**
     * This method submits an HTTP HEAD request against the supplied URL
     *
     * @param url - The REST endpoint to submit the POST request to.
     * @param headers - The headers that should be passed in the request
     * @param responseType - The expected format of the response.
     *
     * @return The result of the HEAD request.
     */
    public OperationResult head(String url, Map<String, List<String>> headers, MediaType responseType) {
        return processRequest(headOp, url, null, headers, null, responseType);
    }

    /**
     * This method submits an HTTP GET request against the supplied URL.
     *
     * @param url - The REST endpoint to submit the GET request to.
     * @param headers - The headers that should be passed in the request
     * @param responseType - The expected format of the response.
     *
     * @return The result of the GET request.
     */
    public OperationResult get(String url, Map<String, List<String>> headers, MediaType responseType) {
        return processRequest(getOp, url, null, headers, null, responseType);
    }

    /**
     * This method submits an HTTP GET request against the supplied URL. This variant of the method will perform a
     * requested number of retries in the event that the first request is unsuccessful.
     * 
     * @param url - The REST endpoint to submit the GET request to.
     * @param headers - The headers that should be passed in the request
     * @param responseType - The expected format of the response.
     * @param numRetries - The number of times to try resubmitting the request in the event of a failure.
     * 
     * @return The result of the GET request.
     */
    public OperationResult get(String url, Map<String, List<String>> headers, MediaType responseType, int numRetries) {
        return processRequest(getOp, url, null, headers, null, responseType, numRetries);
    }

    /**
     * This method submits an HTTP DELETE request against the supplied URL.
     *
     * @param url - The REST endpoint to submit the DELETE request to.
     * @param headers - The headers that should be passed in the request
     * @param responseType - The expected format of the response.
     *
     * @return The result of the DELETE request.
     */
    public OperationResult delete(String url, Map<String, List<String>> headers, MediaType responseType) {
        return processRequest(deleteOp, url, null, headers, null, responseType);
    }

    /**
     * This method does a health check ("ping") against the supplied URL.
     *
     * @param url - The REST endpoint to attempt a health check.
     * @param srcAppName - The name of the application using this client.
     * @param destAppName - The name of the destination app.
     *
     * @return A boolean value. True if connection attempt was successful, false otherwise.
     *
     */
    public boolean healthCheck(String url, String srcAppName, String destAppName) {
        return healthCheck(url, srcAppName, destAppName, MediaType.TEXT_PLAIN_TYPE);

    }

    /**
     * This method does a health check ("ping") against the supplied URL.
     *
     * @param url - The REST endpoint to attempt a health check.
     * @param srcAppName - The name of the application using this client.
     * @param destAppName - The name of the destination app.
     * @param responseType - The response type.
     *
     * @return A boolean value. True if connection attempt was successful, false otherwise.
     *
     */
    public boolean healthCheck(String url, String srcAppName, String destAppName, MediaType responseType) {
        Map<String, List<String>> headers = new HashMap<>();
        headers.put(Headers.FROM_APP_ID, Arrays.asList(new String[] {srcAppName}));
        headers.put(Headers.TRANSACTION_ID, Arrays.asList(new String[] {UUID.randomUUID().toString()}));

        try {
            logger.info(RestClientMsgs.HEALTH_CHECK_ATTEMPT, destAppName, url);
            OperationResult result = get(url, headers, responseType);

            if (result != null && result.getFailureCause() == null) {
                logger.info(RestClientMsgs.HEALTH_CHECK_SUCCESS, destAppName, url);
                return true;
            } else {
                logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url,
                        result != null ? result.getFailureCause() : null);
                return false;
            }
        } catch (Exception e) {
            logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url, e.getMessage());
            return false;
        }
    }

    /**
     * This method constructs a client request builder that can be used for submitting REST requests to the supplied URL
     * endpoint.
     *
     * @param client - The REST client we will be using to talk to the server.
     * @param url - The URL endpoint that our request will be submitted to.
     * @param headers - The headers that should be passed in the request
     * @param responseType - The expected format of the response.
     *
     * @return A client request builder.
     */
    private Builder getClientBuilder(Client client, String url, Map<String, List<String>> headers,
            MediaType responseType) {

        WebTarget target = client.target(url);

        Builder builder = target.request().accept(responseType);

        if (headers != null) {
            for (Entry<String, List<String>> header : headers.entrySet()) {
                builder.header(header.getKey(), String.join(";", header.getValue()));
            }

            // Added additional check to prevent adding duplicate authorization header if client is already sending the
            // authorization header
            // AAI-1097 - For AAI calls when Rest authentication mode is selected as SSL_BASIC getting 403 error
            if (clientBuilder.getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC
                    && headers.get(Headers.AUTHORIZATION) == null) {
                builder = builder.header(Headers.AUTHORIZATION, clientBuilder.getBasicAuthenticationCredentials());
            }

        }

        return builder;
    }

    private void debugRequest(String url, String payload, Map<String, List<String>> headers, MediaType responseType) {
        if (!logger.isDebugEnabled()) {
            return;
        }

        StringBuilder debugRequest = new StringBuilder("REQUEST:\n");
        debugRequest.append("URL: ").append(url).append("\n");
        debugRequest.append("Payload: ").append(payload).append("\n");
        debugRequest.append("Response Type: ").append(responseType).append("\n");

        if (headers == null) {
            logger.debug(debugRequest.toString());
            return;
        }

        debugRequest.append("Headers: ");
        for (Entry<String, List<String>> header : headers.entrySet()) {
            debugRequest.append("\n\t").append(header.getKey()).append(":");
            for (String headerEntry : header.getValue()) {
                debugRequest.append("\"").append(headerEntry).append("\" ");
            }
        }

        logger.debug(debugRequest.toString());

    }

    private void debugResponse(OperationResult operationResult, MultivaluedMap<String, Object> headers) {

        if (!logger.isDebugEnabled()) {
            return;
        }

        StringBuilder debugResponse = new StringBuilder("RESPONSE:\n");
        debugResponse.append("Result: ").append(operationResult.getResultCode()).append("\n");
        debugResponse.append("Failure Cause: ").append(operationResult.getFailureCause()).append("\n");
        debugResponse.append("Payload: ").append(operationResult.getResult()).append("\n");

        if (headers == null) {
            logger.debug(debugResponse.toString());
            return;
        }

        debugResponse.append("Headers: ");
        for (Entry<String, List<Object>> header : headers.entrySet()) {
            debugResponse.append("\n\t").append(header.getKey()).append(":");
            for (Object headerEntry : header.getValue()) {
                debugResponse.append("\"").append(headerEntry).append("\" ");
            }
        }

        logger.debug(debugResponse.toString());
    }

    /**
     * This method creates an instance of the low level REST client to use for communicating with the AAI, if one has
     * not already been created, otherwise it returns the already created instance.
     *
     * @return A {@link Client} instance.
     */
    protected Client getClient() throws Exception {

        /*
         * Attempting a new way of doing non-blocking thread-safe lazy-initialization by using Java 1.8 computeIfAbsent
         * functionality. A null value will not be stored, but once a valid mapping has been established, then the same
         * value will be returned.
         * 
         * One awkwardness of the computeIfAbsent is the lack of support for thrown exceptions, which required a bit of
         * hoop jumping to preserve the original exception for the purpose of maintaining the pre-existing this API
         * signature.
         */

        final InitializedClient clientInstance =
                CLIENT_CACHE.computeIfAbsent(REST_CLIENT_INSTANCE, k -> loggedClientInitialization());

        if (clientInstance.getCaughtException() != null) {
            throw new InstantiationException(clientInstance.getCaughtException().getMessage());
        }

        return clientInstance.getClient();

    }

    /**
     * This method will only be called if computerIfAbsent is true. The return value is null, then the result is not
     * stored in the map.
     * 
     * @return a new client instance or null
     */
    private InitializedClient loggedClientInitialization() {

        if (logger.isDebugEnabled()) {
            logger.debug("Instantiating REST client with following parameters:");
            logger.debug(clientBuilder.toString());
        }

        InitializedClient initClient = new InitializedClient();

        try {
            initClient.setClient(clientBuilder.getClient());
        } catch (Exception error) {
            initClient.setCaughtException(error);
        }

        return initClient;

    }


    /**
     * This method populates the fields of an {@link OperationResult} instance based on the contents of a
     * {@link Response} received in response to a REST request.
     */
    private void populateOperationResult(Response response, OperationResult opResult) {

        // If we got back a NULL response, then just produce a generic
        // error code and result indicating this.
        if (response == null) {
            opResult.setResultCode(500);
            opResult.setFailureCause("Client response was null");
            return;
        }

        int statusCode = response.getStatus();
        opResult.setResultCode(statusCode);

        if (opResult.wasSuccessful()) {
            if (statusCode != Response.Status.NO_CONTENT.getStatusCode()) {
                opResult.setResult(response.readEntity(String.class));
            }
        } else {
            opResult.setFailureCause(response.readEntity(String.class));
        }

        opResult.setHeaders(convertHeaderObjectsToString(response.getHeaders()));
    }

    private MultivaluedMap<String, String> convertHeaderObjectsToString(MultivaluedMap<String, Object> headers) {
        MultivaluedMap<String, String> result = new MultivaluedHashMap<>();
        headers.forEach((k, v) -> result.addAll(k, v.stream().map(Object::toString).collect(Collectors.toList())));
        return result;
    }

    private class GetRestOperation implements RestOperation {
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            return builder.get();
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.GET;
        }
    }

    private class PutRestOperation implements RestOperation {
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            return builder.put(Entity.entity(payload, contentType));
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.PUT;
        }
    }

    private class PostRestOperation implements RestOperation {
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            return builder.post(Entity.entity(payload, contentType));
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.POST;
        }
    }

    private class DeleteRestOperation implements RestOperation {
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            return builder.delete();
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.DELETE;
        }
    }

    private class HeadRestOperation implements RestOperation {
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            return builder.head();
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.HEAD;
        }
    }

    private class PatchRestOperation implements RestOperation {

        /**
         * Technically there is no standarized PATCH operation for the jersey client, but we can use the method-override
         * approach instead.
         */
        @Override
        public Response processOperation(Builder builder, String payload, MediaType contentType) {
            builder = builder.header("X-HTTP-Method-Override", "PATCH");
            return builder.post(Entity.entity(payload, contentType));
        }

        @Override
        public RequestType getRequestType() {
            return RequestType.PATCH;
        }
    }


    /**
     * Interface used wrap a Jersey REST call using a functional interface.
     */
    private interface RestOperation {

        /**
         * Method used to wrap the functionality of making a REST call out to the endpoint.
         * 
         * @param builder the Jersey builder used to make the request
         * @param payload the request payload
         * @param contentType the content type of the payload
         * @return the response from the REST endpoint
         */
        public Response processOperation(Builder builder, String payload, MediaType contentType);

        /**
         * Returns the REST request type.
         */
        public RequestType getRequestType();

        /**
         * The supported REST request types.
         */
        public enum RequestType {
            GET, PUT, POST, DELETE, PATCH, HEAD
        }
    }

    /*
     * An entity to encapsulate an expected result and a potential failure cause when returning from a functional
     * interface during the computeIfAbsent call.
     */
    private class InitializedClient {
        private Client client;
        private Throwable caughtException;

        public InitializedClient() {
            client = null;
            caughtException = null;
        }

        public Client getClient() {
            return client;
        }

        public void setClient(Client client) {
            this.client = client;
        }

        public Throwable getCaughtException() {
            return caughtException;
        }

        public void setCaughtException(Throwable caughtException) {
            this.caughtException = caughtException;
        }

    }

}