Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class CorrelationHeaderFilter implements Filter {
//...
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String currentCorrId = httpServletRequest.getHeader(RequestCorrelation.CORRELATION_ID_HEADER);
if (!currentRequestIsAsyncDispatcher(httpServletRequest)) {
if (currentCorrId == null) {
currentCorrId = UUID.randomUUID().toString();
LOGGER.info("No correlationId found in Header. Generated : " + currentCorrId);
} else {
LOGGER.info("Found correlationId in Header : " + currentCorrId);
}
RequestCorrelation.setId(currentCorrId);
}
filterChain.doFilter(httpServletRequest, servletResponse);
}
//...
private boolean currentRequestIsAsyncDispatcher(HttpServletRequest httpServletRequest) {
return httpServletRequest.getDispatcherType().equals(DispatcherType.ASYNC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment