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                  if (user != null) {
90                      _remoteUser = user.getScreenName();
91                      _userPrincipal = new ProtectedPrincipal(_remoteUser);
92                  }
93              }
94              catch (Exception e) {
95                  _log.error(e);
96              }
97          }
98          else {
99              if ((userId > 0) && (remoteUser == null)) {
100                 _remoteUser = String.valueOf(userId);
101                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
102             }
103             else {
104                 _remoteUser = remoteUser;
105                 _userPrincipal = request.getUserPrincipal();
106             }
107         }
108     }
109 
110     public Object getAttribute(String name) {
111         if (_include || (name == null)) {
112             return _request.getAttribute(name);
113         }
114 
115         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
116             if (_named) {
117                 return null;
118             }
119             else {
120                 return _portletRequestImpl.getContextPath();
121             }
122         }
123 
124         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
125             if (_named) {
126                 return null;
127             }
128             else {
129                 return _pathInfo;
130             }
131         }
132 
133         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
134             if (_named) {
135                 return null;
136             }
137             else {
138                 return _queryString;
139             }
140         }
141 
142         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
143             if (_named) {
144                 return null;
145             }
146             else {
147                 return _requestURI;
148             }
149         }
150 
151         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
152             if (_named) {
153                 return null;
154             }
155             else {
156                 return _servletPath;
157             }
158         }
159 
160         return _request.getAttribute(name);
161     }
162 
163     public Enumeration<String> getAttributeNames() {
164         return _request.getAttributeNames();
165     }
166 
167     public String getAuthType() {
168         return _request.getAuthType();
169     }
170 
171     public String getCharacterEncoding() {
172         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
173             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
174 
175             return _request.getCharacterEncoding();
176         }
177         else {
178             return null;
179         }
180     }
181 
182     public int getContentLength() {
183         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
184             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
185 
186             return _request.getContentLength();
187         }
188         else {
189             return 0;
190         }
191     }
192 
193     public String getContentType() {
194         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
195             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
196 
197             return _request.getContentType();
198         }
199         else {
200             return null;
201         }
202     }
203 
204     public String getContextPath() {
205         return _portletRequestImpl.getContextPath();
206     }
207 
208     public Cookie[] getCookies() {
209         return _request.getCookies();
210     }
211 
212     public long getDateHeader(String name) {
213         return GetterUtil.getLong(getHeader(name), -1);
214     }
215 
216     public String getHeader(String name) {
217         HttpServletRequest request =
218             _portletRequestImpl.getHttpServletRequest();
219 
220         return request.getHeader(name);
221     }
222 
223     public Enumeration<String> getHeaderNames() {
224         HttpServletRequest request =
225             _portletRequestImpl.getHttpServletRequest();
226 
227         return request.getHeaderNames();
228     }
229 
230     public Enumeration<String> getHeaders(String name) {
231         HttpServletRequest request =
232             _portletRequestImpl.getHttpServletRequest();
233 
234         return request.getHeaders(name);
235     }
236 
237     public ServletInputStream getInputStream() throws IOException {
238         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
239             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
240 
241             return _request.getInputStream();
242         }
243         else {
244             return null;
245         }
246     }
247 
248     public int getIntHeader(String name) {
249         return GetterUtil.getInteger(getHeader(name));
250     }
251 
252     public String getLocalAddr() {
253         return null;
254     }
255 
256     public Locale getLocale() {
257         return _portletRequestImpl.getLocale();
258     }
259 
260     public Enumeration<Locale> getLocales() {
261         return _request.getLocales();
262     }
263 
264     public String getLocalName() {
265         return null;
266     }
267 
268     public int getLocalPort() {
269         return 0;
270     }
271 
272     public String getMethod() {
273         if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
274             return HttpMethods.GET;
275         }
276         else {
277             return _request.getMethod();
278         }
279     }
280 
281     public String getParameter(String name) {
282         return _request.getParameter(name);
283     }
284 
285     public Map<String, String[]> getParameterMap() {
286         return _request.getParameterMap();
287     }
288 
289     public Enumeration<String> getParameterNames() {
290         return _request.getParameterNames();
291     }
292 
293     public String[] getParameterValues(String name) {
294         return _request.getParameterValues(name);
295     }
296 
297     public String getPathInfo() {
298         return _pathInfo;
299     }
300 
301     public String getPathTranslated() {
302         return _request.getPathTranslated();
303     }
304 
305     public String getProtocol() {
306         return "HTTP/1.1";
307     }
308 
309     public String getQueryString() {
310         return _queryString;
311     }
312 
313     public BufferedReader getReader() throws IOException {
314         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
315             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
316 
317             return _request.getReader();
318         }
319         else {
320             return null;
321         }
322     }
323 
324     public String getRealPath(String path) {
325         return null;
326     }
327 
328     public RequestDispatcher getRequestDispatcher(String path) {
329         return _request.getRequestDispatcher(path);
330     }
331 
332     public String getRequestedSessionId() {
333         return _request.getRequestedSessionId();
334     }
335 
336     public String getRemoteAddr() {
337         return null;
338     }
339 
340     public String getRemoteHost() {
341         return null;
342     }
343 
344     public int getRemotePort() {
345         return 0;
346     }
347 
348     public String getRequestURI() {
349         return _requestURI;
350     }
351 
352     public StringBuffer getRequestURL() {
353         return null;
354     }
355 
356     public String getRemoteUser() {
357         return _remoteUser;
358     }
359 
360     public String getScheme() {
361         return _request.getScheme();
362     }
363 
364     public String getServerName() {
365         return _request.getServerName();
366     }
367 
368     public int getServerPort() {
369         return _request.getServerPort();
370     }
371 
372     public String getServletPath() {
373         return _servletPath;
374     }
375 
376     public HttpSession getSession() {
377         HttpSession session = new PortletServletSession(
378             _request.getSession(), _portletRequestImpl);
379 
380         if (ServerDetector.isJetty()) {
381             try {
382                 session = wrapJettySession(session);
383             }
384             catch (Exception e) {
385                 _log.error(e, e);
386             }
387         }
388 
389         return session;
390     }
391 
392     public HttpSession getSession(boolean create) {
393         HttpSession session = new PortletServletSession(
394             _request.getSession(create), _portletRequestImpl);
395 
396         if (ServerDetector.isJetty()) {
397             try {
398                 session = wrapJettySession(session);
399             }
400             catch (Exception e) {
401                 _log.error(e, e);
402             }
403         }
404 
405         return session;
406     }
407 
408     public Principal getUserPrincipal() {
409         return _userPrincipal;
410     }
411 
412     public boolean isRequestedSessionIdFromCookie() {
413         return _request.isRequestedSessionIdFromCookie();
414     }
415 
416     public boolean isRequestedSessionIdFromURL() {
417         return _request.isRequestedSessionIdFromURL();
418     }
419 
420     /**
421      * @deprecated
422      */
423     public boolean isRequestedSessionIdFromUrl() {
424         return _request.isRequestedSessionIdFromUrl();
425     }
426 
427     public boolean isRequestedSessionIdValid() {
428         return _request.isRequestedSessionIdValid();
429     }
430 
431     public boolean isSecure() {
432         return _request.isSecure();
433     }
434 
435     public boolean isUserInRole(String role) {
436         return _portletRequestImpl.isUserInRole(role);
437     }
438 
439     public void removeAttribute(String name) {
440         _request.removeAttribute(name);
441     }
442 
443     public void setAttribute(String name, Object obj) {
444         _request.setAttribute(name, obj);
445     }
446 
447     public void setCharacterEncoding(String encoding)
448         throws UnsupportedEncodingException {
449 
450         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
451             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
452 
453             _request.setCharacterEncoding(encoding);
454         }
455     }
456 
457     protected HttpSession wrapJettySession(HttpSession session)
458         throws Exception {
459 
460         // This must be called through reflection because Resin tries to load
461         // org/mortbay/jetty/servlet/AbstractSessionManager$SessionIf
462 
463         ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
464 
465         Class<?> jettyHttpSessionWrapperClass = classLoader.loadClass(
466             "com.liferay.util.servlet.JettyHttpSessionWrapper");
467 
468         Constructor<?> constructor =
469             jettyHttpSessionWrapperClass.getConstructor(
470                 new Class[] {HttpSession.class});
471 
472         return(HttpSession)constructor.newInstance(new Object[] {session});
473     }
474 
475     private static Log _log = LogFactoryUtil.getLog(
476         PortletServletRequest.class);
477 
478     private HttpServletRequest _request;
479     private PortletRequestImpl _portletRequestImpl;
480     private String _lifecycle;
481     private String _pathInfo;
482     private String _queryString;
483     private String _remoteUser;
484     private String _requestURI;
485     private String _servletPath;
486     private Principal _userPrincipal;
487     private boolean _named;
488     private boolean _include;
489 
490 }