class DoSFilter implements Filter {

  private Timer timer;
  private DoSTracker sourceAddrTracker;

  @Override
  public void init(FilterConfig filterConfig) {<FILL_FUNCTION_BODY>}

  @Override
  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    if (isBanned((HttpServletRequest) request)) {
      HttpServletResponse servletResponse = (HttpServletResponse) response;
      // Send very short response as requests may be very frequent
      servletResponse.setStatus(429); // 429 = Too Many Requests from RFC 6585
      servletResponse.getWriter().write("Forbidden");
    } else {
      chain.doFilter(request, response);
    }
  }

  private boolean isBanned(HttpServletRequest request) {
    String remoteHost = request.getHeader("x-forwarded-for");
    if (remoteHost != null) {
      int comma = remoteHost.indexOf(',');
      if (comma >= 0) {
        remoteHost = remoteHost.substring(0, comma);
      }
      remoteHost = remoteHost.trim();
    }
    // Non-short-circuit "|" below is on purpose
    return
      (remoteHost != null && sourceAddrTracker.isBanned(remoteHost)) |
      sourceAddrTracker.isBanned(request.getRemoteAddr());
  }

  @Override
  public void destroy() {
    if (timer != null) {
      timer.cancel();
    }
  }

}

    int maxAccessPerTime = Integer.parseInt(filterConfig.getInitParameter("maxAccessPerTime"));
    Preconditions.checkArgument(maxAccessPerTime > 0);

    int accessTimeSec = Integer.parseInt(filterConfig.getInitParameter("accessTimeSec"));
    Preconditions.checkArgument(accessTimeSec > 0);
    long accessTimeMS = TimeUnit.MILLISECONDS.convert(accessTimeSec, TimeUnit.SECONDS);

    String maxEntriesValue = filterConfig.getInitParameter("maxEntries");
    int maxEntries = Integer.MAX_VALUE;
    if (maxEntriesValue != null) {
      maxEntries = Integer.parseInt(maxEntriesValue);
      Preconditions.checkArgument(maxEntries > 0);
    }

    String maxLoadValue = filterConfig.getInitParameter("maxLoad");
    Double maxLoad = null;
    if (maxLoadValue != null) {
      maxLoad = Double.valueOf(maxLoadValue);
      Preconditions.checkArgument(maxLoad > 0.0);
    }

    String name = getClass().getSimpleName();
    timer = new Timer(name);
    sourceAddrTracker = new DoSTracker(timer, name, maxAccessPerTime, accessTimeMS, maxEntries, maxLoad);
  
<no_super_class>