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_portrait")) {
98              return ImageLocalUtil.getDefaultUserPortrait();
99          }
100         else {
101             throw new NoSuchImageException(
102                 "No default image exists for " + imageId);
103         }
104     }
105 
106     protected Image getImage(HttpServletRequest req, boolean getDefault)
107         throws NoSuchImageException {
108 
109         long imageId = getImageId(req);
110 
111         Image image = null;
112 
113         if (imageId > 0) {
114             image = ImageLocalUtil.getImage(imageId);
115         }
116         else {
117             String uuid = ParamUtil.getString(req, "uuid");
118             long groupId = ParamUtil.getLong(req, "groupId");
119 
120             try {
121                 IGImage igImage =
122                     IGImageLocalServiceUtil.getImageByUuidAndGroupId(
123                         uuid, groupId);
124 
125                 image = ImageLocalUtil.getImage(igImage.getLargeImageId());
126             }
127             catch (Exception e) {
128             }
129         }
130 
131         if (getDefault) {
132             if (image == null) {
133                 if (_log.isWarnEnabled()) {
134                     _log.warn("Get a default image for " + imageId);
135                 }
136 
137                 image = getDefaultImage(req, imageId);
138             }
139         }
140 
141         return image;
142     }
143 
144     protected long getImageId(HttpServletRequest req) {
145 
146         // The image id may be passed in as image_id, img_id, or i_id
147 
148         long imageId = ParamUtil.getLong(req, "image_id");
149 
150         if (imageId <= 0) {
151             imageId = ParamUtil.getLong(req, "img_id");
152 
153             if (imageId <= 0) {
154                 imageId = ParamUtil.getLong(req, "i_id");
155             }
156         }
157 
158         return imageId;
159     }
160 
161     protected long getLastModified(HttpServletRequest req) {
162         try {
163             Image image = getImage(req, false);
164 
165             if (image == null) {
166                 return -1;
167             }
168 
169             Date modifiedDate = image.getModifiedDate();
170 
171             if (modifiedDate == null) {
172                 modifiedDate = PortalUtil.UP_TIME;
173             }
174 
175             // Round down and remove milliseconds
176 
177             return (modifiedDate.getTime() / 1000) * 1000;
178         }
179         catch (Exception e) {
180             _log.error(e, e);
181 
182             return -1;
183         }
184     }
185 
186     protected void writeImage(HttpServletRequest req, HttpServletResponse res)
187         throws IOException, NoSuchImageException, ServletException {
188 
189         Image image = getImage(req, true);
190 
191         if (image == null) {
192             throw new NoSuchImageException("Image is null");
193         }
194         else {
195             if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
196                 res.setContentType("image/" + image.getType());
197             }
198 
199             try {
200                 ServletResponseUtil.write(res, image.getTextObj());
201             }
202             catch (Exception e) {
203                 if (_log.isWarnEnabled()) {
204                     _log.warn(e, e);
205                 }
206             }
207         }
208     }
209 
210     private static Log _log = LogFactory.getLog(ImageServlet.class);
211 
212 }