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