2 // ========================================================================
3 // Copyright (c) 1995-2016 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.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;
33 @ManagedObject("Filter Mappings")
34 public class FilterMapping implements Dumpable
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;
46 /* ------------------------------------------------------------ */
47 /** Dispatch type from name
49 public static DispatcherType dispatch(String type)
51 if ("request".equalsIgnoreCase(type))
52 return DispatcherType.REQUEST;
53 if ("forward".equalsIgnoreCase(type))
54 return DispatcherType.FORWARD;
55 if ("include".equalsIgnoreCase(type))
56 return DispatcherType.INCLUDE;
57 if ("error".equalsIgnoreCase(type))
58 return DispatcherType.ERROR;
59 if ("async".equalsIgnoreCase(type))
60 return DispatcherType.ASYNC;
61 throw new IllegalArgumentException(type);
64 /* ------------------------------------------------------------ */
65 /** Dispatch type from name
67 public static int dispatch(DispatcherType type)
82 throw new IllegalArgumentException(type.toString());
86 /* ------------------------------------------------------------ */
87 /* ------------------------------------------------------------ */
90 private int _dispatches=DEFAULT;
91 private String _filterName;
92 private transient FilterHolder _holder;
93 private String[] _pathSpecs;
94 private String[] _servletNames;
96 /* ------------------------------------------------------------ */
97 public FilterMapping()
100 /* ------------------------------------------------------------ */
101 /** Check if this filter applies to a path.
102 * @param path The path to check or null to just check type
103 * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE, __ASYNC or __ERROR.
104 * @return True if this filter applies
106 boolean appliesTo(String path, int type)
110 for (int i=0;i<_pathSpecs.length;i++)
111 if (_pathSpecs[i]!=null && PathMap.match(_pathSpecs[i], path,true))
118 /* ------------------------------------------------------------ */
119 /** Check if this filter applies to a particular dispatch type.
120 * @param type The type of request:
121 * {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
122 * @return <code>true</code> if this filter applies
124 boolean appliesTo(int type)
127 return type==REQUEST || type==ASYNC && _holder.isAsyncSupported();
128 return (_dispatches&type)!=0;
131 /* ------------------------------------------------------------ */
132 public boolean appliesTo(DispatcherType t)
134 return appliesTo(dispatch(t));
137 /* ------------------------------------------------------------ */
138 public boolean isDefaultDispatches()
140 return _dispatches==0;
143 /* ------------------------------------------------------------ */
145 * @return Returns the filterName.
147 @ManagedAttribute(value="filter name", readonly=true)
148 public String getFilterName()
153 /* ------------------------------------------------------------ */
155 * @return Returns the holder.
157 FilterHolder getFilterHolder()
162 /* ------------------------------------------------------------ */
164 * @return Returns the pathSpec.
166 @ManagedAttribute(value="url patterns", readonly=true)
167 public String[] getPathSpecs()
172 /* ------------------------------------------------------------ */
173 public void setDispatcherTypes(EnumSet<DispatcherType> dispatcherTypes)
176 if (dispatcherTypes!=null)
178 if (dispatcherTypes.contains(DispatcherType.ERROR))
180 if (dispatcherTypes.contains(DispatcherType.FORWARD))
181 _dispatches|=FORWARD;
182 if (dispatcherTypes.contains(DispatcherType.INCLUDE))
183 _dispatches|=INCLUDE;
184 if (dispatcherTypes.contains(DispatcherType.REQUEST))
185 _dispatches|=REQUEST;
186 if (dispatcherTypes.contains(DispatcherType.ASYNC))
191 /* ------------------------------------------------------------ */
193 * @param dispatches The dispatches to set.
200 public void setDispatches(int dispatches)
202 _dispatches = dispatches;
205 /* ------------------------------------------------------------ */
207 * @param filterName The filterName to set.
209 public void setFilterName(String filterName)
211 _filterName = filterName;
214 /* ------------------------------------------------------------ */
216 * @param holder The holder to set.
218 void setFilterHolder(FilterHolder holder)
221 setFilterName(holder.getName());
224 /* ------------------------------------------------------------ */
226 * @param pathSpecs The Path specifications to which this filter should be mapped.
228 public void setPathSpecs(String[] pathSpecs)
230 _pathSpecs = pathSpecs;
233 /* ------------------------------------------------------------ */
235 * @param pathSpec The pathSpec to set.
237 public void setPathSpec(String pathSpec)
239 _pathSpecs = new String[]{pathSpec};
242 /* ------------------------------------------------------------ */
244 * @return Returns the servletName.
246 @ManagedAttribute(value="servlet names", readonly=true)
247 public String[] getServletNames()
249 return _servletNames;
252 /* ------------------------------------------------------------ */
254 * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
255 * @see #setServletName
257 public void setServletNames(String[] servletNames)
259 _servletNames = servletNames;
262 /* ------------------------------------------------------------ */
264 * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
265 * @see #setServletNames
267 public void setServletName(String servletName)
269 _servletNames = new String[]{servletName};
272 /* ------------------------------------------------------------ */
273 public String toString()
276 TypeUtil.asList(_pathSpecs)+"/"+
277 TypeUtil.asList(_servletNames)+"=="+
282 /* ------------------------------------------------------------ */
283 public void dump(Appendable out, String indent) throws IOException
285 out.append(String.valueOf(this)).append("\n");
288 /* ------------------------------------------------------------ */
291 return ContainerLifeCycle.dump(this);