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 /** 106 * Returns the image down scale parameters, 107 * which is set with the {@link #CONFIGURATION_DOWNSCALE} configuration option.<p> 108 * 109 * If no down scale parameters have been set in the configuration, this will return <code>null</code>. 110 * 111 * @return the image down scale parameters 112 */ 113 public static String getDownScaleParams() { 114 115 return m_downScaleParams; 116 } 117 118 /** 119 * Returns the path of the image cache repository folder in the RFS, 120 * which is set with the {@link #CONFIGURATION_IMAGE_FOLDER} configuration option.<p> 121 * 122 * @return the path of the image cache repository folder in the RFS 123 */ 124 public static String getImageRepositoryPath() { 125 126 return m_vfsDiskCache.getRepositoryPath(); 127 } 128 129 /** 130 * The maximum blur size for image re-scale operations, 131 * which is set with the {@link #CONFIGURATION_MAX_BLUR_SIZE} configuration option.<p> 132 * 133 * The default is 2500 * 2500 pixel.<p> 134 * 135 * @return the maximum blur size for image re-scale operations 136 */ 137 public static int getMaxBlurSize() { 138 139 return m_maxBlurSize; 140 } 141 142 /** 143 * Returns <code>true</code> if the image scaling and processing capabilities for the 144 * OpenCms VFS images have been enabled, <code>false</code> if not.<p> 145 * 146 * Image scaling is enabled by setting the loader parameter <code>image.scaling.enabled</code> 147 * to the value <code>true</code> in the configuration file <code>opencms-vfs.xml</code>.<p> 148 * 149 * Enabling image processing in OpenCms may require several additional configuration steps 150 * on the server running OpenCms, especially in UNIX systems. Here it is often required to have an X window server 151 * configured and accessible so that the required Java ImageIO operations work. 152 * Therefore the image scaling capabilities in OpenCms are disabled by default.<p> 153 * 154 * @return <code>true</code> if the image scaling and processing capabilities for the 155 * OpenCms VFS images have been enabled 156 */ 157 public static boolean isEnabled() { 158 159 return m_enabled; 160 } 161 162 /** The name of the configured image cache repository. */ 163 protected String m_imageRepositoryFolder; 164 165 /** The maximum image size (width or height) to allow when up scaling an image using request parameters. */ 166 protected int m_maxScaleSize = CmsImageScaler.SCALE_DEFAULT_MAX_SIZE; 167 168 /** 169 * Creates a new image loader.<p> 170 */ 171 public CmsImageLoader() { 172 173 super(); 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 * Returns a scaled version of the given OpenCms VFS image resource.<p> 277 * 278 * All results are cached in disk. 279 * If the scaled version does not exist in the cache, it is created. 280 * Unscaled versions of the images are also stored in the cache.<p> 281 * 282 * @param cms the current users OpenCms context 283 * @param resource the base VFS resource for the image 284 * @param scaler the configured image scaler 285 * 286 * @return a scaled version of the given OpenCms VFS image resource 287 * 288 * @throws IOException in case of errors accessing the disk based cache 289 * @throws CmsException in case of errors accessing the OpenCms VFS 290 */ 291 protected CmsFile getScaledImage(CmsObject cms, CmsResource resource, CmsImageScaler scaler) 292 throws IOException, CmsException { 293 294 String cacheParam = scaler.isValid() ? scaler.toString() : null; 295 String cacheName = m_vfsDiskCache.getCacheName(resource, cacheParam); 296 byte[] content = m_vfsDiskCache.getCacheContent(cacheName); 297 298 CmsFile file; 299 if (content != null) { 300 if (resource instanceof CmsFile) { 301 // the original file content must be modified (required e.g. for static export) 302 file = (CmsFile)resource; 303 } else { 304 // this is no file, but we don't want to use "upgrade" since we don't need to read the content from the VFS 305 file = new CmsFile(resource); 306 } 307 // save the content in the file 308 file.setContents(content); 309 } else { 310 // we must read the content from the VFS (if this has not been done yet) 311 file = cms.readFile(resource); 312 // upgrade the file (load the content) 313 if (scaler.isValid()) { 314 if (scaler.getType() == 8) { 315 // only need the focal point for mode 8 316 scaler.setFocalPoint(CmsPreviewService.readFocalPoint(cms, resource)); 317 } 318 // valid scaling parameters found, scale the content 319 content = scaler.scaleImage(file); 320 // exchange the content of the file with the scaled version 321 file.setContents(content); 322 } 323 // save the file content in the cache 324 m_vfsDiskCache.saveCacheFile(cacheName, file.getContents()); 325 } 326 return file; 327 } 328 329 /** 330 * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration() 331 */ 332 @Override 333 public void initConfiguration() { 334 335 if (CmsStringUtil.isEmpty(m_imageRepositoryFolder)) { 336 m_imageRepositoryFolder = IMAGE_REPOSITORY_DEFAULT; 337 } 338 // initialize the image cache 339 if (m_vfsDiskCache == null) { 340 m_vfsDiskCache = new CmsVfsNameBasedDiskCache( 341 OpenCms.getSystemInfo().getWebApplicationRfsPath(), 342 m_imageRepositoryFolder); 343 } 344 OpenCms.addCmsEventListener(this); 345 // output setup information 346 if (CmsLog.INIT.isInfoEnabled()) { 347 CmsLog.INIT.info( 348 Messages.get().getBundle().key( 349 Messages.INIT_IMAGE_REPOSITORY_PATH_1, 350 m_vfsDiskCache.getRepositoryPath())); 351 CmsLog.INIT.info( 352 Messages.get().getBundle().key(Messages.INIT_IMAGE_SCALING_ENABLED_1, Boolean.valueOf(m_enabled))); 353 } 354 } 355 356 /** 357 * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 358 */ 359 @Override 360 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 361 throws IOException, CmsException { 362 363 if (m_enabled) { 364 if (canSendLastModifiedHeader(resource, req, res)) { 365 // no image processing required at all 366 return; 367 } 368 // get the scale information from the request 369 CmsImageScaler scaler = new CmsImageScaler(req, m_maxScaleSize, m_maxBlurSize); 370 // load the file from the cache 371 CmsFile file = getScaledImage(cms, resource, scaler); 372 // now perform standard load operation inherited from dump loader 373 super.load(cms, file, req, res); 374 } else { 375 // scaling is disabled 376 super.load(cms, resource, req, res); 377 } 378 } 379}