01: /*
02: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: */
07: package winstone.testApplication.filters;
08:
09: import java.io.IOException;
10: import java.util.Enumeration;
11:
12: import javax.servlet.Filter;
13: import javax.servlet.FilterChain;
14: import javax.servlet.FilterConfig;
15: import javax.servlet.ServletContext;
16: import javax.servlet.ServletException;
17: import javax.servlet.ServletRequest;
18: import javax.servlet.ServletResponse;
19:
20: /**
21: * Simple timing and request dumping test filter
22: *
23: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
24: * @version $Id: TimingFilter.java,v 1.2 2006/02/28 07:32:50 rickknowles Exp $
25: */
26: public class TimingFilter implements Filter {
27: private boolean dumpRequestParams;
28:
29: private ServletContext context;
30:
31: public void init(FilterConfig config) {
32: String dumpRequestParams = config
33: .getInitParameter("dumpRequestParameters");
34: this .dumpRequestParams = ((dumpRequestParams != null) && dumpRequestParams
35: .equalsIgnoreCase("true"));
36: this .context = config.getServletContext();
37: }
38:
39: public void destroy() {
40: this .context = null;
41: }
42:
43: /**
44: * Times the execution of the rest of the filter chain, optionally dumping
45: * the request parameters to the servlet context log
46: */
47: public void doFilter(ServletRequest request,
48: ServletResponse response, FilterChain chain)
49: throws IOException, ServletException {
50: if (this .dumpRequestParams)
51: for (Enumeration paramNames = request.getParameterNames(); paramNames
52: .hasMoreElements();) {
53: String name = (String) paramNames.nextElement();
54: this .context.log("Request parameter: " + name + "="
55: + request.getParameter(name));
56: }
57:
58: long startTime = System.currentTimeMillis();
59: chain.doFilter(request, response);
60: this .context.log("Filter chain executed in "
61: + (System.currentTimeMillis() - startTime) + "ms");
62: }
63: }
|