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.servlet;
022:
023: import com.liferay.portal.NoSuchImageException;
024: import com.liferay.portal.kernel.servlet.HttpHeaders;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.model.Image;
028: import com.liferay.portal.model.impl.ImageImpl;
029: import com.liferay.portal.service.impl.ImageLocalUtil;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portlet.imagegallery.model.IGImage;
032: import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
033: import com.liferay.util.servlet.ServletResponseUtil;
034:
035: import java.io.IOException;
036:
037: import java.util.Date;
038:
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServlet;
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: /**
048: * <a href="ImageServlet.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: * @author Brett Randall
052: *
053: */
054: public class ImageServlet extends HttpServlet {
055:
056: public void service(HttpServletRequest req, HttpServletResponse res)
057: throws IOException, ServletException {
058:
059: long lastModified = getLastModified(req);
060:
061: if (lastModified > 0) {
062: long ifModifiedSince = req
063: .getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
064:
065: if ((ifModifiedSince > 0)
066: && (ifModifiedSince == lastModified)) {
067: res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
068:
069: return;
070: }
071: }
072:
073: //res.addHeader(HttpHeaders.CACHE_CONTROL, "max-age=0");
074:
075: if (lastModified > 0) {
076: res.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
077: }
078:
079: try {
080: writeImage(req, res);
081: } catch (NoSuchImageException nsie) {
082: PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND,
083: nsie, req, res);
084: }
085: }
086:
087: protected Image getDefaultImage(HttpServletRequest req, long imageId)
088: throws NoSuchImageException {
089:
090: String path = GetterUtil.getString(req.getPathInfo());
091:
092: if (path.startsWith("/company_logo")) {
093: return ImageLocalUtil.getDefaultCompanyLogo();
094: } else if (path.startsWith("/user_portrait")) {
095: return ImageLocalUtil.getDefaultUserPortrait();
096: } else {
097: throw new NoSuchImageException(
098: "No default image exists for " + imageId);
099: }
100: }
101:
102: protected Image getImage(HttpServletRequest req, boolean getDefault)
103: throws NoSuchImageException {
104:
105: long imageId = getImageId(req);
106:
107: Image image = null;
108:
109: if (imageId > 0) {
110: image = ImageLocalUtil.getImage(imageId);
111: } else {
112: String uuid = ParamUtil.getString(req, "uuid");
113: long groupId = ParamUtil.getLong(req, "groupId");
114:
115: try {
116: IGImage igImage = IGImageLocalServiceUtil
117: .getImageByUuidAndGroupId(uuid, groupId);
118:
119: image = ImageLocalUtil.getImage(igImage
120: .getLargeImageId());
121: } catch (Exception e) {
122: }
123: }
124:
125: if (getDefault) {
126: if (image == null) {
127: if (_log.isWarnEnabled()) {
128: _log.warn("Get a default image for " + imageId);
129: }
130:
131: image = getDefaultImage(req, imageId);
132: }
133: }
134:
135: return image;
136: }
137:
138: protected long getImageId(HttpServletRequest req) {
139:
140: // The image id may be passed in as image_id, img_id, or i_id
141:
142: long imageId = ParamUtil.getLong(req, "image_id");
143:
144: if (imageId <= 0) {
145: imageId = ParamUtil.getLong(req, "img_id");
146:
147: if (imageId <= 0) {
148: imageId = ParamUtil.getLong(req, "i_id");
149: }
150: }
151:
152: return imageId;
153: }
154:
155: protected long getLastModified(HttpServletRequest req) {
156: try {
157: Image image = getImage(req, false);
158:
159: if (image == null) {
160: return -1;
161: }
162:
163: Date modifiedDate = image.getModifiedDate();
164:
165: if (modifiedDate == null) {
166: modifiedDate = PortalUtil.UP_TIME;
167: }
168:
169: // Round down and remove milliseconds
170:
171: return (modifiedDate.getTime() / 1000) * 1000;
172: } catch (Exception e) {
173: _log.error(e, e);
174:
175: return -1;
176: }
177: }
178:
179: protected void writeImage(HttpServletRequest req,
180: HttpServletResponse res) throws IOException,
181: NoSuchImageException, ServletException {
182:
183: Image image = getImage(req, true);
184:
185: if (image == null) {
186: throw new NoSuchImageException("Image is null");
187: } else {
188: if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
189: res.setContentType("image/" + image.getType());
190: }
191:
192: try {
193: ServletResponseUtil.write(res, image.getTextObj());
194: } catch (Exception e) {
195: if (_log.isWarnEnabled()) {
196: _log.warn(e, e);
197: }
198: }
199: }
200: }
201:
202: private static Log _log = LogFactory.getLog(ImageServlet.class);
203:
204: }
|