/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils.filters;

import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.web.AttributeHelper;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.MDC;

/**
 * @author CBO
 */
public class Log4jMDCServletFilter implements Filter {

    static private long conversationId = 0;

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            if (MDC.get("user") == null) {
                MDC.put("user", getUser(request));
            }
            if (MDC.get("ip") == null) {
                MDC.put("ip", getIP(request));
            }
            if (MDC.get("convId") == null) {
                MDC.put("convId", getConversationId());
            }
            chain.doFilter(request, response);
        } finally {
            MDC.remove("user");
            MDC.remove("ip");
            MDC.remove("convId");
        }
    }

    private HttpSession getNullableSession(ServletRequest request) {
        if (request instanceof HttpServletRequest) {
            return ((HttpServletRequest) request).getSession(false);
        } else {
            return null;
        }
    }

    private synchronized String getConversationId() {
        return "" + conversationId++;
    }

    private String getUser(ServletRequest request) {
        HttpSession session = getNullableSession(request);
        if (session != null) {
            XemeliosUser user = AttributeHelper.getUser(session);
            if (user != null) {
                return user.getDisplayName() != null ? user.getDisplayName() : "anonyme";
            }
        }
        return "anonyme";
    }

    private String getIP(ServletRequest request) {
        return request.getRemoteAddr();
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }
}
  
  
