1 18 19 package org.apache.struts.taglib.tiles; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.PageContext ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.struts.Globals; 35 import org.apache.struts.taglib.tiles.util.TagUtils; 36 import org.apache.struts.tiles.AttributeDefinition; 37 import org.apache.struts.tiles.ComponentContext; 38 import org.apache.struts.tiles.ComponentDefinition; 39 import org.apache.struts.tiles.Controller; 40 import org.apache.struts.tiles.DefinitionAttribute; 41 import org.apache.struts.tiles.DefinitionNameAttribute; 42 import org.apache.struts.tiles.DefinitionsFactoryException; 43 import org.apache.struts.tiles.DirectStringAttribute; 44 import org.apache.struts.tiles.FactoryNotFoundException; 45 import org.apache.struts.tiles.NoSuchDefinitionException; 46 import org.apache.struts.tiles.TilesUtil; 47 48 55 public class InsertTag 56 extends DefinitionTagSupport 57 implements PutTagParent, ComponentConstants, PutListTagParent { 58 59 63 public static final String ROLE_DELIMITER = ","; 64 65 68 protected static Log log = LogFactory.getLog(InsertTag.class); 69 70 71 72 75 protected boolean flush = true; 76 77 80 protected String name = null; 81 82 85 protected String attribute = null; 86 87 90 protected String beanName = null; 91 92 95 protected String beanProperty = null; 96 97 100 protected String beanScope = null; 101 102 107 protected boolean isErrorIgnored = false; 108 109 112 protected String definitionName = null; 113 114 115 119 protected boolean processEndTag = true; 120 121 124 protected ComponentContext cachedCurrentContext; 125 126 129 protected TagHandler tagHandler = null; 130 131 134 protected PageContext pageContext = null; 135 136 140 public void release() { 141 142 super.release(); 143 attribute = null; 144 beanName = null; 145 beanProperty = null; 146 beanScope = null; 147 148 definitionName = null; 149 flush = true; 150 name = null; 151 page = null; 152 role = null; 153 isErrorIgnored = false; 154 155 releaseInternal(); 156 } 157 158 161 protected void releaseInternal() { 162 cachedCurrentContext = null; 163 processEndTag = true; 164 tagHandler = null; 166 } 167 168 174 public void setPageContext(PageContext pc) { 175 this.pageContext = pc; 176 super.setPageContext(pc); 177 } 178 179 182 public PageContext getPageContext() { 183 return pageContext; 184 } 185 186 189 public void setName(String value) { 190 this.name = value; 191 } 192 193 196 public String getName() { 197 return name; 198 } 199 200 203 public void setComponent(String name) { 204 this.page = name; 205 } 206 207 210 public void setDefinition(String name) { 211 this.definitionName = name; 212 } 213 214 217 public String getDefinitionName() { 218 return definitionName; 219 } 220 221 224 public void setAttribute(String value) { 225 this.attribute = value; 226 } 227 228 231 public void setBeanName(String value) { 232 this.beanName = value; 233 } 234 235 238 public String getBeanName() { 239 return beanName; 240 } 241 242 245 public void setBeanProperty(String value) { 246 this.beanProperty = value; 247 } 248 249 252 public String getBeanProperty() { 253 return beanProperty; 254 } 255 256 259 public void setBeanScope(String value) { 260 this.beanScope = value; 261 } 262 263 266 public String getBeanScope() { 267 return beanScope; 268 } 269 270 273 public void setFlush(boolean flush) { 274 this.flush = flush; 275 } 276 277 280 public boolean getFlush() { 281 return flush; 282 } 283 284 288 public void setFlush(String flush) { 289 this.flush = (Boolean.valueOf(flush).booleanValue()); 290 } 291 292 295 public void setIgnore(boolean ignore) { 296 this.isErrorIgnored = ignore; 297 } 298 299 302 public boolean getIgnore() { 303 return isErrorIgnored; 304 } 305 306 308 312 public void putAttribute(String name, Object value) { 313 tagHandler.putAttribute(name, value); 314 } 315 316 322 public void processNestedTag(PutTag nestedTag) throws JspException { 323 HttpServletRequest request = 325 (HttpServletRequest ) pageContext.getRequest(); 326 String role = nestedTag.getRole(); 327 if (role != null && !request.isUserInRole(role)) { 328 return; 330 } 331 332 putAttribute(nestedTag.getName(), nestedTag.getRealValue()); 333 } 334 335 341 public void processNestedTag(PutListTag nestedTag) throws JspException { 342 HttpServletRequest request = 344 (HttpServletRequest ) pageContext.getRequest(); 345 String role = nestedTag.getRole(); 346 if (role != null && !request.isUserInRole(role)) { 347 return; 349 } 350 351 if (nestedTag.getName() == null) { 353 throw new JspException ("Error - PutList : attribute name is not defined. It is mandatory as the list is added as attribute of 'insert'."); 354 } 355 356 putAttribute(nestedTag.getName(), nestedTag.getList()); 358 } 359 360 364 public void putAttribute(PutListTag nestedTag) throws JspException { 365 HttpServletRequest request = 367 (HttpServletRequest ) pageContext.getRequest(); 368 String role = nestedTag.getRole(); 369 if (role != null && !request.isUserInRole(role)) { 370 return; 372 } 373 374 putAttribute(nestedTag.getName(), nestedTag.getList()); 375 } 376 377 380 private ComponentContext getCurrentContext() { 381 if (cachedCurrentContext == null) { 382 cachedCurrentContext = 383 (ComponentContext) pageContext.getAttribute( 384 ComponentConstants.COMPONENT_CONTEXT, 385 PageContext.REQUEST_SCOPE); 386 } 387 388 return cachedCurrentContext; 389 } 390 391 397 private Controller getController() throws JspException { 398 if (controllerType == null) { 399 return null; 400 } 401 402 try { 403 return ComponentDefinition.createController( 404 controllerName, 405 controllerType); 406 407 } catch (InstantiationException ex) { 408 throw new JspException (ex.getMessage()); 409 } 410 } 411 412 422 public int doStartTag() throws JspException { 423 424 cachedCurrentContext = null; 426 427 HttpServletRequest request = 431 (HttpServletRequest ) pageContext.getRequest(); 432 if (role != null && !request.isUserInRole(role)) { 433 processEndTag = false; 434 return SKIP_BODY; 435 } 436 437 try { 438 tagHandler = createTagHandler(); 439 440 } catch (JspException e) { 441 if (isErrorIgnored) { 442 processEndTag = false; 443 return SKIP_BODY; 444 } else { 445 throw e; 446 } 447 } 448 449 return tagHandler.doStartTag(); 450 } 451 452 456 public int doEndTag() throws JspException { 457 if (!processEndTag) { 458 releaseInternal(); 459 return EVAL_PAGE; 460 } 461 462 int res = tagHandler.doEndTag(); 463 releaseInternal(); 465 return res; 466 } 467 468 471 public TagHandler createTagHandler() throws JspException { 472 if (definitionName != null) { 476 return processDefinitionName(definitionName); 477 } else if (attribute != null) { 478 return processAttribute(attribute); 479 } else if (beanName != null) { 480 return processBean(beanName, beanProperty, beanScope); 481 } else if (name != null) { 482 return processName(name); 483 } else if (page != null) { 484 return processUrl(page); 485 } else { 486 throw new JspException ("Error - Tag Insert : At least one of the following attribute must be defined : template|page|attribute|definition|name|beanName. Check tag syntax"); 487 } 488 } 489 490 498 public TagHandler processObjectValue(Object value) throws JspException { 499 if (value instanceof AttributeDefinition) { 501 return processTypedAttribute((AttributeDefinition) value); 503 504 } else if (value instanceof ComponentDefinition) { 505 return processDefinition((ComponentDefinition) value); 506 } 507 508 return processAsDefinitionOrURL(value.toString()); 510 } 511 512 526 public TagHandler processName(String name) throws JspException { 527 Object attrValue = getCurrentContext().getAttribute(name); 528 529 if (attrValue != null) { 530 return processObjectValue(attrValue); 531 } 532 533 return processAsDefinitionOrURL(name); 534 } 535 536 540 public TagHandler processUrl(String url) throws JspException { 541 return new InsertHandler(url, role, getController()); 542 } 543 544 554 protected TagHandler processDefinitionName(String name) 555 throws JspException { 556 557 try { 558 ComponentDefinition definition = 559 TilesUtil.getDefinition( 560 name, 561 (HttpServletRequest ) pageContext.getRequest(), 562 pageContext.getServletContext()); 563 564 if (definition == null) { throw new NoSuchDefinitionException(); 566 } 567 568 return processDefinition(definition); 569 570 } catch (NoSuchDefinitionException ex) { 571 throw new JspException ( 572 "Error - Tag Insert : Can't get definition '" 573 + definitionName 574 + "'. Check if this name exist in definitions factory."); 575 576 } catch (FactoryNotFoundException ex) { 577 throw new JspException (ex.getMessage()); 578 579 } catch (DefinitionsFactoryException ex) { 580 if (log.isDebugEnabled()) { 581 ex.printStackTrace(); 582 } 583 584 pageContext.setAttribute( 586 Globals.EXCEPTION_KEY, 587 ex, 588 PageContext.REQUEST_SCOPE); 589 throw new JspException (ex.getMessage()); 590 } 591 } 592 593 601 protected TagHandler processDefinition(ComponentDefinition definition) 602 throws JspException { 603 String role = this.role; 605 String page = this.page; 606 Controller controller = null; 607 608 try { 609 controller = definition.getOrCreateController(); 610 611 if (role == null) { 613 role = definition.getRole(); 614 } 615 616 if (page == null) { 617 page = definition.getTemplate(); 618 } 619 620 if (controllerName != null) { 621 controller = 622 ComponentDefinition.createController( 623 controllerName, 624 controllerType); 625 } 626 627 return new InsertHandler( 629 definition.getAttributes(), 630 page, 631 role, 632 controller); 633 634 } catch (InstantiationException ex) { 635 throw new JspException (ex.getMessage()); 636 } 637 } 638 639 650 protected TagHandler processBean( 651 String beanName, 652 String beanProperty, 653 String beanScope) 654 throws JspException { 655 656 Object beanValue = 657 TagUtils.getRealValueFromBean( 658 beanName, 659 beanProperty, 660 beanScope, 661 pageContext); 662 663 if (beanValue == null) { 664 throw new JspException ( 665 "Error - Tag Insert : No value defined for bean '" 666 + beanName 667 + "' with property '" 668 + beanProperty 669 + "' in scope '" 670 + beanScope 671 + "'."); 672 } 673 674 return processObjectValue(beanValue); 675 } 676 677 686 public TagHandler processAttribute(String name) throws JspException { 687 Object attrValue = getCurrentContext().getAttribute(name); 688 689 if (attrValue == null) { 690 throw new JspException ( 691 "Error - Tag Insert : No value found for attribute '" 692 + name 693 + "'."); 694 } 695 696 return processObjectValue(attrValue); 697 } 698 699 705 public TagHandler processAsDefinitionOrURL(String name) 706 throws JspException { 707 try { 708 ComponentDefinition definition = 709 TilesUtil.getDefinition( 710 name, 711 pageContext.getRequest(), 712 pageContext.getServletContext()); 713 714 if (definition != null) { 715 return processDefinition(definition); 716 } 717 718 } catch (DefinitionsFactoryException ex) { 719 } 721 722 return processUrl(name); 724 } 725 726 732 public TagHandler processTypedAttribute(AttributeDefinition value) 733 throws JspException { 734 if (value instanceof DirectStringAttribute) { 735 return new DirectStringHandler((String ) value.getValue()); 736 737 } else if (value instanceof DefinitionAttribute) { 738 return processDefinition((ComponentDefinition) value.getValue()); 739 740 } else if (value instanceof DefinitionNameAttribute) { 741 return processDefinitionName((String ) value.getValue()); 742 } 743 744 return new InsertHandler( 745 (String ) value.getValue(), 746 role, 747 getController()); 748 } 749 750 758 protected void doInclude(String page) 759 throws ServletException , IOException { 760 TilesUtil.doInclude(page, pageContext); 761 } 762 763 765 769 protected interface TagHandler { 770 773 public int doStartTag() throws JspException ; 774 777 public int doEndTag() throws JspException ; 778 781 public void putAttribute(String name, Object value); 782 } 784 786 790 protected class InsertHandler implements TagHandler { 791 protected String page; 792 protected ComponentContext currentContext; 793 protected ComponentContext subCompContext; 794 protected String role; 795 protected Controller controller; 796 797 801 public InsertHandler( 802 Map attributes, 803 String page, 804 String role, 805 Controller controller) { 806 807 this.page = page; 808 this.role = role; 809 this.controller = controller; 810 subCompContext = new ComponentContext(attributes); 811 } 812 813 817 public InsertHandler(String page, String role, Controller controller) { 818 this.page = page; 819 this.role = role; 820 this.controller = controller; 821 subCompContext = new ComponentContext(); 822 } 823 824 827 public int doStartTag() throws JspException { 828 HttpServletRequest request = 830 (HttpServletRequest ) pageContext.getRequest(); 831 832 if (role != null && !request.isUserInRole(role)) { 833 return SKIP_BODY; 834 } 835 836 this.currentContext = getCurrentContext(); 838 return EVAL_BODY_INCLUDE; 839 } 840 841 845 public void putAttribute(String name, Object value) { 846 subCompContext.putAttribute(name, value); 847 } 848 849 852 public int doEndTag() throws JspException { 853 HttpServletRequest request = 855 (HttpServletRequest ) pageContext.getRequest(); 856 857 if (role != null && !request.isUserInRole(role)) { 858 return EVAL_PAGE; 859 } 860 861 try { 862 if (log.isDebugEnabled()) { 863 log.debug("insert page='" + page + "'."); 864 } 865 866 pageContext.setAttribute( 868 ComponentConstants.COMPONENT_CONTEXT, 869 subCompContext, 870 PageContext.REQUEST_SCOPE); 871 872 if (controller != null) { 874 try { 875 controller.execute( 876 subCompContext, 877 (HttpServletRequest ) pageContext.getRequest(), 878 (HttpServletResponse ) pageContext.getResponse(), 879 pageContext.getServletContext()); 880 881 } catch (Exception e) { 882 throw new ServletException (e); 883 } 884 885 } 886 887 if (flush) { 889 pageContext.getOut().flush(); 890 } 891 892 doInclude(page); 893 894 } catch (IOException e) { 895 String msg = 896 "Can't insert page '" + page + "' : " + e.getMessage(); 897 log.error(msg, e); 898 throw new JspException (msg); 899 900 } catch (IllegalArgumentException e) { 901 if (!(page == null && isErrorIgnored)) { 903 String msg = 904 "Can't insert page '" 905 + page 906 + "'. Check if it exists.\n" 907 + e.getMessage(); 908 909 log.error(msg, e); 910 throw new JspException (msg); 911 } 912 913 } catch (ServletException e) { 914 Throwable cause = e; 915 if (e.getRootCause() != null) { 916 cause = e.getRootCause(); 917 } 918 919 String msg = 920 "ServletException in '" + page + "': " + cause.getMessage(); 921 922 log.error(msg, e); 923 throw new JspException (msg); 924 925 } finally { 926 if (currentContext != null) { 929 pageContext.setAttribute( 930 ComponentConstants.COMPONENT_CONTEXT, 931 currentContext, 932 PageContext.REQUEST_SCOPE); 933 } 934 } 935 936 return EVAL_PAGE; 937 } 938 939 947 protected void processException(Throwable ex, String msg) 948 throws JspException { 949 950 try { 951 if (msg == null) { 952 msg = ex.getMessage(); 953 } 954 955 if (log.isDebugEnabled()) { log.debug(msg, ex); 957 pageContext.getOut().println(msg); 958 ex.printStackTrace( 959 new PrintWriter (pageContext.getOut(), true)); 960 } else { pageContext.getOut().println(msg); 962 } 963 964 } catch (IOException ioex) { pageContext.setAttribute( 966 ComponentConstants.EXCEPTION_KEY, 967 ex, 968 PageContext.REQUEST_SCOPE); 969 throw new JspException (msg); 970 } 971 } 972 } 973 974 980 static public boolean userHasRole( 981 HttpServletRequest request, 982 String role) { 983 StringTokenizer st = new StringTokenizer (role, ","); 984 while (st.hasMoreTokens()) { 985 if (request.isUserInRole(st.nextToken())) { 986 return true; 987 } 988 } 989 990 return false; 991 } 992 993 995 998 protected class DirectStringHandler implements TagHandler { 999 1000 private Object value; 1001 1002 1005 public DirectStringHandler(Object value) { 1006 this.value = value; 1007 } 1008 1009 1012 public int doStartTag() throws JspException { 1013 return SKIP_BODY; 1014 } 1015 1016 1020 public void putAttribute(String name, Object value) { 1021 } 1022 1023 1026 public int doEndTag() throws JspException { 1027 try { 1028 if (flush) { 1029 pageContext.getOut().flush(); 1030 } 1031 1032 pageContext.getOut().print(value); 1033 1034 } catch (IOException ex) { 1035 if (log.isDebugEnabled()) { 1036 log.debug("Can't write string '" + value + "' : ", ex); 1037 } 1038 1039 pageContext.setAttribute( 1040 ComponentConstants.EXCEPTION_KEY, 1041 ex, 1042 PageContext.REQUEST_SCOPE); 1043 1044 throw new JspException ( 1045 "Can't write string '" + value + "' : " + ex.getMessage()); 1046 } 1047 1048 return EVAL_PAGE; 1049 } 1050 } 1051} 1052 | Popular Tags |