mwomack 2003/02/19 23:35:38 Modified: src/java/org/apache/log4j/servlet CookieMDCFilter.java Log: Jalopy-ized and checkstyle-d version. Revision Changes Path 1.2 +94 -77 jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/CookieMDCFilter.java Index: CookieMDCFilter.java =================================================================== RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/CookieMDCFilter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CookieMDCFilter.java 4 Feb 2003 06:28:16 -0000 1.1 +++ CookieMDCFilter.java 20 Feb 2003 07:35:38 -0000 1.2 @@ -4,13 +4,16 @@ * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ - package org.apache.log4j.servlet; +import org.apache.log4j.Logger; +import org.apache.log4j.MDC; + +import java.io.IOException; + import java.util.HashMap; import java.util.Iterator; import java.util.StringTokenizer; -import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -21,19 +24,17 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; -import org.apache.log4j.Logger; -import org.apache.log4j.MDC; /** - A useful Servlet 2.3 compatible filter which will search for a predefined - set of cookies in the request and place their values into the log4j MDC. + A useful Servlet 2.3 compatible filter which will search for a predefined + set of cookies in the request and place their values into the log4j MDC. The key used in the MDC is the name of the cookie. The value placed in the MDC is the value of the cookie in the request. - + @author Mark Womack <[EMAIL PROTECTED]> @since 1.3 */ - + // An example of the web.xml configuration: // // <!-- looks for cookies named "JSESSIONID" and "USERID" --> @@ -66,79 +67,95 @@ // </layout> // </appender> public class CookieMDCFilter implements Filter { - private static final Logger _TRACE = - Logger.getLogger(CookieMDCFilter.class); - - /** - The set of cookies names we want to look for. */ - private HashMap cookieMap; + /** + private trace logger. */ + private static final Logger LOG = Logger.getLogger(CookieMDCFilter.class); + + /** + The set of cookies names we want to look for. */ + private HashMap cookieMap; + + /** + Uses filter init parameter to initialize a list of cookies that will be + retrieved from the request and placed into the log4j MDC context. - /** - Uses filter init parameter to initialize a list of cookies that will be - retrieved from the request and placed into the log4j MDC context. */ - public void init(FilterConfig filterConfig) throws ServletException { - - // check for existense of init param - String cookieParam = filterConfig.getInitParameter("cookie-list"); - if (cookieParam == null || cookieParam.length() == 0) - return; - - // parse the list of cookies - StringTokenizer tokenizer = new StringTokenizer(cookieParam); - while (tokenizer.hasMoreTokens()) { - if (cookieMap == null) { - cookieMap = new HashMap(); - } - cookieMap.put(tokenizer.nextToken(), null); - } - - // report the configuration - if (cookieMap != null && _TRACE.isDebugEnabled()) { - Iterator iter = cookieMap.keySet().iterator(); - while (iter.hasNext()) { - _TRACE.debug("configured to search for cookie with name " + - iter.next()); - } - } + @param filterConfig configuration data for this filter. + @throws ServletException */ + public void init(FilterConfig filterConfig) throws ServletException { + // check for existense of init param + String cookieParam = filterConfig.getInitParameter("cookie-list"); + + if ((cookieParam == null) || (cookieParam.length() == 0)) { + return; + } + + // parse the list of cookies + StringTokenizer tokenizer = new StringTokenizer(cookieParam); + + while (tokenizer.hasMoreTokens()) { + if (cookieMap == null) { + cookieMap = new HashMap(); + } + + cookieMap.put(tokenizer.nextToken(), null); } + + // report the configuration + if ((cookieMap != null) && LOG.isDebugEnabled()) { + Iterator iter = cookieMap.keySet().iterator(); + + while (iter.hasNext()) { + LOG.debug("configured to search for cookie with name " + iter.next()); + } + } + } + + /** + Search the request cookies for the cookies whose value we want to + stuff into the log4j MDC. - /** - Search the request cookies for the cookies whose value we want to - stuff into the log4j MDC. */ - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - - // if this filter configured and request has cookies, search - if (cookieMap != null) { - Cookie[] cookies = ((HttpServletRequest)request).getCookies(); - if (cookies != null) { - for (int x = 0; x < cookies.length; x++) { - - // if request has a cookie we are lookig for, put its - // value into the MDC - String name = cookies[x].getName(); - if (cookieMap.containsKey(cookies[x].getName())) { - MDC.put(name, cookies[x].getValue()); - if (_TRACE.isDebugEnabled()) - _TRACE.debug("put into MDC cookie with name " + - name + " and value " + cookies[x].getValue()); - } - else { - if (_TRACE.isDebugEnabled()) - _TRACE.debug("ignoring cookie with name " + name); - - } - } + @param request the servlet request. + @param response the servlet response. + @param chain the filter chain this filter is part of. + @throws IOException indicates communication problem. + @throws ServletException indicates problem processing request. */ + public void doFilter( + ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + // if this filter configured and request has cookies, search + if (cookieMap != null) { + Cookie[] cookies = ((HttpServletRequest) request).getCookies(); + + if (cookies != null) { + for (int x = 0; x < cookies.length; x++) { + // if request has a cookie we are lookig for, put its + // value into the MDC + String name = cookies[x].getName(); + + if (cookieMap.containsKey(cookies[x].getName())) { + MDC.put(name, cookies[x].getValue()); + + if (LOG.isDebugEnabled()) { + LOG.debug( + "put into MDC cookie with name " + name + " and value " + + cookies[x].getValue()); + } + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("ignoring cookie with name " + name); } + } } - - // pass control to the next filter - chain.doFilter(request, response); - } - - /** - Not used. */ - public void destroy() { - // do nothing + } } + + // pass control to the next filter + chain.doFilter(request, response); + } + + /** + Not used. */ + public void destroy() { + // do nothing + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]