1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.HttpMethods;
20  import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.JavaConstants;
23  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
24  import com.liferay.portal.kernel.util.ServerDetector;
25  import com.liferay.portal.model.Portlet;
26  import com.liferay.portal.model.PortletConstants;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.io.BufferedReader;
31  import java.io.IOException;
32  import java.io.UnsupportedEncodingException;
33  
34  import java.lang.reflect.Constructor;
35  
36  import java.security.Principal;
37  
38  import java.util.Enumeration;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.portlet.PortletRequest;
43  
44  import javax.servlet.RequestDispatcher;
45  import javax.servlet.ServletInputStream;
46  import javax.servlet.http.Cookie;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletRequestWrapper;
49  import javax.servlet.http.HttpSession;
50  
51  /**
52   * <a href="PortletServletRequest.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Brian Myunghun Kim
56   */
57  public class PortletServletRequest extends HttpServletRequestWrapper {
58  
59      public PortletServletRequest(
60          HttpServletRequest request, PortletRequestImpl portletRequestImpl,
61          String pathInfo, String queryString, String requestURI,
62          String servletPath, boolean named, boolean include) {
63  
64          super(request);
65  
66          _request = request;
67          _portletRequestImpl = portletRequestImpl;
68          _lifecycle = _portletRequestImpl.getLifecycle();
69          _pathInfo = pathInfo;
70          _queryString = queryString;
71          _requestURI = GetterUtil.getString(requestURI);
72          _servletPath = GetterUtil.getString(servletPath);
73          _named = named;
74          _include = include;
75  
76          long userId = PortalUtil.getUserId(request);
77          String remoteUser = request.getRemoteUser();
78  
79          Portlet portlet = portletRequestImpl.getPortlet();
80  
81          String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
82  
83          if (userPrincipalStrategy.equals(
84                  PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
85  
86              try {
87                  User user = PortalUtil.getUser(request);
88  
89                  _remoteUser = user.getScreenName();
90                  _userPrincipal = new ProtectedPrincipal(_remoteUser);
91              }
92              catch (Exception e) {
93                  _log.error(e);
94              }
95          }
96          else {
97              if ((userId > 0) && (remoteUser == null)) {
98                  _remoteUser = String.valueOf(userId);
99                  _userPrincipal = new ProtectedPrincipal(_remoteUser);
100             }
101             else {
102                 _remoteUser = remoteUser;
103                 _userPrincipal = request.getUserPrincipal();
104             }
105         }
106     }
107 
108     public Object getAttribute(String name) {
109         if (_include || (name == null)) {
110             return _request.getAttribute(name);
111         }
112 
113         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
114             if (_named) {
115                 return null;
116             }
117             else {
118                 return _portletRequestImpl.getContextPath();
119             }
120         }
121 
122         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
123             if (_named) {
124                 return null;
125             }
126             else {
127                 return _pathInfo;
128             }
129         }
130 
131         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
132             if (_named) {
133                 return null;
134             }
135             else {
136                 return _queryString;
137             }
138         }
139 
140         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
141             if (_named) {
142                 return null;
143             }
144             else {
145                 return _requestURI;
146             }
147         }
148 
149         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
150             if (_named) {
151                 return null;
152             }
153             else {
154                 return _servletPath;
155             }
156         }
157 
158         return _request.getAttribute(name);
159     }
160 
161     public Enumeration<String> getAttributeNames() {
162         return _request.getAttributeNames();
163     }
164 
165     public String getAuthType() {
166         return _request.getAuthType();
167     }
168 
169     public String getCharacterEncoding() {
170         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
171             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
172 
173             return _request.getCharacterEncoding();
174         }
175         else {
176             return null;
177         }
178     }
179 
180     public int getContentLength() {
181         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
182             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
183 
184             return _request.getContentLength();
185         }
186         else {
187             return 0;
188         }
189     }
190 
191     public String getContentType() {
192         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
193             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
194 
195             return _request.getContentType();
196         }
197         else {
198             return null;
199         }
200     }
201 
202     public String getContextPath() {
203         return _portletRequestImpl.getContextPath();
204     }
205 
206     public Cookie[] getCookies() {
207         return _request.getCookies();
208     }
209 
210     public long getDateHeader(String name) {
211         return GetterUtil.getLong(getHeader(name), -1);
212     }
213 
214     public String getHeader(String name) {
215         HttpServletRequest request =
216             _portletRequestImpl.getHttpServletRequest();
217 
218         return request.getHeader(name);
219     }
220 
221     public Enumeration<String> getHeaderNames() {
222         HttpServletRequest request =
223             _portletRequestImpl.getHttpServletRequest();
224 
225         return request.getHeaderNames();
226     }
227 
228     public Enumeration<String> getHeaders(String name) {
229         HttpServletRequest request =
230             _portletRequestImpl.getHttpServletRequest();
231 
232         return request.getHeaders(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 _request.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 _portletRequestImpl.getLocale();
256     }
257 
258     public Enumeration<Locale> getLocales() {
259         return _request.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 HttpMethods.GET;
273         }
274         else {
275             return _request.getMethod();
276         }
277     }
278 
279     public String getParameter(String name) {
280         return _request.getParameter(name);
281     }
282 
283     public Map<String, String[]> getParameterMap() {
284         return _request.getParameterMap();
285     }
286 
287     public Enumeration<String> getParameterNames() {
288         return _request.getParameterNames();
289     }
290 
291     public String[] getParameterValues(String name) {
292         return _request.getParameterValues(name);
293     }
294 
295     public String getPathInfo() {
296         return _pathInfo;
297     }
298 
299     public String getPathTranslated() {
300         return _request.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 _request.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 _request.getRequestDispatcher(path);
328     }
329 
330     public String getRequestedSessionId() {
331         return _request.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 _request.getScheme();
360     }
361 
362     public String getServerName() {
363         return _request.getServerName();
364     }
365 
366     public int getServerPort() {
367         return _request.getServerPort();
368     }
369 
370     public String getServletPath() {
371         return _servletPath;
372     }
373 
374     public HttpSession getSession() {
375         HttpSession session = new PortletServletSession(
376             _request.getSession(), _portletRequestImpl);
377 
378         if (ServerDetector.isJetty()) {
379             try {
380                 session = wrapJettySession(session);
381             }
382             catch (Exception e) {
383                 _log.error(e, e);
384             }
385         }
386 
387         return session;
388     }
389 
390     public HttpSession getSession(boolean create) {
391         HttpSession session = new PortletServletSession(
392             _request.getSession(create), _portletRequestImpl);
393 
394         if (ServerDetector.isJetty()) {
395             try {
396                 session = wrapJettySession(session);
397             }
398             catch (Exception e) {
399                 _log.error(e, e);
400             }
401         }
402 
403         return session;
404     }
405 
406     public Principal getUserPrincipal() {
407         return _userPrincipal;
408     }
409 
410     public boolean isRequestedSessionIdFromCookie() {
411         return _request.isRequestedSessionIdFromCookie();
412     }
413 
414     public boolean isRequestedSessionIdFromURL() {
415         return _request.isRequestedSessionIdFromURL();
416     }
417 
418     /**
419      * @deprecated
420      */
421     public boolean isRequestedSessionIdFromUrl() {
422         return _request.isRequestedSessionIdFromUrl();
423     }
424 
425     public boolean isRequestedSessionIdValid() {
426         return _request.isRequestedSessionIdValid();
427     }
428 
429     public boolean isSecure() {
430         return _request.isSecure();
431     }
432 
433     public boolean isUserInRole(String role) {
434         return _portletRequestImpl.isUserInRole(role);
435     }
436 
437     public void removeAttribute(String name) {
438         _request.removeAttribute(name);
439     }
440 
441     public void setAttribute(String name, Object obj) {
442         _request.setAttribute(name, obj);
443     }
444 
445     public void setCharacterEncoding(String encoding)
446         throws UnsupportedEncodingException {
447 
448         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
449             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
450 
451             _request.setCharacterEncoding(encoding);
452         }
453     }
454 
455     protected HttpSession wrapJettySession(HttpSession session)
456         throws Exception {
457 
458         // This must be called through reflection because Resin tries to load
459         // org/mortbay/jetty/servlet/AbstractSessionManager$SessionIf
460 
461         ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
462 
463         Class<?> jettyHttpSessionWrapperClass = classLoader.loadClass(
464             "com.liferay.util.servlet.JettyHttpSessionWrapper");
465 
466         Constructor<?> constructor =
467             jettyHttpSessionWrapperClass.getConstructor(
468                 new Class[] {HttpSession.class});
469 
470         return(HttpSession)constructor.newInstance(new Object[] {session});
471     }
472 
473     private static Log _log = LogFactoryUtil.getLog(
474         PortletServletRequest.class);
475 
476     private HttpServletRequest _request;
477     private PortletRequestImpl _portletRequestImpl;
478     private String _lifecycle;
479     private String _pathInfo;
480     private String _queryString;
481     private String _remoteUser;
482     private String _requestURI;
483     private String _servletPath;
484     private Principal _userPrincipal;
485     private boolean _named;
486     private boolean _include;
487 
488 }