1   /**
2    * Copyright (c) 2000-2007 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.kernel.portlet.LiferayRenderRequest;
26  import com.liferay.portal.kernel.portlet.LiferayWindowState;
27  import com.liferay.portal.kernel.servlet.BrowserSniffer;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.JavaConstants;
31  import com.liferay.portal.kernel.util.LocaleUtil;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Portlet;
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.PortletContextPool;
40  import com.liferay.portal.servlet.PortletContextWrapper;
41  import com.liferay.portal.servlet.SharedSessionUtil;
42  import com.liferay.portal.util.PortalUtil;
43  import com.liferay.util.CollectionFactory;
44  import com.liferay.util.servlet.DynamicServletRequest;
45  import com.liferay.util.servlet.ProtectedPrincipal;
46  import com.liferay.util.servlet.SharedSessionServletRequest;
47  
48  import java.security.Principal;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.Enumeration;
53  import java.util.HashMap;
54  import java.util.Iterator;
55  import java.util.LinkedHashMap;
56  import java.util.List;
57  import java.util.Locale;
58  import java.util.Map;
59  
60  import javax.portlet.PortalContext;
61  import javax.portlet.PortletConfig;
62  import javax.portlet.PortletContext;
63  import javax.portlet.PortletMode;
64  import javax.portlet.PortletPreferences;
65  import javax.portlet.PortletSession;
66  import javax.portlet.RenderRequest;
67  import javax.portlet.RenderResponse;
68  import javax.portlet.WindowState;
69  
70  import javax.servlet.http.HttpServletRequest;
71  
72  import org.apache.commons.logging.Log;
73  import org.apache.commons.logging.LogFactory;
74  import org.apache.struts.Globals;
75  
76  /**
77   * <a href="RenderRequestImpl.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   */
84  public class RenderRequestImpl implements LiferayRenderRequest {
85  
86      public WindowState getWindowState() {
87          return _windowState;
88      }
89  
90      public void setWindowState(WindowState windowState) {
91          _windowState = windowState;
92      }
93  
94      public boolean isWindowStateAllowed(WindowState windowState) {
95          return PortalContextImpl.isSupportedWindowState(windowState);
96      }
97  
98      public PortletMode getPortletMode() {
99          return _portletMode;
100     }
101 
102     public void setPortletMode(PortletMode portletMode) {
103         _portletMode = portletMode;
104     }
105 
106     public boolean isPortletModeAllowed(PortletMode portletMode) {
107         if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
108             return true;
109         }
110         else {
111             return _portlet.hasPortletMode(
112                 getResponseContentType(), portletMode);
113         }
114     }
115 
116     public PortletPreferences getPreferences() {
117         return new PortletPreferencesWrapper(getPreferencesImpl(), false);
118     }
119 
120     public PortletPreferencesImpl getPreferencesImpl() {
121         return (PortletPreferencesImpl)_prefs;
122     }
123 
124     public PortletSession getPortletSession() {
125         return _ses;
126     }
127 
128     public PortletSession getPortletSession(boolean create) {
129         /*HttpSession httpSes = _req.getSession(create);
130 
131         if (httpSes == null) {
132             return null;
133         }
134         else {
135             if (create) {
136                 _ses = new PortletSessionImpl(
137                     _req, _portletName, _portletCtx, _portalSessionId,
138                     _plid);
139             }
140 
141             return _ses;
142         }*/
143 
144         /*if ((_ses == null) && create) {
145             _req.getSession(create);
146 
147             _ses = new PortletSessionImpl(
148                 _req, _portletName, _portletCtx, _portalSessionId, _plid);
149         }*/
150 
151         return _ses;
152     }
153 
154     public String getProperty(String name) {
155         return _portalCtx.getProperty(name);
156     }
157 
158     public Enumeration getProperties(String name) {
159         List values = new ArrayList();
160 
161         String value = _portalCtx.getProperty(name);
162 
163         if (value != null) {
164             values.add(value);
165         }
166 
167         return Collections.enumeration(values);
168     }
169 
170     public Enumeration getPropertyNames() {
171         return _portalCtx.getPropertyNames();
172     }
173 
174     public PortalContext getPortalContext() {
175         return _portalCtx;
176     }
177 
178     public String getAuthType() {
179         return _req.getAuthType();
180     }
181 
182     public String getContextPath() {
183         //return StringPool.SLASH + _req.getContextPath();
184         return StringPool.SLASH + _portletCtx.getPortletContextName();
185     }
186 
187     public String getRemoteUser() {
188         return _remoteUser;
189     }
190 
191     public Principal getUserPrincipal() {
192         return _userPrincipal;
193     }
194 
195     public boolean isUserInRole(String role) {
196         String remoteUser = getRemoteUser();
197 
198         if (remoteUser == null) {
199             return false;
200         }
201         else {
202             try {
203                 long companyId = PortalUtil.getCompanyId(_req);
204                 long userId = GetterUtil.getLong(remoteUser);
205 
206                 return RoleLocalServiceUtil.hasUserRole(
207                     userId, companyId, role, true);
208             }
209             catch (Exception e) {
210                 _log.warn(e);
211             }
212 
213             return _req.isUserInRole(role);
214         }
215     }
216 
217     public Object getAttribute(String name) {
218         if (name == null) {
219             throw new IllegalArgumentException();
220         }
221 
222         if (name.equals(RenderRequest.USER_INFO)) {
223             if (getRemoteUser() != null) {
224                 LinkedHashMap userInfo = new LinkedHashMap();
225 
226                 // Liferay user attributes
227 
228                 try {
229                     User user = PortalUtil.getUser(_req);
230 
231                     UserAttributes userAttributes = new UserAttributes(user);
232 
233                     // Mandatory user attributes
234 
235                     userInfo.put(
236                         UserAttributes.LIFERAY_COMPANY_ID,
237                         userAttributes.getValue(
238                             UserAttributes.LIFERAY_COMPANY_ID));
239 
240                     userInfo.put(
241                         UserAttributes.LIFERAY_USER_ID,
242                         userAttributes.getValue(
243                             UserAttributes.LIFERAY_USER_ID));
244 
245                     // Portlet user attributes
246 
247                     Iterator itr = _portlet.getUserAttributes().iterator();
248 
249                     while (itr.hasNext()) {
250                         String attrName = (String)itr.next();
251                         String attrValue = userAttributes.getValue(attrName);
252 
253                         if (attrValue != null) {
254                             userInfo.put(attrName, attrValue);
255                         }
256                     }
257                 }
258                 catch (Exception e) {
259                     e.printStackTrace();
260                 }
261 
262                 Map unmodifiableUserInfo =
263                     Collections.unmodifiableMap((Map)userInfo.clone());
264 
265                 // Custom user attributes
266 
267                 Map cuaInstances = CollectionFactory.getHashMap();
268 
269                 Iterator itr =
270                     _portlet.getCustomUserAttributes().entrySet().iterator();
271 
272                 while (itr.hasNext()) {
273                     Map.Entry entry = (Map.Entry)itr.next();
274 
275                     String attrName = (String)entry.getKey();
276                     String attrCustomClass = (String)entry.getValue();
277 
278                     CustomUserAttributes cua =
279                         (CustomUserAttributes)cuaInstances.get(attrCustomClass);
280 
281                     if (cua == null) {
282                         if (_portlet.isWARFile()) {
283                             PortletContextWrapper pcw =
284                                 (PortletContextWrapper)PortletContextPool.get(
285                                     _portlet.getRootPortletId());
286 
287                             cua =
288                                 (CustomUserAttributes)
289                                     pcw.getCustomUserAttributes().get(
290                                         attrCustomClass);
291 
292                             cua = (CustomUserAttributes)cua.clone();
293                         }
294                         else {
295                             try {
296                                 cua = (CustomUserAttributes)Class.forName(
297                                     attrCustomClass).newInstance();
298                             }
299                             catch (Exception e) {
300                                 e.printStackTrace();
301                             }
302                         }
303 
304                         cuaInstances.put(attrCustomClass, cua);
305                     }
306 
307                     if (cua != null) {
308                         String attrValue = cua.getValue(
309                             attrName, unmodifiableUserInfo);
310 
311                         if (attrValue != null) {
312                             userInfo.put(attrName, attrValue);
313                         }
314                     }
315                 }
316 
317                 return userInfo;
318             }
319         }
320 
321         return _req.getAttribute(name);
322     }
323 
324     public void setAttribute(String name, Object obj) {
325         if (name == null) {
326             throw new IllegalArgumentException();
327         }
328 
329         if (obj == null) {
330             removeAttribute(name);
331         }
332         else {
333             _req.setAttribute(name, obj);
334         }
335     }
336 
337     public void removeAttribute(String name) {
338         if (name == null) {
339             throw new IllegalArgumentException();
340         }
341 
342         _req.removeAttribute(name);
343     }
344 
345     public Enumeration getAttributeNames() {
346         List names = new ArrayList();
347 
348         Enumeration enu = _req.getAttributeNames();
349 
350         while (enu.hasMoreElements()) {
351             String name = (String)enu.nextElement();
352 
353             if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
354                 names.add(name);
355             }
356         }
357 
358         return Collections.enumeration(names);
359     }
360 
361     public String getParameter(String name) {
362         if (name == null) {
363             throw new IllegalArgumentException();
364         }
365 
366         return _req.getParameter(name);
367     }
368 
369     public Enumeration getParameterNames() {
370         return _req.getParameterNames();
371     }
372 
373     public String[] getParameterValues(String name) {
374         if (name == null) {
375             throw new IllegalArgumentException();
376         }
377 
378         return _req.getParameterValues(name);
379     }
380 
381     public Map getParameterMap() {
382         return _req.getParameterMap();
383     }
384 
385     public boolean isSecure() {
386         return _req.isSecure();
387     }
388 
389     public String getRequestedSessionId() {
390         return _req.getSession().getId();
391     }
392 
393     public boolean isRequestedSessionIdValid() {
394         if (_ses != null) {
395             return _ses.isValid();
396         }
397         else {
398             return _req.isRequestedSessionIdValid();
399         }
400     }
401 
402     public String getResponseContentType() {
403         if (_wapTheme) {
404             return ContentTypes.XHTML_MP;
405         }
406         else {
407             return ContentTypes.TEXT_HTML;
408         }
409     }
410 
411     public Enumeration getResponseContentTypes() {
412         List responseContentTypes = new ArrayList();
413 
414         responseContentTypes.add(getResponseContentType());
415 
416         return Collections.enumeration(responseContentTypes);
417     }
418 
419     public Locale getLocale() {
420         Locale locale = _locale;
421 
422         if (locale == null) {
423             locale = _req.getLocale();
424         }
425 
426         if (locale == null) {
427             locale = LocaleUtil.getDefault();
428         }
429 
430         return locale;
431     }
432 
433     public Enumeration getLocales() {
434         return _req.getLocales();
435     }
436 
437     public String getScheme() {
438         return _req.getScheme();
439     }
440 
441     public String getServerName() {
442         return _req.getServerName();
443     }
444 
445     public int getServerPort() {
446         return _req.getServerPort();
447     }
448 
449     public HttpServletRequest getHttpServletRequest() {
450         return _req;
451     }
452 
453     public String getPortletName() {
454         return _portletName;
455     }
456 
457     public Portlet getPortlet() {
458         return _portlet;
459     }
460 
461     public boolean isPrivateRequestAttributes() {
462         return _portlet.isPrivateRequestAttributes();
463     }
464 
465     public Map getRenderParameters() {
466         return RenderParametersPool.get(_req, _plid, _portletName);
467     }
468 
469     public void defineObjects(PortletConfig portletConfig, RenderResponse res) {
470         setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
471         setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
472         setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, res);
473     }
474 
475     public boolean isAction() {
476         return false;
477     }
478 
479     protected RenderRequestImpl() {
480         if (_log.isDebugEnabled()) {
481             _log.debug("Creating new instance " + hashCode());
482         }
483     }
484 
485     protected void init(
486         HttpServletRequest req, Portlet portlet, CachePortlet cachePortlet,
487         PortletContext portletCtx, WindowState windowState,
488         PortletMode portletMode, PortletPreferences prefs, long plid) {
489 
490         _portletName = portlet.getPortletId();
491 
492         String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
493 
494         Map sharedSessionAttributes =
495             SharedSessionUtil.getSharedSessionAttributes(req);
496 
497         boolean portalSessionShared = false;
498 
499         if (portlet.isWARFile() && !portlet.isPrivateSessionAttributes()) {
500             portalSessionShared = true;
501         }
502 
503         req = new SharedSessionServletRequest(
504             req, sharedSessionAttributes, portalSessionShared);
505 
506         DynamicServletRequest dynamicReq = null;
507 
508         if (portlet.isPrivateRequestAttributes()) {
509             dynamicReq = new NamespaceServletRequest(
510                 req, portletNamespace, portletNamespace, false);
511         }
512         else {
513             dynamicReq = new DynamicServletRequest(req, false);
514         }
515 
516         Enumeration enu = null;
517 
518         Map renderParameters = null;
519 
520         boolean portletFocus = false;
521 
522         if (_portletName.equals(req.getParameter("p_p_id"))) {
523 
524             // Request was targeted to this portlet
525 
526             boolean action = ParamUtil.getBoolean(req, "p_p_action");
527 
528             if (!action) {
529 
530                 // Request was triggered by a render URL
531 
532                portletFocus = true;
533             }
534             else if (action && isAction()) {
535 
536                 // Request was triggered by an action URL and is being processed
537                 // by com.liferay.portlet.ActionRequestImpl
538 
539                portletFocus = true;
540             }
541         }
542 
543         if (portletFocus) {
544             renderParameters = new HashMap();
545 
546             if (!isAction() &&
547                 !LiferayWindowState.isExclusive(req) &&
548                 !LiferayWindowState.isPopUp(req)) {
549 
550                 RenderParametersPool.put(
551                     req, plid, _portletName, renderParameters);
552             }
553 
554             enu = req.getParameterNames();
555         }
556         else {
557             renderParameters = RenderParametersPool.get(
558                 req, plid, _portletName);
559 
560             if (!_portletName.equals(req.getParameter("p_p_id"))) {
561                 _putNamespaceParams(
562                     req, portletNamespace, plid, renderParameters);
563             }
564 
565             enu = Collections.enumeration(renderParameters.keySet());
566         }
567 
568         while (enu.hasMoreElements()) {
569             String param = (String)enu.nextElement();
570 
571             if (param.startsWith(portletNamespace) &&
572                 !cachePortlet.isFacesPortlet()) {
573 
574                 String newParam =
575                     param.substring(portletNamespace.length(), param.length());
576                 String[] values = null;
577 
578                 if (portletFocus) {
579                     values = req.getParameterValues(param);
580 
581                     renderParameters.put(param, values);
582                 }
583                 else {
584                     values = (String[])renderParameters.get(param);
585                 }
586 
587                 dynamicReq.setParameterValues(newParam, values);
588             }
589             else {
590 
591                 // Do not allow reserved or null parameters to pass through.
592                 // Jetty has a bug that adds an additional null parameter
593                 // the enumeration of parameter names.
594 
595                 if (!PortalUtil.isReservedParameter(param) &&
596                     Validator.isNotNull(param)) {
597 
598                     String[] values = null;
599 
600                     if (portletFocus) {
601                         values = req.getParameterValues(param);
602 
603                         renderParameters.put(param, values);
604                     }
605                     else {
606                         values = (String[])renderParameters.get(param);
607                     }
608 
609                     dynamicReq.setParameterValues(param, values);
610                 }
611             }
612         }
613 
614         _req = dynamicReq;
615         _wapTheme = BrowserSniffer.is_wap_xhtml(_req);
616         _portlet = portlet;
617         _portalCtx = new PortalContextImpl();
618         _portletCtx = portletCtx;
619         _windowState = windowState;
620         _portletMode = portletMode;
621         _prefs = prefs;
622         _portalSessionId = _req.getRequestedSessionId();
623         _ses = new PortletSessionImpl(
624             _req, _portletName, _portletCtx, _portalSessionId, plid);
625 
626         long userId = PortalUtil.getUserId(req);
627         String remoteUser = req.getRemoteUser();
628 
629         if ((userId > 0) && (remoteUser == null)) {
630             _remoteUser = String.valueOf(userId);
631             _userPrincipal = new ProtectedPrincipal(_remoteUser);
632         }
633         else {
634             _remoteUser = remoteUser;
635             _userPrincipal = req.getUserPrincipal();
636         }
637 
638         _locale = (Locale)_req.getSession().getAttribute(Globals.LOCALE_KEY);
639         _plid = plid;
640     }
641 
642     protected void recycle() {
643         if (_log.isDebugEnabled()) {
644             _log.debug("Recycling instance " + hashCode());
645         }
646 
647         _req.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
648         _req.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
649         _req.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
650 
651         _req = null;
652         _wapTheme = false;
653         _portlet = null;
654         _portletName = null;
655         _portalCtx = null;
656         _portletCtx = null;
657         _windowState = null;
658         _portletMode = null;
659         _prefs = null;
660         _ses = null;
661         _portalSessionId = null;
662         _remoteUser = null;
663         _userPrincipal = null;
664         _locale = null;
665         _plid = 0;
666     }
667 
668     private void _putNamespaceParams(
669         HttpServletRequest req, String prefix, long plid,
670         Map renderParameters) {
671 
672         // Adds params that are prefixed with given prefix to parameters pool.
673         // Functionality added by Sergey Ponomarev to allow passing parameters
674         // to multiple portlets in one portlet URL.
675 
676         Enumeration names = req.getParameterNames();
677 
678         while (names.hasMoreElements()) {
679             String key = (String)(names.nextElement());
680 
681             if (key.startsWith(prefix)) {
682                 renderParameters.put(key, req.getParameterValues(key));
683             }
684         }
685 
686         RenderParametersPool.put(req, plid, _portletName, renderParameters);
687     }
688 
689     private static Log _log = LogFactory.getLog(RenderRequestImpl.class);
690 
691     private DynamicServletRequest _req;
692     private boolean _wapTheme;
693     private Portlet _portlet;
694     private String _portletName;
695     private PortalContext _portalCtx;
696     private PortletContext _portletCtx;
697     private WindowState _windowState;
698     private PortletMode _portletMode;
699     private PortletPreferences _prefs;
700     private PortletSessionImpl _ses;
701     private String _portalSessionId;
702     private String _remoteUser;
703     private Principal _userPrincipal;
704     private Locale _locale;
705     private long _plid;
706 
707 }