aboutsummaryrefslogtreecommitdiffstats
path: root/services/src/main/java/org/onap/ccsdk/apps/filters/AuditLogFilter.java
blob: 45ce55f4ad94f9f452b4cd1eff0c1d340fe2fbfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.onap.ccsdk.apps.filters
;

import javax.servlet.http.HttpServletRequest;
import org.onap.logging.filter.base.AuditLogServletFilter;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

public class AuditLogFilter extends AuditLogServletFilter {
    private static final String MDC_HTTP_METHOD_KEY = "HttpMethod";

    @Override
    protected void additionalPreHandling(HttpServletRequest httpServletRequest) {
        // Don't overwrite service instance id if it was set outside of this automated method
        if (MDC.get(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID) == null) {
            String serviceInstanceId = getServiceInstanceId(httpServletRequest.getRequestURI());
            if (serviceInstanceId != null) {
                MDC.put(ONAPLogConstants.MDCs.SERVICE_INSTANCE_ID, serviceInstanceId);
            }
        }
        MDC.put(MDC_HTTP_METHOD_KEY, httpServletRequest.getMethod());
    }

    // restconf URLs follow a pattern, this method attempts to extract the service instance id according to that pattern
    protected String getServiceInstanceId(String path) {
        int idx = path.indexOf("service-list");
        if (idx != -1) {
            // chomp off service-list/
            String str = path.substring(idx + 13);
            idx = str.indexOf("/");
            //if there is another forward slash with more information chomp it off
            if (idx != -1) {
                return str.substring(0, idx);
            } else {
                return str;
            }
        }
        return null;
    }

}