Switched from Dropwizard to Springboot
[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.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     public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
40     public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
41
42     private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
43
44     static {
45         INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
46         INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
47     }
48
49     @Override
50     public void init(FilterConfig filterConfig) {
51
52     }
53
54     @Override
55     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
56                          FilterChain filterChain) throws IOException, ServletException {
57         HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
58         AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
59                 httpServletRequest);
60         log.warn(ENTRY, "Entering.");
61
62         String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
63         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
64         String validatedRequestID = TransactionIdUtils.validate(requestID);
65         if (validatedRequestID != null) {
66             httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, validatedRequestID);
67         } else {
68             log.warn("A mal-formatted request ID has been detected: {}. It will be replaced by the default ID: {}",
69                     requestID, DEFAULT_REQUEST_ID);
70             requestID = DEFAULT_REQUEST_ID;
71         }
72
73         String invocationID = TransactionIdUtils.getUUID();
74         httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);
75
76         MDC.put("RequestID", requestID);
77         MDC.put("InvocationID", invocationID);
78
79         log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
80         try {
81             filterChain.doFilter(requestWithTransactionId, httpServletResponse);
82         } finally {
83             log.debug(EXIT, "Exiting.");
84             MDC.remove("RequestID");
85             MDC.remove("InvocationID");
86         }
87     }
88
89     @Override
90     public void destroy() {
91
92     }
93
94     public String ensureTransactionIdIsPresent(
95             AddHeadersHttpServletRequestWrapper request) {
96         String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
97
98         if (StringUtils.isBlank(requestId)) {
99             requestId = TransactionIdUtils.getUUID();
100             log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
101             log.info("Request ID ({} header) not exist. It was generated: {}",
102                     TransactionIdUtils.REQUEST_ID_HEADER, requestId);
103             request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);
104         }
105         return requestId;
106     }
107 }