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