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