2 * Copyright 2018 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 java.io.IOException;
19 import javax.servlet.Filter;
20 import javax.servlet.FilterChain;
21 import javax.servlet.FilterConfig;
22 import javax.servlet.ServletException;
23 import javax.servlet.ServletRequest;
24 import javax.servlet.ServletResponse;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import lombok.extern.slf4j.Slf4j;
28 import org.apache.commons.lang3.StringUtils;
29 import org.jvnet.hk2.annotations.Service;
31 import org.slf4j.Marker;
32 import org.slf4j.MarkerFactory;
37 public class TransactionIdFilter implements Filter {
39 public static final Marker INVOKE_SYNCHRONOUS;
40 public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
41 public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
44 INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
45 INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
49 public void init(FilterConfig filterConfig) throws ServletException {
54 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
55 FilterChain filterChain) throws IOException, ServletException {
56 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
57 AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
59 log.warn(ENTRY, "Entering.");
61 String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
62 HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
63 httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestID);
65 String invocationID = TransactionIdUtils.getUUID();
66 httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER,invocationID);
68 MDC.put("RequestID", requestID);
69 MDC.put("InvocationID", invocationID);
71 log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
73 filterChain.doFilter(requestWithTransactionId, httpServletResponse);
75 log.debug(EXIT, "Exiting.");
76 MDC.remove("RequestID");
77 MDC.remove("InvocationID");
82 public void destroy() {
86 public String ensureTransactionIdIsPresent(
87 AddHeadersHttpServletRequestWrapper request) {
88 String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
90 if (StringUtils.isBlank(requestId)) {
91 requestId = TransactionIdUtils.getUUID();
92 log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
93 log.info("Request ID ({} header) not exist. It was generated: {}",
94 TransactionIdUtils.REQUEST_ID_HEADER, requestId);
95 request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);