]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/servlet/FilterMapping.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / servlet / FilterMapping.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
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     /** Dispatch types */
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     /** Dispatch type from name
48      */
49     public static DispatcherType dispatch(String type)
50     {
51         if ("request".equalsIgnoreCase(type))
52             return DispatcherType.REQUEST;
53         if ("forward".equalsIgnoreCase(type))
54             return DispatcherType.FORWARD;
55         if ("include".equalsIgnoreCase(type))
56             return DispatcherType.INCLUDE;
57         if ("error".equalsIgnoreCase(type))
58             return DispatcherType.ERROR;
59         if ("async".equalsIgnoreCase(type))
60             return DispatcherType.ASYNC;
61         throw new IllegalArgumentException(type);
62     }
63
64     /* ------------------------------------------------------------ */
65     /** Dispatch type from name
66      */
67     public static int dispatch(DispatcherType type)
68     {
69         switch(type)
70         {
71           case REQUEST:
72                   return REQUEST;
73           case ASYNC:
74                   return ASYNC;
75           case FORWARD:
76                   return FORWARD;
77           case INCLUDE:
78                   return INCLUDE;
79           case ERROR:
80                   return ERROR;
81         }
82         throw new IllegalArgumentException(type.toString());
83     }
84
85
86     /* ------------------------------------------------------------ */
87     /* ------------------------------------------------------------ */
88
89
90     private int _dispatches=DEFAULT;
91     private String _filterName;
92     private transient FilterHolder _holder;
93     private String[] _pathSpecs;
94     private String[] _servletNames;
95
96     /* ------------------------------------------------------------ */
97     public FilterMapping()
98     {}
99
100     /* ------------------------------------------------------------ */
101     /** Check if this filter applies to a path.
102      * @param path The path to check or null to just check type
103      * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE, __ASYNC or __ERROR.
104      * @return True if this filter applies
105      */
106     boolean appliesTo(String path, int type)
107     {
108         if (appliesTo(type))
109         {
110             for (int i=0;i<_pathSpecs.length;i++)
111                 if (_pathSpecs[i]!=null &&  PathMap.match(_pathSpecs[i], path,true))
112                     return true;
113         }
114
115         return false;
116     }
117
118     /* ------------------------------------------------------------ */
119     /** Check if this filter applies to a particular dispatch type.
120      * @param type The type of request:
121      *      {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
122      * @return <code>true</code> if this filter applies
123      */
124     boolean appliesTo(int type)
125     {
126         if (_dispatches==0)
127                 return type==REQUEST || type==ASYNC && _holder.isAsyncSupported();
128         return (_dispatches&type)!=0;
129     }
130
131     /* ------------------------------------------------------------ */
132     public boolean appliesTo(DispatcherType t)
133     {
134         return appliesTo(dispatch(t));
135     }
136     
137     /* ------------------------------------------------------------ */
138     public boolean isDefaultDispatches()
139     {
140         return _dispatches==0;
141     }
142     
143     /* ------------------------------------------------------------ */
144     /**
145      * @return Returns the filterName.
146      */
147     @ManagedAttribute(value="filter name", readonly=true)
148     public String getFilterName()
149     {
150         return _filterName;
151     }
152
153     /* ------------------------------------------------------------ */
154     /**
155      * @return Returns the holder.
156      */
157     FilterHolder getFilterHolder()
158     {
159         return _holder;
160     }
161
162     /* ------------------------------------------------------------ */
163     /**
164      * @return Returns the pathSpec.
165      */
166     @ManagedAttribute(value="url patterns", readonly=true)
167     public String[] getPathSpecs()
168     {
169         return _pathSpecs;
170     }
171
172     /* ------------------------------------------------------------ */
173     public void setDispatcherTypes(EnumSet<DispatcherType> dispatcherTypes)
174     {
175         _dispatches=DEFAULT;
176         if (dispatcherTypes!=null)
177         {
178             if (dispatcherTypes.contains(DispatcherType.ERROR))
179                 _dispatches|=ERROR;
180             if (dispatcherTypes.contains(DispatcherType.FORWARD))
181                 _dispatches|=FORWARD;
182             if (dispatcherTypes.contains(DispatcherType.INCLUDE))
183                 _dispatches|=INCLUDE;
184             if (dispatcherTypes.contains(DispatcherType.REQUEST))
185                 _dispatches|=REQUEST;
186             if (dispatcherTypes.contains(DispatcherType.ASYNC))
187                 _dispatches|=ASYNC;
188         }
189     }
190
191     /* ------------------------------------------------------------ */
192     /**
193      * @param dispatches The dispatches to set.
194      * @see #DEFAULT
195      * @see #REQUEST
196      * @see #ERROR
197      * @see #FORWARD
198      * @see #INCLUDE
199      */
200     public void setDispatches(int dispatches)
201     {
202         _dispatches = dispatches;
203     }
204
205     /* ------------------------------------------------------------ */
206     /**
207      * @param filterName The filterName to set.
208      */
209     public void setFilterName(String filterName)
210     {
211         _filterName = filterName;
212     }
213
214     /* ------------------------------------------------------------ */
215     /**
216      * @param holder The holder to set.
217      */
218     void setFilterHolder(FilterHolder holder)
219     {
220         _holder = holder;
221         setFilterName(holder.getName());
222     }
223
224     /* ------------------------------------------------------------ */
225     /**
226      * @param pathSpecs The Path specifications to which this filter should be mapped.
227      */
228     public void setPathSpecs(String[] pathSpecs)
229     {
230         _pathSpecs = pathSpecs;
231     }
232
233     /* ------------------------------------------------------------ */
234     /**
235      * @param pathSpec The pathSpec to set.
236      */
237     public void setPathSpec(String pathSpec)
238     {
239         _pathSpecs = new String[]{pathSpec};
240     }
241
242     /* ------------------------------------------------------------ */
243     /**
244      * @return Returns the servletName.
245      */
246     @ManagedAttribute(value="servlet names", readonly=true)
247     public String[] getServletNames()
248     {
249         return _servletNames;
250     }
251
252     /* ------------------------------------------------------------ */
253     /**
254      * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
255      * @see #setServletName
256      */
257     public void setServletNames(String[] servletNames)
258     {
259         _servletNames = servletNames;
260     }
261
262     /* ------------------------------------------------------------ */
263     /**
264      * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
265      * @see #setServletNames
266      */
267     public void setServletName(String servletName)
268     {
269         _servletNames = new String[]{servletName};
270     }
271
272     /* ------------------------------------------------------------ */
273     public String toString()
274     {
275         return
276         TypeUtil.asList(_pathSpecs)+"/"+
277         TypeUtil.asList(_servletNames)+"=="+
278         _dispatches+"=>"+
279         _filterName;
280     }
281
282     /* ------------------------------------------------------------ */
283     public void dump(Appendable out, String indent) throws IOException
284     {
285         out.append(String.valueOf(this)).append("\n");
286     }
287
288     /* ------------------------------------------------------------ */
289     public String dump()
290     {
291         return ContainerLifeCycle.dump(this);
292     }
293 }