package @FILTER_PCK@;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import @UTIL_PCK@.Constants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class LogMdcFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if (request instanceof HttpServletRequest) {
			HttpServletRequest httpRequest = (HttpServletRequest) request;

			String key = Constants.CORRELATION_KEY;
			String uid = httpRequest.getHeader(key);
			if (uid != null) {
				if (log.isDebugEnabled()) {
					log.debug("Setting log MDC attribute key {} to uid {}", key, uid);
				}
				MDC.put(key, uid);
			}
		}
		chain.doFilter(request, response);
	}
}
