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