1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchImageException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.HttpHeaders;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Image;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.impl.ImageImpl;
34  import com.liferay.portal.service.ImageLocalServiceUtil;
35  import com.liferay.portal.service.UserLocalServiceUtil;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portlet.imagegallery.model.IGImage;
38  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.io.IOException;
42  
43  import java.util.Date;
44  
45  import javax.servlet.ServletConfig;
46  import javax.servlet.ServletException;
47  import javax.servlet.http.HttpServlet;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  /**
52   * <a href="ImageServlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Brett Randall
56   *
57   */
58  public class ImageServlet extends HttpServlet {
59  
60      public void init(ServletConfig servletConfig) throws ServletException {
61          super.init(servletConfig);
62  
63          _lastModified = GetterUtil.getBoolean(
64              servletConfig.getInitParameter("last_modified"), true);
65      }
66  
67      public void service(
68              HttpServletRequest request, HttpServletResponse response)
69          throws IOException, ServletException {
70  
71          if (_lastModified) {
72              long lastModified = getLastModified(request);
73  
74              if (lastModified > 0) {
75                  long ifModifiedSince = request.getDateHeader(
76                      HttpHeaders.IF_MODIFIED_SINCE);
77  
78                  if ((ifModifiedSince > 0) &&
79                      (ifModifiedSince == lastModified)) {
80  
81                      response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
82  
83                      return;
84                  }
85              }
86  
87              if (lastModified > 0) {
88                  response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
89              }
90          }
91  
92          try {
93              writeImage(request, response);
94          }
95          catch (Exception e) {
96              PortalUtil.sendError(
97                  HttpServletResponse.SC_NOT_FOUND, e, request, response);
98          }
99      }
100 
101     protected Image getDefaultImage(HttpServletRequest request, long imageId)
102         throws NoSuchImageException {
103 
104         String path = GetterUtil.getString(request.getPathInfo());
105 
106         if (path.startsWith("/company_logo")) {
107             return ImageLocalServiceUtil.getDefaultCompanyLogo();
108         }
109         else if (path.startsWith("/user_female_portrait")) {
110             return ImageLocalServiceUtil.getDefaultUserFemalePortrait();
111         }
112         else if (path.startsWith("/user_male_portrait")) {
113             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
114         }
115         else if (path.startsWith("/user_portrait")) {
116             return ImageLocalServiceUtil.getDefaultUserMalePortrait();
117         }
118         else {
119             throw new NoSuchImageException(
120                 "No default image exists for " + imageId);
121         }
122     }
123 
124     protected Image getImage(HttpServletRequest request, boolean getDefault)
125         throws PortalException, SystemException {
126 
127         long imageId = getImageId(request);
128 
129         Image image = null;
130 
131         if (imageId > 0) {
132             image = ImageLocalServiceUtil.getImage(imageId);
133         }
134         else {
135             String uuid = ParamUtil.getString(request, "uuid");
136             long groupId = ParamUtil.getLong(request, "groupId");
137 
138             try {
139                 if (Validator.isNotNull(uuid) && (groupId > 0)) {
140                     IGImage igImage =
141                         IGImageLocalServiceUtil.getImageByUuidAndGroupId(
142                             uuid, groupId);
143 
144                     image = ImageLocalServiceUtil.getImage(
145                         igImage.getLargeImageId());
146                 }
147             }
148             catch (Exception e) {
149             }
150         }
151 
152         if (getDefault) {
153             if (image == null) {
154                 if (_log.isWarnEnabled()) {
155                     _log.warn("Get a default image for " + imageId);
156                 }
157 
158                 image = getDefaultImage(request, imageId);
159             }
160         }
161 
162         return image;
163     }
164 
165     protected long getImageId(HttpServletRequest request) {
166 
167         // The image id may be passed in as image_id, img_id, or i_id
168 
169         long imageId = ParamUtil.getLong(request, "image_id");
170 
171         if (imageId <= 0) {
172             imageId = ParamUtil.getLong(request, "img_id");
173         }
174 
175         if (imageId <= 0) {
176             imageId = ParamUtil.getLong(request, "i_id");
177         }
178 
179         if (imageId <= 0) {
180             long companyId = ParamUtil.getLong(request, "companyId");
181             String screenName = ParamUtil.getString(request, "screenName");
182 
183             try {
184                 if ((companyId > 0) && Validator.isNotNull(screenName)) {
185                     User user = UserLocalServiceUtil.getUserByScreenName(
186                         companyId, screenName);
187 
188                     imageId = user.getPortraitId();
189                 }
190             }
191             catch (Exception e) {
192             }
193         }
194 
195         return imageId;
196     }
197 
198     protected long getLastModified(HttpServletRequest request) {
199         try {
200             Image image = getImage(request, false);
201 
202             if (image == null) {
203                 return -1;
204             }
205 
206             Date modifiedDate = image.getModifiedDate();
207 
208             if (modifiedDate == null) {
209                 modifiedDate = PortalUtil.getUptime();
210             }
211 
212             // Round down and remove milliseconds
213 
214             return (modifiedDate.getTime() / 1000) * 1000;
215         }
216         catch (Exception e) {
217             _log.error(e, e);
218 
219             return -1;
220         }
221     }
222 
223     protected void writeImage(
224             HttpServletRequest request, HttpServletResponse response)
225         throws PortalException, SystemException {
226 
227         Image image = getImage(request, true);
228 
229         if (image == null) {
230             throw new NoSuchImageException("Image is null");
231         }
232         else {
233             if (!image.getType().equals(ImageImpl.TYPE_NOT_AVAILABLE)) {
234                 response.setContentType("image/" + image.getType());
235             }
236 
237             try {
238                 ServletResponseUtil.write(response, image.getTextObj());
239             }
240             catch (Exception e) {
241                 if (_log.isWarnEnabled()) {
242                     _log.warn(e, e);
243                 }
244             }
245         }
246     }
247 
248     private static Log _log = LogFactoryUtil.getLog(ImageServlet.class);
249 
250     private boolean _lastModified = true;
251 
252 }