|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.acegisecurity.util; |
|
16 |
| |
|
17 |
| import org.springframework.beans.factory.BeanFactoryUtils; |
|
18 |
| |
|
19 |
| import org.springframework.context.ApplicationContext; |
|
20 |
| |
|
21 |
| import org.springframework.web.context.support.WebApplicationContextUtils; |
|
22 |
| |
|
23 |
| import java.io.IOException; |
|
24 |
| |
|
25 |
| import java.util.Map; |
|
26 |
| |
|
27 |
| import javax.servlet.Filter; |
|
28 |
| import javax.servlet.FilterChain; |
|
29 |
| import javax.servlet.FilterConfig; |
|
30 |
| import javax.servlet.ServletException; |
|
31 |
| import javax.servlet.ServletRequest; |
|
32 |
| import javax.servlet.ServletResponse; |
|
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 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| public class FilterToBeanProxy implements Filter { |
|
103 |
| private Filter delegate; |
|
104 |
| private FilterConfig filterConfig; |
|
105 |
| private boolean initialized = false; |
|
106 |
| private boolean servletContainerManaged = false; |
|
107 |
| |
|
108 |
5
| public void destroy() {
|
|
109 |
5
| if ((delegate != null) && servletContainerManaged) {
|
|
110 |
0
| delegate.destroy();
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
4
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
115 |
| FilterChain chain) throws IOException, ServletException { |
|
116 |
4
| if (!initialized) {
|
|
117 |
1
| doInit();
|
|
118 |
| } |
|
119 |
| |
|
120 |
4
| delegate.doFilter(request, response, chain);
|
|
121 |
| } |
|
122 |
| |
|
123 |
9
| public void init(FilterConfig filterConfig) throws ServletException {
|
|
124 |
9
| this.filterConfig = filterConfig;
|
|
125 |
| |
|
126 |
9
| String strategy = filterConfig.getInitParameter("init");
|
|
127 |
| |
|
128 |
9
| if ((strategy != null) && strategy.toLowerCase().equals("lazy")) {
|
|
129 |
1
| return;
|
|
130 |
| } |
|
131 |
| |
|
132 |
8
| doInit();
|
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
0
| protected ApplicationContext getContext(FilterConfig filterConfig) {
|
|
144 |
0
| return WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
|
|
145 |
| } |
|
146 |
| |
|
147 |
9
| private synchronized void doInit() throws ServletException {
|
|
148 |
9
| if (initialized) {
|
|
149 |
| |
|
150 |
0
| return;
|
|
151 |
| } |
|
152 |
| |
|
153 |
9
| String targetBean = filterConfig.getInitParameter("targetBean");
|
|
154 |
| |
|
155 |
9
| if ("".equals(targetBean)) {
|
|
156 |
1
| targetBean = null;
|
|
157 |
| } |
|
158 |
| |
|
159 |
9
| String lifecycle = filterConfig.getInitParameter("lifecycle");
|
|
160 |
| |
|
161 |
9
| if ("servlet-container-managed".equals(lifecycle)) {
|
|
162 |
0
| servletContainerManaged = true;
|
|
163 |
| } |
|
164 |
| |
|
165 |
9
| ApplicationContext ctx = this.getContext(filterConfig);
|
|
166 |
| |
|
167 |
9
| String beanName = null;
|
|
168 |
| |
|
169 |
9
| if ((targetBean != null) && ctx.containsBean(targetBean)) {
|
|
170 |
2
| beanName = targetBean;
|
|
171 |
7
| } else if (targetBean != null) {
|
|
172 |
1
| throw new ServletException("targetBean '" + targetBean +
|
|
173 |
| "' not found in context"); |
|
174 |
| } else { |
|
175 |
6
| String targetClassString = filterConfig.getInitParameter(
|
|
176 |
| "targetClass"); |
|
177 |
| |
|
178 |
6
| if ((targetClassString == null) || "".equals(targetClassString)) {
|
|
179 |
1
| throw new ServletException(
|
|
180 |
| "targetClass or targetBean must be specified"); |
|
181 |
| } |
|
182 |
| |
|
183 |
5
| Class targetClass;
|
|
184 |
| |
|
185 |
5
| try {
|
|
186 |
5
| targetClass = Thread.currentThread().getContextClassLoader()
|
|
187 |
| .loadClass(targetClassString); |
|
188 |
| } catch (ClassNotFoundException ex) { |
|
189 |
1
| throw new ServletException("Class of type " +
|
|
190 |
| targetClassString + " not found in classloader"); |
|
191 |
| } |
|
192 |
| |
|
193 |
4
| Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx,
|
|
194 |
| targetClass, true, true); |
|
195 |
| |
|
196 |
4
| if (beans.size() == 0) {
|
|
197 |
1
| throw new ServletException(
|
|
198 |
| "Bean context must contain at least one bean of type " + |
|
199 |
| targetClassString); |
|
200 |
| } |
|
201 |
| |
|
202 |
3
| beanName = (String) beans.keySet().iterator().next();
|
|
203 |
| } |
|
204 |
| |
|
205 |
5
| Object object = ctx.getBean(beanName);
|
|
206 |
| |
|
207 |
5
| if (!(object instanceof Filter)) {
|
|
208 |
1
| throw new ServletException("Bean '" + beanName +
|
|
209 |
| "' does not implement javax.servlet.Filter"); |
|
210 |
| } |
|
211 |
| |
|
212 |
4
| delegate = (Filter) object;
|
|
213 |
| |
|
214 |
4
| if (servletContainerManaged) {
|
|
215 |
0
| delegate.init(filterConfig);
|
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
4
| initialized = true;
|
|
222 |
| } |
|
223 |
| } |