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, 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.ade.contenteditor;
029
030import org.opencms.ade.contenteditor.shared.CmsContentDefinition;
031import org.opencms.ade.contenteditor.shared.CmsExternalWidgetConfiguration;
032import org.opencms.ade.contenteditor.shared.rpc.I_CmsContentService;
033import org.opencms.gwt.CmsGwtActionElement;
034import org.opencms.gwt.shared.CmsCoreData;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.util.CmsRequestUtil;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.workplace.editors.CmsEditor;
039
040import java.util.HashSet;
041import java.util.Set;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047/**
048 * The content editor action element.<p>
049 */
050public class CmsContentEditorActionElement extends CmsGwtActionElement {
051
052    /** The OpenCms module name. */
053    public static final String CMS_MODULE_NAME = "org.opencms.ade.contenteditor";
054
055    /** The GWT module name. */
056    public static final String GWT_MODULE_NAME = CmsCoreData.ModuleKey.contenteditor.name();
057
058    /**
059     * Constructor.<p>
060     *
061     * @param context the page context
062     * @param req the servlet request
063     * @param res the servlet response
064     */
065    public CmsContentEditorActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) {
066
067        super(context, req, res);
068    }
069
070    /**
071     * @see org.opencms.gwt.CmsGwtActionElement#export()
072     */
073    @Override
074    public String export() throws Exception {
075
076        return "";
077    }
078
079    /**
080     * @see org.opencms.gwt.CmsGwtActionElement#exportAll()
081     */
082    @Override
083    public String exportAll() throws Exception {
084
085        CmsRequestUtil.disableCrossSiteFrameEmbedding(getResponse());
086        StringBuffer sb = new StringBuffer();
087        sb.append(super.export());
088        sb.append(exportModuleScriptTag(GWT_MODULE_NAME));
089        sb.append(getPrefetch());
090        return sb.toString();
091    }
092
093    /**
094     * Adds link and script tags to the buffer if required for external widgets.<p>
095     *
096     * @param sb the string buffer to append the tags to
097     * @param definition the content definition
098     */
099    private void addExternalResourceTags(StringBuffer sb, CmsContentDefinition definition) {
100
101        Set<String> includedScripts = new HashSet<String>();
102        Set<String> includedStyles = new HashSet<String>();
103        for (CmsExternalWidgetConfiguration configuration : definition.getExternalWidgetConfigurations()) {
104            for (String css : configuration.getCssResourceLinks()) {
105                // avoid including the same resource twice
106                if (!includedStyles.contains(css)) {
107                    sb.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"").append(css).append("\"></link>");
108                    includedStyles.add(css);
109                }
110            }
111            for (String script : configuration.getJavaScriptResourceLinks()) {
112                // avoid including the same resource twice
113                if (!includedScripts.contains(script)) {
114                    sb.append("<script src=\"").append(script).append("\"></script>");
115                    includedScripts.add(script);
116                }
117            }
118        }
119    }
120
121    /**
122     * Returns the prefetch data include.<p>
123     *
124     * @return the prefetch data include
125     *
126     * @throws Exception if something goes wrong
127     */
128    private String getPrefetch() throws Exception {
129
130        long timer = 0;
131        if (CmsContentService.LOG.isDebugEnabled()) {
132            timer = System.currentTimeMillis();
133        }
134        CmsContentDefinition definition = CmsContentService.prefetch(getRequest());
135        StringBuffer sb = new StringBuffer();
136        String backlink = getRequest().getParameter(CmsEditor.PARAM_BACKLINK);
137        if (CmsStringUtil.isEmptyOrWhitespaceOnly(backlink) || !CmsRequestUtil.checkBacklink(backlink, getRequest())) {
138            backlink = CmsVaadinUtils.getWorkplaceLink();
139        } else {
140            backlink = link(backlink);
141        }
142        sb.append(
143            wrapScript(I_CmsContentService.PARAM_BACKLINK, "='", CmsStringUtil.escapeJavaScript(backlink) + "';\n"));
144        String prefetchedData = exportDictionary(
145            I_CmsContentService.DICT_CONTENT_DEFINITION,
146            I_CmsContentService.class.getMethod("prefetch"),
147            definition);
148        sb.append(prefetchedData);
149        addExternalResourceTags(sb, definition);
150        if (CmsContentService.LOG.isDebugEnabled()) {
151            CmsContentService.LOG.debug(
152                Messages.get().getBundle().key(
153                    Messages.LOG_TAKE_PREFETCHING_TIME_FOR_RESOURCE_2,
154                    definition.getSitePath(),
155                    "" + (System.currentTimeMillis() - timer)));
156        }
157        return sb.toString();
158    }
159}