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