1 31 32 package org.opencms.loader; 33 34 import org.opencms.cache.CmsVfsNameBasedDiskCache; 35 import org.opencms.file.CmsFile; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsResource; 38 import org.opencms.main.CmsException; 39 import org.opencms.main.CmsLog; 40 import org.opencms.main.OpenCms; 41 import org.opencms.util.CmsStringUtil; 42 43 import java.io.IOException ; 44 import java.util.Map ; 45 import java.util.TreeMap ; 46 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 49 50 import org.apache.commons.logging.Log; 51 52 68 public class CmsImageLoader extends CmsDumpLoader { 69 70 71 public static final String CONFIGURATION_DOWNSCALE = "image.scaling.downscale"; 72 73 74 public static final String CONFIGURATION_IMAGE_FOLDER = "image.folder"; 75 76 77 public static final String CONFIGURATION_MAX_BLUR_SIZE = "image.scaling.maxblursize"; 78 79 80 public static final String CONFIGURATION_MAX_SCALE_SIZE = "image.scaling.maxsize"; 81 82 83 public static final String CONFIGURATION_SCALING_ENABLED = "image.scaling.enabled"; 84 85 86 public static final String IMAGE_REPOSITORY_DEFAULT = "/WEB-INF/imagecache/"; 87 88 89 public static final int RESOURCE_LOADER_ID_IMAGE_LOADER = 2; 90 91 92 protected static final Log LOG = CmsLog.getLog(CmsImageLoader.class); 93 94 95 protected static String m_downScaleParams; 96 97 98 protected static boolean m_enabled; 99 100 101 protected static int m_maxBlurSize = CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE; 102 103 104 protected static CmsVfsNameBasedDiskCache m_vfsDiskCache; 105 106 107 protected String m_imageRepositoryFolder; 108 109 110 protected int m_maxScaleSize = CmsImageScaler.SCALE_DEFAULT_MAX_SIZE; 111 112 115 public CmsImageLoader() { 116 117 super(); 118 } 119 120 128 public static String getDownScaleParams() { 129 130 return m_downScaleParams; 131 } 132 133 139 public static String getImageRepositoryPath() { 140 141 return m_vfsDiskCache.getRepositoryPath(); 142 } 143 144 152 public static int getMaxBlurSize() { 153 154 return m_maxBlurSize; 155 } 156 157 172 public static boolean isEnabled() { 173 174 return m_enabled; 175 } 176 177 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(paramValue, CmsImageScaler.SCALE_DEFAULT_MAX_SIZE, paramName); 191 } 192 if (CONFIGURATION_MAX_BLUR_SIZE.equals(paramName)) { 193 m_maxBlurSize = CmsStringUtil.getIntValue( 194 paramValue, 195 CmsImageScaler.SCALE_DEFAULT_MAX_BLUR_SIZE, 196 paramName); 197 } 198 if (CONFIGURATION_DOWNSCALE.equals(paramName)) { 199 m_downScaleParams = paramValue.trim(); 200 } 201 } 202 super.addConfigurationParameter(paramName, paramValue); 203 } 204 205 208 public void destroy() { 209 210 m_enabled = false; 211 m_imageRepositoryFolder = null; 212 m_vfsDiskCache = null; 213 } 214 215 218 public Map getConfiguration() { 219 220 Map config = super.getConfiguration(); 221 TreeMap result = new TreeMap (); 222 if (config != null) { 223 result.putAll(config); 224 } 225 result.put(CONFIGURATION_SCALING_ENABLED, String.valueOf(m_enabled)); 226 result.put(CONFIGURATION_IMAGE_FOLDER, m_imageRepositoryFolder); 227 return result; 228 } 229 230 233 public int getLoaderId() { 234 235 return RESOURCE_LOADER_ID_IMAGE_LOADER; 236 } 237 238 241 public void initConfiguration() { 242 243 if (CmsStringUtil.isEmpty(m_imageRepositoryFolder)) { 244 m_imageRepositoryFolder = IMAGE_REPOSITORY_DEFAULT; 245 } 246 if (m_vfsDiskCache == null) { 248 m_vfsDiskCache = new CmsVfsNameBasedDiskCache( 249 OpenCms.getSystemInfo().getWebApplicationRfsPath(), 250 m_imageRepositoryFolder); 251 } 252 if (CmsLog.INIT.isInfoEnabled()) { 254 CmsLog.INIT.info(Messages.get().getBundle().key( 255 Messages.INIT_IMAGE_REPOSITORY_PATH_1, 256 m_vfsDiskCache.getRepositoryPath())); 257 CmsLog.INIT.info(Messages.get().getBundle().key( 258 Messages.INIT_IMAGE_SCALING_ENABLED_1, 259 Boolean.valueOf(m_enabled))); 260 } 261 } 262 263 266 public void load(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 267 throws IOException , CmsException { 268 269 if (m_enabled) { 270 if (canSendLastModifiedHeader(resource, req, res)) { 271 return; 273 } 274 CmsImageScaler scaler = new CmsImageScaler(req, m_maxScaleSize, m_maxBlurSize); 276 CmsFile file = getScaledImage(cms, resource, scaler); 278 super.load(cms, file, req, res); 280 } else { 281 super.load(cms, resource, req, res); 283 } 284 } 285 286 302 protected CmsFile getScaledImage(CmsObject cms, CmsResource resource, CmsImageScaler scaler) 303 throws IOException , CmsException { 304 305 String cacheParam = scaler.isValid() ? scaler.toString() : null; 306 String cacheName = m_vfsDiskCache.getCacheName(resource, cacheParam); 307 byte[] content = m_vfsDiskCache.getCacheContent(cacheName); 308 309 CmsFile file; 310 if (content != null) { 311 if (resource instanceof CmsFile) { 312 file = (CmsFile)resource; 314 } else { 315 file = new CmsFile(resource); 317 } 318 file.setContents(content); 320 } else { 321 file = CmsFile.upgrade(resource, cms); 323 if (scaler.isValid()) { 325 content = scaler.scaleImage(file); 327 file.setContents(content); 329 } 330 m_vfsDiskCache.saveCacheFile(cacheName, file.getContents()); 332 } 333 return file; 334 } 335 } | Popular Tags |