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