KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > types > CmsResourceTypeImage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/CmsResourceTypeImage.java,v $
3  * Date : $Date: 2006/07/20 12:38:54 $
4  * Version: $Revision: 1.15 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file.types;
33
34 import org.opencms.configuration.CmsConfigurationException;
35 import org.opencms.db.CmsSecurityManager;
36 import org.opencms.file.CmsFile;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsProperty;
39 import org.opencms.file.CmsPropertyDefinition;
40 import org.opencms.file.CmsResource;
41 import org.opencms.file.CmsResourceFilter;
42 import org.opencms.file.CmsVfsException;
43 import org.opencms.loader.CmsDumpLoader;
44 import org.opencms.loader.CmsImageLoader;
45 import org.opencms.loader.CmsImageScaler;
46 import org.opencms.main.CmsException;
47 import org.opencms.main.CmsLog;
48 import org.opencms.main.OpenCms;
49 import org.opencms.security.CmsPermissionSet;
50 import org.opencms.security.CmsSecurityException;
51 import org.opencms.util.CmsStringUtil;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.List JavaDoc;
55
56 import org.apache.commons.logging.Log;
57
58 /**
59  * Resource type descriptor for the type "image".<p>
60  *
61  * @author Alexander Kandzior
62  *
63  * @version $Revision: 1.15 $
64  *
65  * @since 6.0.0
66  */

67 public class CmsResourceTypeImage extends A_CmsResourceType {
68
69     /**
70      * A data container for image size and scale operations.<p>
71      */

72     protected class CmsImageAdjuster {
73
74         /** The image byte content. */
75         private byte[] m_content;
76
77         /** The (optional) image scaler that contains the image downscale settings. */
78         private CmsImageScaler m_imageDownScaler;
79
80         /** The image properties. */
81         private List JavaDoc m_properties;
82
83         /** The image root path. */
84         private String JavaDoc m_rootPath;
85
86         /**
87          * Creates a new image data container.<p>
88          *
89          * @param content the image byte content
90          * @param rootPath the image root path
91          * @param properties the image properties
92          * @param downScaler the (optional) image scaler that contains the image downscale settings
93          */

94         public CmsImageAdjuster(byte[] content, String JavaDoc rootPath, List JavaDoc properties, CmsImageScaler downScaler) {
95
96             m_content = content;
97             m_rootPath = rootPath;
98             m_properties = properties;
99             m_imageDownScaler = downScaler;
100         }
101
102         /**
103          * Calculates the image size and adjusts the image dimensions (if required) accoring to the configured
104          * image downscale settings.<p>
105          *
106          * The image dimensions are always calculated from the given image. The internal list of properties is updated
107          * with a value for <code>{@link CmsPropertyDefinition#PROPERTY_IMAGE_SIZE}</code> that
108          * contains the calculated image dimensions.<p>
109          */

110         public void adjust() {
111
112             CmsImageScaler scaler = new CmsImageScaler(getContent(), getRootPath());
113             if (!scaler.isValid()) {
114                 // error calculating image dimensions - this image can't be scaled or resized
115
return;
116             }
117
118             // check if the image is to big and needs to be rescaled
119
if (scaler.isDownScaleRequired(m_imageDownScaler)) {
120                 // image is to big, perform rescale operation
121
CmsImageScaler downScaler = scaler.getDownScaler(m_imageDownScaler);
122                 // perform the rescale using the adjusted size
123
m_content = downScaler.scaleImage(m_content, m_rootPath);
124                 // image size has been changed, adjust the scaler for later setting of properties
125
scaler.setHeight(downScaler.getHeight());
126                 scaler.setWidth(downScaler.getWidth());
127             }
128
129             CmsProperty p = new CmsProperty(CmsPropertyDefinition.PROPERTY_IMAGE_SIZE, null, scaler.toString());
130             // create the new property list if required (don't modify the original List)
131
List JavaDoc result = new ArrayList JavaDoc();
132             if ((m_properties != null) && (m_properties.size() > 0)) {
133                 result.addAll(m_properties);
134                 result.remove(p);
135             }
136             // add the updated property
137
result.add(p);
138             // store the changed properties
139
m_properties = result;
140         }
141
142         /**
143          * Returns the image content.<p>
144          *
145          * @return the image content
146          */

147         public byte[] getContent() {
148
149             return m_content;
150         }
151
152         /**
153          * Returns the image properties.<p>
154          *
155          * @return the image properties
156          */

157         public List JavaDoc getProperties() {
158
159             return m_properties;
160         }
161
162         /**
163          * Returns the image VFS root path.<p>
164          *
165          * @return the image VFS root path
166          */

167         public String JavaDoc getRootPath() {
168
169             return m_rootPath;
170         }
171     }
172
173     /** The log object for this class. */
174     public static final Log LOG = CmsLog.getLog(CmsResourceTypeImage.class);
175
176     /**
177      * The value for the {@link CmsPropertyDefinition#PROPERTY_IMAGE_SIZE} property if resources in
178      * a folder should never be downscaled.<p>
179      */

180     public static final String JavaDoc PROPERTY_VALUE_UNLIMITED = "unlimited";
181
182     /** The image scaler for the image downscale operation (if configured). */
183     private static CmsImageScaler m_downScaler;
184
185     /** Indicates that the static configuration of the resource type has been frozen. */
186     private static boolean m_staticFrozen;
187
188     /** The static resource loader id of this resource type. */
189     private static int m_staticLoaderId;
190
191     /** The static type id of this resource type. */
192     private static int m_staticTypeId;
193
194     /** The type id of this resource type. */
195     private static final int RESOURCE_TYPE_ID = 3;
196
197     /** The name of this resource type. */
198     private static final String JavaDoc RESOURCE_TYPE_NAME = "image";
199
200     /**
201      * Default constructor, used to initialize member variables.<p>
202      */

203     public CmsResourceTypeImage() {
204
205         super();
206         m_typeId = RESOURCE_TYPE_ID;
207         m_typeName = RESOURCE_TYPE_NAME;
208     }
209
210     /**
211      * Returns the image downscaler to use when writing an image resource to the given root path.<p>
212      *
213      * If <code>null</code> is returned, image downscaling must not be used for the resource with the given path.
214      * This may be the case if image downscaling is not configured at all, or if image downscaling has been disabled
215      * for the parent folder by setting the folders property {@link CmsPropertyDefinition#PROPERTY_IMAGE_SIZE}
216      * to the value {@link #PROPERTY_VALUE_UNLIMITED}.<p>
217      *
218      * @param cms the current OpenCms user context
219      * @param rootPath the root path of the resource to write
220      *
221      * @return the downscaler to use, or <code>null</code> if no downscaling is required for the resource
222      */

223     public static CmsImageScaler getDownScaler(CmsObject cms, String JavaDoc rootPath) {
224
225         if (m_downScaler == null) {
226             // downscaling is not configured at all
227
return null;
228         }
229         // try to read the image.size property from the parent folder
230
String JavaDoc parentFolder = CmsResource.getParentFolder(rootPath);
231         parentFolder = cms.getRequestContext().removeSiteRoot(parentFolder);
232         try {
233             CmsProperty fileSizeProperty = cms.readPropertyObject(
234                 parentFolder,
235                 CmsPropertyDefinition.PROPERTY_IMAGE_SIZE,
236                 false);
237             if (!fileSizeProperty.isNullProperty()) {
238                 // image.size property has been set
239
String JavaDoc value = fileSizeProperty.getValue().trim();
240                 if (CmsStringUtil.isNotEmpty(value)) {
241                     if (PROPERTY_VALUE_UNLIMITED.equals(value)) {
242                         // in this case no downscaling must be done
243
return null;
244                     } else {
245                         CmsImageScaler scaler = new CmsImageScaler(value);
246                         if (scaler.isValid()) {
247                             // special folder based scaler settings have been set
248
return scaler;
249                         }
250                     }
251                 }
252             }
253         } catch (CmsException e) {
254             // ignore, continue with given downScaler
255
}
256         return (CmsImageScaler)m_downScaler.clone();
257     }
258
259     /**
260      * Returns the static type id of this (default) resource type.<p>
261      *
262      * @return the static type id of this (default) resource type
263      */

264     public static int getStaticTypeId() {
265
266         return m_staticTypeId;
267     }
268
269     /**
270      * Returns the static type name of this (default) resource type.<p>
271      *
272      * @return the static type name of this (default) resource type
273      */

274     public static String JavaDoc getStaticTypeName() {
275
276         return RESOURCE_TYPE_NAME;
277     }
278
279     /**
280      * @see org.opencms.file.types.I_CmsResourceType#createResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, java.lang.String, byte[], java.util.List)
281      */

282     public CmsResource createResource(
283         CmsObject cms,
284         CmsSecurityManager securityManager,
285         String JavaDoc resourcename,
286         byte[] content,
287         List JavaDoc properties) throws CmsException {
288
289         if (CmsImageLoader.isEnabled()) {
290             String JavaDoc rootPath = cms.getRequestContext().addSiteRoot(resourcename);
291             // get the downscaler to use
292
CmsImageScaler downScaler = getDownScaler(cms, rootPath);
293             // create a new image scale adjuster
294
CmsImageAdjuster adjuster = new CmsImageAdjuster(content, rootPath, properties, downScaler);
295             // update the image scale adjuster - this will calculate the image dimensions and (optionally) downscale the size
296
adjuster.adjust();
297             // continue with the updated content and properties
298
content = adjuster.getContent();
299             properties = adjuster.getProperties();
300         }
301         return super.createResource(cms, securityManager, resourcename, content, properties);
302     }
303
304     /**
305      * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
306      */

307     public int getLoaderId() {
308
309         return m_staticLoaderId;
310     }
311
312     /**
313      * @see org.opencms.file.types.I_CmsResourceType#importResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, java.lang.String, org.opencms.file.CmsResource, byte[], java.util.List)
314      */

315     public CmsResource importResource(
316         CmsObject cms,
317         CmsSecurityManager securityManager,
318         String JavaDoc resourcename,
319         CmsResource resource,
320         byte[] content,
321         List JavaDoc properties) throws CmsException {
322
323         if (CmsImageLoader.isEnabled()) {
324             // siblings have null content in import
325
if (content != null) {
326                 // get the downscaler to use
327
CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath());
328                 // create a new image scale adjuster
329
CmsImageAdjuster adjuster = new CmsImageAdjuster(
330                     content,
331                     resource.getRootPath(),
332                     properties,
333                     downScaler);
334                 // update the image scale adjuster - this will calculate the image dimensions and (optionally) adjust the size
335
adjuster.adjust();
336                 // continue with the updated content and properties
337
content = adjuster.getContent();
338                 properties = adjuster.getProperties();
339             }
340         }
341         return super.importResource(cms, securityManager, resourcename, resource, content, properties);
342     }
343
344     /**
345      * @see org.opencms.file.types.A_CmsResourceType#initConfiguration(java.lang.String, java.lang.String, String)
346      */

347     public void initConfiguration(String JavaDoc name, String JavaDoc id, String JavaDoc className) throws CmsConfigurationException {
348
349         if ((OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) && m_staticFrozen) {
350             // configuration already frozen
351
throw new CmsConfigurationException(Messages.get().container(
352                 Messages.ERR_CONFIG_FROZEN_3,
353                 this.getClass().getName(),
354                 getStaticTypeName(),
355                 new Integer JavaDoc(getStaticTypeId())));
356         }
357
358         if (!RESOURCE_TYPE_NAME.equals(name)) {
359             // default resource type MUST have default name
360
throw new CmsConfigurationException(Messages.get().container(
361                 Messages.ERR_INVALID_RESTYPE_CONFIG_NAME_3,
362                 this.getClass().getName(),
363                 RESOURCE_TYPE_NAME,
364                 name));
365         }
366
367         // freeze the configuration
368
m_staticFrozen = true;
369
370         super.initConfiguration(RESOURCE_TYPE_NAME, id, className);
371         // set static members with values from the configuration
372
m_staticTypeId = m_typeId;
373
374         if (CmsImageLoader.isEnabled()) {
375             // the image loader is enabled, image operations are supported
376
m_staticLoaderId = CmsImageLoader.RESOURCE_LOADER_ID_IMAGE_LOADER;
377             // set the maximum size scaler
378
String JavaDoc downScaleParams = CmsImageLoader.getDownScaleParams();
379             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(downScaleParams)) {
380                 m_downScaler = new CmsImageScaler(downScaleParams);
381                 if (!m_downScaler.isValid()) {
382                     // ignore invalid parameters
383
m_downScaler = null;
384                 }
385             }
386         } else {
387             // no image operations are supported, use dump loader
388
m_staticLoaderId = CmsDumpLoader.RESOURCE_LOADER_ID;
389             // disable maximum image size operation
390
m_downScaler = null;
391         }
392     }
393
394     /**
395      * @see org.opencms.file.types.I_CmsResourceType#replaceResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, int, byte[], java.util.List)
396      */

397     public void replaceResource(
398         CmsObject cms,
399         CmsSecurityManager securityManager,
400         CmsResource resource,
401         int type,
402         byte[] content,
403         List JavaDoc properties) throws CmsException {
404
405         if (CmsImageLoader.isEnabled()) {
406             // check if the user has write access and if resource is locked
407
// done here so that no image operations are performed in case no write access is granted
408
securityManager.checkPermissions(
409                 cms.getRequestContext(),
410                 resource,
411                 CmsPermissionSet.ACCESS_WRITE,
412                 true,
413                 CmsResourceFilter.ALL);
414
415             // get the downscaler to use
416
CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath());
417             // create a new image scale adjuster
418
CmsImageAdjuster adjuster = new CmsImageAdjuster(content, resource.getRootPath(), properties, downScaler);
419             // update the image scale adjuster - this will calculate the image dimensions and (optionally) adjust the size
420
adjuster.adjust();
421             // continue with the updated content
422
content = adjuster.getContent();
423             if (adjuster.getProperties() != null) {
424                 // write properties
425
writePropertyObjects(cms, securityManager, resource, adjuster.getProperties());
426             }
427         }
428         super.replaceResource(cms, securityManager, resource, type, content, properties);
429     }
430
431     /**
432      * @see org.opencms.file.types.I_CmsResourceType#writeFile(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsFile)
433      */

434     public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource)
435     throws CmsException, CmsVfsException, CmsSecurityException {
436
437         if (CmsImageLoader.isEnabled()) {
438             // check if the user has write access and if resource is locked
439
// done here so that no image operations are performed in case no write access is granted
440
securityManager.checkPermissions(
441                 cms.getRequestContext(),
442                 resource,
443                 CmsPermissionSet.ACCESS_WRITE,
444                 true,
445                 CmsResourceFilter.ALL);
446
447             // get the downscaler to use
448
CmsImageScaler downScaler = getDownScaler(cms, resource.getRootPath());
449             // create a new image scale adjuster
450
CmsImageAdjuster adjuster = new CmsImageAdjuster(
451                 resource.getContents(),
452                 resource.getRootPath(),
453                 null,
454                 downScaler);
455             // update the image scale adjuster - this will calculate the image dimensions and (optionally) adjust the size
456
adjuster.adjust();
457             // continue with the updated content
458
resource.setContents(adjuster.getContent());
459             if (adjuster.getProperties() != null) {
460                 // write properties
461
writePropertyObjects(cms, securityManager, resource, adjuster.getProperties());
462             }
463         }
464         return super.writeFile(cms, securityManager, resource);
465     }
466 }
Popular Tags