001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (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, 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.configuration.CmsParameterConfiguration; 031import org.opencms.file.CmsFile; 032import org.opencms.file.CmsObject; 033import org.opencms.file.CmsResource; 034import org.opencms.flex.CmsFlexController; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.main.OpenCms; 038import org.opencms.util.CmsRequestUtil; 039import org.opencms.util.CmsStringUtil; 040import org.opencms.workplace.CmsWorkplaceManager; 041 042import java.io.IOException; 043import java.util.Iterator; 044import java.util.Locale; 045 046import javax.servlet.ServletRequest; 047import javax.servlet.ServletResponse; 048import javax.servlet.http.HttpServletRequest; 049import javax.servlet.http.HttpServletResponse; 050 051/** 052 * Dump loader for binary or other unprocessed resource types.<p> 053 * 054 * This loader is also used to deliver static sub-elements of pages processed 055 * by other loaders.<p> 056 * 057 * @since 6.0.0 058 */ 059public class CmsDumpLoader implements I_CmsResourceLoader { 060 061 /** The id of this loader. */ 062 public static final int RESOURCE_LOADER_ID = 1; 063 064 /** The maximum age for dumped contents in the clients cache. */ 065 private static long m_clientCacheMaxAge; 066 067 /** The resource loader configuration. */ 068 private CmsParameterConfiguration m_configuration; 069 070 /** 071 * The constructor of the class is empty and does nothing.<p> 072 */ 073 public CmsDumpLoader() { 074 075 m_configuration = new CmsParameterConfiguration(); 076 } 077 078 /** 079 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String) 080 */ 081 public void addConfigurationParameter(String paramName, String paramValue) { 082 083 m_configuration.put(paramName, paramValue); 084 } 085 086 /** 087 * Destroy this ResourceLoder, this is a NOOP so far.<p> 088 */ 089 public void destroy() { 090 091 // NOOP 092 } 093 094 /** 095 * @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) 096 */ 097 public byte[] dump( 098 CmsObject cms, 099 CmsResource resource, 100 String element, 101 Locale locale, 102 HttpServletRequest req, 103 HttpServletResponse res) throws CmsException { 104 105 return cms.readFile(resource).getContents(); 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 IOException, CmsException { 113 114 CmsFile file = cms.readFile(resource); 115 116 // if no request and response are given, the resource only must be exported and no 117 // output must be generated 118 if ((req != null) && (res != null)) { 119 // overwrite headers if set as default 120 for (Iterator<String> i = OpenCms.getStaticExportManager().getExportHeaders().listIterator(); i.hasNext();) { 121 String header = i.next(); 122 123 // set header only if format is "key: value" 124 String[] parts = CmsStringUtil.splitAsArray(header, ':'); 125 if (parts.length == 2) { 126 res.setHeader(parts[0], parts[1]); 127 } 128 } 129 load(cms, file, req, res); 130 } 131 132 return file.getContents(); 133 } 134 135 /** 136 * Will always return <code>null</code> since this loader does not 137 * need to be configured.<p> 138 * 139 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration() 140 */ 141 public CmsParameterConfiguration getConfiguration() { 142 143 // return the configuration in an immutable form 144 return m_configuration; 145 } 146 147 /** 148 * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId() 149 */ 150 public int getLoaderId() { 151 152 return RESOURCE_LOADER_ID; 153 } 154 155 /** 156 * Return a String describing the ResourceLoader, 157 * which is (localized to the system default locale) 158 * <code>"The OpenCms default resource loader for unprocessed files"</code>.<p> 159 * 160 * @return a describing String for the ResourceLoader 161 */ 162 public String getResourceLoaderInfo() { 163 164 return Messages.get().getBundle().key(Messages.GUI_LOADER_DUMB_DEFAULT_DESC_0); 165 } 166 167 /** 168 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 169 */ 170 public void initConfiguration() { 171 172 Object maxAge = m_configuration.get("client.cache.maxage"); 173 if (maxAge == null) { 174 m_clientCacheMaxAge = -1; 175 } else { 176 m_clientCacheMaxAge = Long.parseLong(String.valueOf(maxAge)); 177 } 178 179 if (CmsLog.INIT.isInfoEnabled()) { 180 if (maxAge != null) { 181 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_CLIENT_CACHE_MAX_AGE_1, maxAge)); 182 } 183 CmsLog.INIT.info(Messages.get().getBundle().key( 184 Messages.INIT_LOADER_INITIALIZED_1, 185 this.getClass().getName())); 186 } 187 } 188 189 /** 190 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled() 191 */ 192 public boolean isStaticExportEnabled() { 193 194 return true; 195 } 196 197 /** 198 * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable() 199 */ 200 public boolean isStaticExportProcessable() { 201 202 return false; 203 } 204 205 /** 206 * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates() 207 */ 208 public boolean isUsableForTemplates() { 209 210 return false; 211 } 212 213 /** 214 * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate() 215 */ 216 public boolean isUsingUriWhenLoadingTemplate() { 217 218 return false; 219 } 220 221 /** 222 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 223 */ 224 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 225 throws IOException, CmsException { 226 227 if (canSendLastModifiedHeader(resource, req, res)) { 228 // no further processing required 229 return; 230 } 231 232 // make sure we have the file contents available 233 CmsFile file = cms.readFile(resource); 234 235 // set response status to "200 - OK" (required for static export "on-demand") 236 res.setStatus(HttpServletResponse.SC_OK); 237 // set content length header 238 res.setContentLength(file.getContents().length); 239 240 if (CmsWorkplaceManager.isWorkplaceUser(req)) { 241 // prevent caching for Workplace users 242 res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, System.currentTimeMillis()); 243 CmsRequestUtil.setNoCacheHeaders(res); 244 } else { 245 // set date last modified header 246 res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, file.getDateLastModified()); 247 248 // set "Expires" only if cache control is not already set 249 if (!res.containsHeader(CmsRequestUtil.HEADER_CACHE_CONTROL)) { 250 long expireTime = resource.getDateExpired(); 251 if (expireTime == CmsResource.DATE_EXPIRED_DEFAULT) { 252 expireTime--; 253 // flex controller will automatically reduce this to a reasonable value 254 } 255 // now set "Expires" header 256 CmsFlexController.setDateExpiresHeader(res, expireTime, m_clientCacheMaxAge); 257 } 258 } 259 260 service(cms, file, req, res); 261 } 262 263 /** 264 * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse) 265 */ 266 public void service(CmsObject cms, CmsResource resource, ServletRequest req, ServletResponse res) 267 throws CmsException, IOException { 268 269 res.getOutputStream().write(cms.readFile(resource).getContents()); 270 } 271 272 /** 273 * Checks if the requested resource must be send to the client by checking the "If-Modified-Since" http header.<p> 274 * 275 * If the resource has not been modified, the "304 - not modified" 276 * header is send to the client and <code>true</code> 277 * is returned, otherwise nothing is send and <code>false</code> is returned.<p> 278 * 279 * @param resource the resource to check 280 * @param req the current request 281 * @param res the current response 282 * 283 * @return <code>true</code> if the "304 - not modified" header has been send to the client 284 */ 285 protected boolean canSendLastModifiedHeader(CmsResource resource, HttpServletRequest req, HttpServletResponse res) { 286 287 // resource state must be unchanged 288 if (resource.getState().isUnchanged() 289 // the request must not have been send by a workplace user (we can't use "304 - not modified" in workplace 290 && !CmsWorkplaceManager.isWorkplaceUser(req) 291 // last modified header must match the time form the resource 292 && CmsFlexController.isNotModifiedSince(req, resource.getDateLastModified())) { 293 long now = System.currentTimeMillis(); 294 if ((resource.getDateReleased() < now) && (resource.getDateExpired() > now)) { 295 // resource is available and not expired 296 CmsFlexController.setDateExpiresHeader(res, resource.getDateExpired(), m_clientCacheMaxAge); 297 // set status 304 - not modified 298 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 299 return true; 300 } 301 } 302 return false; 303 } 304}