1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.NoSuchImageException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.servlet.HttpHeaders;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Image;
33  import com.liferay.portal.model.impl.ImageImpl;
34  import com.liferay.portal.service.ImageLocalServiceUtil;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.io.IOException;
41  
42  import java.util.Date;
43  
44  import javax.servlet.ServletException;
45  import javax.servlet.http.HttpServlet;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  
52  /**
53   * <a href="ImageServlet.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   * @author Brett Randall
57   *
58   */
59  public class ImageServlet extends HttpServlet {
60  
61      public void service(
62              HttpServletRequest request, HttpServletResponse response)
63          throws IOException, ServletException {
64  
65          long lastModified = getLastModified(request);
66  
67          if (lastModified > 0) {
68              long ifModifiedSince =
69                  request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
70  
71              if ((ifModifiedSince > 0) && (ifModifiedSince == lastModified)) {
72                  response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
73  
74                  return;
75              }
76          }
77  
78          //res.addHeader(HttpHeaders.CACHE_CONTROL, "max-age=0");
79  
80          if (lastModified > 0) {
81              response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
82          }
83  
84          try {
85              writeImage(request, response);
86          }
87          catch (Exception e) {
88              PortalUtil.sendError(
89                  HttpServletResponse.SC_NOT_FOUND, e, request, response);
90          }
91      }
92  
93      protected Image getDefaultImage(HttpServletRequest request, long imageId)
94          throws NoSuchImageException {
95  
96          String path = GetterUtil.getString(request.getPathInfo());
97  
98          if (path.startsWith("/company_logo")) {
99              return ImageLocalServiceUtil.getDefaultCompanyLogo();
100         }
101         else if (path.startsWith("/user_female_portrait")) {
102             return ImageLocalServiceUtil.getDefaultUserFemalePortrait();
103         }
104         else if (path.startsWith("/user_male_portrait")) {
105             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
106         }
107         else if (path.startsWith("/user_portrait")) {
108             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
109         }
110         else {
111             throw new NoSuchImageException(
112                 "No default image exists for " + imageId);
113         }
114     }
115 
116     protected Image getImage(HttpServletRequest request, boolean getDefault)
117         throws PortalException, SystemException {
118 
119         long imageId = getImageId(request);
120 
121         Image image = null;
122 
123         if (imageId > 0) {
124             image = ImageLocalServiceUtil.getImage(imageId);
125         }
126         else {
127             String uuid = ParamUtil.getString(request, "uuid");
128             long groupId = ParamUtil.getLong(request, "groupId");
129 
130             try {
131                 if (Validator.isNotNull(uuid) && (groupId > 0)) {
132                     IGImage igImage =
133                         IGImageLocalServiceUtil.getImageByUuidAndGroupId(
134                             uuid, groupId);
135 
136                     image = ImageLocalServiceUtil.getImage(
137                         igImage.getLargeImageId());
138                 }
139             }
140             catch (Exception e) {
141             }
142         }
143 
144         if (getDefault) {
145             if (image == null) {
146                 if (_log.isWarnEnabled()) {
147                     _log.warn("Get a default image for " + imageId);
148                 }
149 
150                 image = getDefaultImage(request, imageId);
151             }
152         }
153 
154         return image;
155     }
156 
157     protected long getImageId(HttpServletRequest request) {
158 
159         // The image id may be passed in as image_id, img_id, or i_id
160 
161         long imageId = ParamUtil.getLong(request, "image_id");
162 
163         if (imageId <= 0) {
164             imageId = ParamUtil.getLong(request, "img_id");
165 
166             if (imageId <= 0) {
167                 imageId = ParamUtil.getLong(request, "i_id");
168             }
169         }
170 
171         return imageId;
172     }
173 
174     protected long getLastModified(HttpServletRequest request) {
175         try {
176             Image image = getImage(request, false);
177 
178             if (image == null) {
179                 return -1;
180             }
181 
182             Date modifiedDate = image.getModifiedDate();
183 
184             if (modifiedDate == null) {
185                 modifiedDate = PortalUtil.getUptime();
186             }
187 
188             // Round down and remove milliseconds
189 
190             return (modifiedDate.getTime() / 1000) * 1000;
191         }
192         catch (Exception e) {
193             _log.error(e, e);
194 
195             return -1;
196         }
197     }
198 
199     protected void writeImage(
200             HttpServletRequest request, HttpServletResponse response)
201         throws PortalException, SystemException {
202 
203         Image image = getImage(request, true);
204 
205         if (image == null) {
206             throw new NoSuchImageException("Image is null");
207         }
208         else {
209             if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
210                 response.setContentType("image/" + image.getType());
211             }
212 
213             try {
214                 ServletResponseUtil.write(response, image.getTextObj());
215             }
216             catch (Exception e) {
217                 if (_log.isWarnEnabled()) {
218                     _log.warn(e, e);
219                 }
220             }
221         }
222     }
223 
224     private static Log _log = LogFactory.getLog(ImageServlet.class);
225 
226 }