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.ServletConfig;
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServlet;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  
53  /**
54   * <a href="ImageServlet.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Brett Randall
58   *
59   */
60  public class ImageServlet extends HttpServlet {
61  
62      public void init(ServletConfig filterConfig) throws ServletException {
63          super.init(filterConfig);
64  
65          _lastModified = GetterUtil.getBoolean(
66              filterConfig.getInitParameter("last_modified"), true);
67      }
68  
69      public void service(
70              HttpServletRequest request, HttpServletResponse response)
71          throws IOException, ServletException {
72  
73          if (_lastModified) {
74              long lastModified = getLastModified(request);
75  
76              if (lastModified > 0) {
77                  long ifModifiedSince = request.getDateHeader(
78                      HttpHeaders.IF_MODIFIED_SINCE);
79  
80                  if ((ifModifiedSince > 0) &&
81                      (ifModifiedSince == lastModified)) {
82  
83                      response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
84  
85                      return;
86                  }
87              }
88  
89              if (lastModified > 0) {
90                  response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
91              }
92          }
93  
94          try {
95              writeImage(request, response);
96          }
97          catch (Exception e) {
98              PortalUtil.sendError(
99                  HttpServletResponse.SC_NOT_FOUND, e, request, response);
100         }
101     }
102 
103     protected Image getDefaultImage(HttpServletRequest request, long imageId)
104         throws NoSuchImageException {
105 
106         String path = GetterUtil.getString(request.getPathInfo());
107 
108         if (path.startsWith("/company_logo")) {
109             return ImageLocalServiceUtil.getDefaultCompanyLogo();
110         }
111         else if (path.startsWith("/user_female_portrait")) {
112             return ImageLocalServiceUtil.getDefaultUserFemalePortrait();
113         }
114         else if (path.startsWith("/user_male_portrait")) {
115             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
116         }
117         else if (path.startsWith("/user_portrait")) {
118             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
119         }
120         else {
121             throw new NoSuchImageException(
122                 "No default image exists for " + imageId);
123         }
124     }
125 
126     protected Image getImage(HttpServletRequest request, boolean getDefault)
127         throws PortalException, SystemException {
128 
129         long imageId = getImageId(request);
130 
131         Image image = null;
132 
133         if (imageId > 0) {
134             image = ImageLocalServiceUtil.getImage(imageId);
135         }
136         else {
137             String uuid = ParamUtil.getString(request, "uuid");
138             long groupId = ParamUtil.getLong(request, "groupId");
139 
140             try {
141                 if (Validator.isNotNull(uuid) && (groupId > 0)) {
142                     IGImage igImage =
143                         IGImageLocalServiceUtil.getImageByUuidAndGroupId(
144                             uuid, groupId);
145 
146                     image = ImageLocalServiceUtil.getImage(
147                         igImage.getLargeImageId());
148                 }
149             }
150             catch (Exception e) {
151             }
152         }
153 
154         if (getDefault) {
155             if (image == null) {
156                 if (_log.isWarnEnabled()) {
157                     _log.warn("Get a default image for " + imageId);
158                 }
159 
160                 image = getDefaultImage(request, imageId);
161             }
162         }
163 
164         return image;
165     }
166 
167     protected long getImageId(HttpServletRequest request) {
168 
169         // The image id may be passed in as image_id, img_id, or i_id
170 
171         long imageId = ParamUtil.getLong(request, "image_id");
172 
173         if (imageId <= 0) {
174             imageId = ParamUtil.getLong(request, "img_id");
175 
176             if (imageId <= 0) {
177                 imageId = ParamUtil.getLong(request, "i_id");
178             }
179         }
180 
181         return imageId;
182     }
183 
184     protected long getLastModified(HttpServletRequest request) {
185         try {
186             Image image = getImage(request, false);
187 
188             if (image == null) {
189                 return -1;
190             }
191 
192             Date modifiedDate = image.getModifiedDate();
193 
194             if (modifiedDate == null) {
195                 modifiedDate = PortalUtil.getUptime();
196             }
197 
198             // Round down and remove milliseconds
199 
200             return (modifiedDate.getTime() / 1000) * 1000;
201         }
202         catch (Exception e) {
203             _log.error(e, e);
204 
205             return -1;
206         }
207     }
208 
209     protected void writeImage(
210             HttpServletRequest request, HttpServletResponse response)
211         throws PortalException, SystemException {
212 
213         Image image = getImage(request, true);
214 
215         if (image == null) {
216             throw new NoSuchImageException("Image is null");
217         }
218         else {
219             if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
220                 response.setContentType("image/" + image.getType());
221             }
222 
223             try {
224                 ServletResponseUtil.write(response, image.getTextObj());
225             }
226             catch (Exception e) {
227                 if (_log.isWarnEnabled()) {
228                     _log.warn(e, e);
229                 }
230             }
231         }
232     }
233 
234     private static Log _log = LogFactory.getLog(ImageServlet.class);
235 
236     private boolean _lastModified = true;
237 
238 }