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