1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
56  
57      public void afterPropertiesSet() {
58          ClassLoader classLoader = getClass().getClassLoader();
59  
60          try {
61              InputStream is = classLoader.getResourceAsStream(
62                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
63  
64              if (is == null) {
65                  _log.error("Default spacer is not available");
66              }
67  
68              _defaultSpacer = getImage(is);
69          }
70          catch (IOException ioe) {
71              _log.error(
72                  "Unable to configure the default spacer: " + ioe.getMessage());
73          }
74  
75          try {
76              InputStream is = classLoader.getResourceAsStream(
77                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
78  
79              if (is == null) {
80                  _log.error("Default company logo is not available");
81              }
82  
83              _defaultCompanyLogo = getImage(is);
84          }
85          catch (IOException ioe) {
86              _log.error(
87                  "Unable to configure the default company logo: " +
88                      ioe.getMessage());
89          }
90  
91          try {
92              InputStream is = classLoader.getResourceAsStream(
93                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
94  
95              if (is == null) {
96                  _log.error("Default user female portrait is not available");
97              }
98  
99              _defaultUserFemalePortrait = getImage(is);
100         }
101         catch (IOException ioe) {
102             _log.error(
103                 "Unable to configure the default user female portrait: " +
104                     ioe.getMessage());
105         }
106 
107         try {
108             InputStream is = classLoader.getResourceAsStream(
109                 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
110 
111             if (is == null) {
112                 _log.error("Default user male portrait is not available");
113             }
114 
115             _defaultUserMalePortrait = getImage(is);
116         }
117         catch (IOException ioe) {
118             _log.error(
119                 "Unable to configure the default user male portrait: " +
120                     ioe.getMessage());
121         }
122     }
123 
124     public void deleteImage(long imageId) throws SystemException {
125         try {
126             if (imageId > 0) {
127                 imagePersistence.remove(imageId);
128             }
129         }
130         catch (NoSuchImageException nsie) {
131         }
132     }
133 
134     public Image getCompanyLogo(long imageId) {
135         Image image = getImage(imageId);
136 
137         if (image == null) {
138             image = getDefaultCompanyLogo();
139         }
140 
141         return image;
142     }
143 
144     public Image getDefaultCompanyLogo() {
145         return _defaultCompanyLogo;
146     }
147 
148     public Image getDefaultSpacer() {
149         return _defaultSpacer;
150     }
151 
152     public Image getDefaultUserFemalePortrait() {
153         return _defaultUserFemalePortrait;
154     }
155 
156     public Image getDefaultUserMalePortrait() {
157         return _defaultUserMalePortrait;
158     }
159 
160     public Image getImage(long imageId) {
161         try {
162             if (imageId > 0) {
163                 return imagePersistence.findByPrimaryKey(imageId);
164             }
165         }
166         catch (Exception e) {
167             if (_log.isWarnEnabled()) {
168                 _log.warn(
169                     "Unable to get image " + imageId + ": " + e.getMessage());
170             }
171         }
172 
173         return null;
174     }
175 
176     public Image getImage(byte[] bytes) throws IOException {
177         return getImage(null, bytes);
178     }
179 
180     public Image getImage(File file) throws IOException {
181         return getImage(new FileInputStream(file));
182     }
183 
184     public Image getImage(InputStream is) throws IOException {
185         return getImage(is, null);
186     }
187 
188     public Image getImageOrDefault(long imageId) {
189         Image image = getImage(imageId);
190 
191         if (image == null) {
192             image = getDefaultSpacer();
193         }
194 
195         return image;
196     }
197 
198     public List<Image> getImages() throws SystemException {
199         return imagePersistence.findAll();
200     }
201 
202     public List<Image> getImages(int start, int end) throws SystemException {
203         return imagePersistence.findAll(start, end);
204     }
205 
206     public List<Image> getImagesBySize(int size) throws SystemException {
207         return imagePersistence.findBySize(size);
208     }
209 
210     public boolean isNullOrDefaultSpacer(byte[] bytes) {
211         if ((bytes == null) || (bytes.length == 0) ||
212             (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
213 
214             return true;
215         }
216         else {
217             return false;
218         }
219     }
220 
221     public Image updateImage(long imageId, byte[] bytes)
222         throws SystemException {
223 
224         try {
225             Image image = getImage(bytes);
226 
227             return updateImage(
228                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
229                 image.getWidth(), image.getSize());
230         }
231         catch (IOException ioe) {
232             throw new SystemException(ioe);
233         }
234     }
235 
236     public Image updateImage(long imageId, File file)
237         throws SystemException {
238 
239         try {
240             Image image = getImage(file);
241 
242             return updateImage(
243                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
244                 image.getWidth(), image.getSize());
245         }
246         catch (IOException ioe) {
247             throw new SystemException(ioe);
248         }
249     }
250 
251     public Image updateImage(long imageId, InputStream is)
252         throws SystemException {
253 
254         try {
255             Image image = getImage(is);
256 
257             return updateImage(
258                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
259                 image.getWidth(), image.getSize());
260         }
261         catch (IOException ioe) {
262             throw new SystemException(ioe);
263         }
264     }
265 
266     public Image updateImage(
267             long imageId, byte[] bytes, String type, int height, int width,
268             int size)
269         throws SystemException {
270 
271         Image image = imagePersistence.fetchByPrimaryKey(imageId);
272 
273         if (image == null) {
274             image = imagePersistence.create(imageId);
275         }
276 
277         image.setModifiedDate(new Date());
278         image.setTextObj(bytes);
279         image.setType(type);
280         image.setHeight(height);
281         image.setWidth(width);
282         image.setSize(size);
283 
284         imagePersistence.update(image, false);
285 
286         ImageServletTokenUtil.resetToken(imageId);
287 
288         return image;
289     }
290 
291     protected Image getImage(InputStream is, byte[] bytes) throws IOException {
292         try {
293             if (is != null) {
294                 bytes = FileUtil.getBytes(is);
295             }
296 
297             ImageBag imageBag = ImageProcessorUtil.read(bytes);
298 
299             RenderedImage renderedImage = imageBag.getRenderedImage();
300             String type = imageBag.getType();
301 
302             if (renderedImage == null) {
303                 throw new IOException(
304                     "Unable to retreive rendered image from input stream " +
305                         "with type " + type);
306             }
307 
308             int height = renderedImage.getHeight();
309             int width = renderedImage.getWidth();
310             int size = bytes.length;
311 
312             Image image = new ImageImpl();
313 
314             image.setTextObj(bytes);
315             image.setType(type);
316             image.setHeight(height);
317             image.setWidth(width);
318             image.setSize(size);
319 
320             return image;
321         }
322         finally {
323             if (is != null) {
324                 try {
325                     is.close();
326                 }
327                 catch (IOException ioe) {
328                     if (_log.isWarnEnabled()) {
329                         _log.warn(ioe);
330                     }
331                 }
332             }
333         }
334     }
335 
336     private static Log _log =
337         LogFactoryUtil.getLog(ImageLocalServiceImpl.class);
338 
339     private Image _defaultSpacer;
340     private Image _defaultCompanyLogo;
341     private Image _defaultUserFemalePortrait;
342     private Image _defaultUserMalePortrait;
343 
344 }