Add log filter
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / transactionid / TransactionIdFilter.java
1 /**
2  * Copyright 2018 ZTE Corporation.
3  *
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  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 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;
30 import org.slf4j.MDC;
31 import org.slf4j.Marker;
32 import org.slf4j.MarkerFactory;
33
34
35 @Service
36 @Slf4j
37 public class TransactionIdFilter implements Filter {
38
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");
42
43     static {
44         INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE");
45         INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS"));
46     }
47
48     @Override
49     public void init(FilterConfig filterConfig) throws ServletException {
50
51     }
52
53     @Override
54     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
55             FilterChain filterChain) throws IOException, ServletException {
56         HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
57         AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
58                 httpServletRequest);
59         log.warn(ENTRY, "Entering.");
60
61         String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
62         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
63         httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestID);
64
65         String invocationID = TransactionIdUtils.getUUID();
66         httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER,invocationID);
67
68         MDC.put("RequestID", requestID);
69         MDC.put("InvocationID", invocationID);
70
71         log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
72         try {
73             filterChain.doFilter(requestWithTransactionId, httpServletResponse);
74         } finally {
75             log.debug(EXIT, "Exiting.");
76             MDC.remove("RequestID");
77             MDC.remove("InvocationID");
78         }
79     }
80
81     @Override
82     public void destroy() {
83
84     }
85
86     public String ensureTransactionIdIsPresent(
87             AddHeadersHttpServletRequestWrapper request) {
88         String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER);
89
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);
96         }
97         return requestId;
98     }
99 }