Sync local changes to support GRA microservice
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / filters / AuditLogFilter.java
1 package org.onap.ccsdk.apps.filters
2 ;
3
4 import javax.servlet.http.HttpServletRequest;
5 import org.onap.logging.filter.base.AuditLogServletFilter;
6 import org.onap.logging.ref.slf4j.ONAPLogConstants;
7 import org.slf4j.MDC;
8 import org.springframework.stereotype.Component;
9
10 public class AuditLogFilter extends AuditLogServletFilter {
11     private static final String MDC_HTTP_METHOD_KEY = "HttpMethod";
12
13     @Override
14     protected void additionalPreHandling(HttpServletRequest httpServletRequest) {
15         // Don't overwrite service instance id if it was set outside of this automated method
16         if (MDC.get(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID) == null) {
17             String serviceInstanceId = getServiceInstanceId(httpServletRequest.getRequestURI());
18             if (serviceInstanceId != null) {
19                 MDC.put(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID, serviceInstanceId);
20             }
21         }
22         MDC.put(MDC_HTTP_METHOD_KEY, httpServletRequest.getMethod());
23     }
24
25     // restconf URLs follow a pattern, this method attempts to extract the service instance id according to that pattern
26     protected String getServiceInstanceId(String path) {
27         int idx = path.indexOf("service-list");
28         if (idx != -1) {
29             // chomp off service-list/
30             String str = path.substring(idx + 13);
31             idx = str.indexOf("/");
32             //if there is another forward slash with more information chomp it off
33             if (idx != -1) {
34                 return str.substring(0, idx);
35             } else {
36                 return str;
37             }
38         }
39         return null;
40     }
41
42 }