View Javadoc

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