-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractComboFilter.java
97 lines (87 loc) · 2.89 KB
/
AbstractComboFilter.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.github.bordertech.taskmaster.servlet.combo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
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.annotation.WebFilter;
/**
* Combo Filter that allows a group of Filters to be defined in one class.
* <p>
* This allows multiple filters to be easily annotated in one class instead of multiple entries in a web.xml.
* </p>
* <p>
* The class is abstract as projects are expected to extend this class and annotate it with the {@link WebFilter} annotation.
* </p>
*
* @see WebFilter
* @see Filter
*/
public abstract class AbstractComboFilter implements Filter {
private final List<Filter> filters;
/**
* @param filter the first filter to combine
* @param filters the other filters to combine into a single filter
*
*/
protected AbstractComboFilter(final Filter filter, final Filter... filters) {
if (filter == null) {
throw new IllegalArgumentException("Filter cannot be null");
}
boolean hasNull = Arrays.stream(filters).anyMatch(Objects::isNull);
if (hasNull) {
throw new IllegalArgumentException("Filters cannot contain a null filter");
}
List<Filter> backing = new ArrayList<>();
backing.add(filter);
backing.addAll(Arrays.asList(filters));
this.filters = Collections.unmodifiableList(backing);
}
/**
* @param filters the group of filters to combine into a single filter
*/
protected AbstractComboFilter(final List<Filter> filters) {
if (filters == null || filters.isEmpty()) {
throw new IllegalArgumentException("List of filters cannot be null or empty");
}
boolean hasNull = filters.stream().anyMatch(Objects::isNull);
if (hasNull) {
throw new IllegalArgumentException("Filters list cannot contain a null filter");
}
this.filters = Collections.unmodifiableList(filters);
}
/**
* @return the list of filters to be combined
*/
public final List<Filter> getFilters() {
return filters;
}
@Override
public void init(final FilterConfig config) throws ServletException {
// Config each filter
for (Filter filter : getFilters()) {
filter.init(config);
}
}
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
ComboFilterChain combo = new ComboFilterChain(chain, getFilters());
combo.doFilter(request, response);
}
@Override
public void destroy() {
// Destroy in reverse order
for (int i = getFilters().size(); i-- > 0;) {
Filter filter = getFilters().get(i);
filter.destroy();
}
}
}