1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.JavaConstants;
27 import com.liferay.portal.kernel.util.ServerDetector;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.model.Portlet;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.model.impl.PortletImpl;
32 import com.liferay.portal.service.RoleLocalServiceUtil;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.util.servlet.ProtectedPrincipal;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.UnsupportedEncodingException;
39
40 import java.security.Principal;
41
42 import java.util.Enumeration;
43 import java.util.Locale;
44
45 import javax.servlet.ServletInputStream;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletRequestWrapper;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51
52
59 public class PortletServletRequest extends HttpServletRequestWrapper {
60
61 public PortletServletRequest(
62 HttpServletRequest req, RenderRequestImpl renderReq, String pathInfo,
63 String queryString, String requestURI, String servletPath) {
64
65 super(req);
66
67 _req = req;
68 _renderReq = renderReq;
69 _pathInfo = GetterUtil.getString(pathInfo);
70 _queryString = GetterUtil.getString(queryString);
71 _requestURI = GetterUtil.getString(requestURI);
72 _servletPath = GetterUtil.getString(servletPath);
73
74 long userId = PortalUtil.getUserId(req);
75 String remoteUser = req.getRemoteUser();
76
77 Portlet portlet = renderReq.getPortlet();
78
79 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
80
81 if (userPrincipalStrategy.equals(
82 PortletImpl.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
83
84 try {
85 User user = PortalUtil.getUser(req);
86
87 _remoteUser = user.getScreenName();
88 _remoteUserId = user.getUserId();
89 _userPrincipal = new ProtectedPrincipal(_remoteUser);
90 }
91 catch (Exception e) {
92 _log.error(e);
93 }
94 }
95 else {
96 if ((userId > 0) && (remoteUser == null)) {
97 _remoteUser = String.valueOf(userId);
98 _remoteUserId = userId;
99 _userPrincipal = new ProtectedPrincipal(_remoteUser);
100 }
101 else {
102 _remoteUser = remoteUser;
103 _remoteUserId = GetterUtil.getLong(remoteUser);
104 _userPrincipal = req.getUserPrincipal();
105 }
106 }
107 }
108
109 public Object getAttribute(String name) {
110 Object retVal = super.getAttribute(name);
111
112 if (name == null) {
113 return retVal;
114 }
115
116 if (ServerDetector.isWebSphere()) {
117 if (_renderReq.getPortlet().isWARFile()) {
118 if (name.equals(
119 JavaConstants.JAVAX_SERVLET_INCLUDE_CONTEXT_PATH)) {
120
121 retVal = _renderReq.getContextPath();
122 }
123 else if (name.equals(
124 JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
125
126 retVal = _pathInfo;
127 }
128 else if (name.equals(
129 JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING)) {
130
131 retVal = _queryString;
132 }
133 else if (name.equals(
134 JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI)) {
135
136 retVal = _requestURI;
137 }
138 else if (name.equals(
139 JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH)) {
140
141 retVal = _servletPath;
142 }
143 }
144
145 if ((name.startsWith(JavaConstants.JAVAX_SERVLET_INCLUDE)) &&
146 (retVal == null)) {
147
148 retVal = StringPool.BLANK;
149 }
150 }
151
152 return retVal;
153 }
154
155 public String getCharacterEncoding() {
156 if (_isUploadRequest()) {
157 return super.getCharacterEncoding();
158 }
159 else {
160 return null;
161 }
162 }
163
164 public void setCharacterEncoding(String encoding)
165 throws UnsupportedEncodingException {
166 }
167
168 public int getContentLength() {
169 if (_isUploadRequest()) {
170 return super.getContentLength();
171 }
172 else {
173 return 0;
174 }
175 }
176
177 public String getContentType() {
178 if (_isUploadRequest()) {
179 return super.getContentType();
180 }
181 else {
182 return null;
183 }
184 }
185
186 public String getContextPath() {
187 return _renderReq.getContextPath();
188 }
189
190 public ServletInputStream getInputStream() throws IOException {
191 if (_isUploadRequest()) {
192 return super.getInputStream();
193 }
194 else {
195 return null;
196 }
197 }
198
199 public Locale getLocale() {
200 return _renderReq.getLocale();
201 }
202
203 public Enumeration getLocales() {
204 return _renderReq.getLocales();
205 }
206
207 public String getPathInfo() {
208 return _pathInfo;
209 }
210
211 public String getProtocol() {
212 return null;
213 }
214
215 public String getQueryString() {
216 return _queryString;
217 }
218
219 public BufferedReader getReader() throws IOException {
220 if (_isUploadRequest()) {
221 return super.getReader();
222 }
223 else {
224 return null;
225 }
226 }
227
228 public String getRealPath(String path) {
229 return null;
230 }
231
232 public String getRemoteAddr() {
233 return null;
234 }
235
236 public String getRemoteHost() {
237 return null;
238 }
239
240 public String getRequestURI() {
241 return _requestURI;
242 }
243
244 public StringBuffer getRequestURL() {
245 return null;
246 }
247
248 public String getServletPath() {
249 return _servletPath;
250 }
251
252 public String getRemoteUser() {
253 return _remoteUser;
254 }
255
256 public Principal getUserPrincipal() {
257 return _userPrincipal;
258 }
259
260 public boolean isUserInRole(String role) {
261 if (_remoteUserId <= 0) {
262 return false;
263 }
264 else {
265 try {
266 long companyId = PortalUtil.getCompanyId(_req);
267
268 return RoleLocalServiceUtil.hasUserRole(
269 _remoteUserId, companyId, role, true);
270 }
271 catch (Exception e) {
272 _log.error(e);
273 }
274
275 return super.isUserInRole(role);
276 }
277 }
278
279 private boolean _isUploadRequest() {
280 if (!_uploadRequestInvoked) {
281 _uploadRequestInvoked = true;
282
283 if (PortalUtil.getUploadServletRequest(this) != null) {
284 _uploadRequest = true;
285 }
286 }
287
288 return _uploadRequest;
289 }
290
291 private static Log _log = LogFactory.getLog(PortletServletRequest.class);
292
293 private HttpServletRequest _req;
294 private RenderRequestImpl _renderReq;
295 private String _pathInfo;
296 private String _queryString;
297 private String _requestURI;
298 private String _servletPath;
299 private String _remoteUser;
300 private long _remoteUserId;
301 private Principal _userPrincipal;
302 private boolean _uploadRequest;
303 private boolean _uploadRequestInvoked;
304
305 }