/**
- * Copyright 2018 - 2021 ZTE Corporation.
+ * Copyright 2018-2022 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
-import org.jvnet.hk2.annotations.Service;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
+import org.springframework.stereotype.Component;
import javax.servlet.*;
+import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
-@Service
@Slf4j
+@Component
+@WebFilter(urlPatterns = {"/*"})
public class TransactionIdFilter implements Filter {
public static final Marker INVOKE_SYNCHRONOUS;
- public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
- public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
-
private static final String DEFAULT_REQUEST_ID = UUID.randomUUID().toString();
static {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper(
httpServletRequest);
- log.warn(ENTRY, "Entering.");
String requestID = ensureTransactionIdIsPresent(requestWithTransactionId);
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
MDC.put("RequestID", requestID);
MDC.put("InvocationID", invocationID);
- log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... ");
try {
filterChain.doFilter(requestWithTransactionId, httpServletResponse);
} finally {
- log.debug(EXIT, "Exiting.");
MDC.remove("RequestID");
MDC.remove("InvocationID");
}
if (StringUtils.isBlank(requestId)) {
requestId = TransactionIdUtils.getUUID();
- log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation.");
log.info("Request ID ({} header) not exist. It was generated: {}",
TransactionIdUtils.REQUEST_ID_HEADER, requestId);
request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId);