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