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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
21 import java.io.IOException;
22 import java.util.EnumSet;
24 import javax.servlet.DispatcherType;
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;
34 @ManagedObject("Filter Mappings")
35 public class FilterMapping implements Dumpable
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;
47 /* ------------------------------------------------------------ */
48 /** Dispatch type from name
50 public static DispatcherType dispatch(String type)
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);
65 /* ------------------------------------------------------------ */
66 /** Dispatch type from name
68 public static int dispatch(DispatcherType type)
83 throw new IllegalArgumentException(type.toString());
87 /* ------------------------------------------------------------ */
88 /* ------------------------------------------------------------ */
91 private int _dispatches=DEFAULT;
92 private String _filterName;
93 private transient FilterHolder _holder;
94 private String[] _pathSpecs;
95 private String[] _servletNames;
97 /* ------------------------------------------------------------ */
98 public FilterMapping()
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
107 boolean appliesTo(String path, int type)
111 for (int i=0;i<_pathSpecs.length;i++)
112 if (_pathSpecs[i]!=null && PathMap.match(_pathSpecs[i], path,true))
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
125 boolean appliesTo(int type)
128 return type==REQUEST || type==ASYNC && _holder.isAsyncSupported();
129 return (_dispatches&type)!=0;
132 /* ------------------------------------------------------------ */
133 public boolean appliesTo(DispatcherType t)
135 return appliesTo(dispatch(t));
138 /* ------------------------------------------------------------ */
139 public boolean isDefaultDispatches()
141 return _dispatches==0;
144 /* ------------------------------------------------------------ */
146 * @return Returns the filterName.
148 @ManagedAttribute(value="filter name", readonly=true)
149 public String getFilterName()
154 /* ------------------------------------------------------------ */
156 * @return Returns the holder.
158 FilterHolder getFilterHolder()
163 /* ------------------------------------------------------------ */
165 * @return Returns the pathSpec.
167 @ManagedAttribute(value="url patterns", readonly=true)
168 public String[] getPathSpecs()
173 /* ------------------------------------------------------------ */
174 public void setDispatcherTypes(EnumSet<DispatcherType> dispatcherTypes)
177 if (dispatcherTypes!=null)
179 if (dispatcherTypes.contains(DispatcherType.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))
192 /* ------------------------------------------------------------ */
194 * @param dispatches The dispatches to set.
201 public void setDispatches(int dispatches)
203 _dispatches = dispatches;
206 /* ------------------------------------------------------------ */
208 * @param filterName The filterName to set.
210 public void setFilterName(String filterName)
212 _filterName = filterName;
215 /* ------------------------------------------------------------ */
217 * @param holder The holder to set.
219 void setFilterHolder(FilterHolder holder)
222 setFilterName(holder.getName());
225 /* ------------------------------------------------------------ */
227 * @param pathSpecs The Path specifications to which this filter should be mapped.
229 public void setPathSpecs(String[] pathSpecs)
231 _pathSpecs = pathSpecs;
234 /* ------------------------------------------------------------ */
236 * @param pathSpec The pathSpec to set.
238 public void setPathSpec(String pathSpec)
240 _pathSpecs = new String[]{pathSpec};
243 /* ------------------------------------------------------------ */
245 * @return Returns the servletName.
247 @ManagedAttribute(value="servlet names", readonly=true)
248 public String[] getServletNames()
250 return _servletNames;
253 /* ------------------------------------------------------------ */
255 * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
256 * @see #setServletName
258 public void setServletNames(String[] servletNames)
260 _servletNames = servletNames;
263 /* ------------------------------------------------------------ */
265 * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
266 * @see #setServletNames
268 public void setServletName(String servletName)
270 _servletNames = new String[]{servletName};
273 /* ------------------------------------------------------------ */
274 public String toString()
277 TypeUtil.asList(_pathSpecs)+"/"+
278 TypeUtil.asList(_servletNames)+"=="+
283 /* ------------------------------------------------------------ */
284 public void dump(Appendable out, String indent) throws IOException
286 out.append(String.valueOf(this)).append("\n");
289 /* ------------------------------------------------------------ */
292 return ContainerLifeCycle.dump(this);