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