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;
20 import org.jvnet.hk2.annotations.Service;
22 import org.slf4j.Marker;
23 import org.slf4j.MarkerFactory;
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;
34 public class TransactionIdFilter implements Filter {
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");
40 private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
43 INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
44 INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
48 public void init(FilterConfig filterConfig) {
53 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
54 FilterChain filterChain) throws IOException, ServletException {
55 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
56 AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
58 log.warn(ENTRY, "Entering.");
60 String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
61 HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
62 if (TransactionIdUtils.validate(requestID)) {
63 httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestID);
65 log.warn("A mal-formatted request ID has been detected: {}. It will be replaced by the default ID: {}",
66 requestID, DEFAULT_REQUEST_ID);
67 requestID = DEFAULT_REQUEST_ID;
70 String invocationID = TransactionIdUtils.getUUID();
71 httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER, invocationID);
73 MDC.put("RequestID", requestID);
74 MDC.put("InvocationID", invocationID);
76 log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
78 filterChain.doFilter(requestWithTransactionId, httpServletResponse);
80 log.debug(EXIT, "Exiting.");
81 MDC.remove("RequestID");
82 MDC.remove("InvocationID");
87 public void destroy() {
91 public String ensureTransactionIdIsPresent(
92 AddHeadersHttpServletRequestWrapper request) {
93 String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
95 if (StringUtils.isBlank(requestId)) {
96 requestId = TransactionIdUtils.getUUID();
97 log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
98 log.info("Request ID ({} header) not exist. It was generated: {}",
99 TransactionIdUtils.REQUEST_ID_HEADER, requestId);
100 request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);