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