View Javadoc

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