1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.commsen.stopwatch.web.filter;
18
19 import java.io.IOException;
20
21 import javax.servlet.Filter;
22 import javax.servlet.FilterChain;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.HttpServletRequest;
28
29 import com.commsen.stopwatch.Stopwatch;
30
31 /***
32 * TODO Dokumentacja
33 *
34 * @author Milen Dyankov
35 *
36 */
37 public class RequestTimingFilter implements Filter {
38
39 private static final String PARAM_SKIP_ON_ERROR = "skipOnError";
40
41 String group = null;
42 boolean skipOnError = true;
43
44
45 /***
46 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
47 */
48 public void init(FilterConfig config) throws ServletException {
49 group = config.getServletContext().getServletContextName();
50 String skipOnErrorParam = config.getInitParameter(PARAM_SKIP_ON_ERROR);
51 if (skipOnErrorParam != null && skipOnErrorParam.trim().length() > 0)
52 skipOnError = Boolean.valueOf(skipOnErrorParam).booleanValue();
53 }
54
55
56 /***
57 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
58 */
59 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
60 String label = null;
61 if (request instanceof HttpServletRequest) {
62 HttpServletRequest servletRequest = (HttpServletRequest)request;
63 label = servletRequest.getRequestURI();
64 }
65
66
67 long swId = Stopwatch.start(group, label);
68 try {
69 chain.doFilter(request, response);
70 } catch (RuntimeException e) {
71 if (skipOnError) {
72 Stopwatch.skip(swId);
73 }
74 throw e;
75 } finally {
76 Stopwatch.stop(swId);
77 }
78 }
79
80
81 /***
82 * @see javax.servlet.Filter#destroy()
83 */
84 public void destroy() {
85
86
87 }
88
89 }