Carry forward honolulu fixes
[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 @Component
11 public class AuditLogFilter extends AuditLogServletFilter {
12     private static final String MDC_HTTP_METHOD_KEY = "HttpMethod";
13
14     @Override
15     protected void additionalPreHandling(HttpServletRequest httpServletRequest) {
16         // Don't overwrite service instance id if it was set outside of this automated method
17         if (MDC.get(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID) == null) {
18             String serviceInstanceId = getServiceInstanceId(httpServletRequest.getRequestURI());
19             if (serviceInstanceId != null) {
20                 MDC.put(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID, serviceInstanceId);
21             }
22         }
23         MDC.put(MDC_HTTP_METHOD_KEY, httpServletRequest.getMethod());
24     }
25
26     // restconf URLs follow a pattern, this method attempts to extract the service instance id according to that pattern
27     protected String getServiceInstanceId(String path) {
28         int idx = path.indexOf("service-list");
29         if (idx != -1) {
30             // chomp off service-list/
31             String str = path.substring(idx + 13);
32             idx = str.indexOf("/");
33             //if there is another forward slash with more information chomp it off
34             if (idx != -1) {
35                 return str.substring(0, idx);
36             } else {
37                 return str;
38             }
39         }
40         return null;
41     }
42
43 }