001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.configuration.CmsParameterConfiguration; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.i18n.CmsEncoder; 034import org.opencms.i18n.CmsLocaleManager; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.main.CmsRuntimeException; 038import org.opencms.main.OpenCms; 039import org.opencms.util.CmsRequestUtil; 040import org.opencms.util.CmsStringUtil; 041 042import java.io.IOException; 043import java.util.Locale; 044import java.util.Map; 045 046import jakarta.servlet.ServletRequest; 047import jakarta.servlet.ServletResponse; 048import jakarta.servlet.http.HttpServletRequest; 049import jakarta.servlet.http.HttpServletResponse; 050 051/** 052 * Loader for "pointers" to resources in the VFS or to external resources.<p> 053 * 054 * @since 6.0.0 055 */ 056public class CmsPointerLoader extends CmsDumpLoader { 057 058 /** 059 * The configuration parameter for the OpenCms XML configuration to enable 060 * that the parameters in requests to pointer resources are appended to the 061 * pointer target link. 062 */ 063 public static final String CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED = "pointer.requestparamsupport.enabled"; 064 065 /** The id of this loader. */ 066 public static final int RESOURCE_POINTER_LOADER_ID = 4; 067 068 /** 069 * Flag that controls if parameters in requests to pointer resources are 070 * appended to the target link when redirecting. 071 */ 072 protected static boolean m_requestParamSupportEnabled; 073 074 /** The html-code prefix for generating the export file for external links. */ 075 private static String EXPORT_PREFIX = "<html>\n<head>\n<meta http-equiv=" 076 + '"' 077 + "refresh" 078 + '"' 079 + " content=" 080 + '"' 081 + "0; url="; 082 083 /** The html-code suffix for generating the export file for external links. */ 084 private static String EXPORT_SUFFIX = '"' + ">\n</head>\n<body></body>\n</html>"; 085 086 /** 087 * The constructor of the class is empty and does nothing.<p> 088 */ 089 public CmsPointerLoader() { 090 091 // NOOP 092 } 093 094 /** 095 * Returns <code>true</code> if parameters in requests to pointer resources 096 * are appended to the target link when redirecting. 097 * <p> 098 * This is controlled by the configuration of this loader in 099 * <code>opencms-system.xml</code>. 100 * <p> 101 * 102 * @return <code>true</code> if parameters in requests to pointer resources 103 * are appended to the target link when redirecting. 104 */ 105 public static boolean isRequestParamSupportEnabled() { 106 107 return m_requestParamSupportEnabled; 108 } 109 110 /** 111 * Internal helper that is used by 112 * <code>{@link #load(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code> 113 * and 114 * <code>{@link #export(CmsObject, CmsResource, HttpServletRequest, HttpServletResponse)}</code> 115 * to handle conditional request parameter support for links to pointer 116 * resources. 117 * <p> 118 * 119 * @param pointerLink 120 * the link to append request parameters to 121 * 122 * @param req 123 * the original request to the pointer 124 * 125 * @return the pointer with the parameters 126 */ 127 private static String appendLinkParams(String pointerLink, HttpServletRequest req) { 128 129 String result = pointerLink; 130 if (isRequestParamSupportEnabled()) { 131 Map<String, String[]> params = req.getParameterMap(); 132 if (params.size() > 0) { 133 result = CmsRequestUtil.appendParameters(result, params, false); 134 } 135 } 136 return result; 137 } 138 139 /** 140 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String) 141 */ 142 @Override 143 public void addConfigurationParameter(String paramName, String paramValue) { 144 145 if (CmsStringUtil.isNotEmpty(paramName) && CmsStringUtil.isNotEmpty(paramValue)) { 146 if (CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED.equals(paramName)) { 147 m_requestParamSupportEnabled = Boolean.valueOf(paramValue).booleanValue(); 148 } 149 } 150 } 151 152 /** 153 * Destroy this ResourceLoder, this is a NOOP so far.<p> 154 */ 155 @Override 156 public void destroy() { 157 158 // NOOP 159 } 160 161 /** 162 * @see org.opencms.loader.I_CmsResourceLoader#dump(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.lang.String, java.util.Locale, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse) 163 */ 164 @Override 165 public byte[] dump( 166 CmsObject cms, 167 CmsResource resource, 168 String element, 169 Locale locale, 170 HttpServletRequest req, 171 HttpServletResponse res) 172 throws CmsException { 173 174 return cms.readFile(resource).getContents(); 175 } 176 177 /** 178 * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse) 179 */ 180 @Override 181 public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 182 throws IOException, CmsException { 183 184 String pointer = new String( 185 cms.readFile(resource).getContents(), 186 CmsLocaleManager.getResourceEncoding(cms, resource)); 187 StringBuffer result = new StringBuffer(128); 188 result.append(EXPORT_PREFIX); 189 // conditionally append parameters of the current request: 190 pointer = appendLinkParams(pointer, req); 191 if (pointer.indexOf(':') < 0) { 192 result.append(OpenCms.getLinkManager().substituteLink(cms, pointer)); 193 } else { 194 result.append(pointer); 195 } 196 result.append(EXPORT_SUFFIX); 197 load(cms, resource, req, res); 198 return result.toString().getBytes(OpenCms.getSystemInfo().getDefaultEncoding()); 199 } 200 201 /** 202 * Will always return <code>null</code> since this loader does not 203 * need to be cnofigured.<p> 204 * 205 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration() 206 */ 207 @Override 208 public CmsParameterConfiguration getConfiguration() { 209 210 CmsParameterConfiguration result = new CmsParameterConfiguration(); 211 CmsParameterConfiguration config = super.getConfiguration(); 212 if (config != null) { 213 result.putAll(config); 214 } 215 result.put(CONFIGURATION_REQUEST_PARAM_SUPPORT_ENABLED, String.valueOf(m_requestParamSupportEnabled)); 216 return result; 217 } 218 219 /** 220 * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId() 221 */ 222 @Override 223 public int getLoaderId() { 224 225 return RESOURCE_POINTER_LOADER_ID; 226 } 227 228 /** 229 * Return a String describing the ResourceLoader, 230 * which is (localized to the system default locale) 231 * <code>"The OpenCms default resource loader for pointers"</code>.<p> 232 * 233 * @return a describing String for the ResourceLoader 234 */ 235 @Override 236 public String getResourceLoaderInfo() { 237 238 return Messages.get().getBundle().key(Messages.GUI_LOADER_POINTER_DEFAULT_DESC_0); 239 } 240 241 /** 242 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 243 */ 244 @Override 245 public void initConfiguration() { 246 247 if (CmsLog.INIT.isInfoEnabled()) { 248 CmsLog.INIT.info( 249 Messages.get().getBundle().key(Messages.INIT_LOADER_INITIALIZED_1, this.getClass().getName())); 250 } 251 } 252 253 /** 254 * Returns true if request parameter support is disabled. <p> 255 * 256 * @return 257 * true if request parameter support is disabled 258 * 259 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled() 260 */ 261 @Override 262 public boolean isStaticExportEnabled() { 263 264 return !m_requestParamSupportEnabled; 265 } 266 267 /** 268 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable() 269 */ 270 @Override 271 public boolean isStaticExportProcessable() { 272 273 return false; 274 } 275 276 /** 277 * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates() 278 */ 279 @Override 280 public boolean isUsableForTemplates() { 281 282 return false; 283 } 284 285 /** 286 * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate() 287 */ 288 @Override 289 public boolean isUsingUriWhenLoadingTemplate() { 290 291 return false; 292 } 293 294 /** 295 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse) 296 */ 297 @Override 298 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 299 throws IOException, CmsException { 300 301 if ((res == null) || res.isCommitted()) { 302 // nothing we can do 303 return; 304 } 305 306 String pointer = new String( 307 cms.readFile(resource).getContents(), 308 CmsLocaleManager.getResourceEncoding(cms, resource)); 309 if (CmsStringUtil.isEmptyOrWhitespaceOnly(pointer)) { 310 throw new CmsLoaderException( 311 Messages.get().container(Messages.ERR_INVALID_POINTER_FILE_1, resource.getName())); 312 } 313 if (pointer.indexOf(':') < 0) { 314 pointer = OpenCms.getLinkManager().substituteLink(cms, pointer); 315 } else { 316 pointer = CmsEncoder.convertHostToPunycode(pointer); 317 } 318 319 // conditionally append parameters of the current request: 320 pointer = appendLinkParams(pointer, req); 321 322 res.sendRedirect(pointer); 323 } 324 325 /** 326 * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse) 327 */ 328 @Override 329 public void service(CmsObject cms, CmsResource file, ServletRequest req, ServletResponse res) { 330 331 throw new CmsRuntimeException( 332 Messages.get().container(Messages.ERR_SERVICE_UNSUPPORTED_1, getClass().getName())); 333 } 334}