1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.servlet;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.EnumSet;
25 import java.util.List;
26
27 import javax.servlet.DispatcherType;
28 import javax.servlet.Filter;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.FilterRegistration;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletException;
33
34 import org.eclipse.jetty.util.TypeUtil;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
37
38
39
40
41
42 public class FilterHolder extends Holder<Filter>
43 {
44 private static final Logger LOG = Log.getLogger(FilterHolder.class);
45
46
47 private transient Filter _filter;
48 private transient Config _config;
49 private transient FilterRegistration.Dynamic _registration;
50
51
52
53
54 public FilterHolder()
55 {
56 this(Source.EMBEDDED);
57 }
58
59
60
61
62
63 public FilterHolder(Holder.Source source)
64 {
65 super(source);
66 }
67
68
69
70
71 public FilterHolder(Class<? extends Filter> filter)
72 {
73 this(Source.EMBEDDED);
74 setHeldClass(filter);
75 }
76
77
78
79
80 public FilterHolder(Filter filter)
81 {
82 this(Source.EMBEDDED);
83 setFilter(filter);
84 }
85
86
87 @Override
88 public void doStart()
89 throws Exception
90 {
91 super.doStart();
92
93 if (!javax.servlet.Filter.class
94 .isAssignableFrom(_class))
95 {
96 String msg = _class+" is not a javax.servlet.Filter";
97 super.stop();
98 throw new IllegalStateException(msg);
99 }
100 }
101
102
103
104
105
106
107
108 @Override
109 public void initialize() throws Exception
110 {
111 super.initialize();
112
113 if (_filter==null)
114 {
115 try
116 {
117 ServletContext context=_servletHandler.getServletContext();
118 _filter=(context instanceof ServletContextHandler.Context)
119 ?((ServletContextHandler.Context)context).createFilter(getHeldClass())
120 :getHeldClass().newInstance();
121 }
122 catch (ServletException se)
123 {
124 Throwable cause = se.getRootCause();
125 if (cause instanceof InstantiationException)
126 throw (InstantiationException)cause;
127 if (cause instanceof IllegalAccessException)
128 throw (IllegalAccessException)cause;
129 throw se;
130 }
131 }
132
133 _config=new Config();
134 LOG.debug("Filter.init {}",_filter);
135 _filter.init(_config);
136 }
137
138
139
140 @Override
141 public void doStop()
142 throws Exception
143 {
144 if (_filter!=null)
145 {
146 try
147 {
148 destroyInstance(_filter);
149 }
150 catch (Exception e)
151 {
152 LOG.warn(e);
153 }
154 }
155 if (!_extInstance)
156 _filter=null;
157
158 _config=null;
159 super.doStop();
160 }
161
162
163 @Override
164 public void destroyInstance (Object o)
165 throws Exception
166 {
167 if (o==null)
168 return;
169 Filter f = (Filter)o;
170 f.destroy();
171 getServletHandler().destroyFilter(f);
172 }
173
174
175 public synchronized void setFilter(Filter filter)
176 {
177 _filter=filter;
178 _extInstance=true;
179 setHeldClass(filter.getClass());
180 if (getName()==null)
181 setName(filter.getClass().getName());
182 }
183
184
185 public Filter getFilter()
186 {
187 return _filter;
188 }
189
190
191 @Override
192 public String toString()
193 {
194 return getName();
195 }
196
197
198 public FilterRegistration.Dynamic getRegistration()
199 {
200 if (_registration == null)
201 _registration = new Registration();
202 return _registration;
203 }
204
205
206
207
208 protected class Registration extends HolderRegistration implements FilterRegistration.Dynamic
209 {
210 public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames)
211 {
212 illegalStateIfContextStarted();
213 FilterMapping mapping = new FilterMapping();
214 mapping.setFilterHolder(FilterHolder.this);
215 mapping.setServletNames(servletNames);
216 mapping.setDispatcherTypes(dispatcherTypes);
217 if (isMatchAfter)
218 _servletHandler.addFilterMapping(mapping);
219 else
220 _servletHandler.prependFilterMapping(mapping);
221 }
222
223 public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns)
224 {
225 illegalStateIfContextStarted();
226 FilterMapping mapping = new FilterMapping();
227 mapping.setFilterHolder(FilterHolder.this);
228 mapping.setPathSpecs(urlPatterns);
229 mapping.setDispatcherTypes(dispatcherTypes);
230 if (isMatchAfter)
231 _servletHandler.addFilterMapping(mapping);
232 else
233 _servletHandler.prependFilterMapping(mapping);
234 }
235
236 public Collection<String> getServletNameMappings()
237 {
238 FilterMapping[] mappings =_servletHandler.getFilterMappings();
239 List<String> names=new ArrayList<String>();
240 for (FilterMapping mapping : mappings)
241 {
242 if (mapping.getFilterHolder()!=FilterHolder.this)
243 continue;
244 String[] servlets=mapping.getServletNames();
245 if (servlets!=null && servlets.length>0)
246 names.addAll(Arrays.asList(servlets));
247 }
248 return names;
249 }
250
251 public Collection<String> getUrlPatternMappings()
252 {
253 FilterMapping[] mappings =_servletHandler.getFilterMappings();
254 List<String> patterns=new ArrayList<String>();
255 for (FilterMapping mapping : mappings)
256 {
257 if (mapping.getFilterHolder()!=FilterHolder.this)
258 continue;
259 String[] specs=mapping.getPathSpecs();
260 patterns.addAll(TypeUtil.asList(specs));
261 }
262 return patterns;
263 }
264 }
265
266
267
268
269 class Config extends HolderConfig implements FilterConfig
270 {
271
272 public String getFilterName()
273 {
274 return _name;
275 }
276 }
277 }