001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.helpers;
015
016import java.io.IOException;
017
018import jakarta.servlet.Filter;
019import jakarta.servlet.FilterChain;
020import jakarta.servlet.FilterConfig;
021import jakarta.servlet.ServletException;
022import jakarta.servlet.ServletRequest;
023import jakarta.servlet.ServletResponse;
024import jakarta.servlet.http.HttpServletRequest;
025
026import org.slf4j.MDC;
027
028import ch.qos.logback.classic.ClassicConstants;
029
030/**
031 * A servlet filter that inserts various values retrieved from the incoming http
032 * request into the MDC.
033 * <p/>
034 * <p/>
035 * The values are removed after the request is processed.
036 *
037 * @author Ceki G&uuml;lc&uuml;
038 */
039public class MDCInsertingServletFilter implements Filter {
040
041    public void destroy() {
042        // do nothing
043    }
044
045    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
046            throws IOException, ServletException {
047
048        insertIntoMDC(request);
049        try {
050            chain.doFilter(request, response);
051        } finally {
052            clearMDC();
053        }
054    }
055
056    void insertIntoMDC(ServletRequest request) {
057
058        MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());
059
060        if (request instanceof HttpServletRequest) {
061            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
062            MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
063            StringBuffer requestURL = httpServletRequest.getRequestURL();
064            if (requestURL != null) {
065                MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
066            }
067            MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
068            MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
069            MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
070            MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
071        }
072
073    }
074
075    void clearMDC() {
076        MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
077        MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
078        MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
079        // removing possibly nonexistent item is OK
080        MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
081        MDC.remove(ClassicConstants.REQUEST_METHOD);
082        MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
083        MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
084    }
085
086    public void init(FilterConfig arg0) throws ServletException {
087        // do nothing
088    }
089}