aboutsummaryrefslogtreecommitdiffstats
path: root/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/ControllerAuditLogFilter.java
blob: 7805e365df06a39980e72d78250c94c3aafa89e7 (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
package org.onap.ccsdk.sli.core.filters;

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

public class ControllerAuditLogFilter 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.getPathInfo());
            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;
    }

}