001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp;
029
030import org.opencms.file.CmsObject;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.i18n.CmsMessageContainer;
033import org.opencms.jsp.util.CmsJspResourceAccessBean;
034import org.opencms.main.OpenCms;
035
036import javax.servlet.jsp.JspException;
037import javax.servlet.jsp.JspTagException;
038import javax.servlet.jsp.tagext.Tag;
039
040/**
041 * Used to access resource information from the current open <code>&lt;cms:resourceload&gt;</code>
042 * tag using JSP page context and the JSP EL.<p>
043 *
044 * The tag will create an instance of a {@link CmsJspResourceAccessBean} that is stored in the selected context.
045 * Use the options provided by the bean to access the resource directly.<p>
046 *
047 * For example together with the JSTL, use this tag inside an open tag like this:<pre>
048 * &lt;cms:resourceload ... &gt;
049 *     &lt;cms:resourceaccess var="myVarName" scope="page" /&gt;
050 *     ... other code ...
051 * &lt;/cms:resourceload&gt;</pre>
052 *
053 * @since 8.0
054 */
055public class CmsJspTagResourceAccess extends CmsJspScopedVarBodyTagSuport {
056
057    /** Serial version UID required for safe serialization. */
058    private static final long serialVersionUID = 2588220869205763894L;
059
060    /**
061     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
062     */
063    @Override
064    public int doEndTag() {
065
066        if (OpenCms.getSystemInfo().getServletContainerSettings().isReleaseTagsAfterEnd()) {
067            // need to release manually, JSP container may not call release as required (happens with Tomcat)
068            release();
069        }
070        return EVAL_PAGE;
071    }
072
073    /**
074     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
075     */
076    @Override
077    public int doStartTag() throws JspException {
078
079        // get the current users OpenCms context
080        CmsObject cms = CmsFlexController.getCmsObject(pageContext.getRequest());
081
082        // get a reference to the parent "content container" class
083        Tag ancestor = findAncestorWithClass(this, I_CmsResourceContainer.class);
084        if (ancestor == null) {
085            CmsMessageContainer errMsgContainer = Messages.get().container(
086                Messages.ERR_PARENTLESS_TAG_1,
087                "resourceaccess");
088            String msg = Messages.getLocalizedMessage(errMsgContainer, pageContext);
089            throw new JspTagException(msg);
090        }
091        // get the currently open resource container
092        I_CmsResourceContainer resourceContainer = (I_CmsResourceContainer)ancestor;
093
094        // initialize a new instance of a resource access bean
095        CmsJspResourceAccessBean bean = new CmsJspResourceAccessBean(cms, resourceContainer.getResource());
096
097        // store the resource in the selected page context scope
098        storeAttribute(bean);
099
100        return SKIP_BODY;
101    }
102}