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.io.IOException;
22 import java.util.EnumSet;
23
24 import javax.servlet.DispatcherType;
25
26 import org.eclipse.jetty.http.PathMap;
27 import org.eclipse.jetty.util.TypeUtil;
28 import org.eclipse.jetty.util.annotation.ManagedAttribute;
29 import org.eclipse.jetty.util.annotation.ManagedObject;
30 import org.eclipse.jetty.util.component.ContainerLifeCycle;
31 import org.eclipse.jetty.util.component.Dumpable;
32
33 @ManagedObject("Filter Mappings")
34 public class FilterMapping implements Dumpable
35 {
36
37 public static final int DEFAULT=0;
38 public static final int REQUEST=1;
39 public static final int FORWARD=2;
40 public static final int INCLUDE=4;
41 public static final int ERROR=8;
42 public static final int ASYNC=16;
43 public static final int ALL=31;
44
45
46
47
48
49 public static DispatcherType dispatch(String type)
50 {
51 if ("request".equalsIgnoreCase(type))
52 return DispatcherType.REQUEST;
53 if ("forward".equalsIgnoreCase(type))
54 return DispatcherType.FORWARD;
55 if ("include".equalsIgnoreCase(type))
56 return DispatcherType.INCLUDE;
57 if ("error".equalsIgnoreCase(type))
58 return DispatcherType.ERROR;
59 if ("async".equalsIgnoreCase(type))
60 return DispatcherType.ASYNC;
61 throw new IllegalArgumentException(type);
62 }
63
64
65
66
67 public static int dispatch(DispatcherType type)
68 {
69 switch(type)
70 {
71 case REQUEST:
72 return REQUEST;
73 case ASYNC:
74 return ASYNC;
75 case FORWARD:
76 return FORWARD;
77 case INCLUDE:
78 return INCLUDE;
79 case ERROR:
80 return ERROR;
81 }
82 throw new IllegalArgumentException(type.toString());
83 }
84
85
86
87
88
89
90 private int _dispatches=DEFAULT;
91 private String _filterName;
92 private transient FilterHolder _holder;
93 private String[] _pathSpecs;
94 private String[] _servletNames;
95
96
97 public FilterMapping()
98 {}
99
100
101
102
103
104
105
106 boolean appliesTo(String path, int type)
107 {
108 if (appliesTo(type))
109 {
110 for (int i=0;i<_pathSpecs.length;i++)
111 if (_pathSpecs[i]!=null && PathMap.match(_pathSpecs[i], path,true))
112 return true;
113 }
114
115 return false;
116 }
117
118
119
120
121
122
123
124 boolean appliesTo(int type)
125 {
126 if (_dispatches==0)
127 return type==REQUEST || type==ASYNC && _holder.isAsyncSupported();
128 return (_dispatches&type)!=0;
129 }
130
131
132 public boolean appliesTo(DispatcherType t)
133 {
134 return appliesTo(dispatch(t));
135 }
136
137
138 public boolean isDefaultDispatches()
139 {
140 return _dispatches==0;
141 }
142
143
144
145
146
147 @ManagedAttribute(value="filter name", readonly=true)
148 public String getFilterName()
149 {
150 return _filterName;
151 }
152
153
154
155
156
157 FilterHolder getFilterHolder()
158 {
159 return _holder;
160 }
161
162
163
164
165
166 @ManagedAttribute(value="url patterns", readonly=true)
167 public String[] getPathSpecs()
168 {
169 return _pathSpecs;
170 }
171
172
173 public void setDispatcherTypes(EnumSet<DispatcherType> dispatcherTypes)
174 {
175 _dispatches=DEFAULT;
176 if (dispatcherTypes!=null)
177 {
178 if (dispatcherTypes.contains(DispatcherType.ERROR))
179 _dispatches|=ERROR;
180 if (dispatcherTypes.contains(DispatcherType.FORWARD))
181 _dispatches|=FORWARD;
182 if (dispatcherTypes.contains(DispatcherType.INCLUDE))
183 _dispatches|=INCLUDE;
184 if (dispatcherTypes.contains(DispatcherType.REQUEST))
185 _dispatches|=REQUEST;
186 if (dispatcherTypes.contains(DispatcherType.ASYNC))
187 _dispatches|=ASYNC;
188 }
189 }
190
191
192
193
194
195
196
197
198
199
200 public void setDispatches(int dispatches)
201 {
202 _dispatches = dispatches;
203 }
204
205
206
207
208
209 public void setFilterName(String filterName)
210 {
211 _filterName = filterName;
212 }
213
214
215
216
217
218 void setFilterHolder(FilterHolder holder)
219 {
220 _holder = holder;
221 setFilterName(holder.getName());
222 }
223
224
225
226
227
228 public void setPathSpecs(String[] pathSpecs)
229 {
230 _pathSpecs = pathSpecs;
231 }
232
233
234
235
236
237 public void setPathSpec(String pathSpec)
238 {
239 _pathSpecs = new String[]{pathSpec};
240 }
241
242
243
244
245
246 @ManagedAttribute(value="servlet names", readonly=true)
247 public String[] getServletNames()
248 {
249 return _servletNames;
250 }
251
252
253
254
255
256
257 public void setServletNames(String[] servletNames)
258 {
259 _servletNames = servletNames;
260 }
261
262
263
264
265
266
267 public void setServletName(String servletName)
268 {
269 _servletNames = new String[]{servletName};
270 }
271
272
273 public String toString()
274 {
275 return
276 TypeUtil.asList(_pathSpecs)+"/"+
277 TypeUtil.asList(_servletNames)+"=="+
278 _dispatches+"=>"+
279 _filterName;
280 }
281
282
283 public void dump(Appendable out, String indent) throws IOException
284 {
285 out.append(String.valueOf(this)).append("\n");
286 }
287
288
289 public String dump()
290 {
291 return ContainerLifeCycle.dump(this);
292 }
293 }