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