1 23 24 30 package org.dbforms.util; 31 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 37 import java.util.Enumeration ; 38 import java.util.Hashtable ; 39 40 import javax.servlet.RequestDispatcher ; 41 42 51 import javax.servlet.Servlet ; 52 import javax.servlet.ServletConfig ; 53 import javax.servlet.ServletContext ; 54 import javax.servlet.ServletException ; 55 import javax.servlet.ServletRequest ; 56 import javax.servlet.ServletResponse ; 57 import javax.servlet.http.HttpServletRequest ; 58 import javax.servlet.http.HttpSession ; 59 import javax.servlet.jsp.JspException ; 60 import javax.servlet.jsp.JspWriter ; 61 import javax.servlet.jsp.PageContext ; 62 63 69 public class PageContextBuffer extends PageContext { 70 private Exception exception; 71 72 77 78 private org.apache.commons.el.ExpressionEvaluatorImpl elExprEval = new org.apache.commons.el.ExpressionEvaluatorImpl(); 80 81 private Hashtable nametable; 82 83 private HttpSession session; 84 85 private JspWriterBuffer out; 86 87 private Servlet servlet; 88 89 private ServletConfig servletConfig; 90 91 private ServletContext servletContext; 92 93 private ServletRequest request; 94 95 private ServletResponse response; 96 97 private String errorPageURL; 98 99 private org.apache.commons.el.VariableResolverImpl variableResolver = new org.apache.commons.el.VariableResolverImpl( 101 this); 102 103 private boolean needsSession; 104 105 108 public PageContextBuffer() { 109 } 110 111 119 public void setAttribute(String name, Object attribute) { 120 if ((name == null) || (attribute == null)) { 121 throw new NullPointerException (); 122 } 123 124 nametable.put(name, attribute); 125 } 126 127 137 public void setAttribute(String name, Object attribute, int scope) { 138 if ((name == null) || (attribute == null)) { 139 throw new NullPointerException (); 140 } 141 142 switch (scope) { 143 case APPLICATION_SCOPE: 144 break; 145 146 case PAGE_SCOPE: 147 setAttribute(name, attribute); 148 149 break; 150 151 case REQUEST_SCOPE: 152 break; 153 154 case SESSION_SCOPE: 155 156 if (!needsSession) { 157 throw new IllegalArgumentException ( 158 "Invalid scope - Page does not participate in a session"); 159 } 160 161 break; 162 163 default: 164 throw new IllegalArgumentException ("Invalid scope"); 165 } 166 } 167 168 176 public Object getAttribute(String name) { 177 if (name == null) { 178 throw new IllegalArgumentException (); 179 } 180 181 return (nametable.get(name)); 182 } 183 184 194 public Object getAttribute(String name, int scope) { 195 Object obj = null; 196 197 if (name == null) { 198 throw new NullPointerException (); 199 } 200 201 switch (scope) { 202 case PAGE_SCOPE: 203 obj = getAttribute(name); 204 205 break; 206 207 case REQUEST_SCOPE: 208 obj = request.getAttribute(name); 209 210 break; 211 212 case SESSION_SCOPE: 213 214 if (needsSession) { 215 216 obj = session.getAttribute(name); 217 } else { 218 throw new IllegalArgumentException ( 219 "Invalid scope - Page does not participate in a session"); 220 } 221 222 break; 223 224 case APPLICATION_SCOPE: 225 obj = servletContext.getAttribute(name); 226 227 break; 228 229 default: 230 throw new IllegalArgumentException ("Invalid scope"); 231 } 232 233 return obj; 234 } 235 236 244 public Enumeration getAttributeNamesInScope(int scope) { 245 switch (scope) { 246 case PAGE_SCOPE: 247 return nametable.keys(); 248 249 case REQUEST_SCOPE: 250 return request.getAttributeNames(); 251 252 case SESSION_SCOPE: 253 254 if (!needsSession) { 255 throw new IllegalArgumentException ( 256 "Invalid scope - Page does not participate in a session"); 257 } 258 259 return session.getAttributeNames(); 260 261 case APPLICATION_SCOPE: 262 return servletContext.getAttributeNames(); 263 264 default: 265 throw new IllegalArgumentException ("Invalid scope"); 266 } 267 } 268 269 277 public int getAttributesScope(String name) { 278 int scope = 0; 279 280 281 if (getAttribute(name, PAGE_SCOPE) != null) { 282 scope = PAGE_SCOPE; 283 } 284 285 if (getAttribute(name, REQUEST_SCOPE) != null) { 286 scope = REQUEST_SCOPE; 287 } 288 289 if (needsSession) { 290 if (getAttribute(name, SESSION_SCOPE) != null) { 291 scope = SESSION_SCOPE; 292 } 293 } 294 295 if (getAttribute(name, APPLICATION_SCOPE) != null) { 296 scope = APPLICATION_SCOPE; 297 } 298 299 return scope; 300 } 301 302 307 public StringBuffer getBuffer() { 308 return out.getBuffer(); 309 } 310 311 316 public Exception getException() { 317 return exception; 318 } 319 320 325 public javax.servlet.jsp.el.ExpressionEvaluator getExpressionEvaluator() { 326 return elExprEval; 327 } 328 329 334 public JspWriter getOut() { 335 return out; 336 } 337 338 343 public Object getPage() { 344 return servlet; 345 } 346 347 352 public ServletRequest getRequest() { 353 return request; 354 } 355 356 361 public ServletResponse getResponse() { 362 return response; 363 } 364 365 370 public String getResult() { 371 return out.getResult(); 372 } 373 374 379 public ServletConfig getServletConfig() { 380 return servletConfig; 381 } 382 383 388 public ServletContext getServletContext() { 389 return servletContext; 390 } 391 392 397 public HttpSession getSession() { 398 return session; 399 } 400 401 406 public javax.servlet.jsp.el.VariableResolver getVariableResolver() { 407 return variableResolver; 408 } 409 410 418 public Object findAttribute(String name) { 419 Object obj = null; 420 421 422 if ((obj = getAttribute(name, PAGE_SCOPE)) != null) { 423 return obj; 424 } 425 426 if ((obj = getAttribute(name, REQUEST_SCOPE)) != null) { 427 return obj; 428 } 429 430 if (needsSession) { 431 if ((obj = getAttribute(name, SESSION_SCOPE)) != null) { 432 return obj; 433 } 434 } 435 436 if ((obj = getAttribute(name, APPLICATION_SCOPE)) != null) { 437 return obj; 438 } 439 440 return obj; 441 } 442 443 454 public void forward(String relativeURLPath) throws ServletException , 455 IOException { 456 if (relativeURLPath == null) { 457 throw new ServletException ( 458 "[PageContext.forward()] Got 'null' URL - Probably caused " 459 + "by a non-existent Request Time Attribute Value."); 460 } 461 462 String path = decodePath(relativeURLPath); 463 464 RequestDispatcher requestDispatcher = servletContext 465 .getRequestDispatcher(path); 466 467 requestDispatcher.forward(request, response); 468 } 469 470 481 public void handlePageException(Throwable t) throws IOException , 482 ServletException { 483 request.setAttribute("javax.servlet.jsp.jspException", t); 485 486 if ((errorPageURL != null) && !errorPageURL.equals("")) { 487 try { 488 forward(errorPageURL); 489 } catch (IllegalStateException ise) { 490 include(errorPageURL); 491 } 492 } else { 493 if (t instanceof IOException ) { 497 throw (IOException ) t; 498 } 499 500 if (t instanceof ServletException ) { 501 throw (ServletException ) t; 502 } 503 504 if (t instanceof RuntimeException ) { 505 throw (RuntimeException ) t; 506 } 507 508 if (t instanceof JspException ) { 509 Throwable rootCause = ((JspException ) t).getRootCause(); 510 511 if (rootCause != null) { 512 throw new ServletException (t.getMessage(), rootCause); 513 } 514 throw new ServletException (t); 515 } 516 517 throw new ServletException (t); 518 } 519 } 520 521 532 public void handlePageException(Exception e) throws ServletException , 533 IOException { 534 if (!errorPageURL.equals("")) { 537 setAttribute(EXCEPTION, e, REQUEST_SCOPE); 538 forward(errorPageURL); 539 } else { 540 System.out.println("Unhandled Page Exception:"); 541 e.printStackTrace(); 542 throw new ServletException ("An unhandled Page Exception occurred"); 543 } 544 } 545 546 557 public void include(String relativeURLPath) throws ServletException , 558 IOException { 559 if (relativeURLPath == null) { 560 throw new ServletException ( 561 "[PageContext.include()] Got 'null' URL. Probably caused " 562 + " by a non-existent Request Time Attribute Value."); 563 } 564 565 out.flush(); 566 567 String path = decodePath(relativeURLPath); 568 RequestDispatcher requestDispatcher = servletContext 569 .getRequestDispatcher(path); 570 571 if (requestDispatcher == null) { 572 InputStream is = servletContext 573 .getResourceAsStream(relativeURLPath); 574 575 if (is == null) { 576 throw new ServletException ( 577 "[PageContext.include()] Unable to obtain include resource " 578 + relativeURLPath); 579 } 580 581 BufferedReader in = new BufferedReader (new InputStreamReader (is)); 582 583 try { 584 int c; 585 586 while ((c = in.read()) > 0) { 587 out.write(c); 588 } 589 } finally { 590 in.close(); 591 } 592 } else { 593 HttpServletRequest httpReq = (HttpServletRequest ) request; 594 595 596 String attrib; 597 598 if ((attrib = httpReq.getRequestURI()) != null) { 599 request.setAttribute("javax.servlet.include.request_uri", 600 attrib); 601 } 602 603 if ((attrib = httpReq.getServletPath()) != null) { 604 request.setAttribute("javax.servlet.include.servlet_path", 605 attrib); 606 } 607 608 if ((attrib = httpReq.getPathInfo()) != null) { 609 request.setAttribute("javax.servlet.include.path_info", attrib); 610 } 611 612 if ((attrib = httpReq.getQueryString()) != null) { 613 request.setAttribute("javax.servlet.include.query_string", 614 attrib); 615 } 616 617 requestDispatcher.include(request, response); 618 } 619 } 620 621 634 public void include(String relativeURLPath, boolean flush) 635 throws ServletException , IOException { 636 include(relativeURLPath); 637 out.flush(); 638 } 639 640 658 public void initialize(Servlet aservlet, ServletRequest arequest, 659 ServletResponse aresponse, String aerrorPageURL, 660 boolean aneedsSession, int bufferSize, boolean autoFlush) { 661 this.servlet = aservlet; 662 this.request = arequest; 663 this.response = aresponse; 664 this.errorPageURL = aerrorPageURL; 665 this.needsSession = aneedsSession; 666 667 if (needsSession) { 668 session = ((HttpServletRequest ) request).getSession(); 669 } 670 671 nametable = new Hashtable (); 672 out = new JspWriterBuffer(bufferSize, autoFlush, response); 673 exception = null; 674 675 servletConfig = servlet.getServletConfig(); 676 servletContext = servletConfig.getServletContext(); 677 } 678 679 682 public void release() { 683 this.servlet = null; 684 this.request = null; 685 this.response = null; 686 this.errorPageURL = null; 687 688 nametable = null; 689 out = null; 690 exception = null; 691 692 servletConfig = null; 693 servletContext = null; 694 } 695 696 702 public void removeAttribute(String name) { 703 nametable.remove(name); 704 } 705 706 714 public void removeAttribute(String name, int scope) { 715 switch (scope) { 716 case PAGE_SCOPE: 717 break; 718 719 case REQUEST_SCOPE: 720 throw new IllegalArgumentException ( 721 "Invalid scope - Can't remove attribute from REQUEST_SCOPE"); 722 723 case SESSION_SCOPE: 724 725 if (!needsSession) { 726 throw new IllegalArgumentException ( 727 "Invalid Scope - Page does not participate in an HTTP session"); 728 } 729 730 break; 731 732 case APPLICATION_SCOPE: 733 break; 734 735 default: 736 throw new IllegalArgumentException ("Invalid Scope"); 737 } 738 } 739 740 private String decodePath(String relativeURLPath) { 741 String path; 742 743 if (relativeURLPath.startsWith("/")) { 744 path = relativeURLPath; 745 } else { 746 path = ((HttpServletRequest ) request).getServletPath(); 747 path = path.substring(0, path.lastIndexOf("/")); 748 path = path + "/" + relativeURLPath; 749 } 750 751 return path; 753 } 754 } 755 | Popular Tags |