KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > FilterMapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.dispatch;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.Filter JavaDoc;
36 import javax.servlet.FilterChain JavaDoc;
37 import javax.servlet.ServletContext JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Manages dispatching: servlets and filters.
44  */

45 public class FilterMapper {
46   static final Logger JavaDoc log = Log.open(FilterMapper.class);
47   static final L10N L = new L10N(FilterMapper.class);
48
49   private ServletContext JavaDoc _servletContext;
50   
51   private int _unique;
52   private FilterManager _filterManager;
53
54   private ArrayList JavaDoc<FilterMapping> _filterMap
55   = new ArrayList JavaDoc<FilterMapping>();
56
57   private ArrayList JavaDoc<FilterChainBuilder> _topFilters
58   = new ArrayList JavaDoc<FilterChainBuilder>();
59   
60   private ArrayList JavaDoc<FilterChainBuilder> _bottomFilters
61   = new ArrayList JavaDoc<FilterChainBuilder>();
62
63   /**
64    * Sets the servlet context.
65    */

66   public void setServletContext(ServletContext JavaDoc servletContext)
67   {
68     _servletContext = servletContext;
69   }
70
71   /**
72    * Gets the servlet context.
73    */

74   public ServletContext JavaDoc getServletContext()
75   {
76     return _servletContext;
77   }
78
79   /**
80    * Returns the filter manager.
81    */

82   public FilterManager getFilterManager()
83   {
84     return _filterManager;
85   }
86
87   /**
88    * Sets the filter manager.
89    */

90   public void setFilterManager(FilterManager manager)
91   {
92     _filterManager = manager;
93   }
94
95   /**
96    * Adds a filter mapping
97    */

98   public void addFilterMapping(FilterMapping mapping)
99     throws ServletException JavaDoc
100   {
101     try {
102       String JavaDoc filterName = mapping.getFilterName();
103
104       if (filterName == null)
105     filterName = mapping.getFilterClassName();
106
107       if (_filterManager.getFilter(filterName) == null)
108         throw new ServletConfigException(L.l("`{0}' is an unknown filter-name. filter-mapping requires that the named filter be defined in a <filter> configuration before the <filter-mapping>.", filterName));
109
110       _filterMap.add(mapping);
111
112       log.fine("filter-mapping " + mapping + " -> " + filterName);
113     } catch (ServletException JavaDoc e) {
114       throw e;
115     } catch (Exception JavaDoc e) {
116       throw new ServletException JavaDoc(e);
117     }
118   }
119
120   /**
121    * Adds a top-filter. Top filters are added to every request
122    * in the filter chain.
123    */

124   public void addTopFilter(FilterChainBuilder filterBuilder)
125   {
126     _topFilters.add(filterBuilder);
127   }
128   
129   /**
130    * Fills in the invocation.
131    */

132   public void buildDispatchChain(Invocation invocation,
133                                  FilterChain JavaDoc chain)
134     throws ServletException JavaDoc
135   {
136     synchronized (_filterMap) {
137       for (int i = _filterMap.size() - 1; i >= 0; i--) {
138         FilterMapping map = _filterMap.get(i);
139
140         if (map.isMatch(invocation.getServletName())) {
141           String JavaDoc filterName = map.getFilterName();
142         
143           Filter JavaDoc filter = _filterManager.createFilter(filterName);
144
145           chain = new FilterFilterChain(chain, filter);
146         }
147       }
148     }
149
150     synchronized (_filterMap) {
151       for (int i = _filterMap.size() - 1; i >= 0; i--) {
152         FilterMapping map = _filterMap.get(i);
153
154         if (map.isMatch(invocation)) {
155           String JavaDoc filterName = map.getFilterName();
156         
157           Filter JavaDoc filter = _filterManager.createFilter(filterName);
158
159           chain = new FilterFilterChain(chain, filter);
160         }
161       }
162     }
163
164     for (int i = 0; i < _topFilters.size(); i++) {
165       FilterChainBuilder filterBuilder;
166       filterBuilder = _topFilters.get(i);
167
168       chain = filterBuilder.build(chain, invocation);
169     }
170
171     invocation.setFilterChain(chain);
172   }
173 }
174
Popular Tags