View Javadoc

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