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 private boolean _redispatch; 31 private boolean _initial; 32 33 34 /* ------------------------------------------------------------ */ 35 public FilterMapping() 36 {} 37 38 /* ------------------------------------------------------------ */ 39 /** Check if this filter applies to a path. 40 * @param path The path to check or null to just check type 41 * @param type The type of request: __REQUEST,__FORWARD,__INCLUDE or __ERROR. 42 * @return True if this filter applies 43 */ 44 boolean appliesTo(String path, int type,boolean initial) 45 { 46 if (_initial==_redispatch || _initial&&initial || _redispatch&&!initial) 47 { 48 if ( ((_dispatches&type)!=0 || (_dispatches==0 && type==Handler.REQUEST)) && _pathSpecs!=null ) 49 { 50 for (int i=0;i<_pathSpecs.length;i++) 51 if (_pathSpecs[i]!=null && PathMap.match(_pathSpecs[i], path,true)) 52 return true; 53 } 54 } 55 return false; 56 } 57 58 /* ------------------------------------------------------------ */ 59 /** Check if this filter applies to a particular dispatch type. 60 * @param type The type of request: 61 * {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}. 62 * @return <code>true</code> if this filter applies 63 */ 64 boolean appliesTo(int type, boolean initial) 65 { 66 if (_initial==_redispatch || _initial&&initial || _redispatch&&!initial) 67 { 68 if ( ((_dispatches&type)!=0 || (_dispatches==0 && type==Handler.REQUEST))) 69 return true; 70 } 71 return false; 72 } 73 74 75 /* ------------------------------------------------------------ */ 76 /** 77 * @return Returns the dispatches. 78 */ 79 public int getDispatches() 80 { 81 return _dispatches; 82 } 83 84 /* ------------------------------------------------------------ */ 85 /** 86 * @return Returns the filterName. 87 */ 88 public String getFilterName() 89 { 90 return _filterName; 91 } 92 93 /* ------------------------------------------------------------ */ 94 /** 95 * @return Returns the holder. 96 */ 97 FilterHolder getFilterHolder() 98 { 99 return _holder; 100 } 101 102 /* ------------------------------------------------------------ */ 103 /** 104 * @return Returns the pathSpec. 105 */ 106 public String[] getPathSpecs() 107 { 108 return _pathSpecs; 109 } 110 111 /* ------------------------------------------------------------ */ 112 /** 113 * @param dispatches The dispatches to set. 114 * @see Handler#DEFAULT 115 * @see Handler#REQUEST 116 * @see Handler#ERROR 117 * @see Handler#FORWARD 118 * @see Handler#INCLUDE 119 */ 120 public void setDispatches(int dispatches) 121 { 122 _dispatches = dispatches; 123 } 124 125 /* ------------------------------------------------------------ */ 126 /** 127 * @param filterName The filterName to set. 128 */ 129 public void setFilterName(String filterName) 130 { 131 _filterName = filterName; 132 } 133 134 /* ------------------------------------------------------------ */ 135 /** 136 * @param holder The holder to set. 137 */ 138 void setFilterHolder(FilterHolder holder) 139 { 140 _holder = holder; 141 } 142 143 /* ------------------------------------------------------------ */ 144 /** 145 * @param pathSpecs The Path specifications to which this filter should be mapped. 146 */ 147 public void setPathSpecs(String[] pathSpecs) 148 { 149 _pathSpecs = pathSpecs; 150 } 151 152 /* ------------------------------------------------------------ */ 153 /** 154 * @param pathSpec The pathSpec to set. 155 */ 156 public void setPathSpec(String pathSpec) 157 { 158 _pathSpecs = new String[]{pathSpec}; 159 } 160 161 /* ------------------------------------------------------------ */ 162 /** 163 * @return Returns the servletName. 164 */ 165 public String[] getServletNames() 166 { 167 return _servletNames; 168 } 169 170 /* ------------------------------------------------------------ */ 171 /** 172 * @param servletNames Maps the {@link #setFilterName(String) named filter} to multiple servlets 173 * @see #setServletName 174 */ 175 public void setServletNames(String[] servletNames) 176 { 177 _servletNames = servletNames; 178 } 179 180 /* ------------------------------------------------------------ */ 181 /** 182 * @param servletName Maps the {@link #setFilterName(String) named filter} to a single servlet 183 * @see #setServletNames 184 */ 185 public void setServletName(String servletName) 186 { 187 _servletNames = new String[]{servletName}; 188 } 189 190 /* ------------------------------------------------------------ */ 191 public String toString() 192 { 193 return "(F="+_filterName+","+(_pathSpecs==null?"[]":Arrays.asList(_pathSpecs).toString())+","+(_servletNames==null?"[]":Arrays.asList(_servletNames).toString())+","+_dispatches+")"; 194 } 195 196 /* ------------------------------------------------------------ */ 197 /** 198 * @return the redispatch 199 */ 200 public boolean isRedispatchLifeCycle() 201 { 202 return _redispatch; 203 } 204 205 /* ------------------------------------------------------------ */ 206 /** 207 * @param redispatch the redispatch to set 208 */ 209 public void setRedispatchLifeCycle(boolean redispatch) 210 { 211 _redispatch = redispatch; 212 } 213 214 /* ------------------------------------------------------------ */ 215 /** 216 * @return the initial 217 */ 218 public boolean isInitialLifeCycle() 219 { 220 return _initial; 221 } 222 223 /* ------------------------------------------------------------ */ 224 /** 225 * @param initial the initial to set 226 */ 227 public void setInitialLifeCycle(boolean initial) 228 { 229 _initial = initial; 230 } 231 }