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