Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mortbay/jetty/servlet/FilterHolder.java


1   // ===========================================================================
2   // Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved.
3   // $Id: FilterHolder.java,v 1.22 2003/09/18 13:29:24 gregwilkins Exp $
4   // ---------------------------------------------------------------------------
5   
6   package org.mortbay.jetty.servlet;
7   
8   import java.util.Enumeration;
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  import javax.servlet.Filter;
13  import javax.servlet.FilterConfig;
14  import javax.servlet.ServletContext;
15  
16  import org.mortbay.http.HttpHandler;
17  import org.mortbay.http.PathMap;
18  import org.mortbay.util.LazyList;
19  
20  /* --------------------------------------------------------------------- */
21  /** 
22   * @version $Id: FilterHolder.java,v 1.22 2003/09/18 13:29:24 gregwilkins Exp $
23   * @author Greg Wilkins
24   */
25  public class FilterHolder
26      extends Holder
27  {
28      /* ------------------------------------------------------------ */
29      public static final int
30          __REQUEST=1,
31          __FORWARD=2,
32          __INCLUDE=4,
33          __ERROR=8,
34          __ALL=15;
35      
36  
37      public static int type(String type)
38      {
39          if ("request".equalsIgnoreCase(type))
40              return __REQUEST;
41          if ("forward".equalsIgnoreCase(type))
42              return __FORWARD;
43          if ("include".equalsIgnoreCase(type))
44              return __INCLUDE;
45          if ("error".equalsIgnoreCase(type))
46              return __ERROR;
47          return 0;
48      }
49      
50      /* ------------------------------------------------------------ */
51      private PathMap _pathSpecs;
52      private int _appliesTo;
53      private Object _servlets;
54  
55      private transient Filter _filter;
56      private transient Config _config;
57          
58      /* ---------------------------------------------------------------- */
59      /** Constructor for Serialization.
60       */
61      public FilterHolder()
62      {}
63      
64      /* ---------------------------------------------------------------- */
65      public FilterHolder(HttpHandler httpHandler,
66                          String name,
67                          String className)
68      {
69          super(httpHandler,name,className);
70      }
71  
72      /* ------------------------------------------------------------ */
73      /** Add a type that this filter applies to.
74       * @param type Of __REQUEST, __FORWARD, __INCLUDE or __ERROR
75       */
76      public void addAppliesTo(int type)
77      {
78          _appliesTo|=type;
79      }
80  
81      /* ------------------------------------------------------------ */
82      /** Add a type that this filter applies to.
83       * @param type "REQUEST", "FORWARD", "INCLUDE" or "ERROR"
84       */
85      public void addAppliesTo(String type)
86      {
87          _appliesTo|=type(type);
88      }
89      
90      /* ------------------------------------------------------------ */
91      /** Add A servlet that this filter applies to.
92       * @param servlet 
93       */
94      public void addServlet(String servlet)
95      {
96          _servlets=LazyList.add(_servlets,servlet);
97      }
98      
99      /* ------------------------------------------------------------ */
100     /** Add A path spec that this filter applies to.
101      * @param pathSpec 
102      */
103     public void addPathSpec(String pathSpec)
104     {
105         if (_pathSpecs==null)
106             _pathSpecs=new PathMap();
107         _pathSpecs.put(pathSpec,pathSpec);
108     }
109     
110     /* ------------------------------------------------------------ */
111     public boolean isMappedToPath()
112     {
113         return _pathSpecs!=null;
114     }
115 
116     /* ------------------------------------------------------------ */
117     /** Check if this filter applies.
118      * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE or __ERROR.
119      * @return True if this filter applies
120      */
121     public boolean appliesTo(int type)
122     {
123         return  (_appliesTo&type)!=0 || _appliesTo==0&&type==__REQUEST ;
124     }
125     
126     /* ------------------------------------------------------------ */
127     /** Check if this filter applies to a path.
128      * @param path The path to check.
129      * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE or __ERROR.
130      * @return True if this filter applies
131      */
132     public boolean appliesTo(String path, int type)
133     {
134         return
135             ((_appliesTo&type)!=0 || _appliesTo==0&&type==__REQUEST ) &&
136             _pathSpecs!=null &&
137             _pathSpecs.getMatch(path)!=null;
138     }
139     
140     /* ------------------------------------------------------------ */
141     public String appliedPathSpec(String path)
142     {
143         if (_pathSpecs==null)
144             return null;
145         Map.Entry entry = _pathSpecs.getMatch(path);
146         if (entry==null)
147             return null;
148         return (String)entry.getKey();
149     }
150 
151     /* ------------------------------------------------------------ */
152     public void start()
153         throws Exception
154     {
155         super.start();
156         
157         if (!javax.servlet.Filter.class
158             .isAssignableFrom(_class))
159         {
160             super.stop();
161             throw new IllegalStateException(_class+" is not a javax.servlet.Filter");
162         }
163 
164         _filter=(Filter)newInstance();
165         _config=new Config();
166         _filter.init(_config);
167     }
168 
169     /* ------------------------------------------------------------ */
170     public void stop()
171     {
172         if (_filter!=null)
173             _filter.destroy();
174         _filter=null;
175         _config=null;
176         super.stop();   
177     }
178     
179     /* ------------------------------------------------------------ */
180     public Filter getFilter()
181     {
182         return _filter;
183     }
184 
185     /* ------------------------------------------------------------ */
186     public String[] getPaths()
187     {
188         if (_pathSpecs==null)
189             return null;
190         int s = _pathSpecs.keySet().size();
191         return (String[]) _pathSpecs.keySet().toArray(new String[s]);
192     }
193     
194     /* ------------------------------------------------------------ */
195     public String[] getServlets()
196     {
197         if (_servlets==null)
198             return null;
199         int s = LazyList.size(_servlets);
200         return (String[])LazyList.getList(_servlets).toArray(new String[s]);
201     }
202     
203     /* ------------------------------------------------------------ */
204     public String toString()
205     {
206         StringBuffer buf = new StringBuffer();
207         buf.append(getName());
208         buf.append('[');
209         buf.append(getClassName());
210         for (int i=0;i<LazyList.size(_servlets);i++)
211         {
212             buf.append(',');
213             buf.append(LazyList.get(_servlets,i));
214         }
215         if (_pathSpecs!=null)
216         {
217             Iterator iter = _pathSpecs.keySet().iterator();
218             while (iter.hasNext())
219             {
220                 buf.append(',');
221                 buf.append(iter.next());
222             }
223         }
224         buf.append(']');
225         return buf.toString();
226     }
227     
228     /* ------------------------------------------------------------ */
229     /* ------------------------------------------------------------ */
230     /* ------------------------------------------------------------ */
231     class Config implements FilterConfig
232     {
233         /* ------------------------------------------------------------ */
234         public String getFilterName()
235         {
236             return FilterHolder.this.getName();
237         }
238 
239         /* ------------------------------------------------------------ */
240         public ServletContext getServletContext()
241         {
242             return ((WebApplicationHandler)_httpHandler).getServletContext();
243         }
244         
245         /* -------------------------------------------------------- */
246         public String getInitParameter(String param)
247         {
248             return FilterHolder.this.getInitParameter(param);
249         }
250     
251         /* -------------------------------------------------------- */
252         public Enumeration getInitParameterNames()
253         {
254             return FilterHolder.this.getInitParameterNames();
255         }
256     }
257     
258 }
259 
260 
261 
262 
263