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