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.CmsFile;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.file.types.CmsResourceTypeXmlContent;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.workplace.CmsWorkplaceSettings;
040import org.opencms.xml.content.CmsXmlContent;
041
042import java.io.IOException;
043import java.io.UnsupportedEncodingException;
044
045import javax.servlet.ServletException;
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.jsp.JspException;
048
049import org.apache.commons.logging.Log;
050
051/**
052 * Creates the output for editing a resource (text or JSP files).<p>
053 *
054 * The following files use this class:
055 * <ul>
056 * <li>/editors/simple/editor.jsp</li>
057 * </ul>
058 * <p>
059 *
060 * @since 6.0.0
061 */
062public class CmsSimpleEditor extends CmsEditor {
063
064    /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
065    private static final String EDITOR_TYPE = "simple";
066
067    /** The log object for this class. */
068    private static final Log LOG = CmsLog.getLog(CmsSimpleEditor.class);
069
070    /**
071     * Public constructor.<p>
072     *
073     * @param jsp an initialized JSP action element
074     */
075    public CmsSimpleEditor(CmsJspActionElement jsp) {
076
077        super(jsp);
078    }
079
080    /**
081     * @see org.opencms.workplace.editors.CmsEditor#actionClear(boolean)
082     */
083    @Override
084    public void actionClear(boolean forceUnlock) {
085
086        boolean modified = Boolean.valueOf(getParamModified()).booleanValue();
087        if (forceUnlock || !modified) {
088            // unlock the resource when force unlock is true or resource was not modified
089            try {
090                getCms().unlockResource(getParamResource());
091            } catch (CmsException e) {
092                // should usually never happen
093                if (LOG.isInfoEnabled()) {
094                    LOG.info(e.getLocalizedMessage(), e);
095                }
096            }
097        }
098    }
099
100    /**
101     * Performs the exit editor action.<p>
102     *
103     * @see org.opencms.workplace.editors.CmsEditor#actionExit()
104     */
105    @Override
106    public void actionExit() throws IOException, JspException, ServletException {
107
108        if (getAction() == ACTION_CANCEL) {
109            // save and exit was canceled
110            return;
111        }
112
113        // unlock resource, if no modified
114        actionClear(false);
115
116        // close the editor
117        actionClose();
118    }
119
120    /**
121     * Performs the save content action.<p>
122     *
123     * @see org.opencms.workplace.editors.CmsEditor#actionSave()
124     */
125    @Override
126    public void actionSave() throws JspException {
127
128        CmsFile editFile = null;
129        try {
130            editFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
131            String decodedContent = decodeContentParameter(getParamContent(), getFileEncoding(), editFile);
132            try {
133                editFile.setContents(decodedContent.getBytes(getFileEncoding()));
134            } catch (UnsupportedEncodingException e) {
135                throw new CmsException(
136                    Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()),
137                    e);
138            }
139            if (OpenCms.getWorkplaceManager().isXmlContentAutoCorrect()
140                && (CmsResourceTypeXmlContent.isXmlContent(editFile))) {
141                // enable auto correction in case of XML content (if configured)
142                getCms().getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE);
143            }
144            // the file content might have been modified during the write operation
145            CmsFile writtenFile = getCms().writeFile(editFile);
146            decodedContent = CmsEncoder.createString(writtenFile.getContents(), getFileEncoding());
147            setParamContent(encodeContent(decodedContent));
148            // set the modified parameter
149            setParamModified(Boolean.TRUE.toString());
150        } catch (CmsException e) {
151            showErrorPage(e);
152        }
153
154        if (getAction() != ACTION_CANCEL) {
155            // save successful, set save action
156            setAction(ACTION_SAVE);
157        }
158    }
159
160    /**
161     * @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
162     */
163    @Override
164    public String getEditorResourceUri() {
165
166        return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
167    }
168
169    /**
170     * Decodes the content from the content request parameter.<p>
171     *
172     * @param encodedContent the encoded content
173     * @param encoding the encoding to use
174     * @param originalFile the current file state
175     *
176     * @return the decoded content
177     */
178    protected String decodeContentParameter(String encodedContent, String encoding, CmsFile originalFile) {
179
180        return decodeContent(encodedContent);
181    }
182
183    /**
184     * Initializes the editor content when opening the editor for the first time.<p>
185     */
186    @Override
187    protected void initContent() {
188
189        // save initialized instance of this class in request attribute for included sub-elements
190        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
191        // get the default encoding
192        String content = getParamContent();
193        if (CmsStringUtil.isNotEmpty(content)) {
194            // content already read, must be decoded
195            setParamContent(decodeContent(content));
196            return;
197        } else {
198            content = "";
199        }
200
201        try {
202            // lock resource if autolock is enabled
203            checkLock(getParamResource());
204            CmsFile editFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
205            content = CmsEncoder.createString(editFile.getContents(), getFileEncoding());
206        } catch (CmsException e) {
207            // reading of file contents failed, show error dialog
208            try {
209                showErrorPage(this, e);
210            } catch (JspException exc) {
211                // should usually never happen
212                if (LOG.isInfoEnabled()) {
213                    LOG.info(exc);
214                }
215            }
216        }
217        setParamContent(content);
218    }
219
220    /**
221     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
222     */
223    @Override
224    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
225
226        // fill the parameter values in the get/set methods
227        fillParamValues(request);
228        // set the dialog type
229        setParamDialogtype(EDITOR_TYPE);
230
231        // set the action for the JSP switch
232        if (EDITOR_SAVE.equals(getParamAction())) {
233            setAction(ACTION_SAVE);
234        } else if (EDITOR_SAVEEXIT.equals(getParamAction())) {
235            setAction(ACTION_SAVEEXIT);
236        } else if (EDITOR_EXIT.equals(getParamAction())) {
237            setAction(ACTION_EXIT);
238        } else if (EDITOR_SHOW.equals(getParamAction())) {
239            setAction(ACTION_SHOW);
240        } else if (EDITOR_SHOW_ERRORMESSAGE.equals(getParamAction())) {
241            setAction(ACTION_SHOW_ERRORMESSAGE);
242        } else {
243            // initial call of editor
244            setAction(ACTION_DEFAULT);
245            initContent();
246        }
247
248        setParamContent(encodeContent(getParamContent()));
249    }
250}