1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.JavaConstants;
28 import com.liferay.portal.model.Portlet;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.model.impl.PortletImpl;
31 import com.liferay.portal.service.RoleLocalServiceUtil;
32 import com.liferay.portal.util.PortalUtil;
33
34 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.io.UnsupportedEncodingException;
37
38 import java.security.Principal;
39
40 import java.util.Enumeration;
41 import java.util.Locale;
42 import java.util.Map;
43
44 import javax.portlet.PortletRequest;
45
46 import javax.servlet.RequestDispatcher;
47 import javax.servlet.ServletInputStream;
48 import javax.servlet.http.Cookie;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletRequestWrapper;
51 import javax.servlet.http.HttpSession;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56
63 public class PortletServletRequest extends HttpServletRequestWrapper {
64
65 public PortletServletRequest(
66 HttpServletRequest req, PortletRequestImpl portletReq, String pathInfo,
67 String queryString, String requestURI, String servletPath,
68 boolean named, boolean include) {
69
70 super(req);
71
72 _req = req;
73 _portletReq = portletReq;
74 _lifecycle = _portletReq.getLifecycle();
75 _pathInfo = GetterUtil.getString(pathInfo);
76 _queryString = GetterUtil.getString(queryString);
77 _requestURI = GetterUtil.getString(requestURI);
78 _servletPath = GetterUtil.getString(servletPath);
79 _named = named;
80 _include = include;
81
82 long userId = PortalUtil.getUserId(req);
83 String remoteUser = req.getRemoteUser();
84
85 Portlet portlet = portletReq.getPortlet();
86
87 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
88
89 if (userPrincipalStrategy.equals(
90 PortletImpl.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
91
92 try {
93 User user = PortalUtil.getUser(req);
94
95 _remoteUser = user.getScreenName();
96 _remoteUserId = user.getUserId();
97 _userPrincipal = new ProtectedPrincipal(_remoteUser);
98 }
99 catch (Exception e) {
100 _log.error(e);
101 }
102 }
103 else {
104 if ((userId > 0) && (remoteUser == null)) {
105 _remoteUser = String.valueOf(userId);
106 _remoteUserId = userId;
107 _userPrincipal = new ProtectedPrincipal(_remoteUser);
108 }
109 else {
110 _remoteUser = remoteUser;
111 _remoteUserId = GetterUtil.getLong(remoteUser);
112 _userPrincipal = req.getUserPrincipal();
113 }
114 }
115 }
116
117 public Object getAttribute(String name) {
118 if (_include || (name == null)) {
119 return _req.getAttribute(name);
120 }
121
122 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
123 if (_named) {
124 return null;
125 }
126 else {
127 return _portletReq.getContextPath();
128 }
129 }
130
131 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
132 if (_named) {
133 return null;
134 }
135 else {
136 return _pathInfo;
137 }
138 }
139
140 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
141 if (_named) {
142 return null;
143 }
144 else {
145 return _queryString;
146 }
147 }
148
149 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
150 if (_named) {
151 return null;
152 }
153 else {
154 return _requestURI;
155 }
156 }
157
158 if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
159 if (_named) {
160 return null;
161 }
162 else {
163 return _servletPath;
164 }
165 }
166
167 return _req.getAttribute(name);
168 }
169
170 public Enumeration<String> getAttributeNames() {
171 return _req.getAttributeNames();
172 }
173
174 public String getAuthType() {
175 return _req.getAuthType();
176 }
177
178 public String getCharacterEncoding() {
179 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
180 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
181
182 return _req.getCharacterEncoding();
183 }
184 else {
185 return null;
186 }
187 }
188
189 public int getContentLength() {
190 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
191 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
192
193 return _req.getContentLength();
194 }
195 else {
196 return 0;
197 }
198 }
199
200 public String getContentType() {
201 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
202 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
203
204 return _req.getContentType();
205 }
206 else {
207 return null;
208 }
209 }
210
211 public String getContextPath() {
212 return _portletReq.getContextPath();
213 }
214
215 public Cookie[] getCookies() {
216 return _req.getCookies();
217 }
218
219 public long getDateHeader(String name) {
220 return GetterUtil.getLong(getHeader(name));
221 }
222
223 public String getHeader(String name) {
224 return _portletReq.getProperty("header." + name);
225 }
226
227 public Enumeration<String> getHeaderNames() {
228 return _portletReq.getProperties("header.");
229 }
230
231 public Enumeration<String> getHeaders(String name) {
232 return _portletReq.getProperties("header." + name);
233 }
234
235 public ServletInputStream getInputStream() throws IOException {
236 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
237 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
238
239 return _req.getInputStream();
240 }
241 else {
242 return null;
243 }
244 }
245
246 public int getIntHeader(String name) {
247 return GetterUtil.getInteger(getHeader(name));
248 }
249
250 public String getLocalAddr() {
251 return null;
252 }
253
254 public Locale getLocale() {
255 return _portletReq.getLocale();
256 }
257
258 public Enumeration<Locale> getLocales() {
259 return _req.getLocales();
260 }
261
262 public String getLocalName() {
263 return null;
264 }
265
266 public int getLocalPort() {
267 return 0;
268 }
269
270 public String getMethod() {
271 if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
272 return "GET";
273 }
274 else {
275 return _req.getMethod();
276 }
277 }
278
279 public String getParameter(String name) {
280 return _req.getParameter(name);
281 }
282
283 public Map<String, String[]> getParameterMap() {
284 return _req.getParameterMap();
285 }
286
287 public Enumeration<String> getParameterNames() {
288 return _req.getParameterNames();
289 }
290
291 public String[] getParameterValues(String name) {
292 return _req.getParameterValues(name);
293 }
294
295 public String getPathInfo() {
296 return _pathInfo;
297 }
298
299 public String getPathTranslated() {
300 return _req.getPathTranslated();
301 }
302
303 public String getProtocol() {
304 return "HTTP/1.1";
305 }
306
307 public String getQueryString() {
308 return _queryString;
309 }
310
311 public BufferedReader getReader() throws IOException {
312 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
313 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
314
315 return _req.getReader();
316 }
317 else {
318 return null;
319 }
320 }
321
322 public String getRealPath(String path) {
323 return null;
324 }
325
326 public RequestDispatcher getRequestDispatcher(String path) {
327 return _req.getRequestDispatcher(path);
328 }
329
330 public String getRequestedSessionId() {
331 return _req.getRequestedSessionId();
332 }
333
334 public String getRemoteAddr() {
335 return null;
336 }
337
338 public String getRemoteHost() {
339 return null;
340 }
341
342 public int getRemotePort() {
343 return 0;
344 }
345
346 public String getRequestURI() {
347 return _requestURI;
348 }
349
350 public StringBuffer getRequestURL() {
351 return null;
352 }
353
354 public String getRemoteUser() {
355 return _remoteUser;
356 }
357
358 public String getScheme() {
359 return _req.getScheme();
360 }
361
362 public String getServerName() {
363 return _req.getServerName();
364 }
365
366 public int getServerPort() {
367 return _req.getServerPort();
368 }
369
370 public String getServletPath() {
371 return _servletPath;
372 }
373
374 public HttpSession getSession() {
375 return new PortletServletSession(_req.getSession(), _portletReq);
376 }
377
378 public HttpSession getSession(boolean create) {
379 return new PortletServletSession(_req.getSession(create), _portletReq);
380 }
381
382 public Principal getUserPrincipal() {
383 return _userPrincipal;
384 }
385
386 public boolean isRequestedSessionIdFromCookie() {
387 return _req.isRequestedSessionIdFromCookie();
388 }
389
390 public boolean isRequestedSessionIdFromURL() {
391 return _req.isRequestedSessionIdFromURL();
392 }
393
394
397 public boolean isRequestedSessionIdFromUrl() {
398 return _req.isRequestedSessionIdFromUrl();
399 }
400
401 public boolean isRequestedSessionIdValid() {
402 return _req.isRequestedSessionIdValid();
403 }
404
405 public boolean isSecure() {
406 return _req.isSecure();
407 }
408
409 public boolean isUserInRole(String role) {
410 if (_remoteUserId <= 0) {
411 return false;
412 }
413 else {
414 try {
415 long companyId = PortalUtil.getCompanyId(_req);
416
417 return RoleLocalServiceUtil.hasUserRole(
418 _remoteUserId, companyId, role, true);
419 }
420 catch (Exception e) {
421 _log.error(e);
422 }
423
424 return _req.isUserInRole(role);
425 }
426 }
427
428 public void removeAttribute(String name) {
429 _req.removeAttribute(name);
430 }
431
432 public void setAttribute(String name, Object obj) {
433 _req.setAttribute(name, obj);
434 }
435
436 public void setCharacterEncoding(String encoding)
437 throws UnsupportedEncodingException {
438
439 if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
440 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
441
442 _req.setCharacterEncoding(encoding);
443 }
444 }
445
446 private static Log _log = LogFactory.getLog(PortletServletRequest.class);
447
448 private HttpServletRequest _req;
449 private PortletRequestImpl _portletReq;
450 private String _lifecycle;
451 private String _pathInfo;
452 private String _queryString;
453 private String _remoteUser;
454 private long _remoteUserId;
455 private String _requestURI;
456 private String _servletPath;
457 private Principal _userPrincipal;
458 private boolean _named;
459 private boolean _include;
460
461 }