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.loader; 029 030import org.opencms.ade.galleries.CmsPreviewService; 031import org.opencms.cache.CmsVfsNameBasedDiskCache; 032import org.opencms.configuration.CmsParameterConfiguration; 033import org.opencms.file.CmsFile; 034import org.opencms.file.CmsObject; 035import org.opencms.file.CmsResource; 036import org.opencms.main.CmsEvent; 037import org.opencms.main.CmsException; 038import org.opencms.main.CmsLog; 039import org.opencms.main.I_CmsEventListener; 040import org.opencms.main.OpenCms; 041import org.opencms.scheduler.jobs.CmsImageCacheCleanupJob; 042import org.opencms.util.CmsStringUtil; 043 044import java.io.IOException; 045import java.util.Map; 046 047import javax.servlet.http.HttpServletRequest; 048import javax.servlet.http.HttpServletResponse; 049 050import org.apache.commons.logging.Log; 051 052/** 053 * Loader for images from the OpenCms VSF with integrated image scaling and processing capabilities.<p> 054 * 055 * To scale or process an image, the parameter <code>{@link org.opencms.loader.CmsImageScaler#PARAM_SCALE}</code> 056 * has to be appended to the image URI. The value for the parameter needs to be composed from the <code>SCALE_PARAM</code> 057 * options provided by the constants in the <code>{@link org.opencms.file.types.CmsResourceTypeImage}</code> class.<p> 058 * 059 * For example, to scale an image to exact 800x600 pixel with center fitting and a background color of grey, 060 * the following parameter String can be used: <code>w:800,h:600,t:0,c:c0c0c0</code>.<p> 061 * 062 * @since 6.2.0 063 */ 064public class CmsImageLoader extends CmsDumpLoader implements I_CmsEventListener { 065 066 /** The configuration parameter for the OpenCms XML configuration to set the image down scale operation. */ 067 public static final String CONFIGURATION_DOWNSCALE = "image.scaling.downscale"; 068 069 /** The configuration parameter for the OpenCms XML configuration to set the image cache repository. */ 070 public static final String CONFIGURATION_IMAGE_FOLDER = "image.folder"; 071 072 /** The configuration parameter for the OpenCms XML configuration to set the maximum image blur size. */ 073 public static final String CONFIGURATION_MAX_BLUR_SIZE = "image.scaling.maxblursize"; 074 075 /** The configuration parameter for the OpenCms XML configuration to set the maximum image scale size. */ 076 public static final String CONFIGURATION_MAX_SCALE_SIZE = "image.scaling.maxsize"; 077 078 /** The configuration parameter for the OpenCms XML configuration to enable the image scaling. */ 079 public static final String CONFIGURATION_SCALING_ENABLED = "image.scaling.enabled"; 080 081 /** Default name for the image cache repository. */ 082 public static final String IMAGE_REPOSITORY_DEFAULT = "/WEB-INF/imagecache/"; 083 084 /** Clear event parameter. */ 085 public static final String PARAM_CLEAR_IMAGES_CACHE = "_IMAGES_CACHE_"; 086 087 /** The id of this loader. */ 088 public static final int RESOURCE_LOADER_ID_IMAGE_LOADER = 2; 089 090 /** The log object for this class. */ 091 protected static final Log LOG = CmsLog.getLog(CmsImageLoader.class); 092 093 /** The (optional) image down scale parameters for image write operations. */ 094 protected static String m_downScaleParams; 095 096 /** Indicates if image scaling is active. */ 097 protected static boolean m_enabled; 098 099 /** The maximum image size (width * height) to apply image blurring when down scaling (setting this to high may cause "out of memory" errors). */ 100 protected static int m_maxBlurSize = CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE; 101 102 /** The disk cache to use for saving scaled image versions. */ 103 protected static CmsVfsNameBasedDiskCache m_vfsDiskCache; 104 105 /** The name of the configured image cache repository. */ 106 protected String m_imageRepositoryFolder; 107 108 /** The maximum image size (width or height) to allow when up scaling an image using request parameters. */ 109 protected int m_maxScaleSize = CmsImageScaler.SCALE_DEFAULT_MAX_SIZE; 110 111 /** 112 * Creates a new image loader.<p> 113 */ 114 public CmsImageLoader() { 115 116 super(); 117 } 118 119 /** 120 * Returns the image down scale parameters, 121 * which is set with the {@link #CONFIGURATION_DOWNSCALE} configuration option.<p> 122 * 123 * If no down scale parameters have been set in the configuration, this will return <code>null</code>. 124 * 125 * @return the image down scale parameters 126 */ 127 public static String getDownScaleParams() { 128 129 return m_downScaleParams; 130 } 131 132 /** 133 * Returns the path of the image cache repository folder in the RFS, 134 * which is set with the {@link #CONFIGURATION_IMAGE_FOLDER} configuration option.<p> 135 * 136 * @return the path of the image cache repository folder in the RFS 137 */ 138 public static String getImageRepositoryPath() { 139 140 return m_vfsDiskCache.getRepositoryPath(); 141 } 142 143 /** 144 * The maximum blur size for image re-scale operations, 145 * which is set with the {@link #CONFIGURATION_MAX_BLUR_SIZE} configuration option.<p> 146 * 147 * The default is 2500 * 2500 pixel.<p> 148 * 149 * @return the maximum blur size for image re-scale operations 150 */ 151 public static int getMaxBlurSize() { 152 153 return m_maxBlurSize; 154 } 155 156 /** 157 * Returns <code>true</code> if the image scaling and processing capabilities for the 158 * OpenCms VFS images have been enabled, <code>false</code> if not.<p> 159 * 160 * Image scaling is enabled by setting the loader parameter <code>image.scaling.enabled</code> 161 * to the value <code>true</code> in the configuration file <code>opencms-vfs.xml</code>.<p> 162 * 163 * Enabling image processing in OpenCms may require several additional configuration steps 164 * on the server running OpenCms, especially in UNIX systems. Here it is often required to have an X window server 165 * configured and accessible so that the required Java ImageIO operations work. 166 * Therefore the image scaling capabilities in OpenCms are disabled by default.<p> 167 * 168 * @return <code>true</code> if the image scaling and processing capabilities for the 169 * OpenCms VFS images have been enabled 170 */ 171 public static boolean isEnabled() { 172 173 return m_enabled; 174 } 175 176 /** 177 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String) 178 */ 179 @Override 180 public void addConfigurationParameter(String paramName, String paramValue) { 181 182 if (CmsStringUtil.isNotEmpty(paramName) && CmsStringUtil.isNotEmpty(paramValue)) { 183 if (CONFIGURATION_SCALING_ENABLED.equals(paramName)) { 184 m_enabled = Boolean.valueOf(paramValue).booleanValue(); 185 } 186 if (CONFIGURATION_IMAGE_FOLDER.equals(paramName)) { 187 m_imageRepositoryFolder = paramValue.trim(); 188 } 189 if (CONFIGURATION_MAX_SCALE_SIZE.equals(paramName)) { 190 m_maxScaleSize = CmsStringUtil.getIntValue( 191 paramValue, 192 CmsImageScaler.SCALE_DEFAULT_MAX_SIZE, 193 paramName); 194 } 195 if (CONFIGURATION_MAX_BLUR_SIZE.equals(paramName)) { 196 m_maxBlurSize = CmsStringUtil.getIntValue( 197 paramValue, 198 CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE, 199 paramName); 200 } 201 if (CONFIGURATION_DOWNSCALE.equals(paramName)) { 202 m_downScaleParams = paramValue.trim(); 203 } 204 } 205 super.addConfigurationParameter(paramName, paramValue); 206 } 207 208 /** 209 * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent) 210 */ 211 public void cmsEvent(CmsEvent event) { 212 213 if (event == null) { 214 return; 215 } 216 // only react on the clear caches event 217 int type = event.getType(); 218 if (type != I_CmsEventListener.EVENT_CLEAR_CACHES) { 219 return; 220 } 221 // only react if the clear images cache parameter is set 222 Map<String, ?> data = event.getData(); 223 if (data == null) { 224 return; 225 } 226 Object param = data.get(PARAM_CLEAR_IMAGES_CACHE); 227 if (param == null) { 228 return; 229 } 230 float age = -1; 231 if (param instanceof String) { 232 age = Float.valueOf((String)param).floatValue(); 233 } else if (param instanceof Number) { 234 age = ((Number)param).floatValue(); 235 } 236 CmsImageCacheCleanupJob.cleanImageCache(age); 237 } 238 239 /** 240 * @see org.opencms.loader.I_CmsResourceLoader#destroy() 241 */ 242 @Override 243 public void destroy() { 244 245 m_enabled = false; 246 m_imageRepositoryFolder = null; 247 m_vfsDiskCache = null; 248 } 249 250 /** 251 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration() 252 */ 253 @Override 254 public CmsParameterConfiguration getConfiguration() { 255 256 CmsParameterConfiguration result = new CmsParameterConfiguration(); 257 CmsParameterConfiguration config = super.getConfiguration(); 258 if (config != null) { 259 result.putAll(config); 260 } 261 result.put(CONFIGURATION_SCALING_ENABLED, String.valueOf(m_enabled)); 262 result.put(CONFIGURATION_IMAGE_FOLDER, m_imageRepositoryFolder); 263 return result; 264 } 265 266 /** 267 * @see org.opencms.loader.I_CmsResourceLoader#getLoaderId() 268 */ 269 @Override 270 public int getLoaderId() { 271 272 return RESOURCE_LOADER_ID_IMAGE_LOADER; 273 } 274 275 /** 276 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 277 */ 278 @Override 279 public void initConfiguration() { 280 281 if (CmsStringUtil.isEmpty(m_imageRepositoryFolder)) { 282 m_imageRepositoryFolder = IMAGE_REPOSITORY_DEFAULT; 283 } 284 // initialize the image cache 285 if (m_vfsDiskCache == null) { 286 m_vfsDiskCache = new CmsVfsNameBasedDiskCache( 287 OpenCms.getSystemInfo().getWebApplicationRfsPath(), 288 m_imageRepositoryFolder); 289 } 290 OpenCms.addCmsEventListener(this); 291 // output setup information 292 if (CmsLog.INIT.isInfoEnabled()) { 293 CmsLog.INIT.info( 294 Messages.get().getBundle().key( 295 Messages.INIT_IMAGE_REPOSITORY_PATH_1, 296 m_vfsDiskCache.getRepositoryPath())); 297 CmsLog.INIT.info( 298 Messages.get().getBundle().key(Messages.INIT_IMAGE_SCALING_ENABLED_1, Boolean.valueOf(m_enabled))); 299 } 300 } 301 302 /** 303 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 304 */ 305 @Override 306 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 307 throws IOException, CmsException { 308 309 if (m_enabled) { 310 if (canSendLastModifiedHeader(resource, req, res)) { 311 // no image processing required at all 312 return; 313 } 314 // get the scale information from the request 315 CmsImageScaler scaler = new CmsImageScaler(req, m_maxScaleSize, m_maxBlurSize); 316 // load the file from the cache 317 CmsFile file = getScaledImage(cms, resource, scaler); 318 // now perform standard load operation inherited from dump loader 319 super.load(cms, file, req, res); 320 } else { 321 // scaling is disabled 322 super.load(cms, resource, req, res); 323 } 324 } 325 326 /** 327 * Returns a scaled version of the given OpenCms VFS image resource.<p> 328 * 329 * All results are cached in disk. 330 * If the scaled version does not exist in the cache, it is created. 331 * Unscaled versions of the images are also stored in the cache.<p> 332 * 333 * @param cms the current users OpenCms context 334 * @param resource the base VFS resource for the image 335 * @param scaler the configured image scaler 336 * 337 * @return a scaled version of the given OpenCms VFS image resource 338 * 339 * @throws IOException in case of errors accessing the disk based cache 340 * @throws CmsException in case of errors accessing the OpenCms VFS 341 */ 342 protected CmsFile getScaledImage(CmsObject cms, CmsResource resource, CmsImageScaler scaler) 343 throws IOException, CmsException { 344 345 String cacheParam = scaler.isValid() ? scaler.toString() : null; 346 String cacheName = m_vfsDiskCache.getCacheName(resource, cacheParam); 347 byte[] content = m_vfsDiskCache.getCacheContent(cacheName); 348 349 CmsFile file; 350 if (content != null) { 351 if (resource instanceof CmsFile) { 352 // the original file content must be modified (required e.g. for static export) 353 file = (CmsFile)resource; 354 } else { 355 // this is no file, but we don't want to use "upgrade" since we don't need to read the content from the VFS 356 file = new CmsFile(resource); 357 } 358 // save the content in the file 359 file.setContents(content); 360 } else { 361 // we must read the content from the VFS (if this has not been done yet) 362 file = cms.readFile(resource); 363 // upgrade the file (load the content) 364 if (scaler.isValid()) { 365 if (scaler.getType() == 8) { 366 // only need the focal point for mode 8 367 scaler.setFocalPoint(CmsPreviewService.readFocalPoint(cms, resource)); 368 } 369 // valid scaling parameters found, scale the content 370 content = scaler.scaleImage(file); 371 // exchange the content of the file with the scaled version 372 file.setContents(content); 373 } 374 // save the file content in the cache 375 m_vfsDiskCache.saveCacheFile(cacheName, file.getContents()); 376 } 377 return file; 378 } 379}