1 20 package org.enhydra.barracuda.core.helper.servlet; 21 22 import java.io.*; 23 import java.util.*; 24 import java.security.*; 25 import javax.servlet.*; 26 import javax.servlet.http.*; 27 28 import org.apache.log4j.Logger; 29 30 import org.enhydra.barracuda.core.helper.state.*; 31 import org.enhydra.barracuda.plankton.data.*; 32 33 46 public class DefaultServletRequestWrapper extends HttpServletRequestWrapper implements BarracudaServletRequestWrapper { 47 48 protected static final Logger logger = Logger.getLogger(DefaultServletRequestWrapper.class.getName()); 49 50 HttpServletRequest req = null; 51 List paramList = null; 52 String method = null; 53 54 61 public DefaultServletRequestWrapper(HttpServletRequest ireq) { 62 super(ireq); 63 setRequest(ireq); 64 } 65 66 67 73 public void setMethod(String imethod) { 74 method = imethod; 75 } 76 77 80 public String getMethod() { 81 return (method!=null ? method : req.getMethod()); 82 } 83 84 95 public void addParameter(String name, String value) { 96 if (name==null) return; 98 99 if (paramList==null) setupParamList(); 101 102 paramList.add(new Param(name, value)); 104 } 105 106 113 public String getParameter(String name) { 114 if (name==null) return null; 116 117 if (paramList!=null) { 119 Iterator it = paramList.iterator(); 120 while (it.hasNext()) { 121 Param param = (Param) it.next(); 122 if (param.getKey().equals(name)) return param.getValue(); 123 } 124 return null; 125 126 } else { 128 String s = req.getParameter(name); 138 if (s==null) { 139 String queryString = req.getQueryString(); 141 if (queryString!=null) { 142 try { 146 queryString = java.net.URLDecoder.decode(queryString); 147 } catch (Exception e) { 148 logger.warn("failed to decode queryString, but allowing to continue", e); 149 } 150 159 164 int startPos = queryString.indexOf("&"+name + "="); 168 int endPos = -1; 169 170 if (startPos == -1) { 171 if (queryString.startsWith(name+"=")) { 173 startPos = 0; 174 } 175 } else { 176 startPos++; 178 } 179 181 if (startPos!=-1) { 182 startPos = startPos + name.length() + 1; 183 endPos = queryString.indexOf("&", startPos); 184 185 if (endPos==-1) { 186 s = queryString.substring(startPos); 187 } 188 else { 189 s = queryString.substring(startPos, endPos); 190 } 191 } 192 } 193 } 194 return s; 195 } 197 } 198 199 205 public Enumeration getParameterNames() { 206 if (paramList!=null) { 208 return new LocalEnumerator(paramList); 209 210 } else { 212 return req.getParameterNames(); 213 } 214 } 215 216 222 public Map getParameterMap() { 223 if (paramList!=null) { 225 Iterator it = paramList.iterator(); 226 Map paramMap = new HashMap(paramList.size()); 227 237 while (it.hasNext()) { 239 Param param = (Param) it.next(); 240 String key = param.getKey(); 241 List valList = (List) paramMap.get(key); 242 if (valList==null) { 243 valList = new ArrayList(10); 244 paramMap.put(key, valList); 245 } 246 valList.add(param.getValue()); 247 } 248 249 it = paramMap.keySet().iterator(); 252 while (it.hasNext()) { 253 Object key = it.next(); 254 List valList = (List) paramMap.get(key); 255 int idx = -1; 261 String[] valArr = new String[valList.size()]; 262 Iterator it2 = valList.iterator(); 263 while (it2.hasNext()) { 264 valArr[++idx] = (String) it2.next(); 265 } 266 paramMap.put(key, valArr); 267 } 269 271 return paramMap; 272 273 } else { 275 return req.getParameterMap(); 276 } 277 } 278 280 288 public String[] getParameterValues(String name) { 289 if (name==null) return null; 291 292 if (paramList!=null) { 294 List valueList = new ArrayList(paramList.size()); 295 Iterator it = paramList.iterator(); 296 while (it.hasNext()) { 297 Param param = (Param) it.next(); 298 if (param.getKey().equals(name)) valueList.add(param.getValue()); 299 } 300 int idx = -1; 301 String[] valueArr = new String[valueList.size()]; 302 it = valueList.iterator(); 303 while (it.hasNext()) { 304 valueArr[++idx] = (String) it.next(); 305 } 306 if (valueArr.length==0) return null; 307 else return valueArr; 308 309 } else { 311 return req.getParameterValues(name); 312 } 313 } 314 315 320 public void removeParameter(String name) { 321 if (name==null) return; 323 324 if (paramList==null) setupParamList(); 326 327 for (int i=0, max=paramList.size(); i<max; i++) { 329 Param param = (Param) paramList.get(i); 330 if (param.getKey().equals(name)) { 331 paramList.remove(i); 332 break; 333 } 334 } 335 336 } 337 338 343 public void removeAllParameters(String name) { 344 if (name==null) return; 346 347 if (paramList==null) setupParamList(); 349 350 for (int i=paramList.size()-1; i>=0; i--) { 352 Param param = (Param) paramList.get(i); 353 if (param.getKey().equals(name)) paramList.remove(i); 354 } 355 } 356 357 361 public void resetParameters() { 362 paramList=null; 363 } 364 365 374 public HttpServletRequest getCoreRequest() { 375 return req; 376 } 377 378 379 385 public void setRequest(ServletRequest ireq) { 386 super.setRequest(ireq); 387 388 if (req!=ireq && ireq instanceof HttpServletRequest) { 389 req = (HttpServletRequest) ireq; 391 392 ParamPersister.reconstituteReqParamState(this); 394 } 395 } 396 397 398 399 400 402 403 404 405 406 private void setupParamList() { 408 if (paramList!=null) return; 410 411 paramList = new ArrayList(10); 413 414 Enumeration enum = req.getParameterNames(); 418 while (enum.hasMoreElements()) { 419 String key = (String) enum.nextElement(); 421 422 String[] vals = req.getParameterValues(key); 424 for (int i=0, max=vals.length; i<max; i++) { 425 paramList.add(new Param(key, vals[i])); 426 } 427 } 428 } 429 430 434 class LocalEnumerator implements Enumeration { 435 List keyList = null; 436 Iterator it = null; 437 438 public LocalEnumerator(List iparamList) { 439 keyList = new ArrayList(iparamList.size()); 440 it = iparamList.iterator(); 441 while (it.hasNext()) { 442 Param param = (Param) it.next(); 443 if (!keyList.contains(param.getKey())) keyList.add(param.getKey()); 444 } 445 it = keyList.iterator(); 446 } 447 448 public boolean hasMoreElements() { 449 return (it.hasNext()); 450 } 451 452 public Object nextElement() { 453 return it.next(); 454 } 455 } 456 } | Popular Tags |