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, 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.jsonpart;
029
030import org.opencms.flex.CmsFlexController;
031import org.opencms.main.CmsLog;
032
033import java.io.IOException;
034
035import javax.servlet.jsp.JspException;
036import javax.servlet.jsp.tagext.TagSupport;
037import javax.servlet.jsp.tagext.TryCatchFinally;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Tag used to convert the HTML output of this tag's contents to encoded JSON.<p>
043 *
044 * It only makes sense to use this tag in combination with the servlet filter org.opencms.jsp.jsonpart.CmsJsonPartFilter.
045 * This tag converts the text generated by its contained JSP code and converts it into a special encoded form, which is then
046 * used by the filter to generate JSON. The 'element' attribute on this tag can be used to control the JSON key which will be used for
047 * the content.
048 */
049public class CmsJspTagJsonPart extends TagSupport implements TryCatchFinally {
050
051    /** Serial version id. */
052    private static final long serialVersionUID = 1L;
053
054    /** Log instance for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsJspTagJsonPart.class);
056
057    /** The name to be used as a key for the JSON part. */
058    private String m_element;
059
060    /** Variable to keep track of whether we still need to write the end marker. */
061    private boolean m_needEnd;
062
063    /**
064     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
065     */
066    public void doCatch(Throwable arg0) throws Throwable {
067
068        throw arg0;
069    }
070
071    /**
072     * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
073     */
074    @Override
075    public int doEndTag() throws JspException {
076
077        if (CmsFlexController.isCmsRequest(pageContext.getRequest())
078            && CmsJsonPartFilter.isJsonRequest(pageContext.getRequest())) {
079            try {
080                pageContext.getOut().write(CmsJsonPart.END);
081                m_needEnd = false;
082            } catch (IOException e) {
083                throw new JspException(e);
084            }
085        }
086        return EVAL_PAGE;
087    }
088
089    /**
090     * @see javax.servlet.jsp.tagext.TryCatchFinally#doFinally()
091     */
092    public void doFinally() {
093
094        if (m_needEnd) { // Exception happened before we could write the end marker
095            m_needEnd = false;
096            try {
097                pageContext.getOut().write(CmsJsonPart.END);
098            } catch (IOException e) {
099                LOG.error(e.getLocalizedMessage(), e);
100            }
101        }
102    }
103
104    /**
105     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
106     */
107    @Override
108    public int doStartTag() throws JspException {
109
110        if (null != findAncestorWithClass(this, CmsJspTagJsonPart.class)) {
111            throw new JspException("cms:jsonpart tag must not be nested!");
112        }
113
114        if (CmsFlexController.isCmsRequest(pageContext.getRequest())
115            && CmsJsonPartFilter.isJsonRequest(pageContext.getRequest())) {
116            try {
117                pageContext.getOut().write(CmsJsonPart.getHeader(getElement()));
118                m_needEnd = true;
119            } catch (IOException e) {
120                throw new JspException(e);
121            }
122        }
123        return EVAL_BODY_INCLUDE;
124    }
125
126    /**
127     * Returns the name to be used as the JSON key.<p>
128     *
129     * @return the name to be used as a JSON key
130     */
131    public String getElement() {
132
133        return m_element;
134    }
135
136    /**
137     * Sets the name to be used as a JSON key.<p>
138     *
139     * @param elementName the name to be used as a JSON key
140     */
141    public void setElement(String elementName) {
142
143        m_element = elementName;
144    }
145
146}