class TimeoutFilter implements Filter {

  private ExecutorService executorService;
  private TimeLimiter timeLimiter;
  private int timeoutSec;

  @Override
  public void init(FilterConfig filterConfig) {
    executorService = Executors.newCachedThreadPool();
    timeLimiter = SimpleTimeLimiter.create(executorService);
    timeoutSec = Integer.parseInt(filterConfig.getInitParameter("timeoutSec"));
  }

  @Override
  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {<FILL_FUNCTION_BODY>}

  @Override
  public void destroy() {
    if (executorService != null) {
      executorService.shutdownNow();
    }
  }

}

    try {
      timeLimiter.callWithTimeout(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
          chain.doFilter(request, response);
          return null;
        }
      }, timeoutSec, TimeUnit.SECONDS);
    } catch (TimeoutException | InterruptedException e) {
      HttpServletResponse servletResponse = (HttpServletResponse) response;
      servletResponse.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
      servletResponse.getWriter().write("Request took too long");
    } catch (ExecutionException e) {
      if (e.getCause() instanceof ServletException) {
        throw (ServletException) e.getCause();
      }
      if (e.getCause() instanceof IOException) {
        throw (IOException) e.getCause();
      }
      throw new ServletException(e.getCause());
    }
  
<no_super_class>