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