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