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