From 44651d903d83f9256c324ec2a3c8886e91e1e433 Mon Sep 17 00:00:00 2001 From: vempo Date: Mon, 6 Aug 2018 16:21:01 +0300 Subject: Minor improvements to logging for servlets Fixed import order, more resilient statuses for Spring, cleaner code and tests, more convenient APIs. Change-Id: I6493c81d9c3c1c543c354562e074876268794438 Issue-ID: SDC-1580 Signed-off-by: vempo --- .../sdc/logging/servlet/CombinedTracker.java | 33 +++++++++++----------- .../openecomp/sdc/logging/servlet/HttpHeader.java | 2 +- .../servlet/jaxrs/LoggingRequestFilter.java | 10 +++++-- 3 files changed, 24 insertions(+), 21 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java index 2dd2c12143..5d940aa821 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/CombinedTracker.java @@ -17,39 +17,38 @@ package org.openecomp.sdc.logging.servlet; import javax.servlet.http.HttpServletRequest; -import org.openecomp.sdc.logging.api.Logger; /** - * Tracker for all the elements of ONAP logging and tracing at an entry point to an application - context and audit. - * The order of invocations is important, assuming the context must be kept as long as audit hasn't been finished. + * Tracker for all the elements of ONAP logging and tracing at an entry point to an application. + * The order of invocations is important, and on {@link #preRequest(HttpServletRequest)} it respects the order of + * trackers passed to the constructor. On {@link #postRequest(RequestProcessingResult)}, the invocation will be in the + * reverse order. * * @author evitaliy * @since 01 Aug 2018 */ public class CombinedTracker implements Tracker { - private final ContextTracker context; - private final AuditTracker audit; + private final Tracker[] trackers; - public CombinedTracker(Logger logger, HttpHeader partnerNameHeader, HttpHeader requestIdHeader) { - this.context = new ContextTracker(partnerNameHeader, requestIdHeader); - this.audit = new AuditTracker(logger); - } - - public CombinedTracker(Class resourceType, HttpHeader partnerNameHeader, HttpHeader requestIdHeader) { - this.context = new ContextTracker(partnerNameHeader, requestIdHeader); - this.audit = new AuditTracker(resourceType); + public CombinedTracker(Tracker... trackers) { + this.trackers = new Tracker[trackers.length]; + System.arraycopy(trackers, 0, this.trackers, 0, trackers.length); } @Override public void preRequest(HttpServletRequest request) { - this.context.preRequest(request); - this.audit.preRequest(request); + + for (Tracker t : trackers) { + t.preRequest(request); + } } @Override public void postRequest(RequestProcessingResult result) { - this.audit.postRequest(result); - this.context.postRequest(result); + + for (int i = trackers.length - 1; i > -1; i--) { + trackers[i].postRequest(result); + } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java index db10c2e4f3..dd5af859d4 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java @@ -43,7 +43,7 @@ public class HttpHeader { * * @param headerNames cannot be null or empty */ - public HttpHeader(String[] headerNames) { + public HttpHeader(String... headerNames) { if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).length < 1) { throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED); diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java index f846359b82..fb651495ac 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/jaxrs/LoggingRequestFilter.java @@ -27,7 +27,9 @@ import javax.ws.rs.core.Context; import javax.ws.rs.ext.Provider; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.logging.servlet.AuditTracker; import org.openecomp.sdc.logging.servlet.CombinedTracker; +import org.openecomp.sdc.logging.servlet.ContextTracker; import org.openecomp.sdc.logging.servlet.HttpHeader; import org.openecomp.sdc.logging.servlet.Tracker; @@ -63,8 +65,8 @@ public class LoggingRequestFilter implements ContainerRequestFilter { private HttpServletRequest httpRequest; - private HttpHeader requestIdHeader = new HttpHeader(new String[] {DEFAULT_REQUEST_ID_HEADER}); - private HttpHeader partnerNameHeader = new HttpHeader(new String[] {DEFAULT_PARTNER_NAME_HEADER}); + private HttpHeader requestIdHeader = new HttpHeader(DEFAULT_REQUEST_ID_HEADER); + private HttpHeader partnerNameHeader = new HttpHeader(DEFAULT_PARTNER_NAME_HEADER); private ResourceInfo resource; @@ -107,7 +109,9 @@ public class LoggingRequestFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { Class resourceClass = resource.getResourceMethod().getDeclaringClass(); - Tracker tracker = new CombinedTracker(resourceClass, partnerNameHeader, requestIdHeader); + Tracker tracker = new CombinedTracker( + new ContextTracker(partnerNameHeader, requestIdHeader), + new AuditTracker(resourceClass)); requestContext.setProperty(LOGGING_TRACKER_KEY, tracker); tracker.preRequest(httpRequest); } -- cgit 1.2.3-korg