1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.ui.filters; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
|
18 | |
import javax.servlet.Filter; |
19 | |
import javax.servlet.FilterChain; |
20 | |
import javax.servlet.FilterConfig; |
21 | |
import javax.servlet.ServletException; |
22 | |
import javax.servlet.ServletRequest; |
23 | |
import javax.servlet.ServletResponse; |
24 | |
import javax.servlet.http.HttpServletRequest; |
25 | |
|
26 | |
import net.admin4j.config.Admin4JConfiguration; |
27 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
28 | |
import net.admin4j.util.Admin4jRuntimeException; |
29 | |
import net.admin4j.util.FixedSizeRollingList; |
30 | |
import net.admin4j.util.ServletUtils; |
31 | |
import net.admin4j.vo.HttpRequestVO; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 9 | public class RequestTrackingFilter implements Filter { |
39 | |
|
40 | |
public static final int DEFAULT_NBR_REQUESTS_TRACKED = 5; |
41 | |
public static final String REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME = "admin4j.previous.requests"; |
42 | |
|
43 | |
private int nbrRequestsTracked; |
44 | |
|
45 | |
public void destroy() { |
46 | |
|
47 | |
|
48 | 0 | } |
49 | |
|
50 | |
@SuppressWarnings("unchecked") |
51 | |
public void doFilter(ServletRequest request, ServletResponse response, |
52 | |
FilterChain chain) throws IOException, ServletException { |
53 | 9 | HttpServletRequest httpRequest = (HttpServletRequest)request; |
54 | |
|
55 | 9 | FixedSizeRollingList<HttpRequestVO> list = (FixedSizeRollingList<HttpRequestVO>) |
56 | |
httpRequest.getSession().getAttribute(REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME); |
57 | 9 | if (list == null) { |
58 | 6 | list = new FixedSizeRollingList<HttpRequestVO>(nbrRequestsTracked); |
59 | 6 | httpRequest.getSession().setAttribute(REQUEST_TRACKING_SESSION_ATTRIBUTE_NAME, list); |
60 | |
} |
61 | |
|
62 | 9 | list.add(new HttpRequestVO(httpRequest.getRequestURI(), |
63 | |
ServletUtils.listRequestAttributes(httpRequest))); |
64 | 9 | chain.doFilter(request, response); |
65 | 9 | } |
66 | |
|
67 | |
public void init(FilterConfig config) throws ServletException { |
68 | 6 | String nbrRequestsStr = config.getInitParameter("nbr.requests.tracked"); |
69 | |
|
70 | 6 | if (Admin4JConfiguration.getRequestHistoryNbrRetained() != null) { |
71 | 0 | if (Admin4JConfiguration.getRequestHistoryNbrRetained() > 0) { |
72 | 0 | nbrRequestsTracked = Admin4JConfiguration.getRequestHistoryNbrRetained(); |
73 | |
} |
74 | |
else { |
75 | 0 | throw new Admin4jRuntimeException("request.history.nbr.retained must be greater than zero") |
76 | |
.addContextValue("request.history.nbr.retained", Admin4JConfiguration.getRequestHistoryNbrRetained()); |
77 | |
} |
78 | |
} |
79 | 0 | else if (StringUtils.isEmpty(nbrRequestsStr)) { |
80 | 3 | nbrRequestsTracked = DEFAULT_NBR_REQUESTS_TRACKED; |
81 | |
} |
82 | |
else { |
83 | |
try { |
84 | 3 | nbrRequestsTracked = Integer.valueOf(nbrRequestsStr); |
85 | 0 | } catch (NumberFormatException e) { |
86 | 0 | throw new Admin4jRuntimeException("request.history.nbr.retained must be numeric") |
87 | |
.addContextValue("request.history.nbr.retained", nbrRequestsStr); |
88 | 3 | } |
89 | |
} |
90 | 6 | Admin4JStandardFilterChain.registerFilter(this); |
91 | |
|
92 | 6 | } |
93 | |
|
94 | |
} |