1 17 18 package org.apache.jasper.runtime; 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 import java.security.AccessController ; 23 import java.security.PrivilegedAction ; 24 import java.security.PrivilegedActionException ; 25 import java.security.PrivilegedExceptionAction ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 29 import javax.el.ELContext; 30 import javax.el.ExpressionFactory; 31 import javax.el.ValueExpression; 32 import javax.servlet.Servlet ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.ServletResponse ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.http.HttpSession ; 41 import javax.servlet.jsp.JspException ; 42 import javax.servlet.jsp.JspFactory ; 43 import javax.servlet.jsp.JspWriter ; 44 import javax.servlet.jsp.PageContext ; 45 import javax.servlet.jsp.el.ELException ; 46 import javax.servlet.jsp.el.ExpressionEvaluator ; 47 import javax.servlet.jsp.el.VariableResolver ; 48 import javax.servlet.jsp.tagext.BodyContent ; 49 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 import org.apache.jasper.Constants; 53 import org.apache.jasper.compiler.Localizer; 54 import org.apache.jasper.el.ELContextImpl; 55 import org.apache.jasper.el.ExpressionEvaluatorImpl; 56 import org.apache.jasper.el.FunctionMapperImpl; 57 import org.apache.jasper.el.VariableResolverImpl; 58 import org.apache.jasper.security.SecurityUtil; 59 import org.apache.jasper.util.Enumerator; 60 61 73 public class PageContextImpl extends PageContext { 74 75 private static Log log = LogFactory.getLog(PageContextImpl.class); 77 78 private BodyContentImpl[] outs; 79 80 private int depth; 81 82 private Servlet servlet; 84 85 private ServletConfig config; 86 87 private ServletContext context; 88 89 private JspApplicationContextImpl applicationContext; 90 91 private String errorPageURL; 92 93 private transient HashMap <String , Object > attributes; 95 96 private transient ServletRequest request; 98 99 private transient ServletResponse response; 100 101 private transient HttpSession session; 102 103 private transient ELContextImpl elContext; 104 105 private boolean isIncluded; 106 107 108 private transient JspWriter out; 110 111 private transient JspWriterImpl baseOut; 112 113 116 PageContextImpl() { 117 this.outs = new BodyContentImpl[0]; 118 this.attributes = new HashMap <String , Object >(16); 119 this.depth = -1; 120 } 121 122 public void initialize(Servlet servlet, ServletRequest request, 123 ServletResponse response, String errorPageURL, 124 boolean needsSession, int bufferSize, boolean autoFlush) 125 throws IOException { 126 127 _initialize(servlet, request, response, errorPageURL, needsSession, 128 bufferSize, autoFlush); 129 } 130 131 private void _initialize(Servlet servlet, ServletRequest request, 132 ServletResponse response, String errorPageURL, 133 boolean needsSession, int bufferSize, boolean autoFlush) 134 throws IOException { 135 136 this.servlet = servlet; 138 this.config = servlet.getServletConfig(); 139 this.context = config.getServletContext(); 140 this.errorPageURL = errorPageURL; 141 this.request = request; 142 this.response = response; 143 144 this.applicationContext = JspApplicationContextImpl.getInstance(context); 146 147 if (request instanceof HttpServletRequest && needsSession) 149 this.session = ((HttpServletRequest ) request).getSession(); 150 if (needsSession && session == null) 151 throw new IllegalStateException ( 152 "Page needs a session and none is available"); 153 154 depth = -1; 156 if (this.baseOut == null) { 157 this.baseOut = new JspWriterImpl(response, bufferSize, autoFlush); 158 } else { 159 this.baseOut.init(response, bufferSize, autoFlush); 160 } 161 this.out = baseOut; 162 163 setAttribute(OUT, this.out); 165 setAttribute(REQUEST, request); 166 setAttribute(RESPONSE, response); 167 168 if (session != null) 169 setAttribute(SESSION, session); 170 171 setAttribute(PAGE, servlet); 172 setAttribute(CONFIG, config); 173 setAttribute(PAGECONTEXT, this); 174 setAttribute(APPLICATION, context); 175 176 isIncluded = request.getAttribute("javax.servlet.include.servlet_path") != null; 177 } 178 179 public void release() { 180 out = baseOut; 181 try { 182 if (isIncluded) { 183 ((JspWriterImpl) out).flushBuffer(); 184 } else { 186 ((JspWriterImpl) out).flushBuffer(); 192 } 193 } catch (IOException ex) { 194 IllegalStateException ise = new IllegalStateException (Localizer.getMessage("jsp.error.flush"), ex); 195 throw ise; 196 } finally { 197 servlet = null; 198 config = null; 199 context = null; 200 applicationContext = null; 201 elContext = null; 202 errorPageURL = null; 203 request = null; 204 response = null; 205 depth = -1; 206 baseOut.recycle(); 207 session = null; 208 attributes.clear(); 209 } 210 } 211 212 public Object getAttribute(final String name) { 213 214 if (name == null) { 215 throw new NullPointerException (Localizer 216 .getMessage("jsp.error.attribute.null_name")); 217 } 218 219 if (SecurityUtil.isPackageProtectionEnabled()) { 220 return AccessController.doPrivileged(new PrivilegedAction () { 221 public Object run() { 222 return doGetAttribute(name); 223 } 224 }); 225 } else { 226 return doGetAttribute(name); 227 } 228 229 } 230 231 private Object doGetAttribute(String name) { 232 return attributes.get(name); 233 } 234 235 public Object getAttribute(final String name, final int scope) { 236 237 if (name == null) { 238 throw new NullPointerException (Localizer 239 .getMessage("jsp.error.attribute.null_name")); 240 } 241 242 if (SecurityUtil.isPackageProtectionEnabled()) { 243 return AccessController.doPrivileged(new PrivilegedAction () { 244 public Object run() { 245 return doGetAttribute(name, scope); 246 } 247 }); 248 } else { 249 return doGetAttribute(name, scope); 250 } 251 252 } 253 254 private Object doGetAttribute(String name, int scope) { 255 switch (scope) { 256 case PAGE_SCOPE: 257 return attributes.get(name); 258 259 case REQUEST_SCOPE: 260 return request.getAttribute(name); 261 262 case SESSION_SCOPE: 263 if (session == null) { 264 throw new IllegalStateException (Localizer 265 .getMessage("jsp.error.page.noSession")); 266 } 267 return session.getAttribute(name); 268 269 case APPLICATION_SCOPE: 270 return context.getAttribute(name); 271 272 default: 273 throw new IllegalArgumentException ("Invalid scope"); 274 } 275 } 276 277 public void setAttribute(final String name, final Object attribute) { 278 279 if (name == null) { 280 throw new NullPointerException (Localizer 281 .getMessage("jsp.error.attribute.null_name")); 282 } 283 284 if (SecurityUtil.isPackageProtectionEnabled()) { 285 AccessController.doPrivileged(new PrivilegedAction () { 286 public Object run() { 287 doSetAttribute(name, attribute); 288 return null; 289 } 290 }); 291 } else { 292 doSetAttribute(name, attribute); 293 } 294 } 295 296 private void doSetAttribute(String name, Object attribute) { 297 if (attribute != null) { 298 attributes.put(name, attribute); 299 } else { 300 removeAttribute(name, PAGE_SCOPE); 301 } 302 } 303 304 public void setAttribute(final String name, final Object o, final int scope) { 305 306 if (name == null) { 307 throw new NullPointerException (Localizer 308 .getMessage("jsp.error.attribute.null_name")); 309 } 310 311 if (SecurityUtil.isPackageProtectionEnabled()) { 312 AccessController.doPrivileged(new PrivilegedAction () { 313 public Object run() { 314 doSetAttribute(name, o, scope); 315 return null; 316 } 317 }); 318 } else { 319 doSetAttribute(name, o, scope); 320 } 321 322 } 323 324 private void doSetAttribute(String name, Object o, int scope) { 325 if (o != null) { 326 switch (scope) { 327 case PAGE_SCOPE: 328 attributes.put(name, o); 329 break; 330 331 case REQUEST_SCOPE: 332 request.setAttribute(name, o); 333 break; 334 335 case SESSION_SCOPE: 336 if (session == null) { 337 throw new IllegalStateException (Localizer 338 .getMessage("jsp.error.page.noSession")); 339 } 340 session.setAttribute(name, o); 341 break; 342 343 case APPLICATION_SCOPE: 344 context.setAttribute(name, o); 345 break; 346 347 default: 348 throw new IllegalArgumentException ("Invalid scope"); 349 } 350 } else { 351 removeAttribute(name, scope); 352 } 353 } 354 355 public void removeAttribute(final String name, final int scope) { 356 357 if (name == null) { 358 throw new NullPointerException (Localizer 359 .getMessage("jsp.error.attribute.null_name")); 360 } 361 if (SecurityUtil.isPackageProtectionEnabled()) { 362 AccessController.doPrivileged(new PrivilegedAction () { 363 public Object run() { 364 doRemoveAttribute(name, scope); 365 return null; 366 } 367 }); 368 } else { 369 doRemoveAttribute(name, scope); 370 } 371 } 372 373 private void doRemoveAttribute(String name, int scope) { 374 switch (scope) { 375 case PAGE_SCOPE: 376 attributes.remove(name); 377 break; 378 379 case REQUEST_SCOPE: 380 request.removeAttribute(name); 381 break; 382 383 case SESSION_SCOPE: 384 if (session == null) { 385 throw new IllegalStateException (Localizer 386 .getMessage("jsp.error.page.noSession")); 387 } 388 session.removeAttribute(name); 389 break; 390 391 case APPLICATION_SCOPE: 392 context.removeAttribute(name); 393 break; 394 395 default: 396 throw new IllegalArgumentException ("Invalid scope"); 397 } 398 } 399 400 public int getAttributesScope(final String name) { 401 402 if (name == null) { 403 throw new NullPointerException (Localizer 404 .getMessage("jsp.error.attribute.null_name")); 405 } 406 407 if (SecurityUtil.isPackageProtectionEnabled()) { 408 return ((Integer ) AccessController 409 .doPrivileged(new PrivilegedAction () { 410 public Object run() { 411 return new Integer (doGetAttributeScope(name)); 412 } 413 })).intValue(); 414 } else { 415 return doGetAttributeScope(name); 416 } 417 } 418 419 private int doGetAttributeScope(String name) { 420 if (attributes.get(name) != null) 421 return PAGE_SCOPE; 422 423 if (request.getAttribute(name) != null) 424 return REQUEST_SCOPE; 425 426 if (session != null) { 427 if (session.getAttribute(name) != null) 428 return SESSION_SCOPE; 429 } 430 431 if (context.getAttribute(name) != null) 432 return APPLICATION_SCOPE; 433 434 return 0; 435 } 436 437 public Object findAttribute(final String name) { 438 if (SecurityUtil.isPackageProtectionEnabled()) { 439 return AccessController.doPrivileged(new PrivilegedAction () { 440 public Object run() { 441 if (name == null) { 442 throw new NullPointerException (Localizer 443 .getMessage("jsp.error.attribute.null_name")); 444 } 445 446 return doFindAttribute(name); 447 } 448 }); 449 } else { 450 if (name == null) { 451 throw new NullPointerException (Localizer 452 .getMessage("jsp.error.attribute.null_name")); 453 } 454 455 return doFindAttribute(name); 456 } 457 } 458 459 private Object doFindAttribute(String name) { 460 461 Object o = attributes.get(name); 462 if (o != null) 463 return o; 464 465 o = request.getAttribute(name); 466 if (o != null) 467 return o; 468 469 if (session != null) { 470 o = session.getAttribute(name); 471 if (o != null) 472 return o; 473 } 474 475 return context.getAttribute(name); 476 } 477 478 public Enumeration <String > getAttributeNamesInScope(final int scope) { 479 if (SecurityUtil.isPackageProtectionEnabled()) { 480 return (Enumeration ) AccessController 481 .doPrivileged(new PrivilegedAction () { 482 public Object run() { 483 return doGetAttributeNamesInScope(scope); 484 } 485 }); 486 } else { 487 return doGetAttributeNamesInScope(scope); 488 } 489 } 490 491 private Enumeration doGetAttributeNamesInScope(int scope) { 492 switch (scope) { 493 case PAGE_SCOPE: 494 return new Enumerator(attributes.keySet().iterator()); 495 496 case REQUEST_SCOPE: 497 return request.getAttributeNames(); 498 499 case SESSION_SCOPE: 500 if (session == null) { 501 throw new IllegalStateException (Localizer 502 .getMessage("jsp.error.page.noSession")); 503 } 504 return session.getAttributeNames(); 505 506 case APPLICATION_SCOPE: 507 return context.getAttributeNames(); 508 509 default: 510 throw new IllegalArgumentException ("Invalid scope"); 511 } 512 } 513 514 public void removeAttribute(final String name) { 515 516 if (name == null) { 517 throw new NullPointerException (Localizer 518 .getMessage("jsp.error.attribute.null_name")); 519 } 520 521 if (SecurityUtil.isPackageProtectionEnabled()) { 522 AccessController.doPrivileged(new PrivilegedAction () { 523 public Object run() { 524 doRemoveAttribute(name); 525 return null; 526 } 527 }); 528 } else { 529 doRemoveAttribute(name); 530 } 531 } 532 533 private void doRemoveAttribute(String name) { 534 try { 535 removeAttribute(name, PAGE_SCOPE); 536 removeAttribute(name, REQUEST_SCOPE); 537 if (session != null) { 538 removeAttribute(name, SESSION_SCOPE); 539 } 540 removeAttribute(name, APPLICATION_SCOPE); 541 } catch (Exception ex) { 542 } 545 } 546 547 public JspWriter getOut() { 548 return out; 549 } 550 551 public HttpSession getSession() { 552 return session; 553 } 554 555 public Servlet getServlet() { 556 return servlet; 557 } 558 559 public ServletConfig getServletConfig() { 560 return config; 561 } 562 563 public ServletContext getServletContext() { 564 return config.getServletContext(); 565 } 566 567 public ServletRequest getRequest() { 568 return request; 569 } 570 571 public ServletResponse getResponse() { 572 return response; 573 } 574 575 582 public Exception getException() { 583 Throwable t = JspRuntimeLibrary.getThrowable(request); 584 585 if ((t != null) && (!(t instanceof Exception ))) { 587 t = new JspException (t); 588 } 589 590 return (Exception ) t; 591 } 592 593 public Object getPage() { 594 return servlet; 595 } 596 597 private final String getAbsolutePathRelativeToContext(String relativeUrlPath) { 598 String path = relativeUrlPath; 599 600 if (!path.startsWith("/")) { 601 String uri = (String ) request 602 .getAttribute("javax.servlet.include.servlet_path"); 603 if (uri == null) 604 uri = ((HttpServletRequest ) request).getServletPath(); 605 String baseURI = uri.substring(0, uri.lastIndexOf('/')); 606 path = baseURI + '/' + path; 607 } 608 609 return path; 610 } 611 612 public void include(String relativeUrlPath) throws ServletException , 613 IOException { 614 JspRuntimeLibrary 615 .include(request, response, relativeUrlPath, out, true); 616 } 617 618 public void include(final String relativeUrlPath, final boolean flush) 619 throws ServletException , IOException { 620 if (SecurityUtil.isPackageProtectionEnabled()) { 621 try { 622 AccessController.doPrivileged(new PrivilegedExceptionAction () { 623 public Object run() throws Exception { 624 doInclude(relativeUrlPath, flush); 625 return null; 626 } 627 }); 628 } catch (PrivilegedActionException e) { 629 Exception ex = e.getException(); 630 if (ex instanceof IOException ) { 631 throw (IOException ) ex; 632 } else { 633 throw (ServletException ) ex; 634 } 635 } 636 } else { 637 doInclude(relativeUrlPath, flush); 638 } 639 } 640 641 private void doInclude(String relativeUrlPath, boolean flush) 642 throws ServletException , IOException { 643 JspRuntimeLibrary.include(request, response, relativeUrlPath, out, 644 flush); 645 } 646 647 public VariableResolver getVariableResolver() { 648 return new VariableResolverImpl(this.getELContext()); 649 } 650 651 public void forward(final String relativeUrlPath) throws ServletException , 652 IOException { 653 if (SecurityUtil.isPackageProtectionEnabled()) { 654 try { 655 AccessController.doPrivileged(new PrivilegedExceptionAction () { 656 public Object run() throws Exception { 657 doForward(relativeUrlPath); 658 return null; 659 } 660 }); 661 } catch (PrivilegedActionException e) { 662 Exception ex = e.getException(); 663 if (ex instanceof IOException ) { 664 throw (IOException ) ex; 665 } else { 666 throw (ServletException ) ex; 667 } 668 } 669 } else { 670 doForward(relativeUrlPath); 671 } 672 } 673 674 private void doForward(String relativeUrlPath) throws ServletException , 675 IOException { 676 677 try { 679 out.clear(); 680 } catch (IOException ex) { 681 IllegalStateException ise = new IllegalStateException (Localizer 682 .getMessage("jsp.error.attempt_to_clear_flushed_buffer")); 683 ise.initCause(ex); 684 throw ise; 685 } 686 687 while (response instanceof ServletResponseWrapperInclude) { 689 response = ((ServletResponseWrapperInclude) response).getResponse(); 690 } 691 692 final String path = getAbsolutePathRelativeToContext(relativeUrlPath); 693 String includeUri = (String ) request 694 .getAttribute(Constants.INC_SERVLET_PATH); 695 696 if (includeUri != null) 697 request.removeAttribute(Constants.INC_SERVLET_PATH); 698 try { 699 context.getRequestDispatcher(path).forward(request, response); 700 } finally { 701 if (includeUri != null) 702 request.setAttribute(Constants.INC_SERVLET_PATH, includeUri); 703 } 704 } 705 706 public BodyContent pushBody() { 707 return (BodyContent ) pushBody(null); 708 } 709 710 public JspWriter pushBody(Writer writer) { 711 depth++; 712 if (depth >= outs.length) { 713 BodyContentImpl[] newOuts = new BodyContentImpl[depth + 1]; 714 for (int i = 0; i < outs.length; i++) { 715 newOuts[i] = outs[i]; 716 } 717 newOuts[depth] = new BodyContentImpl(out); 718 outs = newOuts; 719 } 720 721 outs[depth].setWriter(writer); 722 out = outs[depth]; 723 724 setAttribute(OUT, out); 727 728 return outs[depth]; 729 } 730 731 public JspWriter popBody() { 732 depth--; 733 if (depth >= 0) { 734 out = outs[depth]; 735 } else { 736 out = baseOut; 737 } 738 739 setAttribute(OUT, out); 742 743 return out; 744 } 745 746 751 public ExpressionEvaluator getExpressionEvaluator() { 752 return new ExpressionEvaluatorImpl(this.applicationContext.getExpressionFactory()); 753 } 754 755 public void handlePageException(Exception ex) throws IOException , 756 ServletException { 757 handlePageException((Throwable ) ex); 760 } 761 762 public void handlePageException(final Throwable t) throws IOException , 763 ServletException { 764 if (t == null) 765 throw new NullPointerException ("null Throwable"); 766 767 if (SecurityUtil.isPackageProtectionEnabled()) { 768 try { 769 AccessController.doPrivileged(new PrivilegedExceptionAction () { 770 public Object run() throws Exception { 771 doHandlePageException(t); 772 return null; 773 } 774 }); 775 } catch (PrivilegedActionException e) { 776 Exception ex = e.getException(); 777 if (ex instanceof IOException ) { 778 throw (IOException ) ex; 779 } else { 780 throw (ServletException ) ex; 781 } 782 } 783 } else { 784 doHandlePageException(t); 785 } 786 787 } 788 789 private void doHandlePageException(Throwable t) throws IOException , 790 ServletException { 791 792 if (errorPageURL != null && !errorPageURL.equals("")) { 793 794 803 request.setAttribute("javax.servlet.jsp.jspException", t); 804 request.setAttribute("javax.servlet.error.status_code", 805 new Integer (HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); 806 request.setAttribute("javax.servlet.error.request_uri", 807 ((HttpServletRequest ) request).getRequestURI()); 808 request.setAttribute("javax.servlet.error.servlet_name", config 809 .getServletName()); 810 try { 811 forward(errorPageURL); 812 } catch (IllegalStateException ise) { 813 include(errorPageURL); 814 } 815 816 818 Object newException = request 819 .getAttribute("javax.servlet.error.exception"); 820 821 if ((newException != null) && (newException == t)) { 823 request.removeAttribute("javax.servlet.error.exception"); 824 } 825 826 request.removeAttribute("javax.servlet.error.status_code"); 828 request.removeAttribute("javax.servlet.error.request_uri"); 829 request.removeAttribute("javax.servlet.error.status_code"); 830 request.removeAttribute("javax.servlet.jsp.jspException"); 831 832 } else { 833 if (t instanceof IOException ) 837 throw (IOException ) t; 838 if (t instanceof ServletException ) 839 throw (ServletException ) t; 840 if (t instanceof RuntimeException ) 841 throw (RuntimeException ) t; 842 843 Throwable rootCause = null; 844 if (t instanceof JspException ) { 845 rootCause = ((JspException ) t).getRootCause(); 846 } else if (t instanceof ELException ) { 847 rootCause = ((ELException ) t).getRootCause(); 848 } 849 850 if (rootCause != null) { 851 throw new ServletException (t.getClass().getName() + ": " 852 + t.getMessage(), rootCause); 853 } 854 855 throw new ServletException (t); 856 } 857 } 858 859 private static String XmlEscape(String s) { 860 if (s == null) 861 return null; 862 StringBuffer sb = new StringBuffer (); 863 for (int i = 0; i < s.length(); i++) { 864 char c = s.charAt(i); 865 if (c == '<') { 866 sb.append("<"); 867 } else if (c == '>') { 868 sb.append(">"); 869 } else if (c == '\'') { 870 sb.append("'"); } else if (c == '&') { 872 sb.append("&"); 873 } else if (c == '"') { 874 sb.append("""); } else { 876 sb.append(c); 877 } 878 } 879 return sb.toString(); 880 } 881 882 898 public static Object proprietaryEvaluate(final String expression, 899 final Class expectedType, final PageContext pageContext, 900 final ProtectedFunctionMapper functionMap, final boolean escape) 901 throws ELException { 902 Object retValue; 903 final ExpressionFactory exprFactory = JspFactory.getDefaultFactory().getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory(); 904 if (SecurityUtil.isPackageProtectionEnabled()) { 905 try { 906 retValue = AccessController 907 .doPrivileged(new PrivilegedExceptionAction () { 908 909 public Object run() throws Exception { 910 ELContextImpl ctx = (ELContextImpl) pageContext.getELContext(); 911 ctx.setFunctionMapper(new FunctionMapperImpl(functionMap)); 912 ValueExpression ve = exprFactory.createValueExpression(ctx, expression, expectedType); 913 return ve.getValue(ctx); 914 } 915 }); 916 } catch (PrivilegedActionException ex) { 917 Exception realEx = ex.getException(); 918 if (realEx instanceof ELException ) { 919 throw (ELException ) realEx; 920 } else { 921 throw new ELException (realEx); 922 } 923 } 924 } else { 925 ELContextImpl ctx = (ELContextImpl) pageContext.getELContext(); 926 ctx.setFunctionMapper(new FunctionMapperImpl(functionMap)); 927 ValueExpression ve = exprFactory.createValueExpression(ctx, expression, expectedType); 928 retValue = ve.getValue(ctx); 929 } 930 if (escape && retValue != null) { 931 retValue = XmlEscape(retValue.toString()); 932 } 933 934 return retValue; 935 } 936 937 public ELContext getELContext() { 938 if (this.elContext == null) { 939 this.elContext = this.applicationContext.createELContext(this); 940 } 941 return this.elContext; 942 } 943 944 } 945 | Popular Tags |