View Javadoc

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