1. /*
  2. * Copyright (C) 2005-2007 Alfresco Software Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. * GNU General Public License for more details.
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. * As a special exception to the terms and conditions of version 2.0 of
  16. * the GPL, you may redistribute this Program in connection with Free/Libre
  17. * and Open Source Software ("FLOSS") applications as described in Alfresco's
  18. * FLOSS exception.  You should have recieved a copy of the text describing
  19. * the FLOSS exception, and it is also available here:
  20. * http://www.alfresco.com/legal/licensing"
  21. */
  22. package org.alfresco.web.ui.repo.tag;
  23.  
  24. import java.io.IOException;
  25. import java.io.Writer;
  26.  
  27. import javax.faces.context.FacesContext;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.jsp.JspException;
  30. import javax.servlet.jsp.tagext.TagSupport;
  31.  
  32. import org.alfresco.web.app.Application;
  33. import org.alfresco.web.app.servlet.FacesHelper;
  34. import org.alfresco.web.bean.coci.CCProperties;
  35. import org.alfresco.web.ui.common.Utils;
  36. import org.apache.commons.logging.Log;
  37. import org.apache.commons.logging.LogFactory;
  38.  
  39. /**
  40. * A non-JSF tag library that adds the HTML begin and end tags if running in servlet mode
  41. *
  42. * @author gavinc
  43. */
  44. public class PageTag extends TagSupport
  45. {
  46.    private static final long serialVersionUID = 8142765393181557228L;
  47.    
  48.    private final static String SCRIPTS_START = "<script type=\"text/javascript\" src=\"";
  49.    private final static String SCRIPTS_END   = "\"></script>\n";
  50.    private final static String STYLES_START  = "<link rel=\"stylesheet\" href=\"";
  51.    private final static String STYLES_MAIN   = "\" type=\"text/css\">\n";
  52.    private final static String IE6COND_START = "<!--[if IE 6]>\n";
  53.    private final static String IE6COND_END   = "<![endif]-->\n";
  54.  
  55.    private final static String[] SCRIPTS =
  56.    {
  57.       // menu javascript
  58.       "/scripts/menu.js",
  59.       // webdav javascript
  60.       "/scripts/webdav.js",
  61.       // functional for window.onload
  62.       "/scripts/onload.js",
  63.       // base yahoo file
  64.       "/scripts/ajax/yahoo/yahoo/yahoo-min.js",
  65.       // io handling (AJAX)
  66.       "/scripts/ajax/yahoo/connection/connection-min.js",
  67.       // event handling
  68.       "/scripts/ajax/yahoo/event/event-min.js",
  69.       // mootools
  70.       "/scripts/ajax/mootools.v1.11.js",
  71.       // common Alfresco util methods
  72.       "/scripts/ajax/common.js",
  73.       // pop-up panel helper objects
  74.       "/scripts/ajax/summary-info.js",
  75.       // ajax pickers
  76.       "/scripts/ajax/picker.js",
  77.       "/scripts/ajax/tagger.js"
  78.    };
  79.    
  80.    private final static String[] CSS =
  81.    {
  82.       "/css/main.css",
  83.       "/css/picker.css"
  84.    };
  85.  
  86.    private final static String[] IE6COND_CSS =
  87.    {
  88.       "/css/ie6.css"
  89.    };
  90.  
  91. /**
  92. * Please ensure you understand the terms of the license before changing the contents of this file.
  93. */
  94.    
  95.    private final static String ALF_LOGO_HTTP  = "http://www.alfresco.com/images/alfresco_community_horiz21.gif";
  96.    private final static String ALF_LOGO_HTTPS = "https://www.alfresco.com/images/alfresco_community_horiz21.gif";
  97.    private final static String ALF_URL   = "http://www.alfresco.com";
  98.    private final static String SF_LOGO   = "/images/logo/sflogo.php.png";
  99.    private final static String ALF_TEXT  = "Alfresco Community";
  100.    private final static String ALF_COPY  = "Supplied free of charge with " +
  101.         "<a class='footer' href='http://www.alfresco.com/services/support/communityterms/#support'>no support</a>, " +
  102.         "<a class='footer' href='http://www.alfresco.com/services/support/communityterms/#certification'>no certification</a>, " +
  103.         "<a class='footer' href='http://www.alfresco.com/services/support/communityterms/#maintenance'>no maintenance</a>, " +
  104.         "<a class='footer' href='http://www.alfresco.com/services/support/communityterms/#warranty'>no warranty</a> and " +
  105.         "<a class='footer' href='http://www.alfresco.com/services/support/communityterms/#indemnity'>no indemnity</a> by " +
  106.         "<a class='footer' href='http://www.alfresco.com'>Alfresco</a> or its " +
  107.         "<a class='footer' href='http://www.alfresco.com/partners/'>Certified Partners</a>. " +
  108.         "<a class='footer' href='http://www.alfresco.com/services/support/'>Click here for support</a>. " +
  109.         "Alfresco Software Inc. &copy; 2005-2009 All rights reserved.";
  110.    
  111.    private final static Log logger = LogFactory.getLog(PageTag.class);
  112.    private static String alfresco = null;
  113.    private static String loginPage = null;
  114.    
  115.    private long startTime = 0;
  116.    private String title;
  117.    private String titleId;
  118.    private String doctypeRootElement;
  119.    private String doctypePublic;
  120.    private String doctypeSystem;
  121.    
  122.    /**
  123.     * @return The title for the page
  124.     */
  125.    public String getTitle()
  126.    {
  127.       return title;
  128.    }
  129.  
  130.    /**
  131.     * @param title Sets the page title
  132.     */
  133.    public void setTitle(String title)
  134.    {
  135.       this.title = title;
  136.    }
  137.    
  138.    /**
  139.     * @return The title message Id for the page
  140.     */
  141.    public String getTitleId()
  142.    {
  143.       return titleId;
  144.    }
  145.  
  146.    /**
  147.     * @param titleId Sets the page title message Id
  148.     */
  149.    public void setTitleId(String titleId)
  150.    {
  151.       this.titleId = titleId;
  152.    }
  153.  
  154.    public String getDoctypeRootElement()
  155.    {
  156.       return this.doctypeRootElement;
  157.    }
  158.  
  159.    public void setDoctypeRootElement(final String doctypeRootElement)
  160.    {
  161.       this.doctypeRootElement = doctypeRootElement;
  162.    }
  163.    
  164.    public String getDoctypePublic()
  165.    {
  166.       return this.doctypePublic;
  167.    }
  168.  
  169.    public void setDoctypePublic(final String doctypePublic)
  170.    {
  171.       this.doctypePublic = doctypePublic;
  172.    }
  173.  
  174.    public String getDoctypeSystem()
  175.    {
  176.       return this.doctypeSystem;
  177.    }
  178.    
  179.    public void setDoctypeSystem(final String doctypeSystem)
  180.    {
  181.       this.doctypeSystem = doctypeSystem;
  182.    }
  183.    
  184.    public void release()
  185.    {
  186.       super.release();
  187.       this.title = null;
  188.       this.titleId = null;
  189.       this.doctypeRootElement = null;
  190.       this.doctypeSystem = null;
  191.       this.doctypePublic = null;
  192.    }
  193.  
  194.    /**
  195.     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
  196.     */
  197.    public int doStartTag() throws JspException
  198.    {
  199.       if (logger.isDebugEnabled())
  200.          startTime = System.currentTimeMillis();
  201.      
  202.       try
  203.       {
  204.          String reqPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
  205.          Writer out = pageContext.getOut();
  206.          
  207.          if (!Application.inPortalServer())
  208.          {
  209.             if (this.getDoctypeRootElement() != null &&
  210.                 this.getDoctypePublic() != null)
  211.             {
  212.                out.write("<!DOCTYPE ");
  213.                out.write(this.getDoctypeRootElement().toLowerCase());
  214.                out.write(" PUBLIC \"" + this.getDoctypePublic() + "\"");
  215.                if (this.getDoctypeSystem() != null)
  216.                {
  217.                   out.write(" \"" + this.getDoctypeSystem() + "\"");
  218.                }
  219.                out.write(">\n");
  220.             }
  221.             else
  222.             {
  223.                out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n");
  224.                out.write("    \"http://www.w3.org/TR/html4/loose.dtd\">\n");
  225.             }
  226.             out.write("<html><head><title>");
  227.             if (this.titleId != null && this.titleId.length() != 0)
  228.             {
  229.                out.write(Utils.encode(Application.getMessage(pageContext.getSession(), this.titleId)));
  230.             }
  231.             else if (this.title != null && this.title.length() != 0)
  232.             {
  233.                out.write(Utils.encode(this.title));
  234.             }
  235.             else
  236.             {
  237.                out.write("Alfresco Web Client");
  238.             }
  239.             out.write("</title>\n");
  240.             out.write("<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"" + reqPath +
  241.                       "/wcservice/api/search/keyword/description.xml\" title=\"Alfresco Keyword Search\">\n");
  242.             out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
  243.          }
  244.          
  245.          // CSS style includes
  246.          for (final String css : PageTag.CSS)
  247.          {
  248.             out.write(STYLES_START);
  249.             out.write(reqPath);
  250.             out.write(css);
  251.             out.write(STYLES_MAIN);
  252.          }
  253.          
  254.          // IE6COND_CSS style includes
  255.          out.write(IE6COND_START);
  256.          for (final String ie6cond_css : PageTag.IE6COND_CSS)
  257.          {
  258.             out.write(STYLES_START);
  259.             out.write(reqPath);
  260.             out.write(ie6cond_css);
  261.             out.write(STYLES_MAIN);
  262.          }
  263.          out.write(IE6COND_END);
  264.          
  265.          // JavaScript includes
  266.          for (final String s : PageTag.SCRIPTS)
  267.          {
  268.             out.write(SCRIPTS_START);
  269.             out.write(reqPath);
  270.             out.write(s);
  271.             out.write(SCRIPTS_END);
  272.          }
  273.          
  274.          out.write("<script type=\"text/javascript\">"); // start - generate naked javascript code
  275.  
  276.          // set the context path used by some Alfresco script objects
  277.          out.write("setContextPath('");
  278.          out.write(reqPath);
  279.          out.write("');");
  280.  
  281.          // generate window onload code
  282.          out.write(getWindowOnloadCode());
  283.  
  284.          out.write("</script>\n"); // end - generate naked javascript code
  285.  
  286.          if (!Application.inPortalServer())
  287.          {
  288.             out.write("</head>");
  289.             out.write("<body>\n");
  290.          }
  291.       }
  292.       catch (IOException ioe)
  293.       {
  294.          throw new JspException(ioe.toString());
  295.       }
  296.      
  297.       return EVAL_BODY_INCLUDE;
  298.    }
  299.  
  300.    /**
  301.     * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
  302.     */
  303.    public int doEndTag() throws JspException
  304.    {
  305.       try
  306.       {
  307.          HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
  308.          if (req.getRequestURI().endsWith(getLoginPage()) == false)
  309.          {
  310.             pageContext.getOut().write(getAlfrescoButton());
  311.          }
  312.          
  313.          if (!Application.inPortalServer())
  314.          {
  315.             pageContext.getOut().write("\n</body></html>");
  316.          }
  317.       }
  318.       catch (IOException ioe)
  319.       {
  320.          throw new JspException(ioe.toString());
  321.       }
  322.      
  323.       if (logger.isDebugEnabled())
  324.       {
  325.          long endTime = System.currentTimeMillis();
  326.          logger.debug("Time to generate page: " + (endTime - startTime) + "ms");
  327.       }
  328.      
  329.       return super.doEndTag();
  330.    }
  331.    
  332.    private String getLoginPage()
  333.    {
  334.       if (PageTag.loginPage == null)
  335.       {
  336.          PageTag.loginPage = Application.getLoginPage(pageContext.getServletContext());
  337.       }
  338.      
  339.       return PageTag.loginPage;
  340.    }
  341.  
  342. /**
  343. * Please ensure you understand the terms of the license before changing the contents of this file.
  344. */
  345.  
  346.    private String getAlfrescoButton()
  347.    {
  348.       if (PageTag.alfresco == null)
  349.       {
  350.          final HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
  351.          PageTag.alfresco = ("<center><table style='margin: 0px auto;'><tr><td>" +
  352.                              "<a href='" + ALF_URL + "'>" +
  353.                              "<img style='vertical-align:middle;border-width:0px;' alt='' title='" + ALF_TEXT +
  354.                              "' src='" + ("http".equals(req.getScheme()) ? ALF_LOGO_HTTP : ALF_LOGO_HTTPS) +
  355.                              "'>" +"</a></td><td align='center'>" +
  356.                              "<span class='footer'>" + ALF_COPY +
  357.                              "</span></td><td><a href='http://sourceforge.net/projects/alfresco'>" +
  358.                              "<img style='vertical-align:middle' border='0' alt='' title='SourceForge' width='88' height='31' src='" +
  359.                              req.getContextPath() + SF_LOGO + "'></a>" +
  360.                              "</td></tr></table></center>");
  361.       }
  362.       return PageTag.alfresco;
  363.    }
  364.  
  365.    /**
  366.     * This method generate code for setting window.onload reference if we need (we need to open WebDav or Cifs url or may be else)
  367.     * Using javascript code(function onloadFunc) from onload.js file
  368.     * @return Returns window.onload javascript code
  369.     */
  370.    private String getWindowOnloadCode()
  371.    {
  372.       FacesContext fc = FacesContext.getCurrentInstance();
  373.       if (fc != null)
  374.       {
  375.           CCProperties ccProps = (CCProperties) FacesHelper.getManagedBean(fc, "CCProperties");
  376.          
  377.           StringBuilder onloadCode = new StringBuilder();
  378.           if (ccProps.getWebdavUrl() != null || ccProps.getCifsPath() != null)
  379.           {
  380.              String webdavUrl = (ccProps.getWebdavUrl() != null) ? (ccProps.getWebdavUrl()) : ("");
  381.              String cifsPath = (ccProps.getCifsPath() != null) ? (ccProps.getCifsPath()) : ("");
  382.              
  383.              onloadCode.append("window.onload=onloadFunc('").append(webdavUrl).append("','").append(cifsPath).append("');");
  384.              
  385.              ccProps.setCifsPath(null); // we need reset cifsPath flag
  386.              ccProps.setWebdavUrl(null); // we need reset webdavUrl flag
  387.           }
  388.          
  389.           return onloadCode.toString();
  390.       }
  391.       else
  392.       {
  393.           return "";
  394.       }
  395.    }
  396. }
  397.