001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
025: import com.liferay.portal.model.Image;
026: import com.liferay.portal.model.impl.ImageImpl;
027: import com.liferay.portal.service.ImageLocalServiceUtil;
028: import com.liferay.portal.util.PropsUtil;
029: import com.liferay.util.FileUtil;
030: import com.liferay.util.ImageBag;
031: import com.liferay.util.ImageUtil;
032:
033: import java.awt.image.RenderedImage;
034:
035: import java.io.File;
036: import java.io.FileInputStream;
037: import java.io.IOException;
038: import java.io.InputStream;
039:
040: import java.util.Arrays;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: /**
046: * <a href="ImageLocalUtil.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: * @author Michael Weisser
050: *
051: */
052: public class ImageLocalUtil {
053:
054: public static void deleteImage(long imageId) throws SystemException {
055: ImageLocalServiceUtil.deleteImage(imageId);
056: }
057:
058: public static Image getCompanyLogo(long imageId) {
059: Image image = getImage(imageId);
060:
061: if (image == null) {
062: image = getDefaultCompanyLogo();
063: }
064:
065: return image;
066: }
067:
068: public static Image getDefaultCompanyLogo() {
069: return _instance._defaultCompanyLogo;
070: }
071:
072: public static Image getDefaultSpacer() {
073: return _instance._defaultSpacer;
074: }
075:
076: public static Image getDefaultUserPortrait() {
077: return _instance._defaultUserPortrait;
078: }
079:
080: public static Image getImage(long imageId) {
081: try {
082: if (imageId > 0) {
083: return ImageLocalServiceUtil.getImage(imageId);
084: }
085: } catch (Exception e) {
086: if (_log.isWarnEnabled()) {
087: _log.warn("Unable to get image " + imageId + ": "
088: + e.getMessage());
089: }
090: }
091:
092: return null;
093: }
094:
095: public static Image getImage(byte[] bytes) throws IOException {
096: return _instance._getImage(bytes);
097: }
098:
099: public static Image getImage(File file) throws IOException {
100: return _instance._getImage(file);
101: }
102:
103: public static Image getImage(InputStream is) throws IOException {
104: return _instance._getImage(is);
105: }
106:
107: public static Image getImageOrDefault(long imageId) {
108: Image image = getImage(imageId);
109:
110: if (image == null) {
111: image = getDefaultSpacer();
112: }
113:
114: return image;
115: }
116:
117: public static boolean isNullOrDefaultSpacer(byte[] bytes) {
118: if ((bytes == null)
119: || (bytes.length == 0)
120: || (Arrays.equals(bytes, getDefaultSpacer()
121: .getTextObj()))) {
122:
123: return true;
124: } else {
125: return false;
126: }
127: }
128:
129: public static Image updateImage(long imageId, byte[] bytes)
130: throws SystemException {
131:
132: try {
133: Image image = getImage(bytes);
134:
135: return updateImage(imageId, image.getTextObj(), image
136: .getType(), image.getHeight(), image.getWidth(),
137: image.getSize());
138: } catch (IOException ioe) {
139: throw new SystemException(ioe);
140: }
141: }
142:
143: public static Image updateImage(long imageId, File file)
144: throws SystemException {
145:
146: try {
147: Image image = getImage(file);
148:
149: return updateImage(imageId, image.getTextObj(), image
150: .getType(), image.getHeight(), image.getWidth(),
151: image.getSize());
152: } catch (IOException ioe) {
153: throw new SystemException(ioe);
154: }
155: }
156:
157: public static Image updateImage(long imageId, InputStream is)
158: throws SystemException {
159:
160: try {
161: Image image = getImage(is);
162:
163: return updateImage(imageId, image.getTextObj(), image
164: .getType(), image.getHeight(), image.getWidth(),
165: image.getSize());
166: } catch (IOException ioe) {
167: throw new SystemException(ioe);
168: }
169: }
170:
171: public static Image updateImage(long imageId, byte[] bytes,
172: String type, int height, int width, int size)
173: throws SystemException {
174:
175: Image image = ImageLocalServiceUtil.updateImage(imageId, bytes,
176: type, height, width, size);
177:
178: ImageServletTokenUtil.resetToken(imageId);
179:
180: return image;
181: }
182:
183: private ImageLocalUtil() {
184: ClassLoader classLoader = getClass().getClassLoader();
185:
186: try {
187: InputStream is = classLoader.getResourceAsStream(PropsUtil
188: .get(PropsUtil.IMAGE_DEFAULT_SPACER));
189:
190: if (is == null) {
191: _log.error("Default spacer is not available");
192: }
193:
194: _defaultSpacer = _getImage(is);
195: } catch (IOException ioe) {
196: _log.error("Unable to configure the default spacer: "
197: + ioe.getMessage());
198: }
199:
200: try {
201: InputStream is = classLoader.getResourceAsStream(PropsUtil
202: .get(PropsUtil.IMAGE_DEFAULT_COMPANY_LOGO));
203:
204: if (is == null) {
205: _log.error("Default company logo is not available");
206: }
207:
208: _defaultCompanyLogo = _getImage(is);
209: } catch (IOException ioe) {
210: _log.error("Unable to configure the default company logo: "
211: + ioe.getMessage());
212: }
213:
214: try {
215: InputStream is = classLoader.getResourceAsStream(PropsUtil
216: .get(PropsUtil.IMAGE_DEFAULT_USER_PORTRAIT));
217:
218: if (is == null) {
219: _log.error("Default user portrait is not available");
220: }
221:
222: _defaultUserPortrait = _getImage(is);
223: } catch (IOException ioe) {
224: _log.error("Unable to configure the default use portrait: "
225: + ioe.getMessage());
226: }
227: }
228:
229: private Image _getImage(byte[] bytes) throws IOException {
230: return _getImage(null, bytes);
231: }
232:
233: private Image _getImage(File file) throws IOException {
234: return _getImage(new FileInputStream(file));
235: }
236:
237: private Image _getImage(InputStream is) throws IOException {
238: return _getImage(is, null);
239: }
240:
241: private Image _getImage(InputStream is, byte[] bytes)
242: throws IOException {
243: try {
244: if (is != null) {
245: bytes = FileUtil.getBytes(is);
246: }
247:
248: ImageBag imageBag = ImageUtil.read(bytes);
249:
250: RenderedImage renderedImage = imageBag.getRenderedImage();
251: String type = imageBag.getType();
252:
253: if (renderedImage == null) {
254: throw new IOException(
255: "Unable to retreive rendered image from input stream "
256: + "with type " + type);
257: }
258:
259: int height = renderedImage.getHeight();
260: int width = renderedImage.getWidth();
261: int size = bytes.length;
262:
263: Image image = new ImageImpl();
264:
265: image.setTextObj(bytes);
266: image.setType(type);
267: image.setHeight(height);
268: image.setWidth(width);
269: image.setSize(size);
270:
271: return image;
272: } finally {
273: if (is != null) {
274: try {
275: is.close();
276: } catch (IOException ioe) {
277: if (_log.isWarnEnabled()) {
278: _log.warn(ioe);
279: }
280: }
281: }
282: }
283: }
284:
285: private static Log _log = LogFactory.getLog(ImageLocalUtil.class);
286:
287: private static ImageLocalUtil _instance = new ImageLocalUtil();
288:
289: private Image _defaultSpacer;
290: private Image _defaultCompanyLogo;
291: private Image _defaultUserPortrait;
292:
293: }
|