View Javadoc

1   //========================================================================
2   //$Id: FilterMapping.java,v 1.2 2005/11/01 11:42:53 gregwilkins Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.jetty.servlet;
17  
18  import java.util.Arrays;
19  
20  import org.mortbay.jetty.Handler;
21  
22  
23  public class FilterMapping
24  {
25      private int _dispatches=Handler.REQUEST;
26      private String _filterName;
27      private transient FilterHolder _holder;
28      private String[] _pathSpecs;
29      private String[] _servletNames;
30  
31      /* ------------------------------------------------------------ */
32      public FilterMapping()
33      {}
34      
35      /* ------------------------------------------------------------ */
36      /** Check if this filter applies to a path.
37       * @param path The path to check or null to just check type
38       * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE or __ERROR.
39       * @return True if this filter applies
40       */
41      boolean appliesTo(String path, int type)
42      {
43         if ( ((_dispatches&type)!=0 || (_dispatches==0 && type==Handler.REQUEST)) && _pathSpecs!=null )
44         {
45             for (int i=0;i<_pathSpecs.length;i++)
46                 if (_pathSpecs[i]!=null &&  PathMap.match(_pathSpecs[i], path,true))
47                     return true;
48         }
49         return false;
50      }
51      
52      /* ------------------------------------------------------------ */
53      /** Check if this filter applies to a particular dispatch type.
54       * @param type The type of request:
55       *      {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
56       * @return <code>true</code> if this filter applies
57       */
58      boolean appliesTo(int type)
59      {
60         if ( ((_dispatches&type)!=0 || (_dispatches==0 && type==Handler.REQUEST)))
61             return true;
62         return false;
63      }
64  
65      
66      /* ------------------------------------------------------------ */
67      /**
68       * @return Returns the dispatches.
69       */
70      public int getDispatches()
71      {
72          return _dispatches;
73      }
74      
75      /* ------------------------------------------------------------ */
76      /**
77       * @return Returns the filterName.
78       */
79      public String getFilterName()
80      {
81          return _filterName;
82      }
83      
84      /* ------------------------------------------------------------ */
85      /**
86       * @return Returns the holder.
87       */
88      FilterHolder getFilterHolder()
89      {
90          return _holder;
91      }
92      
93      /* ------------------------------------------------------------ */
94      /**
95       * @return Returns the pathSpec.
96       */
97      public String[] getPathSpecs()
98      {
99          return _pathSpecs;
100     }
101     
102     /* ------------------------------------------------------------ */
103     /**
104      * @param dispatches The dispatches to set.
105      * @see Handler#DEFAULT
106      * @see Handler#REQUEST
107      * @see Handler#ERROR
108      * @see Handler#FORWARD
109      * @see Handler#INCLUDE
110      */
111     public void setDispatches(int dispatches)
112     {
113         _dispatches = dispatches;
114     }
115     
116     /* ------------------------------------------------------------ */
117     /**
118      * @param filterName The filterName to set.
119      */
120     public void setFilterName(String filterName)
121     {
122         _filterName = filterName;
123     }
124     
125     /* ------------------------------------------------------------ */
126     /**
127      * @param holder The holder to set.
128      */
129     void setFilterHolder(FilterHolder holder)
130     {
131         _holder = holder;
132     }
133     
134     /* ------------------------------------------------------------ */
135     /**
136      * @param pathSpecs The Path specifications to which this filter should be mapped. 
137      */
138     public void setPathSpecs(String[] pathSpecs)
139     {
140         _pathSpecs = pathSpecs;
141     }
142     
143     /* ------------------------------------------------------------ */
144     /**
145      * @param pathSpec The pathSpec to set.
146      */
147     public void setPathSpec(String pathSpec)
148     {
149         _pathSpecs = new String[]{pathSpec};
150     }
151     
152     /* ------------------------------------------------------------ */
153     /**
154      * @return Returns the servletName.
155      */
156     public String[] getServletNames()
157     {
158         return _servletNames;
159     }
160     
161     /* ------------------------------------------------------------ */
162     /**
163      * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets
164      * @see #setServletName
165      */
166     public void setServletNames(String[] servletNames)
167     {
168         _servletNames = servletNames;
169     }
170     
171     /* ------------------------------------------------------------ */
172     /**
173      * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet
174      * @see #setServletNames
175      */
176     public void setServletName(String servletName)
177     {
178         _servletNames = new String[]{servletName};
179     }
180 
181     /* ------------------------------------------------------------ */
182     public String toString()
183     {
184         return "(F="+_filterName+","+(_pathSpecs==null?"[]":Arrays.asList(_pathSpecs).toString())+","+(_servletNames==null?"[]":Arrays.asList(_servletNames).toString())+","+_dispatches+")"; 
185     }
186 }