summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilter.java
blob: 9d42b911aaad40c6111e29f332fb63e6861a02b6 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Copyright 2018 - 2021 ZTE Corporation.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.holmes.common.utils.transactionid;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jvnet.hk2.annotations.Service;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;


@Service
@Slf4j
public class TransactionIdFilter implements Filter {

    public static final Marker INVOKE_SYNCHRONOUS;
    public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
    public static final Marker EXIT = MarkerFactory.getMarker("EXIT");

    private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();

    static {
        INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
        INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
    }

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
                httpServletRequest);
        log.warn(ENTRY, "Entering.");

        String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        String validatedRequestID = TransactionIdUtils.validate(requestID);
        if (validatedRequestID != null) {
            httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, validatedRequestID);
        } else {
            log.warn("A mal-formatted request ID has been detected: {}. It will be replaced by the default ID: {}",
                    requestID, DEFAULT_REQUEST_ID);
            requestID = DEFAULT_REQUEST_ID;
        }

        String invocationID = TransactionIdUtils.getUUID();
        httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);

        MDC.put("RequestID", requestID);
        MDC.put("InvocationID", invocationID);

        log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
        try {
            filterChain.doFilter(requestWithTransactionId, httpServletResponse);
        } finally {
            log.debug(EXIT, "Exiting.");
            MDC.remove("RequestID");
            MDC.remove("InvocationID");
        }
    }

    @Override
    public void destroy() {

    }

    public String ensureTransactionIdIsPresent(
            AddHeadersHttpServletRequestWrapper request) {
        String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);

        if (StringUtils.isBlank(requestId)) {
            requestId = TransactionIdUtils.getUUID();
            log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
            log.info("Request ID ({} header) not exist. It was generated: {}",
                    TransactionIdUtils.REQUEST_ID_HEADER, requestId);
            request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);
        }
        return requestId;
    }
}