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.ccpp.PortalProfileFactory;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
21  import com.liferay.portal.kernel.portlet.LiferayWindowState;
22  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
23  import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
24  import com.liferay.portal.kernel.util.ArrayUtil;
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.JavaConstants;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Portlet;
33  import com.liferay.portal.model.PortletApp;
34  import com.liferay.portal.model.PortletConstants;
35  import com.liferay.portal.model.PublicRenderParameter;
36  import com.liferay.portal.model.User;
37  import com.liferay.portal.service.RoleLocalServiceUtil;
38  import com.liferay.portal.servlet.NamespaceServletRequest;
39  import com.liferay.portal.servlet.SharedSessionUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.QNameUtil;
43  import com.liferay.portal.util.WebKeys;
44  import com.liferay.util.servlet.DynamicServletRequest;
45  import com.liferay.util.servlet.SharedSessionServletRequest;
46  
47  import java.security.Principal;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Enumeration;
52  import java.util.HashMap;
53  import java.util.LinkedHashMap;
54  import java.util.List;
55  import java.util.Locale;
56  import java.util.Map;
57  import java.util.Set;
58  
59  import javax.ccpp.Profile;
60  
61  import javax.portlet.PortalContext;
62  import javax.portlet.PortletConfig;
63  import javax.portlet.PortletContext;
64  import javax.portlet.PortletMode;
65  import javax.portlet.PortletPreferences;
66  import javax.portlet.PortletRequest;
67  import javax.portlet.PortletResponse;
68  import javax.portlet.PortletSession;
69  import javax.portlet.WindowState;
70  
71  import javax.servlet.http.Cookie;
72  import javax.servlet.http.HttpServletRequest;
73  
74  import javax.xml.namespace.QName;
75  
76  /**
77   * <a href="PortletRequestImpl.java.html"><b><i>View Source</i></b></a>
78   *
79   * @author Brian Wing Shun Chan
80   * @author Brian Myunghun Kim
81   * @author Sergey Ponomarev
82   */
83  public abstract class PortletRequestImpl implements LiferayPortletRequest {
84  
85      public void defineObjects(
86          PortletConfig portletConfig, PortletResponse portletResponse) {
87  
88          PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
89  
90          setAttribute(WebKeys.PORTLET_ID, portletConfigImpl.getPortletId());
91          setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
92          setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
93          setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
94          setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
95      }
96  
97      public Object getAttribute(String name) {
98          if (name == null) {
99              throw new IllegalArgumentException();
100         }
101 
102         if (name.equals(PortletRequest.CCPP_PROFILE)) {
103             return getCCPPProfile();
104         }
105         else if (name.equals(PortletRequest.USER_INFO)) {
106             Object value = getUserInfo();
107 
108             if (value != null) {
109                 return value;
110             }
111         }
112 
113         return _request.getAttribute(name);
114     }
115 
116     public Enumeration<String> getAttributeNames() {
117         List<String> names = new ArrayList<String>();
118 
119         Enumeration<String> enu = _request.getAttributeNames();
120 
121         while (enu.hasMoreElements()) {
122             String name = enu.nextElement();
123 
124             if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
125                 names.add(name);
126             }
127         }
128 
129         return Collections.enumeration(names);
130     }
131 
132     public String getAuthType() {
133         return _request.getAuthType();
134     }
135 
136     public Profile getCCPPProfile() {
137         if (_profile == null) {
138             _profile = PortalProfileFactory.getCCPPProfile(_request);
139         }
140 
141         return _profile;
142     }
143 
144     public String getContextPath() {
145         //return StringPool.SLASH + _req.getContextPath();
146         return StringPool.SLASH + _portletContext.getPortletContextName();
147     }
148 
149     public Cookie[] getCookies() {
150         return _request.getCookies();
151     }
152 
153     public String getETag() {
154         return null;
155     }
156 
157     public HttpServletRequest getHttpServletRequest() {
158         return _request;
159     }
160 
161     public abstract String getLifecycle();
162 
163     public Locale getLocale() {
164         Locale locale = _locale;
165 
166         if (locale == null) {
167             locale = _request.getLocale();
168         }
169 
170         if (locale == null) {
171             locale = LocaleUtil.getDefault();
172         }
173 
174         return locale;
175     }
176 
177     public Enumeration<Locale> getLocales() {
178         return _request.getLocales();
179     }
180 
181     public String getMethod() {
182         return _request.getMethod();
183     }
184 
185     public HttpServletRequest getOriginalHttpServletRequest() {
186         return _originalRequest;
187     }
188 
189     public String getParameter(String name) {
190         if (name == null) {
191             throw new IllegalArgumentException();
192         }
193 
194         return _request.getParameter(name);
195     }
196 
197     public Map<String, String[]> getParameterMap() {
198         return Collections.unmodifiableMap(_request.getParameterMap());
199     }
200 
201     public Enumeration<String> getParameterNames() {
202         return _request.getParameterNames();
203     }
204 
205     public String[] getParameterValues(String name) {
206         if (name == null) {
207             throw new IllegalArgumentException();
208         }
209 
210         return _request.getParameterValues(name);
211     }
212 
213     public PortalContext getPortalContext() {
214         return _portalContext;
215     }
216 
217     public Portlet getPortlet() {
218         return _portlet;
219     }
220 
221     public PortletContext getPortletContext() {
222         return _portletContext;
223     }
224 
225     public PortletMode getPortletMode() {
226         return _portletMode;
227     }
228 
229     public String getPortletName() {
230         return _portletName;
231     }
232 
233     public PortletSession getPortletSession() {
234         return _session;
235     }
236 
237     public PortletSession getPortletSession(boolean create) {
238         /*HttpSession httpSes = _req.getSession(create);
239 
240         if (httpSes == null) {
241             return null;
242         }
243         else {
244             if (create) {
245                 _session = new PortletSessionImpl(
246                     _req, _portletName, _portletContext, _portalSessionId,
247                     _plid);
248             }
249 
250             return _ses;
251         }*/
252 
253         /*if ((_session == null) && create) {
254             _req.getSession(create);
255 
256             _session = new PortletSessionImpl(
257                 _req, _portletName, _portletContext, _portalSessionId, _plid);
258         }*/
259 
260         if (!create && _invalidSession) {
261             return null;
262         }
263 
264         return _session;
265     }
266 
267     public PortletPreferences getPreferences() {
268         return new PortletPreferencesWrapper(
269             getPreferencesImpl(), getLifecycle());
270     }
271 
272     public PortletPreferencesImpl getPreferencesImpl() {
273         return (PortletPreferencesImpl)_preferences;
274     }
275 
276     public Map<String, String[]> getPrivateParameterMap() {
277         Map<String, String[]> parameterMap = new HashMap<String, String[]>();
278 
279         Enumeration<String> enu = getParameterNames();
280 
281         while (enu.hasMoreElements()) {
282             String name = enu.nextElement();
283 
284             if (_portlet.getPublicRenderParameter(name) == null) {
285                 parameterMap.put(name, getParameterValues(name));
286             }
287         }
288 
289         return parameterMap;
290     }
291 
292     public Enumeration<String> getProperties(String name) {
293         List<String> values = new ArrayList<String>();
294 
295         String value = _portalContext.getProperty(name);
296 
297         if (value != null) {
298             values.add(value);
299         }
300 
301         return Collections.enumeration(values);
302     }
303 
304     public String getProperty(String name) {
305         return _portalContext.getProperty(name);
306     }
307 
308     public Enumeration<String> getPropertyNames() {
309         return _portalContext.getPropertyNames();
310     }
311 
312     public Map<String, String[]> getPublicParameterMap() {
313         Map<String, String[]> parameterMap = new HashMap<String, String[]>();
314 
315         Enumeration<String> enu = getParameterNames();
316 
317         while (enu.hasMoreElements()) {
318             String name = enu.nextElement();
319 
320             if (_portlet.getPublicRenderParameter(name) != null) {
321                 parameterMap.put(name, getParameterValues(name));
322             }
323         }
324 
325         return parameterMap;
326     }
327 
328     public String getRemoteUser() {
329         return _remoteUser;
330     }
331 
332     public Map<String, String[]> getRenderParameters() {
333         return RenderParametersPool.get(_request, _plid, _portletName);
334     }
335 
336     public String getRequestedSessionId() {
337         return _request.getSession().getId();
338     }
339 
340     public String getResponseContentType() {
341         if (_wapTheme) {
342             return ContentTypes.XHTML_MP;
343         }
344         else {
345             return ContentTypes.TEXT_HTML;
346         }
347     }
348 
349     public Enumeration<String> getResponseContentTypes() {
350         List<String> responseContentTypes = new ArrayList<String>();
351 
352         responseContentTypes.add(getResponseContentType());
353 
354         return Collections.enumeration(responseContentTypes);
355     }
356 
357     public String getScheme() {
358         return _request.getScheme();
359     }
360 
361     public String getServerName() {
362         return _request.getServerName();
363     }
364 
365     public int getServerPort() {
366         return _request.getServerPort();
367     }
368 
369     public LinkedHashMap<String, String> getUserInfo() {
370         return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
371     }
372 
373     public Principal getUserPrincipal() {
374         return _userPrincipal;
375     }
376 
377     public String getWindowID() {
378         return _portletName.concat(PortletSessionImpl.LAYOUT_SEPARATOR).concat(
379             String.valueOf(_plid));
380     }
381 
382     public WindowState getWindowState() {
383         return _windowState;
384     }
385 
386     public void invalidateSession() {
387         _invalidSession = true;
388     }
389 
390     public boolean isInvalidParameter(String name) {
391         if (Validator.isNull(name) ||
392             name.startsWith(QNameUtil.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
393             name.startsWith(
394                 QNameUtil.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
395             PortalUtil.isReservedParameter(name)) {
396 
397             return true;
398         }
399         else {
400             return false;
401         }
402     }
403 
404     public boolean isPortletModeAllowed(PortletMode portletMode) {
405         if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
406             return true;
407         }
408         else {
409             return _portlet.hasPortletMode(
410                 getResponseContentType(), portletMode);
411         }
412     }
413 
414     public boolean isPrivateRequestAttributes() {
415         return _portlet.isPrivateRequestAttributes();
416     }
417 
418     public boolean isRequestedSessionIdValid() {
419         if (_session != null) {
420             return _session.isValid();
421         }
422         else {
423             return _request.isRequestedSessionIdValid();
424         }
425     }
426 
427     public boolean isSecure() {
428         return _request.isSecure();
429     }
430 
431     public boolean isUserInRole(String role) {
432         if (_remoteUserId <= 0) {
433             return false;
434         }
435         else {
436             try {
437                 long companyId = PortalUtil.getCompanyId(_request);
438 
439                 String roleLink = _portlet.getRoleMappers().get(role);
440 
441                 if (Validator.isNotNull(roleLink)) {
442                     return RoleLocalServiceUtil.hasUserRole(
443                         _remoteUserId, companyId, roleLink, true);
444                 }
445                 else {
446                     return RoleLocalServiceUtil.hasUserRole(
447                         _remoteUserId, companyId, role, true);
448                 }
449             }
450             catch (Exception e) {
451                 _log.error(e);
452             }
453 
454             return _request.isUserInRole(role);
455         }
456     }
457 
458     public boolean isWindowStateAllowed(WindowState windowState) {
459         return PortalContextImpl.isSupportedWindowState(windowState);
460     }
461 
462     public void removeAttribute(String name) {
463         if (name == null) {
464             throw new IllegalArgumentException();
465         }
466 
467         _request.removeAttribute(name);
468     }
469 
470     public void setAttribute(String name, Object obj) {
471         if (name == null) {
472             throw new IllegalArgumentException();
473         }
474 
475         if (obj == null) {
476             removeAttribute(name);
477         }
478         else {
479             _request.setAttribute(name, obj);
480         }
481     }
482 
483     public void setPortletMode(PortletMode portletMode) {
484         _portletMode = portletMode;
485     }
486 
487     public void setWindowState(WindowState windowState) {
488         _windowState = windowState;
489     }
490 
491     protected void init(
492         HttpServletRequest request, Portlet portlet,
493         InvokerPortlet invokerPortlet, PortletContext portletContext,
494         WindowState windowState, PortletMode portletMode,
495         PortletPreferences preferences, long plid) {
496 
497         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
498             WebKeys.THEME_DISPLAY);
499 
500         _portlet = portlet;
501         _portletName = portlet.getPortletId();
502         _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
503 
504         String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
505 
506         Map<String, Object> sharedSessionAttributes =
507             SharedSessionUtil.getSharedSessionAttributes(request);
508 
509         boolean portalSessionShared = false;
510 
511         PortletApp portletApp = portlet.getPortletApp();
512 
513         if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
514             portalSessionShared = true;
515         }
516 
517         request = new SharedSessionServletRequest(
518             request, sharedSessionAttributes, portalSessionShared);
519 
520         DynamicServletRequest dynamicRequest = null;
521 
522         if (portlet.isPrivateRequestAttributes()) {
523             dynamicRequest = new NamespaceServletRequest(
524                 request, portletNamespace, portletNamespace, false);
525         }
526         else {
527             dynamicRequest = new DynamicServletRequest(request, false);
528         }
529 
530         boolean portletFocus = false;
531 
532         String ppid = ParamUtil.getString(request, "p_p_id");
533 
534         if (_portletName.equals(ppid)) {
535 
536             // Request was targeted to this portlet
537 
538             if (themeDisplay.isLifecycleRender() ||
539                 themeDisplay.isLifecycleResource()) {
540 
541                 // Request was triggered by a render or resource URL
542 
543                 portletFocus = true;
544             }
545             else if (themeDisplay.isLifecycleAction() &&
546                      getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
547 
548                 // Request was triggered by an action URL and is being processed
549                 // by com.liferay.portlet.ActionRequestImpl
550 
551                portletFocus = true;
552             }
553         }
554 
555         Map<String, String[]> renderParameters = RenderParametersPool.get(
556             request, plid, _portletName);
557 
558         if (portletFocus) {
559             renderParameters = new HashMap<String, String[]>();
560 
561             if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
562                 !LiferayWindowState.isExclusive(request) &&
563                 !LiferayWindowState.isPopUp(request)) {
564 
565                 RenderParametersPool.put(
566                     request, plid, _portletName, renderParameters);
567             }
568 
569             Enumeration<String> enu = request.getParameterNames();
570 
571             while (enu.hasMoreElements()) {
572                 String name = enu.nextElement();
573 
574                 if (isInvalidParameter(name)) {
575                     continue;
576                 }
577 
578                 String[] values = request.getParameterValues(name);
579 
580                 if (themeDisplay.isLifecycleRender()) {
581                     renderParameters.put(name, values);
582                 }
583 
584                 if (values == null) {
585                     continue;
586                 }
587 
588                 name = removePortletNamespace(
589                     invokerPortlet, portletNamespace, name);
590 
591                 dynamicRequest.setParameterValues(name, values);
592             }
593         }
594         else {
595             Set<String> names = renderParameters.keySet();
596 
597             for (String name : names) {
598                 String[] values = renderParameters.get(name);
599 
600                 name = removePortletNamespace(
601                     invokerPortlet, portletNamespace, name);
602 
603                 dynamicRequest.setParameterValues(name, values);
604             }
605         }
606 
607         mergePublicRenderParameters(
608             dynamicRequest, preferences, plid, renderParameters);
609 
610         _request = dynamicRequest;
611         _originalRequest = request;
612         _wapTheme = BrowserSnifferUtil.isWap(_request);
613         _portlet = portlet;
614         _portalContext = new PortalContextImpl();
615         _portletContext = portletContext;
616         _windowState = windowState;
617         _portletMode = portletMode;
618         _preferences = preferences;
619         _portalSessionId = _request.getRequestedSessionId();
620         _session = new PortletSessionImpl(
621             _request, _portletName, _portletContext, _portalSessionId, plid);
622 
623         long userId = PortalUtil.getUserId(request);
624         String remoteUser = request.getRemoteUser();
625 
626         String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
627 
628         if (userPrincipalStrategy.equals(
629                 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
630 
631             try {
632                 User user = PortalUtil.getUser(request);
633 
634                 _remoteUser = user.getScreenName();
635                 _remoteUserId = user.getUserId();
636                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
637             }
638             catch (Exception e) {
639                 _log.error(e);
640             }
641         }
642         else {
643             if ((userId > 0) && (remoteUser == null)) {
644                 _remoteUser = String.valueOf(userId);
645                 _remoteUserId = userId;
646                 _userPrincipal = new ProtectedPrincipal(_remoteUser);
647             }
648             else {
649                 _remoteUser = remoteUser;
650                 _remoteUserId = GetterUtil.getLong(remoteUser);
651                 _userPrincipal = request.getUserPrincipal();
652             }
653         }
654 
655         _locale = themeDisplay.getLocale();
656         _plid = plid;
657     }
658 
659     protected void mergePublicRenderParameters(
660         DynamicServletRequest dynamicRequest, PortletPreferences preferences,
661         long plid, Map<String, String[]> renderParameters) {
662 
663         Enumeration<PublicRenderParameter> publicRenderParameters =
664             Collections.enumeration(_portlet.getPublicRenderParameters());
665 
666         while (publicRenderParameters.hasMoreElements()) {
667             PublicRenderParameter publicRenderParameter =
668                 publicRenderParameters.nextElement();
669 
670             String name = publicRenderParameter.getIdentifier();
671 
672             QName qName = publicRenderParameter.getQName();
673 
674             String[] values = _publicRenderParameters.get(
675                 QNameUtil.getKey(qName));
676 
677             if ((values) == null || (values.length == 0)) {
678                 continue;
679             }
680 
681             String[] newValues = dynamicRequest.getParameterValues(name);
682 
683             if (newValues != null) {
684                 values = ArrayUtil.append(newValues, values);
685             }
686 
687             dynamicRequest.setParameterValues(name, values);
688         }
689     }
690 
691     protected void recycle() {
692         _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
693         _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
694         _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
695         _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
696 
697         _request = null;
698         _originalRequest = null;
699         _wapTheme = false;
700         _portlet = null;
701         _portletName = null;
702         _portalContext = null;
703         _portletContext = null;
704         _windowState = null;
705         _portletMode = null;
706         _preferences = null;
707         _session = null;
708         _portalSessionId = null;
709         _remoteUser = null;
710         _userPrincipal = null;
711         _profile = null;
712         _locale = null;
713         _plid = 0;
714         _publicRenderParameters = null;
715     }
716 
717     protected String removePortletNamespace(
718         InvokerPortlet invokerPortlet, String portletNamespace, String name) {
719 
720         if (name.startsWith(portletNamespace) &&
721             !invokerPortlet.isFacesPortlet()) {
722             name = name.substring(portletNamespace.length());
723         }
724 
725         return name;
726     }
727 
728     private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
729 
730     private HttpServletRequest _request;
731     private HttpServletRequest _originalRequest;
732     private boolean _wapTheme;
733     private Portlet _portlet;
734     private String _portletName;
735     private PortalContext _portalContext;
736     private PortletContext _portletContext;
737     private WindowState _windowState;
738     private PortletMode _portletMode;
739     private PortletPreferences _preferences;
740     private PortletSessionImpl _session;
741     private String _portalSessionId;
742     private boolean _invalidSession;
743     private String _remoteUser;
744     private long _remoteUserId;
745     private Principal _userPrincipal;
746     private Profile _profile;
747     private Locale _locale;
748     private long _plid;
749     private Map<String, String[]> _publicRenderParameters;
750 
751 }