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