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.servlet.ImageServletTokenUtil;
30 import com.liferay.portal.kernel.util.FileUtil;
31 import com.liferay.portal.model.Image;
32 import com.liferay.portal.model.impl.ImageImpl;
33 import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
34 import com.liferay.portal.util.PropsKeys;
35 import com.liferay.portal.util.PropsUtil;
36
37 import java.awt.image.RenderedImage;
38
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43
44 import java.util.Arrays;
45 import java.util.Date;
46 import java.util.List;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51
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_USER_FEMALE_PORTRAIT));
96
97 if (is == null) {
98 _log.error("Default user female portrait is not available");
99 }
100
101 _defaultUserFemalePortrait = getImage(is);
102 }
103 catch (IOException ioe) {
104 _log.error(
105 "Unable to configure the default user female portrait: " +
106 ioe.getMessage());
107 }
108
109 try {
110 InputStream is = classLoader.getResourceAsStream(
111 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
112
113 if (is == null) {
114 _log.error("Default user male portrait is not available");
115 }
116
117 _defaultUserMalePortrait = getImage(is);
118 }
119 catch (IOException ioe) {
120 _log.error(
121 "Unable to configure the default user male portrait: " +
122 ioe.getMessage());
123 }
124 }
125
126 public void deleteImage(long imageId) throws SystemException {
127 try {
128 if (imageId > 0) {
129 imagePersistence.remove(imageId);
130 }
131 }
132 catch (NoSuchImageException nsie) {
133 }
134 }
135
136 public Image getCompanyLogo(long imageId) {
137 Image image = getImage(imageId);
138
139 if (image == null) {
140 image = getDefaultCompanyLogo();
141 }
142
143 return image;
144 }
145
146 public Image getDefaultCompanyLogo() {
147 return _defaultCompanyLogo;
148 }
149
150 public Image getDefaultSpacer() {
151 return _defaultSpacer;
152 }
153
154 public Image getDefaultUserFemalePortrait() {
155 return _defaultUserFemalePortrait;
156 }
157
158 public Image getDefaultUserMalePortrait() {
159 return _defaultUserMalePortrait;
160 }
161
162 public Image getImage(long imageId) {
163 try {
164 if (imageId > 0) {
165 return imagePersistence.findByPrimaryKey(imageId);
166 }
167 }
168 catch (Exception e) {
169 if (_log.isWarnEnabled()) {
170 _log.warn(
171 "Unable to get image " + imageId + ": " + e.getMessage());
172 }
173 }
174
175 return null;
176 }
177
178 public Image getImage(byte[] bytes) throws IOException {
179 return getImage(null, bytes);
180 }
181
182 public Image getImage(File file) throws IOException {
183 return getImage(new FileInputStream(file));
184 }
185
186 public Image getImage(InputStream is) throws IOException {
187 return getImage(is, null);
188 }
189
190 public Image getImageOrDefault(long imageId) {
191 Image image = getImage(imageId);
192
193 if (image == null) {
194 image = getDefaultSpacer();
195 }
196
197 return image;
198 }
199
200 public List<Image> getImages() throws SystemException {
201 return imagePersistence.findAll();
202 }
203
204 public List<Image> getImages(int start, int end) throws SystemException {
205 return imagePersistence.findAll(start, end);
206 }
207
208 public List<Image> getImagesBySize(int size) throws SystemException {
209 return imagePersistence.findBySize(size);
210 }
211
212 public boolean isNullOrDefaultSpacer(byte[] bytes) {
213 if ((bytes == null) || (bytes.length == 0) ||
214 (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
215
216 return true;
217 }
218 else {
219 return false;
220 }
221 }
222
223 public Image updateImage(long imageId, byte[] bytes)
224 throws SystemException {
225
226 try {
227 Image image = getImage(bytes);
228
229 return updateImage(
230 imageId, image.getTextObj(), image.getType(), image.getHeight(),
231 image.getWidth(), image.getSize());
232 }
233 catch (IOException ioe) {
234 throw new SystemException(ioe);
235 }
236 }
237
238 public Image updateImage(long imageId, File file)
239 throws SystemException {
240
241 try {
242 Image image = getImage(file);
243
244 return updateImage(
245 imageId, image.getTextObj(), image.getType(), image.getHeight(),
246 image.getWidth(), image.getSize());
247 }
248 catch (IOException ioe) {
249 throw new SystemException(ioe);
250 }
251 }
252
253 public Image updateImage(long imageId, InputStream is)
254 throws SystemException {
255
256 try {
257 Image image = getImage(is);
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(
269 long imageId, byte[] bytes, String type, int height, int width,
270 int size)
271 throws SystemException {
272
273 Image image = imagePersistence.fetchByPrimaryKey(imageId);
274
275 if (image == null) {
276 image = imagePersistence.create(imageId);
277 }
278
279 image.setModifiedDate(new Date());
280 image.setTextObj(bytes);
281 image.setType(type);
282 image.setHeight(height);
283 image.setWidth(width);
284 image.setSize(size);
285
286 imagePersistence.update(image, false);
287
288 ImageServletTokenUtil.resetToken(imageId);
289
290 return image;
291 }
292
293 protected Image getImage(InputStream is, byte[] bytes) throws IOException {
294 try {
295 if (is != null) {
296 bytes = FileUtil.getBytes(is);
297 }
298
299 ImageBag imageBag = ImageProcessorUtil.read(bytes);
300
301 RenderedImage renderedImage = imageBag.getRenderedImage();
302 String type = imageBag.getType();
303
304 if (renderedImage == null) {
305 throw new IOException(
306 "Unable to retreive rendered image from input stream " +
307 "with type " + type);
308 }
309
310 int height = renderedImage.getHeight();
311 int width = renderedImage.getWidth();
312 int size = bytes.length;
313
314 Image image = new ImageImpl();
315
316 image.setTextObj(bytes);
317 image.setType(type);
318 image.setHeight(height);
319 image.setWidth(width);
320 image.setSize(size);
321
322 return image;
323 }
324 finally {
325 if (is != null) {
326 try {
327 is.close();
328 }
329 catch (IOException ioe) {
330 if (_log.isWarnEnabled()) {
331 _log.warn(ioe);
332 }
333 }
334 }
335 }
336 }
337
338 private static Log _log = LogFactory.getLog(ImageLocalServiceImpl.class);
339
340 private Image _defaultSpacer;
341 private Image _defaultCompanyLogo;
342 private Image _defaultUserFemalePortrait;
343 private Image _defaultUserMalePortrait;
344
345 }