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