001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsEditorHandler.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.16 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.editors;
033:
034: import org.opencms.file.CmsResource;
035: import org.opencms.file.CmsResourceFilter;
036: import org.opencms.file.types.CmsResourceTypePlain;
037: import org.opencms.jsp.CmsJspActionElement;
038: import org.opencms.main.CmsException;
039: import org.opencms.main.CmsLog;
040: import org.opencms.main.OpenCms;
041: import org.opencms.util.CmsRequestUtil;
042:
043: import org.apache.commons.logging.Log;
044:
045: /**
046: * This editor handler class returns the editor URI depending on various factors.<p>
047: *
048: * Editor selection criteria:
049: * <ul>
050: * <li>the user preferences</li>
051: * <li>the users current browser</li>
052: * <li>the resource type</li>
053: * </ul>
054: * <p>
055: *
056: * @author Andreas Zahner
057: *
058: * @version $Revision: 1.16 $
059: *
060: * @since 6.0.0
061: *
062: * @see org.opencms.workplace.editors.I_CmsEditorHandler
063: * @see org.opencms.workplace.editors.CmsWorkplaceEditorManager
064: */
065: public class CmsEditorHandler implements I_CmsEditorHandler {
066:
067: /** The log object for this class. */
068: private static final Log LOG = CmsLog
069: .getLog(CmsEditorHandler.class);
070:
071: /**
072: * @see org.opencms.workplace.editors.I_CmsEditorHandler#getEditorUri(java.lang.String, CmsJspActionElement)
073: */
074: public String getEditorUri(String resource, CmsJspActionElement jsp)
075: throws CmsException {
076:
077: // first try to get the "edit as text" and "load default" parameters from the request
078: boolean editAsText = Boolean.valueOf(
079: jsp.getRequest().getParameter(
080: CmsEditor.PARAM_EDITASTEXT)).booleanValue();
081: boolean loadDefault = Boolean.valueOf(
082: jsp.getRequest().getParameter(
083: CmsEditor.PARAM_LOADDEFAULT)).booleanValue();
084: // initialize resource type with -1 (unknown resource type)
085: int resTypeId = -1;
086: String resourceType = "";
087: if (editAsText) {
088: // the resource should be treated as text, set the plain resource id
089: resTypeId = CmsResourceTypePlain.getStaticTypeId();
090: } else {
091: // get the resource type id of the edited resource
092: CmsResource res = jsp.getCmsObject().readResource(resource,
093: CmsResourceFilter.ALL);
094: resTypeId = res.getTypeId();
095: }
096:
097: // get the resource type name
098: resourceType = OpenCms.getResourceManager().getResourceType(
099: resTypeId).getTypeName();
100:
101: // get the editor URI from the editor manager
102: String editorUri = null;
103:
104: // get the browser identification from the request
105: String userAgent = jsp.getRequest().getHeader(
106: CmsRequestUtil.HEADER_USER_AGENT);
107:
108: if (loadDefault) {
109: // get default editor because loaddefault parameter was found
110: editorUri = OpenCms.getWorkplaceManager()
111: .getWorkplaceEditorManager().getDefaultEditorUri(
112: jsp.getRequestContext(), resourceType,
113: userAgent);
114: } else {
115: // get preferred editor
116: editorUri = OpenCms.getWorkplaceManager()
117: .getWorkplaceEditorManager().getEditorUri(
118: jsp.getRequestContext(), resourceType,
119: userAgent);
120: }
121:
122: try {
123: // check the presence of the editor
124: jsp.getCmsObject().readResource(editorUri);
125: } catch (Throwable t) {
126: // preferred or selected editor not found, try default editor
127: if (LOG.isInfoEnabled()) {
128: LOG.info(t);
129: }
130: editorUri = OpenCms.getWorkplaceManager()
131: .getWorkplaceEditorManager().getDefaultEditorUri(
132: jsp.getRequestContext(), resourceType,
133: userAgent);
134: }
135:
136: return editorUri;
137: }
138:
139: }
|