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