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