1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.NoSuchImageException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.image.Hook;
29 import com.liferay.portal.image.HookFactory;
30 import com.liferay.portal.kernel.image.ImageBag;
31 import com.liferay.portal.kernel.image.ImageProcessorUtil;
32 import com.liferay.portal.kernel.log.Log;
33 import com.liferay.portal.kernel.log.LogFactoryUtil;
34 import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
35 import com.liferay.portal.kernel.util.FileUtil;
36 import com.liferay.portal.kernel.util.PropsKeys;
37 import com.liferay.portal.model.Image;
38 import com.liferay.portal.model.impl.ImageImpl;
39 import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
40 import com.liferay.portal.util.PropsUtil;
41
42 import java.awt.image.RenderedImage;
43
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48
49 import java.util.Arrays;
50 import java.util.Date;
51 import java.util.List;
52
53
59 public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
60
61 public void afterPropertiesSet() {
62 ClassLoader classLoader = getClass().getClassLoader();
63
64 try {
65 InputStream is = classLoader.getResourceAsStream(
66 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
67
68 if (is == null) {
69 _log.error("Default spacer is not available");
70 }
71
72 _defaultSpacer = getImage(is);
73 }
74 catch (IOException ioe) {
75 _log.error(
76 "Unable to configure the default spacer: " + ioe.getMessage());
77 }
78
79 try {
80 InputStream is = classLoader.getResourceAsStream(
81 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
82
83 if (is == null) {
84 _log.error("Default company logo is not available");
85 }
86
87 _defaultCompanyLogo = getImage(is);
88 }
89 catch (IOException ioe) {
90 _log.error(
91 "Unable to configure the default company logo: " +
92 ioe.getMessage());
93 }
94
95 try {
96 InputStream is = classLoader.getResourceAsStream(
97 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
98
99 if (is == null) {
100 _log.error("Default organization logo is not available");
101 }
102
103 _defaultOrganizationLogo = getImage(is);
104 }
105 catch (IOException ioe) {
106 _log.error(
107 "Unable to configure the default organization logo: " +
108 ioe.getMessage());
109 }
110
111 try {
112 InputStream is = classLoader.getResourceAsStream(
113 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
114
115 if (is == null) {
116 _log.error("Default user female portrait is not available");
117 }
118
119 _defaultUserFemalePortrait = getImage(is);
120 }
121 catch (IOException ioe) {
122 _log.error(
123 "Unable to configure the default user female portrait: " +
124 ioe.getMessage());
125 }
126
127 try {
128 InputStream is = classLoader.getResourceAsStream(
129 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
130
131 if (is == null) {
132 _log.error("Default user male portrait is not available");
133 }
134
135 _defaultUserMalePortrait = getImage(is);
136 }
137 catch (IOException ioe) {
138 _log.error(
139 "Unable to configure the default user male portrait: " +
140 ioe.getMessage());
141 }
142 }
143
144 public void deleteImage(long imageId)
145 throws PortalException, SystemException {
146
147 try {
148 if (imageId > 0) {
149 Image image = getImage(imageId);
150
151 imagePersistence.remove(imageId);
152
153 Hook hook = HookFactory.getInstance();
154
155 hook.deleteImage(image);
156 }
157 }
158 catch (NoSuchImageException nsie) {
159 }
160 }
161
162 public Image getCompanyLogo(long imageId) {
163 Image image = getImage(imageId);
164
165 if (image == null) {
166 image = getDefaultCompanyLogo();
167 }
168
169 return image;
170 }
171
172 public Image getDefaultCompanyLogo() {
173 return _defaultCompanyLogo;
174 }
175
176 public Image getDefaultOrganizationLogo() {
177 return _defaultOrganizationLogo;
178 }
179
180 public Image getDefaultSpacer() {
181 return _defaultSpacer;
182 }
183
184 public Image getDefaultUserFemalePortrait() {
185 return _defaultUserFemalePortrait;
186 }
187
188 public Image getDefaultUserMalePortrait() {
189 return _defaultUserMalePortrait;
190 }
191
192 public Image getImage(long imageId) {
193 try {
194 if (imageId > 0) {
195 return imagePersistence.findByPrimaryKey(imageId);
196 }
197 }
198 catch (Exception e) {
199 if (_log.isWarnEnabled()) {
200 _log.warn(
201 "Unable to get image " + imageId + ": " + e.getMessage());
202 }
203 }
204
205 return null;
206 }
207
208 public Image getImage(byte[] bytes) throws IOException {
209 return getImage(null, bytes);
210 }
211
212 public Image getImage(File file) throws IOException {
213 return getImage(new FileInputStream(file));
214 }
215
216 public Image getImage(InputStream is) throws IOException {
217 return getImage(is, null);
218 }
219
220 public Image getImageOrDefault(long imageId) {
221 Image image = getImage(imageId);
222
223 if (image == null) {
224 image = getDefaultSpacer();
225 }
226
227 return image;
228 }
229
230 public List<Image> getImages() throws SystemException {
231 return imagePersistence.findAll();
232 }
233
234 public List<Image> getImages(int start, int end) throws SystemException {
235 return imagePersistence.findAll(start, end);
236 }
237
238 public List<Image> getImagesBySize(int size) throws SystemException {
239 return imagePersistence.findBySize(size);
240 }
241
242 public boolean isNullOrDefaultSpacer(byte[] bytes) {
243 if ((bytes == null) || (bytes.length == 0) ||
244 (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
245
246 return true;
247 }
248 else {
249 return false;
250 }
251 }
252
253 public Image updateImage(long imageId, byte[] bytes)
254 throws PortalException, SystemException {
255
256 try {
257 Image image = getImage(bytes);
258
259 return updateImage(
260 imageId, image.getTextObj(), image.getType(), image.getHeight(),
261 image.getWidth(), image.getSize());
262 }
263 catch (IOException ioe) {
264 throw new SystemException(ioe);
265 }
266 }
267
268 public Image updateImage(long imageId, File file)
269 throws PortalException, SystemException {
270
271 try {
272 Image image = getImage(file);
273
274 return updateImage(
275 imageId, image.getTextObj(), image.getType(), image.getHeight(),
276 image.getWidth(), image.getSize());
277 }
278 catch (IOException ioe) {
279 throw new SystemException(ioe);
280 }
281 }
282
283 public Image updateImage(long imageId, InputStream is)
284 throws PortalException, SystemException {
285
286 try {
287 Image image = getImage(is);
288
289 return updateImage(
290 imageId, image.getTextObj(), image.getType(), image.getHeight(),
291 image.getWidth(), image.getSize());
292 }
293 catch (IOException ioe) {
294 throw new SystemException(ioe);
295 }
296 }
297
298 public Image updateImage(
299 long imageId, byte[] bytes, String type, int height, int width,
300 int size)
301 throws PortalException, SystemException {
302
303 Image image = imagePersistence.fetchByPrimaryKey(imageId);
304
305 if (image == null) {
306 image = imagePersistence.create(imageId);
307 }
308
309 image.setModifiedDate(new Date());
310 image.setType(type);
311 image.setHeight(height);
312 image.setWidth(width);
313 image.setSize(size);
314
315 Hook hook = HookFactory.getInstance();
316
317 hook.updateImage(image, type, bytes);
318
319 imagePersistence.update(image, false);
320
321 ImageServletTokenUtil.resetToken(imageId);
322
323 return image;
324 }
325
326 protected Image getImage(InputStream is, byte[] bytes) throws IOException {
327 try {
328 if (is != null) {
329 bytes = FileUtil.getBytes(is);
330 }
331
332 ImageBag imageBag = ImageProcessorUtil.read(bytes);
333
334 RenderedImage renderedImage = imageBag.getRenderedImage();
335 String type = imageBag.getType();
336
337 if (renderedImage == null) {
338 throw new IOException(
339 "Unable to retreive rendered image from input stream " +
340 "with type " + type);
341 }
342
343 int height = renderedImage.getHeight();
344 int width = renderedImage.getWidth();
345 int size = bytes.length;
346
347 Image image = new ImageImpl();
348
349 image.setTextObj(bytes);
350 image.setType(type);
351 image.setHeight(height);
352 image.setWidth(width);
353 image.setSize(size);
354
355 return image;
356 }
357 finally {
358 if (is != null) {
359 try {
360 is.close();
361 }
362 catch (IOException ioe) {
363 if (_log.isWarnEnabled()) {
364 _log.warn(ioe);
365 }
366 }
367 }
368 }
369 }
370
371 private static Log _log =
372 LogFactoryUtil.getLog(ImageLocalServiceImpl.class);
373
374 private Image _defaultSpacer;
375 private Image _defaultCompanyLogo;
376 private Image _defaultOrganizationLogo;
377 private Image _defaultUserFemalePortrait;
378 private Image _defaultUserMalePortrait;
379
380 }