1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchImageException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.image.ImageBag;
28  import com.liferay.portal.kernel.image.ImageProcessorUtil;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
32  import com.liferay.portal.kernel.util.FileUtil;
33  import com.liferay.portal.model.Image;
34  import com.liferay.portal.model.impl.ImageImpl;
35  import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
36  import com.liferay.portal.util.PropsKeys;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import java.awt.image.RenderedImage;
40  
41  import java.io.File;
42  import java.io.FileInputStream;
43  import java.io.IOException;
44  import java.io.InputStream;
45  
46  import java.util.Arrays;
47  import java.util.Date;
48  import java.util.List;
49  
50  /**
51   * <a href="ImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
57  
58      public void afterPropertiesSet() {
59          ClassLoader classLoader = getClass().getClassLoader();
60  
61          try {
62              InputStream is = classLoader.getResourceAsStream(
63                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
64  
65              if (is == null) {
66                  _log.error("Default spacer is not available");
67              }
68  
69              _defaultSpacer = getImage(is);
70          }
71          catch (IOException ioe) {
72              _log.error(
73                  "Unable to configure the default spacer: " + ioe.getMessage());
74          }
75  
76          try {
77              InputStream is = classLoader.getResourceAsStream(
78                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
79  
80              if (is == null) {
81                  _log.error("Default company logo is not available");
82              }
83  
84              _defaultCompanyLogo = getImage(is);
85          }
86          catch (IOException ioe) {
87              _log.error(
88                  "Unable to configure the default company logo: " +
89                      ioe.getMessage());
90          }
91  
92          try {
93              InputStream is = classLoader.getResourceAsStream(
94                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
95  
96              if (is == null) {
97                  _log.error("Default user female portrait is not available");
98              }
99  
100             _defaultUserFemalePortrait = getImage(is);
101         }
102         catch (IOException ioe) {
103             _log.error(
104                 "Unable to configure the default user female portrait: " +
105                     ioe.getMessage());
106         }
107 
108         try {
109             InputStream is = classLoader.getResourceAsStream(
110                 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
111 
112             if (is == null) {
113                 _log.error("Default user male portrait is not available");
114             }
115 
116             _defaultUserMalePortrait = getImage(is);
117         }
118         catch (IOException ioe) {
119             _log.error(
120                 "Unable to configure the default user male portrait: " +
121                     ioe.getMessage());
122         }
123     }
124 
125     public void deleteImage(long imageId) throws SystemException {
126         try {
127             if (imageId > 0) {
128                 imagePersistence.remove(imageId);
129             }
130         }
131         catch (NoSuchImageException nsie) {
132         }
133     }
134 
135     public Image getCompanyLogo(long imageId) {
136         Image image = getImage(imageId);
137 
138         if (image == null) {
139             image = getDefaultCompanyLogo();
140         }
141 
142         return image;
143     }
144 
145     public Image getDefaultCompanyLogo() {
146         return _defaultCompanyLogo;
147     }
148 
149     public Image getDefaultSpacer() {
150         return _defaultSpacer;
151     }
152 
153     public Image getDefaultUserFemalePortrait() {
154         return _defaultUserFemalePortrait;
155     }
156 
157     public Image getDefaultUserMalePortrait() {
158         return _defaultUserMalePortrait;
159     }
160 
161     public Image getImage(long imageId) {
162         try {
163             if (imageId > 0) {
164                 return imagePersistence.findByPrimaryKey(imageId);
165             }
166         }
167         catch (Exception e) {
168             if (_log.isWarnEnabled()) {
169                 _log.warn(
170                     "Unable to get image " + imageId + ": " + e.getMessage());
171             }
172         }
173 
174         return null;
175     }
176 
177     public Image getImage(byte[] bytes) throws IOException {
178         return getImage(null, bytes);
179     }
180 
181     public Image getImage(File file) throws IOException {
182         return getImage(new FileInputStream(file));
183     }
184 
185     public Image getImage(InputStream is) throws IOException {
186         return getImage(is, null);
187     }
188 
189     public Image getImageOrDefault(long imageId) {
190         Image image = getImage(imageId);
191 
192         if (image == null) {
193             image = getDefaultSpacer();
194         }
195 
196         return image;
197     }
198 
199     public List<Image> getImages() throws SystemException {
200         return imagePersistence.findAll();
201     }
202 
203     public List<Image> getImages(int start, int end) throws SystemException {
204         return imagePersistence.findAll(start, end);
205     }
206 
207     public List<Image> getImagesBySize(int size) throws SystemException {
208         return imagePersistence.findBySize(size);
209     }
210 
211     public boolean isNullOrDefaultSpacer(byte[] bytes) {
212         if ((bytes == null) || (bytes.length == 0) ||
213             (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
214 
215             return true;
216         }
217         else {
218             return false;
219         }
220     }
221 
222     public Image updateImage(long imageId, byte[] bytes)
223         throws SystemException {
224 
225         try {
226             Image image = getImage(bytes);
227 
228             return updateImage(
229                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
230                 image.getWidth(), image.getSize());
231         }
232         catch (IOException ioe) {
233             throw new SystemException(ioe);
234         }
235     }
236 
237     public Image updateImage(long imageId, File file)
238         throws SystemException {
239 
240         try {
241             Image image = getImage(file);
242 
243             return updateImage(
244                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
245                 image.getWidth(), image.getSize());
246         }
247         catch (IOException ioe) {
248             throw new SystemException(ioe);
249         }
250     }
251 
252     public Image updateImage(long imageId, InputStream is)
253         throws SystemException {
254 
255         try {
256             Image image = getImage(is);
257 
258             return updateImage(
259                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
260                 image.getWidth(), image.getSize());
261         }
262         catch (IOException ioe) {
263             throw new SystemException(ioe);
264         }
265     }
266 
267     public Image updateImage(
268             long imageId, byte[] bytes, String type, int height, int width,
269             int size)
270         throws SystemException {
271 
272         Image image = imagePersistence.fetchByPrimaryKey(imageId);
273 
274         if (image == null) {
275             image = imagePersistence.create(imageId);
276         }
277 
278         image.setModifiedDate(new Date());
279         image.setTextObj(bytes);
280         image.setType(type);
281         image.setHeight(height);
282         image.setWidth(width);
283         image.setSize(size);
284 
285         imagePersistence.update(image, false);
286 
287         ImageServletTokenUtil.resetToken(imageId);
288 
289         return image;
290     }
291 
292     protected Image getImage(InputStream is, byte[] bytes) throws IOException {
293         try {
294             if (is != null) {
295                 bytes = FileUtil.getBytes(is);
296             }
297 
298             ImageBag imageBag = ImageProcessorUtil.read(bytes);
299 
300             RenderedImage renderedImage = imageBag.getRenderedImage();
301             String type = imageBag.getType();
302 
303             if (renderedImage == null) {
304                 throw new IOException(
305                     "Unable to retreive rendered image from input stream " +
306                         "with type " + type);
307             }
308 
309             int height = renderedImage.getHeight();
310             int width = renderedImage.getWidth();
311             int size = bytes.length;
312 
313             Image image = new ImageImpl();
314 
315             image.setTextObj(bytes);
316             image.setType(type);
317             image.setHeight(height);
318             image.setWidth(width);
319             image.setSize(size);
320 
321             return image;
322         }
323         finally {
324             if (is != null) {
325                 try {
326                     is.close();
327                 }
328                 catch (IOException ioe) {
329                     if (_log.isWarnEnabled()) {
330                         _log.warn(ioe);
331                     }
332                 }
333             }
334         }
335     }
336 
337     private static Log _log =
338          LogFactoryUtil.getLog(ImageLocalServiceImpl.class);
339 
340     private Image _defaultSpacer;
341     private Image _defaultCompanyLogo;
342     private Image _defaultUserFemalePortrait;
343     private Image _defaultUserMalePortrait;
344 
345 }