1 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 ; 36 import javax.servlet.FilterChain ; 37 import javax.servlet.ServletContext ; 38 import javax.servlet.ServletException ; 39 import java.util.ArrayList ; 40 import java.util.logging.Logger ; 41 42 45 public class FilterMapper { 46 static final Logger log = Log.open(FilterMapper.class); 47 static final L10N L = new L10N(FilterMapper.class); 48 49 private ServletContext _servletContext; 50 51 private int _unique; 52 private FilterManager _filterManager; 53 54 private ArrayList <FilterMapping> _filterMap 55 = new ArrayList <FilterMapping>(); 56 57 private ArrayList <FilterChainBuilder> _topFilters 58 = new ArrayList <FilterChainBuilder>(); 59 60 private ArrayList <FilterChainBuilder> _bottomFilters 61 = new ArrayList <FilterChainBuilder>(); 62 63 66 public void setServletContext(ServletContext servletContext) 67 { 68 _servletContext = servletContext; 69 } 70 71 74 public ServletContext getServletContext() 75 { 76 return _servletContext; 77 } 78 79 82 public FilterManager getFilterManager() 83 { 84 return _filterManager; 85 } 86 87 90 public void setFilterManager(FilterManager manager) 91 { 92 _filterManager = manager; 93 } 94 95 98 public void addFilterMapping(FilterMapping mapping) 99 throws ServletException 100 { 101 try { 102 String 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 e) { 114 throw e; 115 } catch (Exception e) { 116 throw new ServletException (e); 117 } 118 } 119 120 124 public void addTopFilter(FilterChainBuilder filterBuilder) 125 { 126 _topFilters.add(filterBuilder); 127 } 128 129 132 public void buildDispatchChain(Invocation invocation, 133 FilterChain chain) 134 throws ServletException 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 filterName = map.getFilterName(); 142 143 Filter 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 filterName = map.getFilterName(); 156 157 Filter 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 |