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.workplace.editors;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsRole;
038import org.opencms.security.CmsRoleViolationException;
039import org.opencms.util.CmsRequestUtil;
040import org.opencms.util.CmsStringUtil;
041import org.opencms.workplace.CmsDialog;
042import org.opencms.workplace.CmsWorkplace;
043
044import java.util.Map;
045
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.PageContext;
049
050import org.apache.commons.logging.Log;
051
052/**
053 * Handles the actions that should be performed before opening the editor frameset.<p>
054 *
055 * For each resource type, a pre editor action class can be defined that is triggered in the workplace JSP
056 * <code>/system/workplace/editors/editor.jsp</code> before the editor is initially opened.
057 * If an action was performed, be sure to use the static method {@link #sendForwardToEditor(CmsDialog, Map)}
058 * to open the editor after the action.<p>
059 *
060 * @since 6.5.4
061 */
062public class CmsPreEditorAction extends CmsDialog {
063
064    /** The log object for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsPreEditorAction.class);
066
067    /** The original request parameters passed to the editor. */
068    private String m_originalParams;
069
070    /**
071     * Public constructor with JSP action element.<p>
072     *
073     * @param jsp an initialized JSP action element
074     */
075    public CmsPreEditorAction(CmsJspActionElement jsp) {
076
077        super(jsp);
078    }
079
080    /**
081     * Public constructor with JSP variables.<p>
082     *
083     * @param context the JSP page context
084     * @param req the JSP request
085     * @param res the JSP response
086     */
087    public CmsPreEditorAction(PageContext context, HttpServletRequest req, HttpServletResponse res) {
088
089        super(context, req, res);
090    }
091
092    /**
093     * Returns if the dialog is currently running in pre editor action mode,
094     * depending on the presence of the original request parameters.<p>
095     * @param dialog the dialog instance currently used
096     * @return true if the dialog is currently running in pre editor action mode, otherwise false
097     */
098    public static boolean isPreEditorMode(CmsDialog dialog) {
099
100        return CmsStringUtil.isNotEmpty(dialog.getParamOriginalParams());
101    }
102
103    /**
104     * Forwards to the editor and opens it after the action was performed.<p>
105     *
106     * @param dialog the dialog instance forwarding to the editor
107     */
108    public static void sendForwardToEditor(CmsDialog dialog) {
109
110        sendForwardToEditor(dialog, null);
111    }
112
113    /**
114     * Forwards to the editor and opens it after the action was performed.<p>
115     *
116     * @param dialog the dialog instance forwarding to the editor
117     * @param additionalParams eventual additional request parameters for the editor to use
118     */
119    public static void sendForwardToEditor(CmsDialog dialog, Map<String, String[]> additionalParams) {
120
121        // create the Map of original request parameters
122        Map<String, String[]> params = CmsRequestUtil.createParameterMap(dialog.getParamOriginalParams());
123        // put the parameter indicating that the pre editor action was executed
124        params.put(PARAM_PREACTIONDONE, new String[] {CmsStringUtil.TRUE});
125        if (additionalParams != null) {
126            // put the additional parameters to the Map
127            params.putAll(additionalParams);
128        }
129        try {
130            // now forward to the editor frameset
131            dialog.sendForward(CmsWorkplace.VFS_PATH_EDITORS + "editor.jsp", params);
132        } catch (Exception e) {
133            // error forwarding, log the exception as error
134            if (LOG.isErrorEnabled()) {
135                LOG.error(e.getLocalizedMessage(), e);
136            }
137        }
138    }
139
140    /**
141     * Returns if an action has to be performed before opening the editor depending on the resource to edit
142     * and request parameter values.<p>
143     *
144     * @return true if an action has to be performed, then the editor frameset is not generated
145     */
146    public boolean doPreAction() {
147
148        String resourceName = getParamResource();
149        try {
150            boolean preActionDone = Boolean.valueOf(getParamPreActionDone()).booleanValue();
151            if (!preActionDone) {
152                // pre editor action not executed yet now check if a pre action class is given for the resource type
153                CmsResource resource = getCms().readResource(resourceName, CmsResourceFilter.ALL);
154                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource.getTypeId());
155                I_CmsPreEditorActionDefinition preAction = OpenCms.getWorkplaceManager().getPreEditorConditionDefinition(
156                    type);
157                if (preAction != null) {
158                    return preAction.doPreAction(resource, this, getOriginalParams());
159                }
160            }
161        } catch (Exception e) {
162            // log error
163            if (LOG.isErrorEnabled()) {
164                LOG.error(e.getLocalizedMessage(), e);
165            }
166        }
167        // nothing to be done as pre action, open the editor
168        return false;
169    }
170
171    /**
172     * Returns the original request parameters for the editor to pass to the pre editor action dialog.<p>
173     *
174     * @return the original request parameters for the editor
175     */
176    public String getOriginalParams() {
177
178        if (m_originalParams == null) {
179            m_originalParams = CmsEncoder.decode(CmsRequestUtil.encodeParams(getJsp().getRequest()));
180        }
181        return m_originalParams;
182    }
183
184    /**
185     * Checks that the current user is a workplace user.<p>
186     *
187     * @throws CmsRoleViolationException if the user does not have the required role
188     */
189    @Override
190    protected void checkRole() throws CmsRoleViolationException {
191
192        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.EDITOR);
193    }
194
195}