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 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     /**
134      * @return Returns the filterName.
135      */
136     @ManagedAttribute(value="filter name", readonly=true)
137     public String getFilterName()
138     {
139         return _filterName;
140     }
141 
142     /* ------------------------------------------------------------ */
143     /**
144      * @return Returns the holder.
145      */
146     FilterHolder getFilterHolder()
147     {
148         return _holder;
149     }
150 
151     /* ------------------------------------------------------------ */
152     /**
153      * @return Returns the pathSpec.
154      */
155     @ManagedAttribute(value="url patterns", readonly=true)
156     public String[] getPathSpecs()
157     {
158         return _pathSpecs;
159     }
160 
161     /* ------------------------------------------------------------ */
162     public void setDispatcherTypes(EnumSet<DispatcherType> dispatcherTypes)
163     {
164         _dispatches=DEFAULT;
165         if (dispatcherTypes!=null)
166         {
167             if (dispatcherTypes.contains(DispatcherType.ERROR))
168                 _dispatches|=ERROR;
169             if (dispatcherTypes.contains(DispatcherType.FORWARD))
170                 _dispatches|=FORWARD;
171             if (dispatcherTypes.contains(DispatcherType.INCLUDE))
172                 _dispatches|=INCLUDE;
173             if (dispatcherTypes.contains(DispatcherType.REQUEST))
174                 _dispatches|=REQUEST;
175             if (dispatcherTypes.contains(DispatcherType.ASYNC))
176                 _dispatches|=ASYNC;
177         }
178     }
179 
180     /* ------------------------------------------------------------ */
181     /**
182      * @param dispatches The dispatches to set.
183      * @see #DEFAULT
184      * @see #REQUEST
185      * @see #ERROR
186      * @see #FORWARD
187      * @see #INCLUDE
188      */
189     public void setDispatches(int dispatches)
190     {
191         _dispatches = dispatches;
192     }
193 
194     /* ------------------------------------------------------------ */
195     /**
196      * @param filterName The filterName to set.
197      */
198     public void setFilterName(String filterName)
199     {
200         _filterName = filterName;
201     }
202 
203     /* ------------------------------------------------------------ */
204     /**
205      * @param holder The holder to set.
206      */
207     void setFilterHolder(FilterHolder holder)
208     {
209         _holder = holder;
210         setFilterName(holder.getName());
211     }
212 
213     /* ------------------------------------------------------------ */
214     /**
215      * @param pathSpecs The Path specifications to which this filter should be mapped.
216      */
217     public void setPathSpecs(String[] pathSpecs)
218     {
219         _pathSpecs = pathSpecs;
220     }
221 
222     /* ------------------------------------------------------------ */
223     /**
224      * @param pathSpec The pathSpec to set.
225      */
226     public void setPathSpec(String pathSpec)
227     {
228         _pathSpecs = new String[]{pathSpec};
229     }
230 
231     /* ------------------------------------------------------------ */
232     /**
233      * @return Returns the servletName.
234      */
235     @ManagedAttribute(value="servlet names", readonly=true)
236     public String[] getServletNames()
237     {
238         return _servletNames;
239     }
240 
241     /* ------------------------------------------------------------ */
242     /**
243      * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
244      * @see #setServletName
245      */
246     public void setServletNames(String[] servletNames)
247     {
248         _servletNames = servletNames;
249     }
250 
251     /* ------------------------------------------------------------ */
252     /**
253      * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
254      * @see #setServletNames
255      */
256     public void setServletName(String servletName)
257     {
258         _servletNames = new String[]{servletName};
259     }
260 
261     /* ------------------------------------------------------------ */
262     public String toString()
263     {
264         return
265         TypeUtil.asList(_pathSpecs)+"/"+
266         TypeUtil.asList(_servletNames)+"=="+
267         _dispatches+"=>"+
268         _filterName;
269     }
270 
271     /* ------------------------------------------------------------ */
272     public void dump(Appendable out, String indent) throws IOException
273     {
274         out.append(String.valueOf(this)).append("\n");
275     }
276 
277     /* ------------------------------------------------------------ */
278     public String dump()
279     {
280         return ContainerLifeCycle.dump(this);
281     }
282 }