@Override
  public void doFilter(ServletRequest request, ServletResponse response)
      throws IOException, ServletException {
    if (scriptIterator.hasNext()) {
      Script script = scriptIterator.next();

      if (logger.isDebugEnabled()) {
        logger.debug("Executing filter script at " + script.getUrl());
      }

      HttpServletRequest httpRequest = (HttpServletRequest) request;
      HttpServletResponse httpResponse = (HttpServletResponse) response;
      Map<String, Object> variables = new HashMap<>();

      GroovyUtils.addCommonVariables(variables, httpRequest, httpResponse, servletContext);
      GroovyUtils.addSecurityVariables(variables);
      GroovyUtils.addFilterChainVariable(variables, this);

      try {
        script.execute(variables);
      } catch (ScriptException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ServletException) {
          throw (ServletException) cause;
        } else {
          throw new ServletException("Error executing filter script at " + script.getUrl(), cause);
        }
      }
    } else {
      delegateChain.doFilter(request, response);
    }
  }
  protected Map<String, Object> createScriptVariables(
      HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> scriptVariables = new HashMap<String, Object>();
    GroovyUtils.addCommonVariables(scriptVariables, request, response, getServletContext());
    GroovyUtils.addSecurityVariables(scriptVariables);

    return scriptVariables;
  }
  protected Map<String, Object> createScriptVariables(
      HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> variables = new HashMap<String, Object>();
    GroovyUtils.addRestScriptVariables(variables, request, response, getServletContext());

    return variables;
  }