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