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