View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
5    *               Henglin Wang <henglinwang@users.sourceforge.net>,
6    *               Keith Naas <knaas@users.sourceforge.net>
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 3 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, see
20   * <http://www.gnu.org/licenses/>.
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   *  &lt;bean id=&quot;throttleFilter&quot;
42   *  class=&quot;org.jumpmind.symmetric.web.ThrottleFilter&quot;&gt;
43   *    &lt;property name=&quot;regexPattern&quot; value=&quot;string&quot; /&gt;
44   *    &lt;property name=&quot;regexPatterns&quot;&gt;
45   *      &lt;list&gt;
46   *        &lt;value value=&quot;string&quot;/&gt;
47   *      &lt;list/&gt;
48   *    &lt;property/&gt;
49   *    &lt;property name=&quot;uriPattern&quot; value=&quot;string&quot; /&gt;
50   *    &lt;property name=&quot;uriPatterns&quot;&gt;
51   *      &lt;list&gt;
52   *        &lt;value value=&quot;string&quot;/&gt;
53   *      &lt;list/&gt;
54   *    &lt;property/&gt;
55   *    &lt;property name=&quot;disabled&quot; value=&quot;boolean&quot; /&gt;
56   *    &lt;property name=&quot;maxBps&quot; value=&quot;long&quot; /&gt;
57   *    &lt;property name=&quot;threshold&quot; value=&quot;long&quot; /&gt;
58   *    &lt;property name=&quot;checkPoint&quot; value=&quot;long&quot; /&gt;
59   *  &lt;/bean&gt;
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      // default threshold before throttling in number of bytes
74      private static final long DEFAULT_THRESHOLD = 8192L;
75  
76      // default frequency to recalculation rate in number of bytes
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 }