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 javax.servlet.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      * @param dispatches The dispatches to set.
176      * @see #DEFAULT
177      * @see #REQUEST
178      * @see #ERROR
179      * @see #FORWARD
180      * @see #INCLUDE
181      */
182     public void setDispatches(int dispatches)
183     {
184         _dispatches = dispatches;
185     }
186     
187     /* ------------------------------------------------------------ */
188     /**
189      * @param filterName The filterName to set.
190      */
191     public void setFilterName(String filterName)
192     {
193         _filterName = filterName;
194     }
195     
196     /* ------------------------------------------------------------ */
197     /**
198      * @param holder The holder to set.
199      */
200     void setFilterHolder(FilterHolder holder)
201     {
202         _holder = holder;
203         setFilterName(holder.getName());
204     }
205     
206     /* ------------------------------------------------------------ */
207     /**
208      * @param pathSpecs The Path specifications to which this filter should be mapped. 
209      */
210     public void setPathSpecs(String[] pathSpecs)
211     {
212         _pathSpecs = pathSpecs;
213     }
214     
215     /* ------------------------------------------------------------ */
216     /**
217      * @param pathSpec The pathSpec to set.
218      */
219     public void setPathSpec(String pathSpec)
220     {
221         _pathSpecs = new String[]{pathSpec};
222     }
223     
224     /* ------------------------------------------------------------ */
225     /**
226      * @return Returns the servletName.
227      */
228     public String[] getServletNames()
229     {
230         return _servletNames;
231     }
232     
233     /* ------------------------------------------------------------ */
234     /**
235      * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
236      * @see #setServletName
237      */
238     public void setServletNames(String[] servletNames)
239     {
240         _servletNames = servletNames;
241     }
242     
243     /* ------------------------------------------------------------ */
244     /**
245      * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
246      * @see #setServletNames
247      */
248     public void setServletName(String servletName)
249     {
250         _servletNames = new String[]{servletName};
251     }
252 
253     /* ------------------------------------------------------------ */
254     public String toString()
255     {
256         return 
257         TypeUtil.asList(_pathSpecs)+"/"+
258         TypeUtil.asList(_servletNames)+"=="+
259         _dispatches+"=>"+
260         _filterName; 
261     }
262 
263     /* ------------------------------------------------------------ */
264     public void dump(Appendable out, String indent) throws IOException
265     {
266         out.append(String.valueOf(this)).append("\n");
267     }
268 
269     /* ------------------------------------------------------------ */
270     public String dump()
271     {
272         return AggregateLifeCycle.dump(this);
273     }    
274 }