1
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
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
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
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
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 }