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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.flex.CmsFlexController;
033import org.opencms.main.CmsLog;
034import org.opencms.ugc.CmsUgcSession;
035import org.opencms.ugc.CmsUgcSessionFactory;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.util.CmsUUID;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.jsp.JspException;
041import javax.servlet.jsp.tagext.TagSupport;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Jsp tag to initialize an editing session for user generated content.<p>
047 */
048public class CmsJspTagUgc extends TagSupport {
049
050    /** Logger instance for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsJspTagUgc.class);
052
053    /** The serial version id. */
054    private static final long serialVersionUID = 7290192201848437667L;
055
056    /** The default page context attribute name for the form error message. */
057    public static final String DEFAULT_ERROR_MESSAGE_ATTR = "formError";
058
059    /** The default page context attribute name for the form session id. */
060    public static final String DEFAULT_SESSION_ID_ATTR = "formSessionId";
061
062    /** The site path to the edit configuration file. */
063    private String m_configPath;
064
065    /** The structure id of the edit resource. */
066    private String m_editId;
067
068    /** The file name of the edit resource. */
069    private String m_editName;
070
071    /** The page context attribute name for the form error message. */
072    private String m_error;
073
074    /** The page context attribute name for the form session id. */
075    private String m_var;
076
077    /**
078     * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
079     */
080    @Override
081    public int doEndTag() throws JspException {
082
083        // reset all tag attribute values
084        m_configPath = null;
085        m_editId = null;
086        m_editName = null;
087        m_error = null;
088        m_var = null;
089        return super.doEndTag();
090    }
091
092    /**
093     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
094     */
095    @Override
096    public int doStartTag() throws JspException {
097
098        HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
099        CmsFlexController controller = CmsFlexController.getController(req);
100        if (controller != null) {
101            CmsObject cms = controller.getCmsObject();
102            try {
103                CmsUgcSession ugcsession = null;
104                if ((m_editName == null) && (m_editId != null)) {
105                    CmsResource res = cms.readResource(new CmsUUID(m_editId));
106                    m_editName = res.getName();
107                }
108                if (m_editName != null) {
109                    ugcsession = CmsUgcSessionFactory.getInstance().createSessionForFile(
110                        cms,
111                        req,
112                        m_configPath,
113                        m_editName);
114                } else {
115                    ugcsession = CmsUgcSessionFactory.getInstance().createSession(cms, req, m_configPath);
116                    ugcsession.createXmlContent();
117                }
118                pageContext.setAttribute(
119                    m_var == null ? DEFAULT_SESSION_ID_ATTR : m_var,
120                    ugcsession.getId().toString());
121            } catch (Exception e) {
122                LOG.warn(e.getLocalizedMessage(), e);
123                pageContext.setAttribute(
124                    m_error == null ? DEFAULT_ERROR_MESSAGE_ATTR : m_error,
125                    e.getLocalizedMessage());
126            }
127        }
128        return super.doStartTag();
129    }
130
131    /**
132     * Returns the site path to the edit configuration file.<p>
133     *
134     * @return site path to the edit configuration file
135     */
136    public String getConfigPath() {
137
138        return m_configPath;
139    }
140
141    /**
142     * Returns the structure id of the edit resource.<p>
143     *
144     * @return structure id of the edit resource
145     */
146    public String getEditId() {
147
148        return m_editId;
149    }
150
151    /**
152     * Returns the file name of the edit resource.<p>
153     *
154     * @return the file name of the edit resource
155     */
156    public String getEditName() {
157
158        return m_editName;
159    }
160
161    /**
162     * Returns the page context attribute name for the form error message.<p>
163     *
164     * @return the page context attribute name for the form error message
165     */
166    public String getError() {
167
168        return m_error;
169    }
170
171    /**
172     * Returns the page context attribute name for the form session id.<p>
173     *
174     * @return the page context attribute name for the form session id
175     */
176    public String getVar() {
177
178        return m_var;
179    }
180
181    /**
182     * Sets the site path to the edit configuration file.<p>
183     *
184     * @param configPath the site path to the edit configuration file
185     */
186    public void setConfigPath(String configPath) {
187
188        m_configPath = CmsStringUtil.isEmptyOrWhitespaceOnly(configPath) ? null : configPath;
189    }
190
191    /**
192     * Sets the structure id of the edit resource.<p>
193     *
194     * @param editId the structure id of the edit resource
195     */
196    public void setEditId(String editId) {
197
198        m_editId = CmsStringUtil.isEmptyOrWhitespaceOnly(editId) ? null : editId;
199    }
200
201    /**
202     * Sets the file name of the edit resource.<p>
203     *
204     * @param editName the file name of the edit resource
205     */
206    public void setEditName(String editName) {
207
208        m_editName = CmsStringUtil.isEmptyOrWhitespaceOnly(editName) ? null : editName;
209    }
210
211    /**
212     * Sets the page context attribute name for the form error message.<p>
213     *
214     * @param error the page context attribute name for the form error message
215     */
216    public void setError(String error) {
217
218        m_error = CmsStringUtil.isEmptyOrWhitespaceOnly(error) ? null : error;
219    }
220
221    /**
222     * Sets the page context attribute name for the form session id.<p>
223     *
224     * @param var page context attribute name for the form session id
225     */
226    public void setVar(String var) {
227
228        m_var = CmsStringUtil.isEmptyOrWhitespaceOnly(var) ? null : var;
229    }
230
231}