1   /**
2    * Copyright (c) 2000-2007 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.kernel.servlet.HttpHeaders;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Image;
28  import com.liferay.portal.model.impl.ImageImpl;
29  import com.liferay.portal.service.impl.ImageLocalUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.io.IOException;
34  
35  import java.util.Date;
36  
37  import javax.servlet.ServletException;
38  import javax.servlet.http.HttpServlet;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="ImageServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Brett Randall
50   *
51   */
52  public class ImageServlet extends HttpServlet {
53  
54      public void service(HttpServletRequest req, HttpServletResponse res)
55          throws IOException, ServletException {
56  
57          long lastModified = getLastModified(req);
58  
59          if (lastModified > 0) {
60              long ifModifiedSince =
61                  req.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
62  
63              if ((ifModifiedSince > 0) && (ifModifiedSince == lastModified)) {
64                  res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
65  
66                  return;
67              }
68          }
69  
70          //res.addHeader(HttpHeaders.CACHE_CONTROL, "max-age=0");
71  
72          if (lastModified > 0) {
73              res.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
74          }
75  
76          writeImage(req, res);
77      }
78  
79      protected Image getDefaultImage(HttpServletRequest req, long imageId) {
80          String path = req.getPathInfo();
81  
82          if (path.startsWith("/company_logo")) {
83              return ImageLocalUtil.getDefaultCompanyLogo();
84          }
85          else if (path.startsWith("/user_portrait")) {
86              return ImageLocalUtil.getDefaultUserPortrait();
87          }
88          else {
89              return ImageLocalUtil.getDefaultSpacer();
90          }
91      }
92  
93      protected long getImageId(HttpServletRequest req) {
94  
95          // The image id may be passed in as image_id, img_id, or i_id
96  
97          long imageId = ParamUtil.getLong(req, "image_id");
98  
99          if (imageId <= 0) {
100             imageId = ParamUtil.getLong(req, "img_id");
101 
102             if (imageId <= 0) {
103                 imageId = ParamUtil.getLong(req, "i_id");
104             }
105         }
106 
107         return imageId;
108     }
109 
110     protected long getLastModified(HttpServletRequest req) {
111         try {
112             long imageId = getImageId(req);
113 
114             Image image = ImageLocalUtil.getImage(imageId);
115 
116             if (image == null) {
117                 return -1;
118             }
119 
120             Date modifiedDate = image.getModifiedDate();
121 
122             if (modifiedDate == null) {
123                 modifiedDate = PortalUtil.UP_TIME;
124             }
125 
126             // Round down and remove milliseconds
127 
128             return (modifiedDate.getTime() / 1000) * 1000;
129         }
130         catch (Exception e) {
131             _log.error(e, e);
132 
133             return -1;
134         }
135     }
136 
137     protected void writeImage(HttpServletRequest req, HttpServletResponse res)
138         throws IOException, ServletException {
139 
140         long imageId = getImageId(req);
141 
142         Image image = ImageLocalUtil.getImage(imageId);
143 
144         if (image == null) {
145             if (_log.isWarnEnabled()) {
146                 _log.warn("Get a default image for " + imageId);
147             }
148 
149             image = getDefaultImage(req, imageId);
150         }
151 
152         if (image == null) {
153             if (_log.isWarnEnabled()) {
154                 _log.warn("No default image exists for " + imageId);
155             }
156         }
157         else {
158             if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
159                 res.setContentType("image/" + image.getType());
160             }
161 
162             try {
163                 ServletResponseUtil.write(res, image.getTextObj());
164             }
165             catch (Exception e) {
166                 if (_log.isWarnEnabled()) {
167                     _log.warn(e, e);
168                 }
169             }
170         }
171     }
172 
173     private static Log _log = LogFactory.getLog(ImageServlet.class);
174 
175 }