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.i18n.CmsEncoder;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWorkplace;
037import org.opencms.workplace.CmsWorkplaceException;
038
039import javax.servlet.jsp.JspException;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * Selects the dialog which should be displayed by OpenCms depending on the configuration value.<p>
045 *
046 * You can define the class of your editor selector in the OpenCms XML configuration files.
047 * The following files use this class:
048 * <ul>
049 * <li>/jsp/editors/editor_main_html
050 * </ul>
051 * <p>
052 *
053 * @since 6.0.0
054 *
055 * @see org.opencms.workplace.editors.I_CmsEditorHandler
056 */
057public class CmsEditorSelector {
058
059    /** The log object for this class. */
060    private static final Log LOG = CmsLog.getLog(CmsEditorSelector.class);
061
062    /** The jsp context. */
063    private CmsJspActionElement m_jsp;
064
065    /** The name of the resource to get the editor for. */
066    private String m_paramResource;
067
068    /**
069     * Public constructor with JSP action element.<p>
070     *
071     * @param jsp an initialized JSP action element
072     */
073    public CmsEditorSelector(CmsJspActionElement jsp) {
074
075        setJsp(jsp);
076        setParamResource(jsp.getRequest().getParameter(CmsDialog.PARAM_RESOURCE));
077    }
078
079    /**
080     * Shows the error dialog when no valid editor is found and returns null for the editor URI.<p>
081     *
082     * @param jsp the instantiated CmsJspActionElement
083     * @param t a throwable object, can be null
084     */
085    private static void showErrorDialog(CmsJspActionElement jsp, Throwable t) {
086
087        CmsDialog wp = new CmsDialog(jsp);
088        wp.setParamMessage(Messages.get().getBundle(wp.getLocale()).key(Messages.ERR_NO_EDITOR_FOUND_0));
089        wp.fillParamValues(jsp.getRequest());
090        try {
091            wp.includeErrorpage(wp, t);
092        } catch (JspException e) {
093            LOG.debug(
094                org.opencms.workplace.commons.Messages.get().getBundle().key(
095                    org.opencms.workplace.commons.Messages.LOG_ERROR_INCLUDE_FAILED_1,
096                    CmsWorkplace.FILE_DIALOG_SCREEN_ERRORPAGE),
097                e);
098        }
099    }
100
101    /**
102     * Returns the uri of the dialog which will be displayed.<p>
103     *
104     * @return the uri of the property dialog
105     */
106    public String getSelectedEditorUri() {
107
108        // get the handler class from the OpenCms runtime property
109        I_CmsEditorHandler editorClass = OpenCms.getWorkplaceManager().getEditorHandler();
110
111        // the resourcenameparameter could be encoded, so decode it
112        String resource = getParamResource();
113        resource = CmsEncoder.unescape(resource, CmsEncoder.ENCODING_UTF_8);
114        if (editorClass == null) {
115            // error getting the dialog class, return to file list
116            return CmsWorkplace.FILE_EXPLORER_FILELIST;
117        }
118        // get the dialog URI from the class defined in the configuration
119        String editorUri = null;
120        try {
121            editorUri = editorClass.getEditorUri(resource, getJsp());
122            if (editorUri == null) {
123                // no valid editor was found, show the error dialog
124                throw new CmsWorkplaceException(Messages.get().container(Messages.ERR_NO_EDITOR_FOUND_0));
125            }
126        } catch (CmsException e) {
127            showErrorDialog(getJsp(), e);
128        }
129        return editorUri;
130    }
131
132    /**
133     * Returns the CmsJspActionElement.<p>
134     *
135     * @return the CmsJspActionElement
136     */
137    private CmsJspActionElement getJsp() {
138
139        return m_jsp;
140    }
141
142    /**
143     * Returns the resource parameter String.<p>
144     *
145     * @return the resource parameter String
146     */
147    private String getParamResource() {
148
149        return m_paramResource;
150    }
151
152    /**
153     * Sets the CmsJspActionElement.<p>
154     *
155     * @param jsp the CmsJspActionElement
156     */
157    private void setJsp(CmsJspActionElement jsp) {
158
159        m_jsp = jsp;
160    }
161
162    /**
163     * Sets the resource parameter String.<p>
164     *
165     * @param resource the resource parameter String
166     */
167    private void setParamResource(String resource) {
168
169        m_paramResource = resource;
170    }
171
172}