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.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.types.CmsResourceTypePlain;
034import org.opencms.file.types.CmsResourceTypeXmlContent;
035import org.opencms.jsp.CmsJspActionElement;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.util.CmsRequestUtil;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * This editor handler class returns the editor URI depending on various factors.<p>
045 *
046 * Editor selection criteria:
047 * <ul>
048 * <li>the user preferences</li>
049 * <li>the users current browser</li>
050 * <li>the resource type</li>
051 * </ul>
052 * <p>
053 *
054 * @since 6.0.0
055 *
056 * @see org.opencms.workplace.editors.I_CmsEditorHandler
057 * @see org.opencms.workplace.editors.CmsWorkplaceEditorManager
058 */
059public class CmsEditorHandler implements I_CmsEditorHandler {
060
061    /** The log object for this class. */
062    private static final Log LOG = CmsLog.getLog(CmsEditorHandler.class);
063
064    /**
065     * @see org.opencms.workplace.editors.I_CmsEditorHandler#getEditorUri(org.opencms.file.CmsObject, java.lang.String, java.lang.String, boolean)
066     */
067    public String getEditorUri(CmsObject cms, String resourceType, String userAgent, boolean loadDefault) {
068
069        // get the editor URI from the editor manager
070        String editorUri = null;
071        if (loadDefault) {
072            // get default editor
073            editorUri = OpenCms.getWorkplaceManager().getWorkplaceEditorManager().getDefaultEditorUri(
074                cms.getRequestContext(),
075                resourceType,
076                userAgent);
077        } else {
078            // get preferred editor
079            editorUri = OpenCms.getWorkplaceManager().getWorkplaceEditorManager().getEditorUri(
080                cms.getRequestContext(),
081                resourceType,
082                userAgent);
083        }
084
085        try {
086            // check the presence of the editor
087            cms.readResource(editorUri);
088        } catch (Throwable t) {
089            // preferred or selected editor not found, try default editor
090            if (LOG.isInfoEnabled()) {
091                LOG.info(t);
092            }
093            editorUri = OpenCms.getWorkplaceManager().getWorkplaceEditorManager().getDefaultEditorUri(
094                cms.getRequestContext(),
095                resourceType,
096                userAgent);
097        }
098
099        Object resObj = cms.getRequestContext().getAttribute("EDITORHANDLER_RESOURCE");
100        if (resObj != null) {
101            CmsResource resource = (CmsResource)resObj;
102            if ((editorUri != null) && editorUri.contains("acacia")) {
103                if (CmsResourceTypeXmlContent.isXmlContent(resource)) {
104                    String fallback = "/system/workplace/editors/xmlcontent/editor.jsp";
105                    if (!CmsWorkplaceEditorManager.checkAcaciaEditorAvailable(cms, resource)) {
106                        editorUri = fallback;
107                    }
108                }
109            }
110        }
111
112        return editorUri;
113    }
114
115    /**
116     * @see org.opencms.workplace.editors.I_CmsEditorHandler#getEditorUri(java.lang.String, CmsJspActionElement)
117     */
118    public String getEditorUri(String resource, CmsJspActionElement jsp) throws CmsException {
119
120        // first try to get the "edit as text" and "load default" parameters from the request
121        boolean editAsText = Boolean.valueOf(jsp.getRequest().getParameter(CmsEditor.PARAM_EDITASTEXT)).booleanValue();
122        boolean loadDefault = Boolean.valueOf(
123            jsp.getRequest().getParameter(CmsEditor.PARAM_LOADDEFAULT)).booleanValue();
124        // initialize resource type with -1 (unknown resource type)
125        int resTypeId = -1;
126        String resourceType = "";
127        if (editAsText) {
128            // the resource should be treated as text, set the plain resource id
129            resTypeId = OpenCms.getResourceManager().getResourceType(
130                CmsResourceTypePlain.getStaticTypeName()).getTypeId();
131        } else {
132            // get the resource type id of the edited resource
133            CmsResource res = jsp.getCmsObject().readResource(resource, CmsResourceFilter.ALL);
134            jsp.getRequestContext().setAttribute("EDITORHANDLER_RESOURCE", res);
135            resTypeId = res.getTypeId();
136        }
137
138        // get the resource type name
139        resourceType = OpenCms.getResourceManager().getResourceType(resTypeId).getTypeName();
140
141        // get the browser identification from the request
142        String userAgent = jsp.getRequest().getHeader(CmsRequestUtil.HEADER_USER_AGENT);
143
144        return getEditorUri(jsp.getCmsObject(), resourceType, userAgent, loadDefault);
145    }
146}