1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.jumpmind.symmetric.web;
23
24 import java.io.IOException;
25
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.commons.lang.ObjectUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /***
37 *
38 * Configured within symmetric-web.xml
39 *
40 * <pre>
41 * <bean id="throttleFilter"
42 * class="org.jumpmind.symmetric.web.ThrottleFilter">
43 * <property name="regexPattern" value="string" />
44 * <property name="regexPatterns">
45 * <list>
46 * <value value="string"/>
47 * <list/>
48 * <property/>
49 * <property name="uriPattern" value="string" />
50 * <property name="uriPatterns">
51 * <list>
52 * <value value="string"/>
53 * <list/>
54 * <property/>
55 * <property name="disabled" value="boolean" />
56 * <property name="maxBps" value="long" />
57 * <property name="threshold" value="long" />
58 * <property name="checkPoint" value="long" />
59 * </bean>
60 * </pre>
61 */
62
63 public class ThrottleFilter extends AbstractFilter {
64
65 private final static Log logger = LogFactory.getLog(ThrottleFilter.class);
66
67 private Long maxBps;
68
69 private Long threshold;
70
71 private Long checkPoint;
72
73
74 private static final long DEFAULT_THRESHOLD = 8192L;
75
76
77 private static final long DEFAULT_CHECK_POINT = 1024L;
78
79 @Override
80 public boolean isContainerCompatible() {
81 return true;
82 }
83
84 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
85 ServletException {
86 ThrottledResponseWrapper wrapper = new ThrottledResponseWrapper((HttpServletResponse) response);
87 wrapper.setCheckPoint((Long) ObjectUtils.defaultIfNull(checkPoint, DEFAULT_CHECK_POINT));
88 wrapper.setMaxBps((Long) ObjectUtils.defaultIfNull(maxBps, 0L));
89 wrapper.setThreshold((Long) ObjectUtils.defaultIfNull(threshold, DEFAULT_THRESHOLD));
90 if (logger.isDebugEnabled()) {
91 logger.debug("Before hit servlet");
92 }
93 chain.doFilter(request, wrapper);
94
95 if (logger.isDebugEnabled()) {
96 logger.info("after hit servlet");
97 }
98 }
99
100 public void setMaxBps(Long maxBps) {
101 this.maxBps = maxBps;
102 }
103
104 public void setThreshold(Long threshold) {
105 this.threshold = threshold;
106 }
107
108 public void setCheckPoint(Long checkPoint) {
109 this.checkPoint = checkPoint;
110 }
111
112 public Long getMaxBps() {
113 return maxBps;
114 }
115
116 public Long getThreshold() {
117 return threshold;
118 }
119
120 public Long getCheckPoint() {
121 return checkPoint;
122 }
123
124 @Override
125 protected Log getLogger() {
126 return logger;
127 }
128
129 }