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