2

I made a simple servlet filter to slow the responses of my app down:

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DelayFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        Integer seconds = 10;
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            throw new ServletException("Interrupted!");
        }
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {}
}

I read a bunch of articles to register it for the app like this: enter link description here Usually there are two methods of registering, one for using web.xml and one for programmatic configuration. The Application I have to work with doesn't use the XML, but neither have any initializer class. Configuration ist done with a Config Class starting like this:

@Configuration
@EnableWebMvc
@EnableAsync
@EnableTransactionManagement
@EnableSpringConfigured
@PropertySource("classpath:config/application.properties")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

So I made an initializer

import javax.servlet.Filter;

public class arachneInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses () {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses () {
        return new Class<?>[]{ApplicationConfiguration.class};
    }

    @Override
    protected String[] getServletMappings () {
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] {
            new DelayFilter()
        };
    }
}

I am not sure, if this is correct or will it change they way my Application acts? Everything seems to be normal at a first glance though. But the filter does not kick in! Any ideas what I did wrong or suggestion how I can add the filter maybe without an Initializer?

Edit: I use Spring MVC 4.3.4.

Paflow
  • 1,581
  • 19
  • 43

2 Answers2

5

The @WebFilter annotation is used to declare a filter in a web application.So the servlet container will process your filter at deployment time and will associate to the specified URL in our case (/*)

Before that you should do it in the web.xml

@WebFilter(urlPatterns = {"/*"}, description = "My delay filter ")
    public class DelayFilter implements Filter {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {}

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            Integer seconds = 10;
            try {
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                throw new ServletException("Interrupted!");
            }
            HttpServletResponse response = (HttpServletResponse) resp;
            response.setHeader("Cache-Control", "no-cache, must-revalidate");
            chain.doFilter(req, resp);
        }

        @Override
        public void destroy() {}
    }
Novy
  • 1,281
  • 10
  • 23
1

There are several programmatic ways to register a filter using Spring MVC. You can check this answer.

isaolmez
  • 847
  • 8
  • 13