// Adapted from org.jboss.seam.web.LoggingFilter in Seam 2.3.1

package org.zanata.security;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.log4j.MDC;
import org.apache.deltaspike.core.api.exclude.Exclude;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import javax.inject.Named;
import org.zanata.servlet.MDCInsertingServletFilter;

/**
 * This filter adds the authenticated user name to the log4j
 * mapped diagnostic context so that it can be included in
 * formatted log output if desired, by adding %X{username}
 * to the pattern.
 *
 * @author Eric Trautman
 */
@WebFilter
public class UsernameLoggingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest) servletRequest).getSession(false);
        if (session != null) {
            ZanataIdentity identity =
                    BeanProvider.getContextualReference(ZanataIdentity.class);
            ZanataCredentials credentials = identity.getCredentials();
            String username = credentials != null ? credentials.getUsername() : null;
            if (username != null) {
                MDC.put(MDCInsertingServletFilter.USERNAME, username);
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
        MDC.remove(MDCInsertingServletFilter.USERNAME);
    }

    @Override
    public void destroy() {
    }
}

@@ -533,26 +533,19 @@
 org.
-apache.log
+slf
 4j.MDC;%0A
 impo
@@ -544,155 +544,8 @@
 DC;%0A
-import org.apache.deltaspike.core.api.exclude.Exclude;%0Aimport org.apache.deltaspike.core.api.projectstage.ProjectStage;%0Aimport javax.inject.Named;%0A
 impo
@@ -640,17 +640,16 @@
 ted user
- 
 name to 
@@ -652,19 +652,19 @@
  to the 
-log
+slf
 4j%0A * ma
@@ -1683,32 +1683,50 @@
     %7D%0A        %7D%0A
+        try %7B%0A    
         filterCh
@@ -1768,24 +1768,48 @@
 tResponse);%0A
+        %7D finally %7B%0A    
         MDC.
@@ -1852,16 +1852,26 @@
 RNAME);%0A
+        %7D%0A
     %7D%0A%0A 

Use SLF4J MDC, not Log4j (#488)