9d42b911aaad40c6111e29f332fb63e6861a02b6
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / transactionid / TransactionIdFilter.java
1 /**
2  * Copyright 2018 - 2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.common.utils.transactionid;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang3.StringUtils;
20 import org.jvnet.hk2.annotations.Service;
21 import org.slf4j.MDC;
22 import org.slf4j.Marker;
23 import org.slf4j.MarkerFactory;
24
25 import javax.servlet.*;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import java.io.IOException;
29 import java.util.UUID;
30
31
32 @Service
33 @Slf4j
34 public class TransactionIdFilter implements Filter {
35
36     public static final Marker INVOKE_SYNCHRONOUS;
37     public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
38     public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
39
40     private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
41
42     static {
43         INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
44         INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
45     }
46
47     @Override
48     public void init(FilterConfig filterConfig) {
49
50     }
51
52     @Override
53     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
54                          FilterChain filterChain) throws IOException, ServletException {
55         HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
56         AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
57                 httpServletRequest);
58         log.warn(ENTRY, "Entering.");
59
60         String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
61         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
62         String validatedRequestID = TransactionIdUtils.validate(requestID);
63         if (validatedRequestID != null) {
64             httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, validatedRequestID);
65         } else {
66             log.warn("A mal-formatted request ID has been detected: {}. It will be replaced by the default ID: {}",
67                     requestID, DEFAULT_REQUEST_ID);
68             requestID = DEFAULT_REQUEST_ID;
69         }
70
71         String invocationID = TransactionIdUtils.getUUID();
72         httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);
73
74         MDC.put("RequestID", requestID);
75         MDC.put("InvocationID", invocationID);
76
77         log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
78         try {
79             filterChain.doFilter(requestWithTransactionId, httpServletResponse);
80         } finally {
81             log.debug(EXIT, "Exiting.");
82             MDC.remove("RequestID");
83             MDC.remove("InvocationID");
84         }
85     }
86
87     @Override
88     public void destroy() {
89
90     }
91
92     public String ensureTransactionIdIsPresent(
93             AddHeadersHttpServletRequestWrapper request) {
94         String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
95
96         if (StringUtils.isBlank(requestId)) {
97             requestId = TransactionIdUtils.getUUID();
98             log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
99             log.info("Request ID ({} header) not exist. It was generated: {}",
100                     TransactionIdUtils.REQUEST_ID_HEADER, requestId);
101             request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);
102         }
103         return requestId;
104     }
105 }