2 * Copyright 2018-2021 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.common.utils.transactionid;
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang3.StringUtils;
21 import org.slf4j.Marker;
22 import org.slf4j.MarkerFactory;
23 import org.springframework.stereotype.Component;
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;
35 @WebFilter(urlPatterns = {"/*"})
36 public class TransactionIdFilter implements Filter {
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");
42 private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
45 INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
46 INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
50 public void init(FilterConfig filterConfig) {
55 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
56 FilterChain filterChain) throws IOException, ServletException {
57 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
58 AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
60 log.warn(ENTRY, "Entering.");
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);
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;
73 String invocationID = TransactionIdUtils.getUUID();
74 httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);
76 MDC.put("RequestID", requestID);
77 MDC.put("InvocationID", invocationID);
79 log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
81 filterChain.doFilter(requestWithTransactionId, httpServletResponse);
83 log.debug(EXIT, "Exiting.");
84 MDC.remove("RequestID");
85 MDC.remove("InvocationID");
90 public void destroy() {
94 public String ensureTransactionIdIsPresent(
95 AddHeadersHttpServletRequestWrapper request) {
96 String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
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);