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.loader; 029 030import org.opencms.ade.configuration.formatters.CmsFormatterConfigurationCache; 031import org.opencms.configuration.CmsParameterConfiguration; 032import org.opencms.file.CmsObject; 033import org.opencms.file.CmsResource; 034import org.opencms.jsp.util.CmsJspStandardContextBean; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.main.OpenCms; 038import org.opencms.xml.containerpage.CmsContainerElementBean; 039 040import java.io.IOException; 041import java.util.Locale; 042 043import javax.servlet.ServletException; 044import javax.servlet.ServletRequest; 045import javax.servlet.ServletResponse; 046import javax.servlet.http.HttpServletRequest; 047import javax.servlet.http.HttpServletResponse; 048 049/** 050 * Loader for macro formatter.<p> 051 */ 052public class CmsMacroFormatterLoader implements I_CmsResourceLoader { 053 054 /** The JSP rendering the macro. */ 055 private static final String RENDER_MACRO_JSP = "/system/modules/org.opencms.base/pages/render-macro.jsp"; 056 057 /** The JSP rendering the macro. */ 058 private static final String RENDER_STRING_TEMPLATE_JSP = "/system/modules/org.opencms.base/pages/render-string-template.jsp"; 059 060 /** The loader id. */ 061 public static final int LOADER_ID = 15; 062 063 /** 064 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String) 065 */ 066 public void addConfigurationParameter(String paramName, String paramValue) { 067 068 // xml document loaders require no parameters 069 } 070 071 /** 072 * @see org.opencms.loader.I_CmsResourceLoader#destroy() 073 */ 074 public void destroy() { 075 076 // NOOP 077 } 078 079 /** 080 * @see org.opencms.loader.I_CmsResourceLoader#dump(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.lang.String, java.util.Locale, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 081 */ 082 public byte[] dump( 083 CmsObject cms, 084 CmsResource resource, 085 String element, 086 Locale selectedLocale, 087 HttpServletRequest req, 088 HttpServletResponse res) 089 throws CmsException, IOException, ServletException { 090 091 CmsResource renderer = null; 092 if (CmsFormatterConfigurationCache.TYPE_MACRO_FORMATTER.equals( 093 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 094 renderer = cms.readResource(RENDER_MACRO_JSP); 095 } else if (CmsFormatterConfigurationCache.TYPE_FLEX_FORMATTER.equals( 096 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 097 renderer = cms.readResource(RENDER_STRING_TEMPLATE_JSP); 098 } 099 if (renderer != null) { 100 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(renderer); 101 ensureElementFormatter(resource, req); 102 return loader.dump(cms, renderer, element, selectedLocale, req, res); 103 } else { 104 return new byte[] {}; 105 } 106 } 107 108 /** 109 * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 110 */ 111 public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 112 throws ServletException, IOException, CmsException { 113 114 CmsResource renderer = null; 115 if (CmsFormatterConfigurationCache.TYPE_MACRO_FORMATTER.equals( 116 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 117 renderer = cms.readResource(RENDER_MACRO_JSP); 118 } else if (CmsFormatterConfigurationCache.TYPE_FLEX_FORMATTER.equals( 119 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 120 renderer = cms.readResource(RENDER_STRING_TEMPLATE_JSP); 121 } 122 if (renderer != null) { 123 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(renderer); 124 ensureElementFormatter(resource, req); 125 return loader.export(cms, renderer, req, res); 126 } else { 127 return new byte[] {}; 128 } 129 } 130 131 /** 132 * Returns <code>null</code> since XML document loaders does usually not need to be configured.<p> 133 * 134 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration() 135 */ 136 public CmsParameterConfiguration getConfiguration() { 137 138 return null; 139 } 140 141 /** 142 * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId() 143 */ 144 public int getLoaderId() { 145 146 return LOADER_ID; 147 } 148 149 /** 150 * @see org.opencms.loader.I_CmsResourceLoader#getResourceLoaderInfo() 151 */ 152 public String getResourceLoaderInfo() { 153 154 return Messages.get().getBundle().key(Messages.GUI_LOADER_MACRO_FORMATTER_DEFAULT_DESC_0); 155 } 156 157 /** 158 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 159 */ 160 public void initConfiguration() { 161 162 if (CmsLog.INIT.isInfoEnabled()) { 163 CmsLog.INIT.info( 164 Messages.get().getBundle().key(Messages.INIT_LOADER_INITIALIZED_1, this.getClass().getName())); 165 } 166 } 167 168 /** 169 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled() 170 */ 171 public boolean isStaticExportEnabled() { 172 173 return true; 174 } 175 176 /** 177 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable() 178 */ 179 public boolean isStaticExportProcessable() { 180 181 return true; 182 } 183 184 /** 185 * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates() 186 */ 187 public boolean isUsableForTemplates() { 188 189 return false; 190 } 191 192 /** 193 * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate() 194 */ 195 public boolean isUsingUriWhenLoadingTemplate() { 196 197 return false; 198 } 199 200 /** 201 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 202 */ 203 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 204 throws ServletException, IOException, CmsException { 205 206 CmsResource renderer = null; 207 if (CmsFormatterConfigurationCache.TYPE_MACRO_FORMATTER.equals( 208 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 209 renderer = cms.readResource(RENDER_MACRO_JSP); 210 } else if (CmsFormatterConfigurationCache.TYPE_FLEX_FORMATTER.equals( 211 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 212 renderer = cms.readResource(RENDER_STRING_TEMPLATE_JSP); 213 } 214 if (renderer != null) { 215 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(renderer); 216 ensureElementFormatter(resource, req); 217 loader.load(cms, renderer, req, res); 218 } 219 } 220 221 /** 222 * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse) 223 */ 224 public void service(CmsObject cms, CmsResource resource, ServletRequest req, ServletResponse res) 225 throws IOException, CmsException, ServletException { 226 227 CmsResource renderer = null; 228 if (CmsFormatterConfigurationCache.TYPE_MACRO_FORMATTER.equals( 229 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 230 renderer = cms.readResource(RENDER_MACRO_JSP); 231 } else if (CmsFormatterConfigurationCache.TYPE_FLEX_FORMATTER.equals( 232 OpenCms.getResourceManager().getResourceType(resource).getTypeName())) { 233 renderer = cms.readResource(RENDER_STRING_TEMPLATE_JSP); 234 } 235 if (renderer != null) { 236 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(renderer); 237 loader.service(cms, renderer, req, res); 238 } 239 } 240 241 /** 242 * Ensure the element formatter id is set in the element bean.<p> 243 * 244 * @param resource the formatter resource 245 * @param req the request 246 */ 247 private void ensureElementFormatter(CmsResource resource, HttpServletRequest req) { 248 249 CmsJspStandardContextBean contextBean = CmsJspStandardContextBean.getInstance(req); 250 CmsContainerElementBean element = contextBean.getElement(); 251 if (!resource.getStructureId().equals(element.getFormatterId())) { 252 element.setFormatterId(resource.getStructureId()); 253 } 254 } 255}