View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package chapter7;
15  
16  import java.io.IOException;
17  import java.security.Principal;
18  
19  import javax.servlet.Filter;
20  import javax.servlet.FilterChain;
21  import javax.servlet.FilterConfig;
22  import javax.servlet.ServletException;
23  import javax.servlet.ServletRequest;
24  import javax.servlet.ServletResponse;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpSession;
27  
28  import org.slf4j.MDC;
29  
30  /**
31   * A simple servlet filter that puts the username
32   * found either in the Principle or as a session attribute
33   * in the MDC.
34   * 
35   * The value is removed from the MDC once the request has been
36   * fully processed.
37   *
38   * To be used, add the following lines to a web.xml file
39   * 
40   * <filter>
41   *   <filter-name>User Servlet Filter</filter-name>
42   *   <filter-class>
43   *     chapter7.UserServletFilter
44   *   </filter-class>
45   * </filter>
46   * <filter-mapping>
47   *   <filter-name>User Servlet Filter</filter-name>
48   *   <url-pattern>/*</url-pattern>
49   * </filter-mapping>
50   *
51   * @author S&eacute;bastien Pennec
52   */
53  public class UserServletFilter implements Filter {
54  
55    boolean userRegistered = false;
56    
57    private final String userKey = "username";
58    
59    public void destroy() {
60    }
61  
62    public void doFilter(ServletRequest request, ServletResponse response,
63        FilterChain chain) throws IOException, ServletException {
64  
65      HttpServletRequest req = (HttpServletRequest) request;
66      Principal principal = req.getUserPrincipal();
67      // Please note that we could have also used a cookie to 
68      // retreive the user name
69      
70      if (principal != null) {
71        String username = principal.getName();
72        registerUsername(username);
73      } else {
74        HttpSession session = req.getSession();
75        String username = (String)session.getAttribute(userKey);
76        registerUsername(username);
77      }
78      
79      try {
80        chain.doFilter(request, response);
81      } finally {
82        if (userRegistered) {
83          MDC.remove(userKey);
84        }
85      }
86    }
87  
88    public void init(FilterConfig arg0) throws ServletException {
89    }
90    
91    private void registerUsername(String username) {
92      if (username != null && username.trim().length() > 0) {
93        MDC.put(userKey, username);
94        userRegistered = true;
95      }
96    }
97  
98  }