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 --- .../logging/servlet/spring/LoggingInterceptor.java | 60 +++++++++++++++++++--- .../servlet/spring/LoggingInterceptorTest.java | 40 +++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring') 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; /** - *

IMPORTANT: For this interceptor to work, all exceptions must be properly handled before being returned to a + *

IMPORTANT: 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.

*

The interceptor must be either registered in Spring configuration XML as a bean, or programmatically as described * in @@ -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 -- cgit 1.2.3-korg