1 16 package org.apache.cocoon.acting; 17 18 import java.util.Collections ; 19 import java.util.Enumeration ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 23 import java.util.Iterator ; 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Redirector; 27 import org.apache.cocoon.environment.Request; 28 import org.apache.cocoon.environment.SourceResolver; 29 30 54 public class RequestParameterExistsAction extends AbstractConfigurableAction 55 { 56 57 protected static class StringParts { 58 String prefix = null; 59 String pstfix = null; 60 int count = 0; 61 62 public StringParts (String pre, String post) { 63 prefix = pre; 64 pstfix = post; 65 } 66 } 67 68 69 70 public Map act( Redirector redirector, 71 SourceResolver resolver, 72 Map objectModel, 73 String source, 74 Parameters parameters 75 ) 76 throws Exception { 77 Request request = ObjectModelHelper.getRequest(objectModel); 78 HashMap results = new HashMap (); 79 HashMap items = new HashMap (); 80 int wildcards = 0; 81 82 if (this.getLogger().isDebugEnabled()) { 84 getLogger().debug("checking default parameters"); 85 } 86 Iterator reqParams = settings.values().iterator(); 87 while (reqParams.hasNext()) { 88 String paramName = (String ) reqParams.next(); 89 StringParts sp = splitParameter(paramName); 90 if (sp != null) { 91 items.put(new Integer (wildcards++), sp); 93 if (this.getLogger().isDebugEnabled()) { 94 getLogger().debug("(default) deferring " + paramName); 95 } 96 } else { 97 String paramValue = request.getParameter(paramName); 98 if (paramValue == null) { 99 return null; 100 } 101 results.put(paramName, paramValue); 102 } 103 } 104 105 if (this.getLogger().isDebugEnabled()) { 107 getLogger().debug("checking sitemap parameters"); 108 } 109 String params = parameters.getParameter("parameters", null); 110 if (params != null) { 111 StringTokenizer st = new StringTokenizer (params); 112 while (st.hasMoreTokens()) { 113 String paramName = st.nextToken(); 114 StringParts sp = splitParameter(paramName); 115 if (sp != null) { 116 items.put(new Integer (wildcards++), sp); 118 if (this.getLogger().isDebugEnabled()) { 119 getLogger().debug("deferring " + paramName); 120 } 121 } else { 122 123 String paramValue = request.getParameter(paramName); 124 if (paramValue == null) { 125 return null; 126 } 127 results.put(paramName, paramValue); 128 } 129 } 130 } 131 132 if (wildcards != 0) { 133 if (this.getLogger().isDebugEnabled()) { 136 getLogger().debug("deferred checking for parameters: " + wildcards); 137 } 138 139 if (this.getLogger().isDebugEnabled()) { 142 getLogger().debug(" checking first"); 143 } 144 HashMap values = new HashMap (); 145 StringParts sp1 = (StringParts) items.get(new Integer (0)); 146 if (this.getLogger().isDebugEnabled()) { 147 getLogger().debug( 148 " Parameter is [" + sp1.prefix + " * " + sp1.pstfix + "] "); 149 } 150 Enumeration requestParams = request.getParameterNames(); 151 Boolean dummy = Boolean.TRUE; 152 while (requestParams.hasMoreElements()) { 153 String paramName = (String ) requestParams.nextElement(); 154 String match = getMatch(paramName, sp1); 155 if (match != null) { 156 if (this.getLogger().isDebugEnabled()) { 157 getLogger().debug( 158 " value is >" 159 + match 160 + "< " 161 + sp1.prefix.length() 162 + " " 163 + paramName.length() 164 + " " 165 + sp1.pstfix.length()); 166 } 167 values.put(match, dummy); 168 sp1.count++; 169 if (this.getLogger().isDebugEnabled()) { 170 getLogger().debug( 171 " Parameter " 172 + sp1.prefix 173 + "*" 174 + sp1.pstfix 175 + " matches " 176 + paramName 177 + " (" 178 + sp1.count 179 + " so far)"); 180 } 181 String paramValue = request.getParameter(paramName); 182 if (paramValue == null) { 183 return null; 184 } 185 results.put(paramName, paramValue); 186 } 187 } 188 189 if (sp1.count == 0) { 190 if (this.getLogger().isDebugEnabled()) { 191 getLogger().debug( 192 " Parameter " 193 + sp1.prefix 194 + "*" 195 + sp1.pstfix 196 + " matches " 197 + sp1.count); 198 } 199 return null; 200 } 201 202 if (this.getLogger().isDebugEnabled()) { 205 getLogger().debug(" checking others"); 206 } 207 requestParams = request.getParameterNames(); 208 while (requestParams.hasMoreElements()) { 209 String paramName = (String ) requestParams.nextElement(); 210 if (this.getLogger().isDebugEnabled()) { 211 getLogger().debug(" checking request parameter " + paramName); 212 } 213 for (int i = wildcards - 1; i > 0; i--) { 214 if (this.getLogger().isDebugEnabled()) { 215 getLogger().debug(" checking against " + i); 216 } 217 StringParts sp = (StringParts) items.get(new Integer (i)); 218 String match = getMatch(paramName, sp); 219 if (this.getLogger().isDebugEnabled()) { 220 getLogger().debug( 221 " Parameter is [" 222 + sp.prefix 223 + " * " 224 + sp.pstfix 225 + "] "); 226 } 227 if (match != null) { 228 if (this.getLogger().isDebugEnabled()) { 229 getLogger().debug( 230 " Parameter " 231 + sp.prefix 232 + "*" 233 + sp.pstfix 234 + " matches " 235 + paramName 236 + " (" 237 + sp.count 238 + " so far)"); 239 } 240 if (values.containsKey(match)) { 241 sp.count++; 242 if (this.getLogger().isDebugEnabled()) { 243 getLogger().debug( 244 " " + paramName + " (verified)"); 245 } 246 String paramValue = request.getParameter(paramName); 247 if (paramValue == null) { 248 return null; 249 } 250 results.put(paramName, paramValue); 251 252 } else { 253 if (this.getLogger().isDebugEnabled()) { 254 getLogger().debug( 255 "Match " 256 + match 257 + "not found for " 258 + sp1.prefix 259 + "*" 260 + sp1.pstfix 261 + " but for " 262 + sp.prefix 263 + "*" 264 + sp.pstfix); 265 } 266 return null; 267 } 268 } 269 } 270 } 271 272 if (this.getLogger().isDebugEnabled()) { 277 getLogger().debug("checking number of matches"); 278 } 279 for (int i = wildcards - 1; i > 0; i--) { 280 StringParts sp = (StringParts) items.get(new Integer (i)); 281 if (sp.count != sp1.count) { 282 if (this.getLogger().isDebugEnabled()) { 283 getLogger().debug( 284 "Found " 285 + sp.count 286 + " matches for " 287 + sp.prefix 288 + "*" 289 + sp.pstfix 290 + " but expected " 291 + sp1.count); 292 } 293 return null; 294 } else { 295 if (this.getLogger().isDebugEnabled()) { 296 getLogger().debug( 297 "Found " 298 + sp.count 299 + " matches for " 300 + sp.prefix 301 + "*" 302 + sp.pstfix 303 + " as expected"); 304 } 305 } 306 } 307 308 } 309 310 return Collections.unmodifiableMap(results); 311 } 312 313 314 318 protected StringParts splitParameter( String paramName ) 319 { 320 int idx = paramName.indexOf("*"); 321 if ( idx != -1 ) { 322 return new StringParts(paramName.substring(0,idx), paramName.substring(idx+1)); 323 } else { 324 return null; 325 } 326 } 327 328 333 protected String getMatch( String paramName, 334 StringParts sp 335 ) 336 { 337 if ( paramName.startsWith( sp.prefix ) && paramName.endsWith( sp.pstfix ) ) { 338 return paramName.substring( sp.prefix.length(), ( paramName.length() - sp.pstfix.length() ) ); 339 } else { 340 return null; 341 } 342 } 343 344 } 345 | Popular Tags |