1 20 package org.apache.cactus.internal; 21 22 import java.io.InputStream ; 23 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 28 import org.apache.cactus.Cookie; 29 import org.apache.cactus.WebRequest; 30 import org.apache.cactus.client.authentication.Authentication; 31 import org.apache.cactus.internal.configuration.Configuration; 32 import org.apache.cactus.util.ChainedRuntimeException; 33 34 41 public abstract class BaseWebRequest implements WebRequest 42 { 43 46 private Configuration configuration; 47 48 51 private Hashtable parametersPost = new Hashtable (); 52 53 56 private Hashtable parametersGet = new Hashtable (); 57 58 61 private Vector cookies = new Vector (); 62 63 66 private Hashtable headers = new Hashtable (); 67 68 71 private InputStream dataStream; 72 73 76 private String contentType = "application/x-www-form-urlencoded"; 77 78 81 private Authentication authentication; 82 83 89 public BaseWebRequest() 90 { 91 } 92 93 96 public BaseWebRequest(Configuration theConfiguration) 97 { 98 this.configuration = theConfiguration; 99 } 100 101 104 protected Configuration getConfiguration() 105 { 106 return this.configuration; 107 } 108 109 113 public void setConfiguration(Configuration theConfiguration) 114 { 115 this.configuration = theConfiguration; 116 } 117 118 121 public void setContentType(String theContentType) 122 { 123 this.contentType = theContentType; 124 } 125 126 129 public String getContentType() 130 { 131 return this.contentType; 132 } 133 134 137 public void setUserData(InputStream theDataStream) 138 { 139 this.dataStream = theDataStream; 140 } 141 142 145 public InputStream getUserData() 146 { 147 return this.dataStream; 148 } 149 150 153 public void addParameter(String theName, String theValue, String theMethod) 154 { 155 Hashtable parameters; 156 157 if (theMethod.equalsIgnoreCase(BaseWebRequest.POST_METHOD)) 159 { 160 parameters = this.parametersPost; 161 } 162 else if (theMethod.equalsIgnoreCase(BaseWebRequest.GET_METHOD)) 163 { 164 parameters = this.parametersGet; 165 } 166 else 167 { 168 throw new ChainedRuntimeException("The method need to be either " 169 + "\"POST\" or \"GET\""); 170 } 171 172 if (parameters.containsKey(theName)) 176 { 177 Vector v = (Vector ) parameters.get(theName); 178 179 v.addElement(theValue); 180 } 181 else 182 { 183 Vector v = new Vector (); 184 185 v.addElement(theValue); 186 parameters.put(theName, v); 187 } 188 } 189 190 193 public void addParameter(String theName, String theValue) 194 { 195 addParameter(theName, theValue, BaseWebRequest.GET_METHOD); 196 } 197 198 201 public Enumeration getParameterNamesPost() 202 { 203 return getParameterNames(this.parametersPost); 204 } 205 206 209 public Enumeration getParameterNamesGet() 210 { 211 return getParameterNames(this.parametersGet); 212 } 213 214 220 private Enumeration getParameterNames(Hashtable theParameters) 221 { 222 return theParameters.keys(); 223 } 224 225 228 public String getParameterGet(String theName) 229 { 230 String [] values = getParameterValuesGet(theName); 231 232 if (values != null) 233 { 234 return values[0]; 235 } 236 237 return null; 238 } 239 240 243 public String getParameterPost(String theName) 244 { 245 String [] values = getParameterValuesPost(theName); 246 247 if (values != null) 248 { 249 return values[0]; 250 } 251 252 return null; 253 } 254 255 258 public String [] getParameterValuesGet(String theName) 259 { 260 return getParameterValues(theName, this.parametersGet); 261 } 262 263 266 public String [] getParameterValuesPost(String theName) 267 { 268 return getParameterValues(theName, this.parametersPost); 269 } 270 271 280 private String [] getParameterValues(String theName, Hashtable theParameters) 281 { 282 if (theParameters.containsKey(theName)) 283 { 284 Vector v = (Vector ) theParameters.get(theName); 285 286 Object [] objs = new Object [v.size()]; 287 288 v.copyInto(objs); 289 290 String [] result = new String [objs.length]; 291 292 for (int i = 0; i < objs.length; i++) 293 { 294 result[i] = (String ) objs[i]; 295 } 296 297 return result; 298 } 299 300 return null; 301 } 302 303 306 public void addCookie(String theName, String theValue) 307 { 308 addCookie("localhost", theName, theValue); 309 } 310 311 314 public void addCookie(String theDomain, String theName, String theValue) 315 { 316 addCookie(new Cookie(theDomain, theName, theValue)); 317 } 318 319 322 public void addCookie(Cookie theCookie) 323 { 324 if (theCookie == null) 325 { 326 throw new IllegalStateException ("The cookie cannot be null"); 327 } 328 this.cookies.addElement(theCookie); 329 } 330 331 334 public Vector getCookies() 335 { 336 return this.cookies; 337 } 338 339 342 public void addHeader(String theName, String theValue) 343 { 344 if (theName.equalsIgnoreCase("Content-type")) 347 { 348 setContentType(theValue); 349 350 return; 351 } 352 353 if (this.headers.containsKey(theName)) 357 { 358 Vector v = (Vector ) this.headers.get(theName); 359 360 v.addElement(theValue); 361 } 362 else 363 { 364 Vector v = new Vector (); 365 366 v.addElement(theValue); 367 this.headers.put(theName, v); 368 } 369 } 370 371 374 public Enumeration getHeaderNames() 375 { 376 return this.headers.keys(); 377 } 378 379 382 public String getHeader(String theName) 383 { 384 String [] values = getHeaderValues(theName); 385 386 if (values != null) 387 { 388 return values[0]; 389 } 390 391 return null; 392 } 393 394 397 public String [] getHeaderValues(String theName) 398 { 399 if (this.headers.containsKey(theName)) 400 { 401 Vector v = (Vector ) this.headers.get(theName); 402 403 Object [] objs = new Object [v.size()]; 404 405 v.copyInto(objs); 406 407 String [] result = new String [objs.length]; 408 409 for (int i = 0; i < objs.length; i++) 410 { 411 result[i] = (String ) objs[i]; 412 } 413 414 return result; 415 } 416 417 return null; 418 } 419 420 423 public String toString() 424 { 425 StringBuffer buffer = new StringBuffer (); 426 427 buffer.append("cookies = ["); 429 buffer.append(toStringAppendCookies()); 430 buffer.append("], "); 431 432 buffer.append("headers = ["); 434 buffer.append(toStringAppendHeaders()); 435 buffer.append("], "); 436 437 buffer.append("GET parameters = ["); 439 buffer.append(toStringAppendParametersGet()); 440 buffer.append("], "); 441 buffer.append("POST parameters = ["); 442 buffer.append(toStringAppendParametersPost()); 443 buffer.append("]"); 444 445 return buffer.toString(); 446 } 447 448 451 private String toStringAppendHeaders() 452 { 453 StringBuffer buffer = new StringBuffer (); 454 455 Enumeration headers = getHeaderNames(); 456 457 while (headers.hasMoreElements()) 458 { 459 buffer.append("["); 460 461 String headerName = (String ) headers.nextElement(); 462 String [] headerValues = getHeaderValues(headerName); 463 464 buffer.append("[" + headerName + "] = ["); 465 466 for (int i = 0; i < (headerValues.length - 1); i++) 467 { 468 buffer.append("[" + headerValues[i] + "], "); 469 } 470 471 buffer.append("[" + headerValues[headerValues.length - 1] + "]]"); 472 buffer.append("]"); 473 } 474 475 return buffer.toString(); 476 } 477 478 481 private String toStringAppendCookies() 482 { 483 StringBuffer buffer = new StringBuffer (); 484 485 Enumeration cookies = getCookies().elements(); 486 487 while (cookies.hasMoreElements()) 488 { 489 Cookie cookie = (Cookie) cookies.nextElement(); 490 491 buffer.append("[" + cookie + "]"); 492 } 493 494 return buffer.toString(); 495 } 496 497 501 private String toStringAppendParametersPost() 502 { 503 return toStringAppendParameters(this.parametersPost); 504 } 505 506 510 private String toStringAppendParametersGet() 511 { 512 return toStringAppendParameters(this.parametersGet); 513 } 514 515 520 private String toStringAppendParameters(Hashtable theParameters) 521 { 522 StringBuffer buffer = new StringBuffer (); 523 524 Enumeration parameters = getParameterNames(theParameters); 525 526 while (parameters.hasMoreElements()) 527 { 528 buffer.append("["); 529 530 String parameterName = (String ) parameters.nextElement(); 531 String [] parameterValues = getParameterValues(parameterName, 532 theParameters); 533 534 buffer.append("[" + parameterName + "] = ["); 535 536 for (int i = 0; i < (parameterValues.length - 1); i++) 537 { 538 buffer.append("[" + parameterValues[i] + "], "); 539 } 540 541 buffer.append("[" + parameterValues[parameterValues.length - 1] 542 + "]]"); 543 buffer.append("]"); 544 } 545 546 return buffer.toString(); 547 } 548 549 552 public void setAuthentication(Authentication theAuthentication) 553 { 554 this.authentication = theAuthentication; 555 } 556 557 560 public Authentication getAuthentication() 561 { 562 return this.authentication; 563 } 564 } 565 | Popular Tags |