summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src
diff options
context:
space:
mode:
authorvempo <vitaliy.emporopulo@amdocs.com>2018-08-06 16:21:01 +0300
committerAvi Gaffa <avi.gaffa@amdocs.com>2018-08-06 16:09:04 +0000
commit44651d903d83f9256c324ec2a3c8886e91e1e433 (patch)
tree0e1961ce6c69dcb8eb18cf8b4e8af1e5eed6e3b4 /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src
parent1ecdbf74427467327de0b1e11cd0a839d6dc05bd (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/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/main/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptor.java60
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-spring/src/test/java/org/openecomp/sdc/logging/servlet/spring/LoggingInterceptorTest.java40
2 files changed, 93 insertions, 7 deletions
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