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