2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.restclient.client;
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;
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;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.client.Invocation.Builder;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.MultivaluedMap;
42 import javax.ws.rs.core.Response;
44 import org.onap.aai.cl.api.LogLine;
45 import org.onap.aai.cl.mdc.MdcContext;
46 import org.onap.aai.cl.mdc.MdcOverride;
47 import org.onap.aai.restclient.enums.RestAuthenticationMode;
48 import org.onap.aai.restclient.logging.RestClientMsgs;
49 import org.onap.aai.restclient.rest.RestClientBuilder;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
57 * This class provides a general client implementation that micro services can use for communicating with the endpoints
58 * via their exposed REST interfaces.
62 public class RestClient {
65 * This is a generic builder that is used for constructing the REST client that we will use to communicate with the
68 private RestClientBuilder clientBuilder;
70 private final ConcurrentMap<String, InitializedClient> CLIENT_CACHE = new ConcurrentHashMap<>();
71 private static final String REST_CLIENT_INSTANCE = "REST_CLIENT_INSTANCE";
73 /** Standard logger for producing log statements. */
74 private static EELFLogger logger = EELFManager.getLogger(RestClient.class.getName());
76 /** Standard logger for producing metric statements. */
77 private static EELFLogger metricsLogger = EELFManager.getMetricsLogger();
78 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
80 /** Reusable function call for GET REST operations. */
81 private final RestOperation getOp = new GetRestOperation();
83 /** Reusable function call for PUT REST operations. */
84 private final RestOperation putOp = new PutRestOperation();
86 /** Reusable function call for POST REST operations. */
87 private final RestOperation postOp = new PostRestOperation();
89 /** Reusable function call for DELETE REST operations. */
90 private final RestOperation deleteOp = new DeleteRestOperation();
92 /** Reusable function call for HEAD REST operations. */
93 private final RestOperation headOp = new HeadRestOperation();
95 /** Reusable function call for PATCH REST operations. */
96 private final RestOperation patchOp = new PatchRestOperation();
99 * Creates a new instance of the {@link RestClient}.
101 public RestClient() {
102 clientBuilder = new RestClientBuilder();
106 * Creates a new instance of the {@link RestClient} using the supplied {@link RestClientBuilder}.
108 * @param rcBuilder - The REST client builder that this instance of the {@link RestClient} should use.
110 public RestClient(RestClientBuilder rcBuilder) {
111 clientBuilder = rcBuilder;
114 public RestClient authenticationMode(RestAuthenticationMode mode) {
115 logger.debug("Set rest authentication mode= " + mode);
116 clientBuilder.setAuthenticationMode(mode);
120 public RestClient basicAuthUsername(String username) {
121 logger.debug("Set SSL BasicAuth username = " + username);
122 clientBuilder.setBasicAuthUsername(username);
126 public RestClient basicAuthPassword(String password) {
128 * purposely not logging out the password, I guess we could obfuscate it if we really want to see it in the logs
130 clientBuilder.setBasicAuthPassword(password);
135 * Sets the flag to indicate whether or not validation should be performed against the host name of the server we
136 * are trying to communicate with.
138 * @parameter validate - Set to true to enable validation, false to disable
140 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
142 public RestClient validateServerHostname(boolean validate) {
143 logger.debug("Set validate server hostname = " + validate);
144 clientBuilder.setValidateServerHostname(validate);
149 * Sets the flag to indicate whether or not validation should be performed against the certificate chain.
151 * @parameter validate - Set to true to enable validation, false to disable.
153 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
155 public RestClient validateServerCertChain(boolean validate) {
156 logger.debug("Set validate server certificate chain = " + validate);
157 clientBuilder.setValidateServerCertChain(validate);
162 * Assigns the client certificate file to use.
164 * @param filename - The name of the certificate file.
166 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
168 public RestClient clientCertFile(String filename) {
169 logger.debug("Set client certificate filename = " + filename);
170 clientBuilder.setClientCertFileName(filename);
175 * Assigns the client certificate password to use.
177 * @param password - The certificate password.
179 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
181 public RestClient clientCertPassword(String password) {
182 clientBuilder.setClientCertPassword(password);
187 * Assigns the name of the trust store file to use.
189 * @param filename - the name of the trust store file.
191 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
193 public RestClient trustStore(String filename) {
194 logger.debug("Set trust store filename = " + filename);
195 clientBuilder.setTruststoreFilename(filename);
200 * Assigns the connection timeout (in ms) to use when connecting to the target server.
202 * @param timeout - The length of time to wait in ms before timing out.
204 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
206 public RestClient connectTimeoutMs(int timeout) {
207 logger.debug("Set connection timeout = " + timeout + " ms");
208 clientBuilder.setConnectTimeoutInMs(timeout);
213 * Assigns the read timeout (in ms) to use when communicating with the target server.
215 * @param timeout The read timeout in milliseconds.
217 * @return The AAIRESTClient instance. This is useful for chaining parameter assignments.
219 public RestClient readTimeoutMs(int timeout) {
220 logger.debug("Set read timeout = " + timeout + " ms");
221 clientBuilder.setReadTimeoutInMs(timeout);
226 * Configures the client for a specific SSL protocol
228 * @param sslProtocol - protocol string constant such as TLS, TLSv1, TLSv1.1, TLSv1.2
230 * @return The AAIRESTClient instance.
232 public RestClient sslProtocol(String sslProtocol) {
233 logger.debug("Set sslProtocol = " + sslProtocol);
234 clientBuilder.setSslProtocol(sslProtocol);
238 private boolean shouldRetry(OperationResult operationResult) {
240 if (operationResult == null) {
244 int resultCode = operationResult.getResultCode();
246 if (resultCode == 200) {
250 if (resultCode == 404) {
259 * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL. This
260 * variant of the method will perform a requested number of retries in the event that the first request is
263 * @param operation - the REST operation type to send to the url
264 * @param url - The REST endpoint to submit the REST request to.
265 * @param payload - They payload to provide in the REST request, if applicable
266 * @param headers - The headers that should be passed in the request
267 * @param contentType - The content type of the payload
268 * @param responseType - The expected format of the response.
270 * @return The result of the REST request.
272 protected OperationResult processRequest(RestOperation operation, String url, String payload,
273 Map<String, List<String>> headers, MediaType contentType, MediaType responseType, int numRetries) {
276 OperationResult result = null;
278 long startTimeInMs = System.currentTimeMillis();
279 for (int retryCount = 0; retryCount < numRetries; retryCount++) {
281 logger.info(RestClientMsgs.HTTP_REQUEST_WITH_RETRIES, operation.getRequestType().toString(), url,
282 Integer.toString(retryCount + 1));
284 // Submit our query to the AAI.
285 result = processRequest(operation, url, payload, headers, contentType, responseType);
287 // If the submission was successful then we're done.
289 if (!shouldRetry(result)) {
291 logger.info(RestClientMsgs.HTTP_REQUEST_TIME_WITH_RETRIES, operation.getRequestType().toString(), url,
292 Long.toString(System.currentTimeMillis() - startTimeInMs), Integer.toString(retryCount));
294 result.setNumRetries(retryCount);
299 // Our submission was unsuccessful...
301 // Sleep between re-tries to be nice to the target system.
304 } catch (InterruptedException e) {
305 logger.error(RestClientMsgs.HTTP_REQUEST_INTERRUPTED, url, e.getLocalizedMessage());
306 Thread.currentThread().interrupt();
311 // If we've gotten this far, then we failed all of our retries.
312 if (result == null) {
313 result = new OperationResult();
316 result.setNumRetries(numRetries);
317 result.setResultCode(504);
318 result.setFailureCause("Failed to get a successful result after multiple retries to target server.");
325 * This method operates on a REST endpoint by submitting an HTTP operation request against the supplied URL.
327 * @param operation - the REST operation type to send to the url
328 * @param url - The REST endpoint to submit the REST request to.
329 * @param payload - They payload to provide in the REST request, if applicable
330 * @param headers - The headers that should be passed in the request
331 * @param contentType - The content type of the payload
332 * @param responseType - The expected format of the response.
334 * @return The result of the REST request.
336 protected OperationResult processRequest(RestOperation operation, String url, String payload,
337 Map<String, List<String>> headers, MediaType contentType, MediaType responseType) {
339 Response clientResponse = null;
340 OperationResult operationResult = new OperationResult();
341 ByteArrayOutputStream baos = new ByteArrayOutputStream();
343 String requestType = operation.getRequestType().name();
345 // Grab the current time so that we can log how long the
346 // query took once we are done.
347 long startTimeInMs = System.currentTimeMillis();
348 MdcOverride override = new MdcOverride();
349 override.addAttribute(MdcContext.MDC_START_TIME, formatter.format(startTimeInMs));
351 logger.info(RestClientMsgs.HTTP_REQUEST, requestType, url);
355 // Get a REST client instance for our request.
356 Client client = getClient();
358 // Debug log the request
359 debugRequest(url, payload, headers, responseType);
361 // Get a client request builder, and submit our GET request.
362 Builder builder = getClientBuilder(client, url, headers, responseType);
363 clientResponse = operation.processOperation(builder, payload, contentType);
365 populateOperationResult(clientResponse, operationResult);
367 // Debug log the response
368 if (clientResponse != null) {
369 debugResponse(operationResult, clientResponse.getHeaders());
372 } catch (Exception ex) {
374 logger.error(RestClientMsgs.HTTP_REQUEST_ERROR, requestType, url, ex.getLocalizedMessage());
375 operationResult.setResultCode(500);
377 .setFailureCause("Error during GET operation to AAI with message = " + ex.getLocalizedMessage());
381 if (logger.isDebugEnabled()) {
382 logger.debug(baos.toString());
385 // Not every valid response code is actually represented by the Response.Status
386 // object, so we need to guard against missing codes, otherwise we throw null
387 // pointer exceptions when we try to generate our metrics logs...
388 Response.Status responseStatus = Response.Status.fromStatusCode(operationResult.getResultCode());
389 String responseStatusCodeString = "";
390 if (responseStatus != null) {
391 responseStatusCodeString = responseStatus.toString();
393 Map<String, String> logFields = new HashMap<String, String>();
394 logFields.put(LogLine.DefinedFields.STATUS_CODE.name(), responseStatusCodeString);
395 logFields.put(LogLine.DefinedFields.RESPONSE_CODE.name(), String.valueOf(operationResult.getResultCode()));
396 logFields.put(LogLine.DefinedFields.RESPONSE_DESCRIPTION.name(), operationResult.getResult());
397 metricsLogger.info(RestClientMsgs.HTTP_REQUEST_TIME, logFields.toString(), 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);
404 return operationResult;
408 * This method submits an HTTP PUT request against the supplied URL.
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.
416 * @return The result of the PUT request.
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);
424 * This method submits an HTTP POST request against the supplied URL.
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.
432 * @return The result of the POST request.
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);
440 * This method submits an HTTP POST request against the supplied URL, and emulates a PATCH operation by setting a
441 * special header value
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.
449 * @return The result of the PATCH request.
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);
457 * This method submits an HTTP HEAD request against the supplied URL
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.
463 * @return The result of the HEAD request.
465 public OperationResult head(String url, Map<String, List<String>> headers, MediaType responseType) {
466 return processRequest(headOp, url, null, headers, null, responseType);
470 * This method submits an HTTP GET request against the supplied URL.
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.
476 * @return The result of the GET request.
478 public OperationResult get(String url, Map<String, List<String>> headers, MediaType responseType) {
479 return processRequest(getOp, url, null, headers, null, responseType);
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.
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.
491 * @return The result of the GET request.
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);
498 * This method submits an HTTP DELETE request against the supplied URL.
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.
504 * @return The result of the DELETE request.
506 public OperationResult delete(String url, Map<String, List<String>> headers, MediaType responseType) {
507 return processRequest(deleteOp, url, null, headers, null, responseType);
511 * This method does a health check ("ping") against the supplied URL.
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.
517 * @return A boolean value. True if connection attempt was successful, false otherwise.
520 public boolean healthCheck(String url, String srcAppName, String destAppName) {
521 return healthCheck(url, srcAppName, destAppName, MediaType.TEXT_PLAIN_TYPE);
526 * This method does a health check ("ping") against the supplied URL.
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.
533 * @return A boolean value. True if connection attempt was successful, false otherwise.
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()}));
542 logger.info(RestClientMsgs.HEALTH_CHECK_ATTEMPT, destAppName, url);
543 OperationResult result = get(url, headers, responseType);
545 if (result != null && result.getFailureCause() == null) {
546 logger.info(RestClientMsgs.HEALTH_CHECK_SUCCESS, destAppName, url);
549 logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url,
550 result != null ? result.getFailureCause() : null);
553 } catch (Exception e) {
554 logger.error(RestClientMsgs.HEALTH_CHECK_FAILURE, destAppName, url, e.getMessage());
560 * This method constructs a client request builder that can be used for submitting REST requests to the supplied URL
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.
568 * @return A client request builder.
570 private Builder getClientBuilder(Client client, String url, Map<String, List<String>> headers,
571 MediaType responseType) {
573 WebTarget target = client.target(url);
575 Builder builder = target.request().accept(responseType);
577 if (headers != null) {
578 for (Entry<String, List<String>> header : headers.entrySet()) {
579 builder.header(header.getKey(), String.join(";", header.getValue()));
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());
595 private void debugRequest(String url, String payload, Map<String, List<String>> headers, MediaType responseType) {
596 if (!logger.isDebugEnabled()) {
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");
605 if (headers == null) {
606 logger.debug(debugRequest.toString());
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("\" ");
618 logger.debug(debugRequest.toString());
622 private void debugResponse(OperationResult operationResult, MultivaluedMap<String, Object> headers) {
624 if (!logger.isDebugEnabled()) {
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");
633 if (headers == null) {
634 logger.debug(debugResponse.toString());
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("\" ");
646 logger.debug(debugResponse.toString());
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.
653 * @return A {@link Client} instance.
655 protected Client getClient() throws Exception {
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.
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
667 final InitializedClient clientInstance =
668 CLIENT_CACHE.computeIfAbsent(REST_CLIENT_INSTANCE, k -> loggedClientInitialization());
670 if (clientInstance.getCaughtException() != null) {
671 throw new InstantiationException(clientInstance.getCaughtException().getMessage());
674 return clientInstance.getClient();
679 * This method will only be called if computerIfAbsent is true. The return value is null, then the result is not
682 * @return a new client instance or null
684 private InitializedClient loggedClientInitialization() {
686 if (logger.isDebugEnabled()) {
687 logger.debug("Instantiating REST client with following parameters:");
688 logger.debug(clientBuilder.toString());
691 InitializedClient initClient = new InitializedClient();
694 initClient.setClient(clientBuilder.getClient());
695 } catch (Exception error) {
696 initClient.setCaughtException(error);
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.
708 private void populateOperationResult(Response response, OperationResult opResult) {
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");
718 int statusCode = response.getStatus();
719 opResult.setResultCode(statusCode);
721 if (opResult.wasSuccessful()) {
722 if (statusCode != Response.Status.NO_CONTENT.getStatusCode()) {
723 opResult.setResult(response.readEntity(String.class));
726 opResult.setFailureCause(response.readEntity(String.class));
729 opResult.setHeaders(convertHeaderObjectsToString(response.getHeaders()));
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())));
738 private class GetRestOperation implements RestOperation {
740 public Response processOperation(Builder builder, String payload, MediaType contentType) {
741 return builder.get();
745 public RequestType getRequestType() {
746 return RequestType.GET;
750 private class PutRestOperation implements RestOperation {
752 public Response processOperation(Builder builder, String payload, MediaType contentType) {
753 return builder.put(Entity.entity(payload, contentType));
757 public RequestType getRequestType() {
758 return RequestType.PUT;
762 private class PostRestOperation implements RestOperation {
764 public Response processOperation(Builder builder, String payload, MediaType contentType) {
765 return builder.post(Entity.entity(payload, contentType));
769 public RequestType getRequestType() {
770 return RequestType.POST;
774 private class DeleteRestOperation implements RestOperation {
776 public Response processOperation(Builder builder, String payload, MediaType contentType) {
777 return builder.delete();
781 public RequestType getRequestType() {
782 return RequestType.DELETE;
786 private class HeadRestOperation implements RestOperation {
788 public Response processOperation(Builder builder, String payload, MediaType contentType) {
789 return builder.head();
793 public RequestType getRequestType() {
794 return RequestType.HEAD;
798 private class PatchRestOperation implements RestOperation {
801 * Technically there is no standarized PATCH operation for the jersey client, but we can use the method-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));
811 public RequestType getRequestType() {
812 return RequestType.PATCH;
818 * Interface used wrap a Jersey REST call using a functional interface.
820 private interface RestOperation {
823 * Method used to wrap the functionality of making a REST call out to the endpoint.
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
830 public Response processOperation(Builder builder, String payload, MediaType contentType);
833 * Returns the REST request type.
835 public RequestType getRequestType();
838 * The supported REST request types.
840 public enum RequestType {
841 GET, PUT, POST, DELETE, PATCH, HEAD
846 * An entity to encapsulate an expected result and a potential failure cause when returning from a functional
847 * interface during the computeIfAbsent call.
849 private class InitializedClient {
850 private Client client;
851 private Throwable caughtException;
853 public InitializedClient() {
855 caughtException = null;
858 public Client getClient() {
862 public void setClient(Client client) {
863 this.client = client;
866 public Throwable getCaughtException() {
867 return caughtException;
870 public void setCaughtException(Throwable caughtException) {
871 this.caughtException = caughtException;