1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.ui.filters; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.util.List; |
18 | |
import java.util.Map; |
19 | |
import java.util.Stack; |
20 | |
import java.util.concurrent.ConcurrentHashMap; |
21 | |
|
22 | |
import javax.servlet.Filter; |
23 | |
import javax.servlet.FilterChain; |
24 | |
import javax.servlet.ServletException; |
25 | |
import javax.servlet.ServletRequest; |
26 | |
import javax.servlet.ServletResponse; |
27 | |
|
28 | |
import org.slf4j.Logger; |
29 | |
import org.slf4j.LoggerFactory; |
30 | |
|
31 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
32 | |
import net.admin4j.deps.commons.lang3.Validate; |
33 | |
import net.admin4j.util.Admin4jRuntimeException; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class Admin4JStandardFilterChain implements FilterChain { |
42 | |
|
43 | 21 | private static Map<String, Filter> filterMap = new ConcurrentHashMap<String, Filter>(); |
44 | 21 | private static ThreadLocal<Stack<Filter>> localFilterStack = new ThreadLocal<Stack<Filter>>(); |
45 | 21 | private static Logger logger = LoggerFactory.getLogger(Admin4JStandardFilterChain.class); |
46 | |
private FilterChain delegateChain; |
47 | |
|
48 | 3 | public Admin4JStandardFilterChain(List<String> filterKey, FilterChain delegateChain) { |
49 | |
Validate.notNull(filterKey, "Null filterKey not allowed."); |
50 | |
Validate.notNull(delegateChain, "Null filterKey not allowed."); |
51 | |
|
52 | 3 | this.delegateChain = delegateChain; |
53 | |
|
54 | 3 | Stack<Filter> localStack = new Stack<Filter>(); |
55 | |
Filter localFilter; |
56 | |
|
57 | 18 | for (int i = filterKey.size() - 1; i>=0; i--) { |
58 | 0 | if ( !StringUtils.isEmpty(filterKey.get(i))) { |
59 | 15 | localFilter = filterMap.get(filterKey.get(i)); |
60 | 15 | if (localFilter == null) { |
61 | 0 | throw new Admin4jRuntimeException("Filter not recognized") |
62 | |
.addContextValue("filterKey", filterKey.get(i)); |
63 | |
} |
64 | 15 | localStack.push(localFilter); |
65 | |
} |
66 | |
} |
67 | |
|
68 | 3 | localFilterStack.set(localStack); |
69 | 3 | } |
70 | |
|
71 | |
public void doFilter(ServletRequest request, ServletResponse response) |
72 | |
throws IOException, ServletException { |
73 | 18 | Filter localFilter = null; |
74 | 18 | if (localFilterStack.get().size() > 0) { |
75 | 15 | localFilter = localFilterStack.get().pop(); |
76 | |
} |
77 | |
|
78 | 18 | if (localFilter != null) { |
79 | 15 | localFilter.doFilter(request, response, this); |
80 | |
} |
81 | 3 | else this.delegateChain.doFilter(request, response); |
82 | |
|
83 | 18 | } |
84 | |
|
85 | |
public static void registerFilter(Filter filter) { |
86 | 42 | registerFilter(filter, false); |
87 | 42 | } |
88 | |
|
89 | |
public static void registerFilter(Filter filter, boolean avoidDuplicateRegistration) { |
90 | |
Validate.notNull(filter, "Null filter not allowed."); |
91 | |
|
92 | 66 | synchronized(filterMap) { |
93 | 66 | if (avoidDuplicateRegistration && !filterMap.containsKey(filter.getClass().getName())) { |
94 | 3 | filterMap.put(filter.getClass().getName(), filter); |
95 | 3 | logger.info("Admin4J Filter registered: {}", filter.getClass().getName()); |
96 | |
} |
97 | 63 | else if ( !avoidDuplicateRegistration) { |
98 | 45 | filterMap.put(filter.getClass().getName(), filter); |
99 | 45 | logger.info("Admin4J Filter registered: {}", filter.getClass().getName()); |
100 | |
} |
101 | 66 | } |
102 | 66 | } |
103 | |
|
104 | |
public static boolean isRegistered(String filterClassName) { |
105 | |
Validate.notEmpty(filterClassName, "Null or blank filterClassName not allowed."); |
106 | 60 | return filterMap.containsKey(filterClassName); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected static void clearFilterMap() { |
113 | 3 | filterMap.clear(); |
114 | 3 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
protected static Filter getRegisteredFilter(String filterClassName) { |
122 | 12 | return filterMap.get(filterClassName); |
123 | |
} |
124 | |
|
125 | |
} |