/
BrokenFilter.java
63 lines (50 loc) · 1.86 KB
/
BrokenFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package demo.filter;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.stereotype.Component;
/**
* A demo filter that deliberately throws an exception if the URL "/broken" is
* invoked. Due to Spring Boot auto-configuration, the exception will be
* reported via the usual Spring Boot page (by default the Whitelabel error page
* but in this application, our Thymeleaf error-page).
* <p>
* As this is a Spring Boot application, the filter can be created as a Spring
* Bean by the component scanner in the usual way and Spring Boot will register
* it as a filter, mapped to "/*" by default. For more control consider creating
* a {@link FilterRegistrationBean} to configure it.
*/
@Component
public class BrokenFilter implements Filter {
@SuppressWarnings("serial")
protected static class FilterException extends RuntimeException {
public FilterException(String message) {
super(message);
}
}
private Logger logger = Logger.getLogger(BrokenFilter.class.getName());
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
if (((HttpServletRequest) request).getRequestURI().endsWith("broken")) {
logger.severe("BROKEN FILTER FORCES AN EXCEPTION");
throw new FilterException("Failure in BrokenFilter");
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void destroy() {
}
}