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 import org.eclipse.jetty.http.PathMap;
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 public void setDispatches(int dispatches)
185 {
186 _dispatches = dispatches;
187 }
188
189
190
191
192
193 public void setFilterName(String filterName)
194 {
195 _filterName = filterName;
196 }
197
198
199
200
201
202 void setFilterHolder(FilterHolder holder)
203 {
204 _holder = holder;
205 setFilterName(holder.getName());
206 }
207
208
209
210
211
212 public void setPathSpecs(String[] pathSpecs)
213 {
214 _pathSpecs = pathSpecs;
215 }
216
217
218
219
220
221 public void setPathSpec(String pathSpec)
222 {
223 _pathSpecs = new String[]{pathSpec};
224 }
225
226
227
228
229
230 public String[] getServletNames()
231 {
232 return _servletNames;
233 }
234
235
236
237
238
239
240 public void setServletNames(String[] servletNames)
241 {
242 _servletNames = servletNames;
243 }
244
245
246
247
248
249
250 public void setServletName(String servletName)
251 {
252 _servletNames = new String[]{servletName};
253 }
254
255
256 public String toString()
257 {
258 return
259 TypeUtil.asList(_pathSpecs)+"/"+
260 TypeUtil.asList(_servletNames)+"=="+
261 _dispatches+"=>"+
262 _filterName;
263 }
264
265
266 public void dump(Appendable out, String indent) throws IOException
267 {
268 out.append(String.valueOf(this)).append("\n");
269 }
270
271
272 public String dump()
273 {
274 return AggregateLifeCycle.dump(this);
275 }
276 }