1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.HttpMethods;
28  import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.JavaConstants;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletConstants;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.util.PortalUtil;
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  import java.util.Map;
45  
46  import javax.portlet.PortletRequest;
47  
48  import javax.servlet.RequestDispatcher;
49  import javax.servlet.ServletInputStream;
50  import javax.servlet.http.Cookie;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletRequestWrapper;
53  import javax.servlet.http.HttpSession;
54  
55  /**
56   * <a href="PortletServletRequest.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Brian Myunghun Kim
60   *
61   */
62  public class PortletServletRequest extends HttpServletRequestWrapper {
63  
64      public PortletServletRequest(
65          HttpServletRequest request, PortletRequestImpl portletRequestImpl,
66          String pathInfo, String queryString, String requestURI,
67          String servletPath, boolean named, boolean include) {
68  
69          super(request);
70  
71          _request = request;
72          _portletRequestImpl = portletRequestImpl;
73          _lifecycle = _portletRequestImpl.getLifecycle();
74          _pathInfo = pathInfo;
75          _queryString = queryString;
76          _requestURI = GetterUtil.getString(requestURI);
77          _servletPath = GetterUtil.getString(servletPath);
78          _named = named;
79          _include = include;
80  
81          long userId = PortalUtil.getUserId(request);
82          String remoteUser = request.getRemoteUser();
83  
84          Portlet portlet = portletRequestImpl.getPortlet();
85  
86          String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
87  
88          if (userPrincipalStrategy.equals(
89                  PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
90  
91              try {
92                  User user = PortalUtil.getUser(request);
93  
94                  _remoteUser = user.getScreenName();
95                  _userPrincipal = new ProtectedPrincipal(_remoteUser);
96              }
97              catch (Exception e) {
98                  _log.error(e);
99              }
100         }
101         else {
102             if ((userId > 0) && (remoteUser == null)) {
103                 _remoteUser = String.valueOf(userId);
104                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
105             }
106             else {
107                 _remoteUser = remoteUser;
108                 _userPrincipal = request.getUserPrincipal();
109             }
110         }
111     }
112 
113     public Object getAttribute(String name) {
114         if (_include || (name == null)) {
115             return _request.getAttribute(name);
116         }
117 
118         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_CONTEXT_PATH)) {
119             if (_named) {
120                 return null;
121             }
122             else {
123                 return _portletRequestImpl.getContextPath();
124             }
125         }
126 
127         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_PATH_INFO)) {
128             if (_named) {
129                 return null;
130             }
131             else {
132                 return _pathInfo;
133             }
134         }
135 
136         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING)) {
137             if (_named) {
138                 return null;
139             }
140             else {
141                 return _queryString;
142             }
143         }
144 
145         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_REQUEST_URI)) {
146             if (_named) {
147                 return null;
148             }
149             else {
150                 return _requestURI;
151             }
152         }
153 
154         if (name.equals(JavaConstants.JAVAX_SERVLET_FORWARD_SERVLET_PATH)) {
155             if (_named) {
156                 return null;
157             }
158             else {
159                 return _servletPath;
160             }
161         }
162 
163         return _request.getAttribute(name);
164     }
165 
166     public Enumeration<String> getAttributeNames() {
167         return _request.getAttributeNames();
168     }
169 
170     public String getAuthType() {
171         return _request.getAuthType();
172     }
173 
174     public String getCharacterEncoding() {
175         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
176             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
177 
178             return _request.getCharacterEncoding();
179         }
180         else {
181             return null;
182         }
183     }
184 
185     public int getContentLength() {
186         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
187             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
188 
189             return _request.getContentLength();
190         }
191         else {
192             return 0;
193         }
194     }
195 
196     public String getContentType() {
197         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
198             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
199 
200             return _request.getContentType();
201         }
202         else {
203             return null;
204         }
205     }
206 
207     public String getContextPath() {
208         return _portletRequestImpl.getContextPath();
209     }
210 
211     public Cookie[] getCookies() {
212         return _request.getCookies();
213     }
214 
215     public long getDateHeader(String name) {
216         return GetterUtil.getLong(getHeader(name), -1);
217     }
218 
219     public String getHeader(String name) {
220         HttpServletRequest request =
221             _portletRequestImpl.getHttpServletRequest();
222 
223         return request.getHeader(name);
224     }
225 
226     public Enumeration<String> getHeaderNames() {
227         HttpServletRequest request =
228             _portletRequestImpl.getHttpServletRequest();
229 
230         return request.getHeaderNames();
231     }
232 
233     public Enumeration<String> getHeaders(String name) {
234         HttpServletRequest request =
235             _portletRequestImpl.getHttpServletRequest();
236 
237         return request.getHeaders(name);
238     }
239 
240     public ServletInputStream getInputStream() throws IOException {
241         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
242             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
243 
244             return _request.getInputStream();
245         }
246         else {
247             return null;
248         }
249     }
250 
251     public int getIntHeader(String name) {
252         return GetterUtil.getInteger(getHeader(name));
253     }
254 
255     public String getLocalAddr() {
256         return null;
257     }
258 
259     public Locale getLocale() {
260         return _portletRequestImpl.getLocale();
261     }
262 
263     public Enumeration<Locale> getLocales() {
264         return _request.getLocales();
265     }
266 
267     public String getLocalName() {
268         return null;
269     }
270 
271     public int getLocalPort() {
272         return 0;
273     }
274 
275     public String getMethod() {
276         if (_lifecycle.equals(PortletRequest.RENDER_PHASE)) {
277             return HttpMethods.GET;
278         }
279         else {
280             return _request.getMethod();
281         }
282     }
283 
284     public String getParameter(String name) {
285         return _request.getParameter(name);
286     }
287 
288     public Map<String, String[]> getParameterMap() {
289         return _request.getParameterMap();
290     }
291 
292     public Enumeration<String> getParameterNames() {
293         return _request.getParameterNames();
294     }
295 
296     public String[] getParameterValues(String name) {
297         return _request.getParameterValues(name);
298     }
299 
300     public String getPathInfo() {
301         return _pathInfo;
302     }
303 
304     public String getPathTranslated() {
305         return _request.getPathTranslated();
306     }
307 
308     public String getProtocol() {
309         return "HTTP/1.1";
310     }
311 
312     public String getQueryString() {
313         return _queryString;
314     }
315 
316     public BufferedReader getReader() throws IOException {
317         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
318             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
319 
320             return _request.getReader();
321         }
322         else {
323             return null;
324         }
325     }
326 
327     public String getRealPath(String path) {
328         return null;
329     }
330 
331     public RequestDispatcher getRequestDispatcher(String path) {
332         return _request.getRequestDispatcher(path);
333     }
334 
335     public String getRequestedSessionId() {
336         return _request.getRequestedSessionId();
337     }
338 
339     public String getRemoteAddr() {
340         return null;
341     }
342 
343     public String getRemoteHost() {
344         return null;
345     }
346 
347     public int getRemotePort() {
348         return 0;
349     }
350 
351     public String getRequestURI() {
352         return _requestURI;
353     }
354 
355     public StringBuffer getRequestURL() {
356         return null;
357     }
358 
359     public String getRemoteUser() {
360         return _remoteUser;
361     }
362 
363     public String getScheme() {
364         return _request.getScheme();
365     }
366 
367     public String getServerName() {
368         return _request.getServerName();
369     }
370 
371     public int getServerPort() {
372         return _request.getServerPort();
373     }
374 
375     public String getServletPath() {
376         return _servletPath;
377     }
378 
379     public HttpSession getSession() {
380         return new PortletServletSession(
381             _request.getSession(), _portletRequestImpl);
382     }
383 
384     public HttpSession getSession(boolean create) {
385         return new PortletServletSession(
386             _request.getSession(create), _portletRequestImpl);
387     }
388 
389     public Principal getUserPrincipal() {
390         return _userPrincipal;
391     }
392 
393     public boolean isRequestedSessionIdFromCookie() {
394         return _request.isRequestedSessionIdFromCookie();
395     }
396 
397     public boolean isRequestedSessionIdFromURL() {
398         return _request.isRequestedSessionIdFromURL();
399     }
400 
401     /**
402      * @deprecated
403      */
404     public boolean isRequestedSessionIdFromUrl() {
405         return _request.isRequestedSessionIdFromUrl();
406     }
407 
408     public boolean isRequestedSessionIdValid() {
409         return _request.isRequestedSessionIdValid();
410     }
411 
412     public boolean isSecure() {
413         return _request.isSecure();
414     }
415 
416     public boolean isUserInRole(String role) {
417         return _portletRequestImpl.isUserInRole(role);
418     }
419 
420     public void removeAttribute(String name) {
421         _request.removeAttribute(name);
422     }
423 
424     public void setAttribute(String name, Object obj) {
425         _request.setAttribute(name, obj);
426     }
427 
428     public void setCharacterEncoding(String encoding)
429         throws UnsupportedEncodingException {
430 
431         if (_lifecycle.equals(PortletRequest.ACTION_PHASE) ||
432             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
433 
434             _request.setCharacterEncoding(encoding);
435         }
436     }
437 
438     private static Log _log =
439          LogFactoryUtil.getLog(PortletServletRequest.class);
440 
441     private HttpServletRequest _request;
442     private PortletRequestImpl _portletRequestImpl;
443     private String _lifecycle;
444     private String _pathInfo;
445     private String _queryString;
446     private String _remoteUser;
447     private String _requestURI;
448     private String _servletPath;
449     private Principal _userPrincipal;
450     private boolean _named;
451     private boolean _include;
452 
453 }