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