001: //========================================================================
002: //$Id: FilterMapping.java,v 1.2 2005/11/01 11:42:53 gregwilkins Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.servlet;
017:
018: import java.util.Arrays;
019:
020: import org.mortbay.jetty.Handler;
021:
022: public class FilterMapping {
023: private int _dispatches = Handler.REQUEST;
024: private String _filterName;
025: private transient FilterHolder _holder;
026: private String[] _pathSpecs;
027: private String[] _servletNames;
028:
029: /* ------------------------------------------------------------ */
030: public FilterMapping() {
031: }
032:
033: /* ------------------------------------------------------------ */
034: /** Check if this filter applies to a path.
035: * @param path The path to check or null to just check type
036: * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE or __ERROR.
037: * @return True if this filter applies
038: */
039: boolean appliesTo(String path, int type) {
040: if (((_dispatches & type) != 0 || (_dispatches == 0 && type == Handler.REQUEST))
041: && _pathSpecs != null) {
042: for (int i = 0; i < _pathSpecs.length; i++)
043: if (_pathSpecs[i] != null
044: && PathMap.match(_pathSpecs[i], path, true))
045: return true;
046: }
047: return false;
048: }
049:
050: /* ------------------------------------------------------------ */
051: /** Check if this filter applies to a particular dispatch type.
052: * @param type The type of request:
053: * {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
054: * @return <code>true</code> if this filter applies
055: */
056: boolean appliesTo(int type) {
057: if (((_dispatches & type) != 0 || (_dispatches == 0 && type == Handler.REQUEST)))
058: return true;
059: return false;
060: }
061:
062: /* ------------------------------------------------------------ */
063: /**
064: * @return Returns the dispatches.
065: */
066: public int getDispatches() {
067: return _dispatches;
068: }
069:
070: /* ------------------------------------------------------------ */
071: /**
072: * @return Returns the filterName.
073: */
074: public String getFilterName() {
075: return _filterName;
076: }
077:
078: /* ------------------------------------------------------------ */
079: /**
080: * @return Returns the holder.
081: */
082: FilterHolder getFilterHolder() {
083: return _holder;
084: }
085:
086: /* ------------------------------------------------------------ */
087: /**
088: * @return Returns the pathSpec.
089: */
090: public String[] getPathSpecs() {
091: return _pathSpecs;
092: }
093:
094: /* ------------------------------------------------------------ */
095: /**
096: * @param dispatches The dispatches to set.
097: * @see Handler#DEFAULT
098: * @see Handler#REQUEST
099: * @see Handler#ERROR
100: * @see Handler#FORWARD
101: * @see Handler#INCLUDE
102: */
103: public void setDispatches(int dispatches) {
104: _dispatches = dispatches;
105: }
106:
107: /* ------------------------------------------------------------ */
108: /**
109: * @param filterName The filterName to set.
110: */
111: public void setFilterName(String filterName) {
112: _filterName = filterName;
113: }
114:
115: /* ------------------------------------------------------------ */
116: /**
117: * @param holder The holder to set.
118: */
119: void setFilterHolder(FilterHolder holder) {
120: _holder = holder;
121: }
122:
123: /* ------------------------------------------------------------ */
124: /**
125: * @param pathSpecs The Path specifications to which this filter should be mapped.
126: */
127: public void setPathSpecs(String[] pathSpecs) {
128: _pathSpecs = pathSpecs;
129: }
130:
131: /* ------------------------------------------------------------ */
132: /**
133: * @param pathSpec The pathSpec to set.
134: */
135: public void setPathSpec(String pathSpec) {
136: _pathSpecs = new String[] { pathSpec };
137: }
138:
139: /* ------------------------------------------------------------ */
140: /**
141: * @return Returns the servletName.
142: */
143: public String[] getServletNames() {
144: return _servletNames;
145: }
146:
147: /* ------------------------------------------------------------ */
148: /**
149: * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
150: * @see #setServletName
151: */
152: public void setServletNames(String[] servletNames) {
153: _servletNames = servletNames;
154: }
155:
156: /* ------------------------------------------------------------ */
157: /**
158: * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
159: * @see #setServletNames
160: */
161: public void setServletName(String servletName) {
162: _servletNames = new String[] { servletName };
163: }
164:
165: /* ------------------------------------------------------------ */
166: public String toString() {
167: return "(F="
168: + _filterName
169: + ","
170: + (_pathSpecs == null ? "[]" : Arrays
171: .asList(_pathSpecs).toString())
172: + ","
173: + (_servletNames == null ? "[]" : Arrays.asList(
174: _servletNames).toString()) + "," + _dispatches
175: + ")";
176: }
177: }
|