1 209 package com.ivata.groupware.web.tag; 210 211 import java.io.File ; 212 import java.io.IOException ; 213 import java.io.UnsupportedEncodingException ; 214 import java.util.List ; 215 import java.util.Locale ; 216 import java.util.Vector ; 217 218 import javax.servlet.http.HttpServletRequest ; 219 import javax.servlet.http.HttpSession ; 220 import javax.servlet.jsp.JspException ; 221 import javax.servlet.jsp.JspWriter ; 222 import javax.servlet.jsp.tagext.BodyContent ; 223 import javax.servlet.jsp.tagext.BodyTagSupport ; 224 225 import org.apache.log4j.Logger; 226 import org.apache.struts.Globals; 227 import org.apache.struts.taglib.TagUtils; 228 import org.picocontainer.PicoContainer; 229 230 import com.ivata.groupware.admin.security.Security; 231 import com.ivata.groupware.admin.security.server.SecuritySession; 232 import com.ivata.groupware.admin.security.user.UserDO; 233 import com.ivata.groupware.admin.setting.Settings; 234 import com.ivata.groupware.container.PicoContainerFactory; 235 import com.ivata.mask.persistence.FinderException; 236 import com.ivata.mask.util.StringHandling; 237 import com.ivata.mask.util.SystemException; 238 import com.ivata.mask.web.RewriteHandling; 239 import com.ivata.mask.web.format.URLFormat; 240 import com.ivata.mask.web.servlet.GenerateCSS; 241 import com.ivata.mask.web.tag.theme.ThemeConstants; 242 243 244 351 public class HeadTag extends BodyTagSupport { 352 353 356 private static final String DEFAULT_CSS_NAME = "default.css"; 357 360 private static final Logger logger = Logger.getLogger(HeadTag.class); 361 364 private static final String MAIN_CSS_NAME = "ivataMain.css"; 365 368 private static final String MSIE_CSS_NAME = "internetExplorer.css"; 369 373 private String bundle = null; 374 377 private boolean frames = false; 378 381 private String javaScript = null; 382 387 private boolean keepOnTop = true; 388 391 private boolean login = false; 392 397 private String title = null; 398 404 private List titleArgs = new Vector (); 405 411 private String titleKey = null; 412 420 private boolean topLevel = false; 421 432 public int doAfterBody() throws JspException { 433 BodyContent bodyContent = getBodyContent(); 434 if(bodyContent != null) { 435 try { 436 JspWriter out = getPreviousOut(); 437 writeTagBodyContent(out, bodyContent); 438 } catch(Exception ex) { 439 throw new JspException (ex); 440 } 441 } 442 443 return SKIP_BODY; 445 } 446 public int doEndTag() throws JspException { 447 HttpSession session = pageContext.getSession(); 448 TagUtils tagUtils = TagUtils.getInstance(); 449 450 HttpServletRequest request = 451 (HttpServletRequest ) pageContext.getRequest(); 452 SecuritySession securitySession = (SecuritySession) 453 session.getAttribute("securitySession"); 454 boolean print = (request.getParameter("print") != null); 455 if (securitySession == null) { 456 try { 457 PicoContainer container = PicoContainerFactory.getInstance() 458 .getGlobalContainer(); 459 Security security = 460 (Security)container.getComponentInstance(Security.class); 461 securitySession = security.loginGuest(); 462 } catch (SystemException e) { 463 throw new JspException (e); 464 } 465 session.setAttribute("securitySession", securitySession); 466 } 467 PicoContainer container = securitySession.getContainer(); 468 Settings settings = 469 (Settings) container.getComponentInstance(Settings.class); 470 UserDO user = securitySession.getUser(); 471 472 String themeName = (String ) pageContext.findAttribute( 475 ThemeConstants.ATTRIBUTE_THEME_NAME); 476 if (StringHandling.isNullOrEmpty(themeName)) { 477 try { 478 themeName = settings.getStringSetting(securitySession, 480 "siteTheme", user); 481 } catch (SystemException e) { 482 throw new JspException (e); 483 } 484 session.setAttribute(ThemeConstants.ATTRIBUTE_THEME_NAME, 485 themeName); 486 } 487 488 String encoding; 489 try { 490 Object localeObject = session.getAttribute(Globals.LOCALE_KEY); 491 if (localeObject == null) { 492 String localeLanguage; 493 try { 494 localeLanguage = settings.getStringSetting(securitySession, 495 "i18nLocaleLanguage", null); 496 } catch (FinderException e) { 497 if (logger.isDebugEnabled()) { 498 logger.warn("Could not retrieve locale language from " 499 + "setting 'i18nLocaleLanguage'. Defaulting " 500 + "to en.", e); 501 } else { 502 logger.warn("Could not retrieve locale language from " 503 + "setting 'i18nLocaleLanguage'. Defaulting " 504 + "to en."); 505 } 506 localeLanguage = "en"; 507 } 508 509 session.setAttribute(Globals.LOCALE_KEY, 510 new Locale (localeLanguage)); 511 } 512 try { 514 encoding = settings.getStringSetting(securitySession, 515 "i18nEncoding", 516 user); 517 } catch (FinderException e) { 518 if (logger.isDebugEnabled()) { 519 logger.warn("Could not retrieve encoding from setting " 520 + "'i18nEncoding'. Defaulting to utf-8.", e); 521 } else { 522 logger.warn("Could not retrieve encoding from setting " 523 + "'i18nEncoding'. Defaulting to utf-8."); 524 } 525 encoding = "utf-8"; 526 } 527 pageContext.getRequest().setCharacterEncoding(encoding); 528 pageContext.getResponse().setContentType("text/HTML; charset=" 529 + encoding); 530 531 } catch(UnsupportedEncodingException e) { 532 throw new JspException (e); 533 534 } catch(SystemException e) { 535 throw new JspException (e); 536 } 537 538 String myTitle = title; 541 if (myTitle==null) { 542 try { 544 myTitle = ""; 545 if (titleKey != null) { 546 myTitle = tagUtils.message(pageContext, bundle, 548 Globals.LOCALE_KEY, titleKey, titleArgs.toArray()); 549 if (myTitle == null) { 550 myTitle = tagUtils.message(pageContext, null, 551 Globals.LOCALE_KEY, titleKey, 552 titleArgs.toArray()); 553 } 554 if (myTitle == null) { 555 StringBuffer warning = 556 new StringBuffer ("Could not find a title for key '"); 557 warning.append(titleKey); 558 warning.append("'. I checked "); 559 if (bundle != null) { 560 warning.append(" bundle '"); 561 warning.append(bundle); 562 warning.append("' and "); 563 } 564 warning.append("the default (null) bundle."); 565 logger.warn(warning.toString()); 566 myTitle = ""; 567 } 568 } 569 570 if (!"".equals(myTitle)) { 571 myTitle += " - "; 572 } 573 try { 574 myTitle += settings.getStringSetting(securitySession, 575 "siteTitle", 576 user); 577 } catch(FinderException e) { 578 if (logger.isDebugEnabled()) { 579 logger.warn("Could locate value for setting " 580 + "'siteTitle'.", e); 581 582 } else { 583 logger.warn("Could locate value for setting " 584 + "'siteTitle'."); 585 } 586 } 587 } catch(SystemException e) { 588 throw new JspException (e); 589 } 590 } 591 if (print) { 592 String printLabel = StringHandling.getNotNull(tagUtils 593 .message(pageContext, null, 594 Globals.LOCALE_KEY, "print.label.title", 595 null), 596 "Printing "); 597 myTitle = printLabel + myTitle; 598 } 599 600 601 StringBuffer javaScriptBuffer = new StringBuffer (); 604 if(javaScript != null) { 606 javaScriptBuffer.append(javaScript); 607 javaScriptBuffer.append('\n'); 608 } 609 if (topLevel) { 611 Integer width = StringHandling.integerValue(request.getParameter( 613 "width")); 614 Integer height = StringHandling.integerValue(request.getParameter( 615 "height")); 616 if ((width != null) 617 && (height != null)){ 618 javaScriptBuffer.append("window.resizeTo("); 619 javaScriptBuffer.append(width); 620 javaScriptBuffer.append(","); 621 javaScriptBuffer.append(height); 622 javaScriptBuffer.append(");\n"); 623 } 624 625 if(keepOnTop && !print) { 627 javaScriptBuffer.append( 628 "if (window != top) {\n top.location.href " 629 + "= location.href ;\n}\nwindow.focus();\n\n"); 630 } 631 } else if (!print) { 632 URLFormat URLFormat = new URLFormat(); 633 634 javaScriptBuffer.append( 637 "if (window == top) {\n top.location.href = \""); 638 javaScriptBuffer.append( 639 RewriteHandling.getContextPath(request)); 640 javaScriptBuffer.append("/index.jsp?uri=\" + escape(\""); 641 642 643 String queryString = request.getQueryString(); 644 StringBuffer uri = new StringBuffer (); 645 uri.append(request.getServletPath()); 646 if(queryString != null) { 647 uri.append(URLFormat.format("?")); 648 uri.append(URLFormat.format(queryString)); 649 } 650 javaScriptBuffer.append(RewriteHandling.rewriteURL(uri.toString(), 651 request.getContextPath())); 652 javaScriptBuffer.append("\");\n}\n"); 653 } 654 String generatedJavaScript; 655 if (javaScriptBuffer.length() > 0) { 656 generatedJavaScript = javaScriptBuffer.toString(); 657 } else { 658 generatedJavaScript = null; 659 } 660 try { 661 JspWriter out = pageContext.getOut(); 662 out.println("<meta http-equiv='Content-Type' content='text/HTML; " 663 + "charset=" 664 + encoding 665 + "'/>"); 666 out.println("<meta http-equiv='Content-Style-Type' " 667 + "content='text/css' />"); 668 669 String servletPath = request.getServletPath(); 673 if (!topLevel && !servletPath.startsWith("/util/iframeContent")) { 674 printStyleSheetLink(request, 675 out, 676 GenerateCSS.CSS_PATH 677 + "/" 678 + MAIN_CSS_NAME, 679 null); 680 } 681 682 if (!StringHandling.isNullOrEmpty(servletPath)) { 688 String fullPath = servletPath; 689 int dotPos = fullPath.indexOf('.'); 690 if (dotPos != 0) { 693 fullPath = fullPath.substring(0, dotPos); 694 } 695 int pos = -1; 697 do { 698 pos = fullPath.indexOf('/', pos + 1); 700 String [] mediaNames = new String [] {null, "print"}; 703 for (int i = 0; i < mediaNames.length; ++i) { 704 String mediaName = mediaNames[i]; 705 String path; 706 if (pos == -1) { 708 path = fullPath + "/"; 709 } else { 710 path = fullPath.substring(0, pos + 1); 711 } 712 if (mediaName != null) { 714 path += mediaName + "/"; 715 } 716 String specificCSSName = GenerateCSS.CSS_PATH 717 + path 718 + DEFAULT_CSS_NAME; 719 File file = new File (pageContext.getServletContext() 721 .getRealPath(specificCSSName)); 722 if (file.exists()) { 723 printStyleSheetLink(request, 724 out, 725 specificCSSName, 726 mediaName); 727 } 728 specificCSSName = GenerateCSS.CSS_PATH 730 + path 731 + themeName 732 + ".css"; 733 file = new File (pageContext.getServletContext() 734 .getRealPath(specificCSSName)); 735 if (file.exists()) { 736 printStyleSheetLink(request, 737 out, 738 specificCSSName, 739 mediaName); 740 } 741 specificCSSName = GenerateCSS.CSS_PATH 743 + path 744 + MSIE_CSS_NAME; 745 file = new File (pageContext.getServletContext() 746 .getRealPath(specificCSSName)); 747 if (file.exists()) { 748 out.println("<!--[if IE]>"); 749 printStyleSheetLink(request, 750 out, 751 specificCSSName, 752 mediaName); 753 out.println("<![endif]-->"); 754 } 755 } 756 } while (pos != -1); 757 } 758 759 out.println("<title>" + myTitle + "</title>"); 760 761 if ((generatedJavaScript != null) 763 && !generatedJavaScript.trim().equals("")) { 764 out.println("<script " 765 + "type='text/javascript'>"); 766 out.println(" <!--"); 767 out.println(generatedJavaScript); 768 out.println(" // -->"); 769 out.println("</script>"); 770 } 771 out.println("</head>"); 772 } catch(IOException e) { 773 throw new JspException (e); 774 } 775 return EVAL_PAGE; 776 } 777 793 public int doStartTag() throws JspException { 794 try { 795 JspWriter out = pageContext.getOut(); 796 out.println("<head>"); 797 out.println("<meta http-equiv='Content-Style-Type' " 798 + "content='text/css' />"); 799 out.println("<meta http-equiv='cache-control' content='no-cache'/>"); 801 out.println("<meta http-equiv='pragma' content='no-cache'/>"); 803 } catch(IOException e) { 804 throw new JspException (e); 805 } 806 BodyContent bodyContent = getBodyContent(); 807 return EVAL_BODY_BUFFERED; 808 } 809 816 public final String getBundle() { 817 return bundle; 818 } 819 820 828 public final String getJavaScript() { 829 return javaScript; 830 } 831 840 public final boolean getKeepOnTop() { 841 return keepOnTop; 842 } 843 844 853 public final boolean getLogin() { 854 return login; 855 } 856 857 876 public final String getTitle() { 877 return title; 878 } 879 880 887 public List getTitleArgs() { 888 return titleArgs; 889 } 890 891 899 public final String getTitleKey() { 900 return titleKey; 901 } 902 903 917 public final boolean getTopLevel() { 918 return topLevel; 919 } 920 924 public boolean isFrames() { 925 return frames; 926 } 927 938 private void printStyleSheetLink(final HttpServletRequest request, 939 final JspWriter out, 940 final String filePath, 941 final String mediaName) 942 throws IOException { 943 out.print("<link rel='stylesheet' HREF='"); 944 out.print(RewriteHandling.rewriteURL(RewriteHandling.getContextPath(request) + filePath, 945 RewriteHandling.getContextPath(request))); 946 if (mediaName != null) { 947 out.print("' media='"); 948 out.print(mediaName); 949 } 950 out.println("' type='text/css' />"); 951 952 } 953 954 961 public final void setBundle(final String bundle) { 962 this.bundle = bundle; 963 } 964 968 public void setFrames(boolean framesParam) { 969 if (logger.isDebugEnabled()) { 970 logger.debug("Setting frames. Before '" + frames + "', after '" 971 + framesParam + "'"); 972 } 973 frames = framesParam; 974 } 975 976 984 public final void setJavaScript(final String javaScript) { 985 this.javaScript = javaScript; 986 } 987 988 996 public final void setKeepOnTop(final boolean keepOnTop) { 997 this.keepOnTop = keepOnTop; 998 } 999 1000 1009 public final void setLogin(final boolean login) { 1010 this.login = login; 1011 } 1012 1013 1030 public final void setTitle(final String title) { 1031 this.title = title; 1032 } 1033 1034 1041 public final void setTitleArgs(final List titleArgsParam) { 1042 this.titleArgs = titleArgsParam; 1043 } 1044 1045 1053 public final void setTitleKey(final String titleKey) { 1054 this.titleKey = titleKey; 1055 } 1056 1057 1072 public final void setTopLevel(final boolean topLevel) { 1073 this.topLevel = topLevel; 1074 } 1075 1076 1084 public void writeTagBodyContent(final JspWriter out, 1085 final BodyContent bodyContent) throws IOException { 1086 HttpSession session = pageContext.getSession(); 1087 out.println(bodyContent.getString()); 1088 bodyContent.clearBody(); 1090 } 1091} 1092 | Popular Tags |