From d4838465bd008b85ab3214b6431e6daa7d059589 Mon Sep 17 00:00:00 2001 From: Einat Vinouze Date: Thu, 12 Sep 2019 15:29:11 +0300 Subject: logging requests and responses in SyncRestClient Issue-ID: VID-611 Signed-off-by: Einat Vinouze Change-Id: I0e64e9520e566f9317f3fcbf0495b67349993ce4 --- .../java/org/onap/vid/client/SyncRestClient.java | 95 ++++++++++++++++++---- 1 file changed, 81 insertions(+), 14 deletions(-) (limited to 'vid-app-common/src/main') diff --git a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java index 18f87223c..f7087568d 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,6 +55,7 @@ import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.properties.VidProperties; import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; public class SyncRestClient implements SyncRestClientInterface { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SyncRestClient.class); @@ -89,68 +91,133 @@ public class SyncRestClient implements SyncRestClientInterface { @Override public HttpResponse post(String url, Map headers, Object body) { - return callWithRetryOverHttp(url, url2 -> restClient.post(url2).headers(headers).body(body).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, body); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.post(url2).headers(headers).body(body).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); + + return response; } @Override public HttpResponse post(String url, Map headers, Object body, Class responseClass) { - return callWithRetryOverHttp(url, - url2 -> restClient.post(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, body); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.post(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); + + return response; } @Override public HttpResponse get(String url, Map headers, Map routeParams) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asJson(); }); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); + + return response; } @Override public HttpResponse get(String url, Map headers, Map routeParams, Class responseClass) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asObject(responseClass); }); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); + + return response; } @Override public HttpResponse getStream(String url, Map headers, Map routeParams) { - return callWithRetryOverHttp(url, url2 -> { + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, routeParams); + + HttpResponse response = callWithRetryOverHttp(url, url2 -> { GetRequest getRequest = restClient.get(url2).headers(headers); routeParams.forEach(getRequest::routeParam); return getRequest.asBinary(); }); + + //no logging of the response since the response is too long + return response; + } @Override public HttpResponse put(String url, Map headers, Object body) { - return callWithRetryOverHttp(url, url2 -> restClient.put(url2).headers(headers).body(body).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, body); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.put(url2).headers(headers).body(body).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); + + return response; } @Override public HttpResponse put(String url, Map headers, Object body, Class responseClass) { - return callWithRetryOverHttp(url, - url2 -> restClient.put(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, body); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.put(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); + + return response; } @Override public HttpResponse delete(String url, Map headers, Object body, Class responseClass) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).body(body).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, body); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).body(body).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override public HttpResponse delete(String url, Map headers, Class responseClass) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).asObject(responseClass)); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).asObject(responseClass)); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override public HttpResponse delete(String url, Map headers) { - return callWithRetryOverHttp(url, url2 -> restClient.delete(url2).headers(headers).asJson()); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + + HttpResponse response = callWithRetryOverHttp(url, + url2 -> restClient.delete(url2).headers(headers).asJson()); + + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + + return response; } @Override @@ -171,7 +238,7 @@ public class SyncRestClient implements SyncRestClientInterface { return patched(httpRequest.apply(url)); } catch (RestClientException e) { if (causedBySslHandshakeError(e)) { - logger.warn(EELFLoggerDelegate.debugLogger, "SSL Handshake problem occured. Will try to retry over Http.", e); + logger.warn("SSL Handshake problem occured. Will try to retry over Http.", e); return patched(httpRequest.apply(url.replaceFirst(HTTPS_SCHEMA, HTTP_SCHEMA))); } throw e; @@ -218,7 +285,7 @@ public class SyncRestClient implements SyncRestClientInterface { return HttpClients.custom().setSSLSocketFactory(sslSf).build(); } catch (IOException | CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { - logger.warn(EELFLoggerDelegate.debugLogger, "Cannot initialize custom http client from current configuration. Using default one.", e); + logger.warn("Cannot initialize custom http client from current configuration. Using default one.", e); return HttpClients.createDefault(); } } -- cgit 1.2.3-korg