3451b85be5ce4bb8d87ea4aff14faeb03cbd6229
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / transactionid / TransactionIdFilter.java
1 /**
2  * Copyright 2018-2022 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.slf4j.MDC;
21 import org.slf4j.Marker;
22 import org.slf4j.MarkerFactory;
23 import org.springframework.stereotype.Component;
24
25 import javax.servlet.*;
26 import javax.servlet.annotation.WebFilter;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import java.io.IOException;
30 import java.util.UUID;
31
32
33 @Slf4j
34 @Component
35 @WebFilter(urlPatterns = {"/*"})
36 public class TransactionIdFilter implements Filter {
37
38     public static final Marker INVOKE_SYNCHRONOUS;
39     private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
40
41     static {
42         INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
43         INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
44     }
45
46     @Override
47     public void init(FilterConfig filterConfig) {
48
49     }
50
51     @Override
52     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
53                          FilterChain filterChain) throws IOException, ServletException {
54         HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
55         AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
56                 httpServletRequest);
57
58         String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
59         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
60         String validatedRequestID = TransactionIdUtils.validate(requestID);
61         if (validatedRequestID != null) {
62             httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, validatedRequestID);
63         } else {
64             log.warn("A mal-formatted request ID has been detected: {}. It will be replaced by the default ID: {}",
65                     requestID, DEFAULT_REQUEST_ID);
66             requestID = DEFAULT_REQUEST_ID;
67         }
68
69         String invocationID = TransactionIdUtils.getUUID();
70         httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);
71
72         MDC.put("RequestID", requestID);
73         MDC.put("InvocationID", invocationID);
74
75         try {
76             filterChain.doFilter(requestWithTransactionId, httpServletResponse);
77         } finally {
78             MDC.remove("RequestID");
79             MDC.remove("InvocationID");
80         }
81     }
82
83     @Override
84     public void destroy() {
85
86     }
87
88     public String ensureTransactionIdIsPresent(
89             AddHeadersHttpServletRequestWrapper request) {
90         String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
91
92         if (StringUtils.isBlank(requestId)) {
93             requestId = TransactionIdUtils.getUUID();
94             log.info("Request ID ({} header) not exist. It was generated: {}",
95                     TransactionIdUtils.REQUEST_ID_HEADER, requestId);
96             request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);
97         }
98         return requestId;
99     }
100 }