diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-08-06 16:21:01 +0300 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-08-06 16:09:04 +0000 |
commit | 44651d903d83f9256c324ec2a3c8886e91e1e433 (patch) | |
tree | 0e1961ce6c69dcb8eb18cf8b4e8af1e5eed6e3b4 /openecomp-be | |
parent | 1ecdbf74427467327de0b1e11cd0a839d6dc05bd (diff) |
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 <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be')
16 files changed, 198 insertions, 56 deletions
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 + * <b>reverse</b> 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); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java new file mode 100644 index 0000000000..4454493c01 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/CombinedTrackerTest.java @@ -0,0 +1,59 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.logging.servlet; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import javax.servlet.http.HttpServletRequest; +import org.junit.Test; + +/** + * Test the construction and invocation of combined tracker. + * + * @author evitaliy + * @since 01 Aug 2018 + */ +public class CombinedTrackerTest { + + @Test(expected = NullPointerException.class) + public void throwExceptionWhenTrackersNull() { + new CombinedTracker((Tracker[]) null); + } + + @Test + public void trackersCalledWhenPreRequest() { + Tracker firstTracker = mock(Tracker.class); + Tracker secondTracker = mock(Tracker.class); + CombinedTracker tracker = new CombinedTracker(firstTracker, secondTracker); + tracker.preRequest(mock(HttpServletRequest.class)); + verify(firstTracker, times(1)).preRequest(any(HttpServletRequest.class)); + verify(secondTracker, times(1)).preRequest(any(HttpServletRequest.class)); + } + + @Test + public void trackersCalledWhenPostRequest() { + Tracker firstTracker = mock(Tracker.class); + Tracker secondTracker = mock(Tracker.class); + CombinedTracker tracker = new CombinedTracker(firstTracker, secondTracker); + tracker.postRequest(mock(RequestProcessingResult.class)); + verify(firstTracker, times(1)).postRequest(any(RequestProcessingResult.class)); + verify(secondTracker, times(1)).postRequest(any(RequestProcessingResult.class)); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java index a247d119c8..59c690d763 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/ContextTrackerTest.java @@ -45,10 +45,10 @@ import org.powermock.modules.junit4.PowerMockRunner; public class ContextTrackerTest { private static final String X_REQUEST_ID = "X-REQUEST-ID"; - private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(new String[] {X_REQUEST_ID}); + private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(X_REQUEST_ID); private static final String X_PARTNER_NAME = "X-PARTNER-NAME"; - private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(new String[] {X_PARTNER_NAME}); + private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(X_PARTNER_NAME); @Test(expected = NullPointerException.class) public void throwExceptionWhenPartnerNamesNull() { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java index 5cbd0c0d76..2effa9fab1 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/servlet/HttpHeaderTest.java @@ -49,7 +49,7 @@ public class HttpHeaderTest { @Test(expected = IllegalArgumentException.class) public void throwExceptionWhenInputArrayEmpty() { - new HttpHeader(new String[0]); + new HttpHeader(); } @Test(expected = IllegalArgumentException.class) @@ -59,7 +59,7 @@ public class HttpHeaderTest { @Test public void valueNotReturnedWhenNameInArrayNotRequested() { - HttpHeader header = new HttpHeader(new String[] {"A"}); + HttpHeader header = new HttpHeader("A"); assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent()); } @@ -71,7 +71,7 @@ public class HttpHeaderTest { @Test public void valueReturnedWhenSinglePossibleHeaderInArrayMatches() { - HttpHeader header = new HttpHeader(new String[] {"B"}); + HttpHeader header = new HttpHeader("B"); assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent()); } @@ -83,7 +83,7 @@ public class HttpHeaderTest { @Test public void valueReturnedWhenLastHeaderInArrayMatches() throws Throwable { - HttpHeader header = new HttpHeader(new String[] {"A", "B"}); + HttpHeader header = new HttpHeader("A", "B"); header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED); } @@ -95,7 +95,7 @@ public class HttpHeaderTest { @Test public void valueReturnedWhenFirstHeaderInArrayMatches() throws Throwable { - HttpHeader header = new HttpHeader(new String[] {"B", "A"}); + HttpHeader header = new HttpHeader("B", "A"); header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED); } @@ -107,7 +107,7 @@ public class HttpHeaderTest { @Test public void valueReturnedWhenMiddleHeaderInArrayMatches() throws Throwable { - HttpHeader header = new HttpHeader(new String[] {"A", "B", "C"}); + HttpHeader header = new HttpHeader("A", "B", "C"); header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/DispatchingAppender.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/DispatchingAppender.java index b4abb8b368..fb0ceb97e2 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/DispatchingAppender.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/DispatchingAppender.java @@ -24,11 +24,10 @@ import ch.qos.logback.core.Appender; import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.joran.spi.DefaultClass; import ch.qos.logback.core.sift.Discriminator; -import org.slf4j.LoggerFactory; - import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.slf4j.LoggerFactory; /** * <p>Allows to use EELF logging configuration almost as is, by using a custom routing function, but pre-configured @@ -55,7 +54,7 @@ public class DispatchingAppender extends AppenderBase<ILoggingEvent> { // "magic" appender to indicate a missing appender private static final Appender<ILoggingEvent> NO_APPENDER = new DispatchingAppender(); - private Map<String, Appender<ILoggingEvent>> appenders = new ConcurrentHashMap<>(); + private final Map<String, Appender<ILoggingEvent>> appenders = new ConcurrentHashMap<>(); private Discriminator<ILoggingEvent> discriminator; private String appenderNamePattern; diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java index 3370ebfaf3..f6e933d57b 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java @@ -16,13 +16,12 @@ package org.openecomp.sdc.logging.slf4j; -import org.openecomp.sdc.logging.context.HostAddressCache; -import org.openecomp.sdc.logging.context.InstanceId; - import java.net.InetAddress; import java.util.EnumMap; import java.util.Map; import java.util.Optional; +import org.openecomp.sdc.logging.context.HostAddressCache; +import org.openecomp.sdc.logging.context.InstanceId; /** * Maps global logging context to corresponding MDC fields. diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java index 9c9410c764..026d3fa99b 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java @@ -16,10 +16,9 @@ package org.openecomp.sdc.logging.slf4j; -import org.slf4j.MDC; - import java.util.EnumMap; import java.util.Map; +import org.slf4j.MDC; /** * Because we don't know which information should be carried over from MDC, and which shouldn't, copy just the keys that diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java index 7d2d846097..17439da161 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java @@ -41,10 +41,10 @@ import org.slf4j.MarkerFactory; */ public class Markers { + public static final Marker AUDIT = MarkerFactory.getMarker("AUDIT"); + public static final Marker METRICS = MarkerFactory.getMarker("METRICS"); + private Markers() { // prevent instantiation } - - public static final Marker AUDIT = MarkerFactory.getMarker("AUDIT"); - public static final Marker METRICS = MarkerFactory.getMarker("METRICS"); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java index 6a74b704de..40684cc132 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java @@ -16,10 +16,9 @@ package org.openecomp.sdc.logging.slf4j; -import org.openecomp.sdc.logging.api.ContextData; - import java.util.EnumMap; import java.util.Map; +import org.openecomp.sdc.logging.api.ContextData; /** * Maps request data sent to the context service to corresponding MDC fields. diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java index 248ad43ca8..fbb5fbf1f2 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java @@ -16,14 +16,13 @@ package org.openecomp.sdc.logging.slf4j; +import java.text.SimpleDateFormat; import org.openecomp.sdc.logging.api.AuditData; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.MetricsData; import org.slf4j.LoggerFactory; import org.slf4j.MDC; -import java.text.SimpleDateFormat; - /** * Delegates log calls to SLF4J API and MDC. * diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java index c18716843d..9d4d507ac5 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java @@ -16,13 +16,12 @@ package org.openecomp.sdc.logging.slf4j; +import java.util.Objects; +import java.util.concurrent.Callable; import org.openecomp.sdc.logging.api.ContextData; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.spi.LoggingServiceProvider; -import java.util.Objects; -import java.util.concurrent.Callable; - /** * Uses SLF4J as backend for logging service. * diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java index 73dd25ccae..f2f0c1d674 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapperTest.java @@ -265,8 +265,8 @@ public class SLF4JLoggerWrapperTest { // build a dynamic proxy to avoid implementing the long list of Logger methods // when we actually need just Logger.info() with the audit marker ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - return SpyLogger.class.cast( - Proxy.newProxyInstance(classLoader, new Class<?>[]{SpyLogger.class}, new SpyingInvocationHandler())); + return (SpyLogger) Proxy.newProxyInstance(classLoader, new Class<?>[] {SpyLogger.class}, + new SpyingInvocationHandler()); } private static class SpyingInvocationHandler implements InvocationHandler { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/main/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptor.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/main/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptor.java index a467a9e38a..cfcb62b12c 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/main/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptor.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/main/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptor.java @@ -18,6 +18,8 @@ package org.openecomp.sdc.logging.servlet.spring; import static org.openecomp.sdc.logging.api.StatusCode.COMPLETE; import static org.openecomp.sdc.logging.api.StatusCode.ERROR; +import static org.springframework.http.HttpStatus.Series.REDIRECTION; +import static org.springframework.http.HttpStatus.Series.SUCCESSFUL; import java.util.Objects; import javax.servlet.http.HttpServletRequest; @@ -25,16 +27,19 @@ import javax.servlet.http.HttpServletResponse; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.logging.api.StatusCode; +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.RequestProcessingResult; import org.openecomp.sdc.logging.servlet.Tracker; import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; /** - * <p>IMPORTANT: For this interceptor to work, all exceptions must be properly handled before being returned to a + * <p><b>IMPORTANT</b>: For this interceptor to work, all exceptions must be properly handled before being returned to a * client. Any unexpected, automatically handled exception bypasses the interceptor and will not be logged.</p> * <p>The interceptor must be either registered in Spring configuration XML as a bean, or programmatically as described * in <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-config-interceptors"> @@ -43,6 +48,7 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; * @author evitaliy * @since 02 Aug 2018 */ +@Component public class LoggingInterceptor extends HandlerInterceptorAdapter { static final String LOGGING_TRACKER_KEY = "onap.logging.tracker"; @@ -60,7 +66,9 @@ public class LoggingInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { Class<?> resourceClass = getResourceType(handler); - Tracker tracker = new CombinedTracker(resourceClass, partnerNameHeader, requestIdHeader); + Tracker tracker = new CombinedTracker( + new ContextTracker(partnerNameHeader, requestIdHeader), + new AuditTracker(resourceClass)); request.setAttribute(LOGGING_TRACKER_KEY, tracker); tracker.preRequest(request); return true; @@ -91,25 +99,63 @@ public class LoggingInterceptor extends HandlerInterceptorAdapter { static class ServletResponseResult implements RequestProcessingResult { - private final HttpStatus status; + private final StatusInfo statusInfo; ServletResponseResult(int status) { - this.status = HttpStatus.valueOf(status); + this.statusInfo = init(status); + } + + private StatusInfo init(int status) { + + try { + return new StatusInfo(HttpStatus.valueOf(status)); + } catch (IllegalArgumentException e) { + return new StatusInfo(status, "Non-standard HTTP status", HttpStatus.Series.valueOf(status)); + } } @Override public int getStatus() { - return status.value(); + return statusInfo.getStatus(); } @Override public StatusCode getStatusCode() { - return status.is2xxSuccessful() || status.is3xxRedirection() ? COMPLETE : ERROR; + return statusInfo.getStatusCode(); } @Override public String getStatusPhrase() { - return status.getReasonPhrase(); + return statusInfo.getReasonPhrase(); + } + } + + private static class StatusInfo { + + private final int status; + private final String reasonPhrase; + private final HttpStatus.Series series; + + private StatusInfo(HttpStatus httpStatus) { + this(httpStatus.value(), httpStatus.getReasonPhrase(), httpStatus.series()); + } + + private StatusInfo(int status, String reasonPhrase, HttpStatus.Series series) { + this.status = status; + this.reasonPhrase = reasonPhrase; + this.series = series; + } + + private int getStatus() { + return status; + } + + private String getReasonPhrase() { + return reasonPhrase; + } + + private StatusCode getStatusCode() { + return series.equals(SUCCESSFUL) || series.equals(REDIRECTION) ? COMPLETE : ERROR; } } }
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/test/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptorTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/test/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptorTest.java index ccd0b70552..1aba519c7b 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/test/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptorTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/test/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptorTest.java @@ -114,4 +114,44 @@ public class LoggingInterceptorTest { assertEquals(status, result.getStatus()); assertEquals(COMPLETE, result.getStatusCode()); } + + @Test + public void errorStatusWhenNonStandardInformationalCode() { + final int status = 133; + LoggingInterceptor.ServletResponseResult result = new LoggingInterceptor.ServletResponseResult(status); + assertEquals(status, result.getStatus()); + assertEquals(ERROR, result.getStatusCode()); + } + + @Test + public void errorStatusWhenNonStandardClientErrorCode() { + final int status = 485; + LoggingInterceptor.ServletResponseResult result = new LoggingInterceptor.ServletResponseResult(status); + assertEquals(status, result.getStatus()); + assertEquals(ERROR, result.getStatusCode()); + } + + @Test + public void errorStatusWhenNonStandardServerErrorCode() { + final int status = 547; + LoggingInterceptor.ServletResponseResult result = new LoggingInterceptor.ServletResponseResult(status); + assertEquals(status, result.getStatus()); + assertEquals(ERROR, result.getStatusCode()); + } + + @Test + public void completeStatusWhenNonStandardSuccessCode() { + final int status = 277; + LoggingInterceptor.ServletResponseResult result = new LoggingInterceptor.ServletResponseResult(status); + assertEquals(status, result.getStatus()); + assertEquals(COMPLETE, result.getStatusCode()); + } + + @Test + public void completeStatusWhenNonStandardRedirectionCode() { + final int status = 364; + LoggingInterceptor.ServletResponseResult result = new LoggingInterceptor.ServletResponseResult(status); + assertEquals(status, result.getStatus()); + assertEquals(COMPLETE, result.getStatusCode()); + } }
\ No newline at end of file |