001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.ccpp.PortalProfileFactory;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021 import com.liferay.portal.kernel.portlet.LiferayPortletSession;
022 import com.liferay.portal.kernel.portlet.LiferayWindowState;
023 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
024 import com.liferay.portal.kernel.servlet.DynamicServletRequest;
025 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
026 import com.liferay.portal.kernel.servlet.ServletContextPool;
027 import com.liferay.portal.kernel.util.ArrayUtil;
028 import com.liferay.portal.kernel.util.ContentTypes;
029 import com.liferay.portal.kernel.util.ContextPathUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.JavaConstants;
032 import com.liferay.portal.kernel.util.LocaleUtil;
033 import com.liferay.portal.kernel.util.ParamUtil;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.Validator;
036 import com.liferay.portal.kernel.xml.QName;
037 import com.liferay.portal.model.Portlet;
038 import com.liferay.portal.model.PortletApp;
039 import com.liferay.portal.model.PortletConstants;
040 import com.liferay.portal.model.PublicRenderParameter;
041 import com.liferay.portal.model.User;
042 import com.liferay.portal.service.RoleLocalServiceUtil;
043 import com.liferay.portal.servlet.NamespaceServletRequest;
044 import com.liferay.portal.servlet.SharedSessionServletRequest;
045 import com.liferay.portal.theme.ThemeDisplay;
046 import com.liferay.portal.util.PortalUtil;
047 import com.liferay.portal.util.WebKeys;
048 import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
049
050 import java.security.Principal;
051
052 import java.util.ArrayList;
053 import java.util.Collections;
054 import java.util.Enumeration;
055 import java.util.HashMap;
056 import java.util.LinkedHashMap;
057 import java.util.List;
058 import java.util.Locale;
059 import java.util.Map;
060
061 import javax.ccpp.Profile;
062
063 import javax.portlet.PortalContext;
064 import javax.portlet.PortletConfig;
065 import javax.portlet.PortletContext;
066 import javax.portlet.PortletMode;
067 import javax.portlet.PortletPreferences;
068 import javax.portlet.PortletRequest;
069 import javax.portlet.PortletResponse;
070 import javax.portlet.PortletSession;
071 import javax.portlet.WindowState;
072 import javax.portlet.filter.PortletRequestWrapper;
073
074 import javax.servlet.ServletContext;
075 import javax.servlet.http.Cookie;
076 import javax.servlet.http.HttpServletRequest;
077 import javax.servlet.http.HttpSession;
078
079
084 public abstract class PortletRequestImpl implements LiferayPortletRequest {
085
086 public static PortletRequestImpl getPortletRequestImpl(
087 PortletRequest portletRequest) {
088
089 while (!(portletRequest instanceof PortletRequestImpl)) {
090 if (portletRequest instanceof PortletRequestWrapper) {
091 PortletRequestWrapper portletRequestWrapper =
092 (PortletRequestWrapper)portletRequest;
093
094 portletRequest = portletRequestWrapper.getRequest();
095 }
096 else {
097 throw new RuntimeException(
098 "Unable to unwrap the portlet request from " +
099 portletRequest.getClass());
100 }
101 }
102
103 return (PortletRequestImpl)portletRequest;
104 }
105
106 public void cleanUp() {
107 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
108 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
109 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
110 _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
111 }
112
113 public void defineObjects(
114 PortletConfig portletConfig, PortletResponse portletResponse) {
115
116 PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
117
118 setAttribute(WebKeys.PORTLET_ID, portletConfigImpl.getPortletId());
119 setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
120 setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
121 setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
122 setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
123 }
124
125 public Object getAttribute(String name) {
126 if (name == null) {
127 throw new IllegalArgumentException();
128 }
129
130 if (name.equals(PortletRequest.CCPP_PROFILE)) {
131 return getCCPPProfile();
132 }
133 else if (name.equals(PortletRequest.USER_INFO)) {
134 Object value = getUserInfo();
135
136 if (value != null) {
137 return value;
138 }
139 }
140
141 return _request.getAttribute(name);
142 }
143
144 public Enumeration<String> getAttributeNames() {
145 List<String> names = new ArrayList<String>();
146
147 Enumeration<String> enu = _request.getAttributeNames();
148
149 while (enu.hasMoreElements()) {
150 String name = enu.nextElement();
151
152 if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
153 names.add(name);
154 }
155 }
156
157 return Collections.enumeration(names);
158 }
159
160 public String getAuthType() {
161 return _request.getAuthType();
162 }
163
164 public Profile getCCPPProfile() {
165 if (_profile == null) {
166 _profile = PortalProfileFactory.getCCPPProfile(_request);
167 }
168
169 return _profile;
170 }
171
172 public String getContextPath() {
173 PortletContextImpl portletContextImpl =
174 (PortletContextImpl)_portletContext;
175
176 ServletContext servletContext = portletContextImpl.getServletContext();
177
178 String servletContextName = servletContext.getServletContextName();
179
180 if (ServletContextPool.containsKey(servletContextName)) {
181 servletContext = ServletContextPool.get(servletContextName);
182
183 return ContextPathUtil.getContextPath(servletContext);
184 }
185
186 return StringPool.SLASH.concat(_portletContext.getPortletContextName());
187 }
188
189 public Cookie[] getCookies() {
190 return _request.getCookies();
191 }
192
193 public String getETag() {
194 return null;
195 }
196
197 public HttpServletRequest getHttpServletRequest() {
198 return _request;
199 }
200
201 public abstract String getLifecycle();
202
203 public Locale getLocale() {
204 Locale locale = _locale;
205
206 if (locale == null) {
207 locale = _request.getLocale();
208 }
209
210 if (locale == null) {
211 locale = LocaleUtil.getDefault();
212 }
213
214 return locale;
215 }
216
217 public Enumeration<Locale> getLocales() {
218 return _request.getLocales();
219 }
220
221 public String getMethod() {
222 return _request.getMethod();
223 }
224
225 public HttpServletRequest getOriginalHttpServletRequest() {
226 return _originalRequest;
227 }
228
229 public String getParameter(String name) {
230 if (name == null) {
231 throw new IllegalArgumentException();
232 }
233
234 if (_portletRequestDispatcherRequest != null) {
235 return _portletRequestDispatcherRequest.getParameter(name);
236 }
237
238 return _request.getParameter(name);
239 }
240
241 public Map<String, String[]> getParameterMap() {
242 if (_portletRequestDispatcherRequest != null) {
243 return Collections.unmodifiableMap(
244 _portletRequestDispatcherRequest.getParameterMap());
245 }
246
247 return Collections.unmodifiableMap(_request.getParameterMap());
248 }
249
250 public Enumeration<String> getParameterNames() {
251 if (_portletRequestDispatcherRequest != null) {
252 return _portletRequestDispatcherRequest.getParameterNames();
253 }
254
255 return _request.getParameterNames();
256 }
257
258 public String[] getParameterValues(String name) {
259 if (name == null) {
260 throw new IllegalArgumentException();
261 }
262
263 if (_portletRequestDispatcherRequest != null) {
264 return _portletRequestDispatcherRequest.getParameterValues(name);
265 }
266
267 return _request.getParameterValues(name);
268 }
269
270 public PortalContext getPortalContext() {
271 return _portalContext;
272 }
273
274 public Portlet getPortlet() {
275 return _portlet;
276 }
277
278 public PortletContext getPortletContext() {
279 return _portletContext;
280 }
281
282 public PortletMode getPortletMode() {
283 return _portletMode;
284 }
285
286 public String getPortletName() {
287 return _portletName;
288 }
289
290 public PortletSession getPortletSession() {
291 return _session;
292 }
293
294 public PortletSession getPortletSession(boolean create) {
295
308
309
315
316 if (!create && _invalidSession) {
317 return null;
318 }
319
320 return _session;
321 }
322
323 public PortletPreferences getPreferences() {
324 return new PortletPreferencesWrapper(
325 getPreferencesImpl(), getLifecycle());
326 }
327
328 public PortletPreferencesImpl getPreferencesImpl() {
329 return (PortletPreferencesImpl)_preferences;
330 }
331
332 public Map<String, String[]> getPrivateParameterMap() {
333 Map<String, String[]> parameterMap = null;
334
335 if (_portletRequestDispatcherRequest != null) {
336 parameterMap = _portletRequestDispatcherRequest.getParameterMap();
337 }
338 else {
339 parameterMap = _request.getParameterMap();
340 }
341
342 Map<String, String[]> privateParameterMap = null;
343
344 for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
345 String name = entry.getKey();
346
347 if (_portlet.getPublicRenderParameter(name) != null) {
348 continue;
349 }
350
351 if (privateParameterMap == null) {
352 privateParameterMap = new HashMap<String, String[]>(
353 parameterMap.size(), 1);
354 }
355
356 privateParameterMap.put(name, entry.getValue());
357 }
358
359 if (privateParameterMap == null) {
360 return Collections.emptyMap();
361 }
362
363 return Collections.unmodifiableMap(privateParameterMap);
364 }
365
366 public Enumeration<String> getProperties(String name) {
367 List<String> values = new ArrayList<String>();
368
369 String value = _portalContext.getProperty(name);
370
371 if (value != null) {
372 values.add(value);
373 }
374
375 return Collections.enumeration(values);
376 }
377
378 public String getProperty(String name) {
379 return _portalContext.getProperty(name);
380 }
381
382 public Enumeration<String> getPropertyNames() {
383 return _portalContext.getPropertyNames();
384 }
385
386 public Map<String, String[]> getPublicParameterMap() {
387 Map<String, String[]> parameterMap = null;
388
389 if (_portletRequestDispatcherRequest != null) {
390 parameterMap = _portletRequestDispatcherRequest.getParameterMap();
391 }
392 else {
393 parameterMap = _request.getParameterMap();
394 }
395
396 Map<String, String[]> publicParameterMap = null;
397
398 for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
399 String name = entry.getKey();
400
401 if (_portlet.getPublicRenderParameter(name) != null) {
402
403 if (publicParameterMap == null) {
404 publicParameterMap = new HashMap<String, String[]>(
405 parameterMap.size(), 1);
406 }
407
408 publicParameterMap.put(name, entry.getValue());
409 }
410 }
411
412 if (publicParameterMap == null) {
413 return Collections.emptyMap();
414 }
415 else {
416 return Collections.unmodifiableMap(publicParameterMap);
417 }
418 }
419
420 public String getRemoteUser() {
421 return _remoteUser;
422 }
423
424 public Map<String, String[]> getRenderParameters() {
425 return RenderParametersPool.get(_request, _plid, _portletName);
426 }
427
428 public String getRequestedSessionId() {
429 if (_session != null) {
430 return _session.getId();
431 }
432 else {
433 HttpSession session = _request.getSession(false);
434
435 if (session == null) {
436 return StringPool.BLANK;
437 }
438 else {
439 return session.getId();
440 }
441 }
442 }
443
444 public String getResponseContentType() {
445 if (_wapTheme) {
446 return ContentTypes.XHTML_MP;
447 }
448 else {
449 return ContentTypes.TEXT_HTML;
450 }
451 }
452
453 public Enumeration<String> getResponseContentTypes() {
454 List<String> responseContentTypes = new ArrayList<String>();
455
456 responseContentTypes.add(getResponseContentType());
457
458 return Collections.enumeration(responseContentTypes);
459 }
460
461 public String getScheme() {
462 return _request.getScheme();
463 }
464
465 public String getServerName() {
466 return _request.getServerName();
467 }
468
469 public int getServerPort() {
470 return _request.getServerPort();
471 }
472
473 public LinkedHashMap<String, String> getUserInfo() {
474 return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
475 }
476
477 public Principal getUserPrincipal() {
478 return _userPrincipal;
479 }
480
481 public String getWindowID() {
482 return _portletName.concat(
483 LiferayPortletSession.LAYOUT_SEPARATOR).concat(
484 String.valueOf(_plid));
485 }
486
487 public WindowState getWindowState() {
488 return _windowState;
489 }
490
491 public void invalidateSession() {
492 _invalidSession = true;
493 }
494
495 public boolean isInvalidParameter(String name) {
496 if (Validator.isNull(name) ||
497 name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
498 name.startsWith(
499 PortletQName.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
500 PortalUtil.isReservedParameter(name)) {
501
502 return true;
503 }
504 else {
505 return false;
506 }
507 }
508
509 public boolean isPortletModeAllowed(PortletMode portletMode) {
510 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
511 return true;
512 }
513 else {
514 return _portlet.hasPortletMode(
515 getResponseContentType(), portletMode);
516 }
517 }
518
519 public boolean isPrivateRequestAttributes() {
520 return _portlet.isPrivateRequestAttributes();
521 }
522
523 public boolean isRequestedSessionIdValid() {
524 return _request.isRequestedSessionIdValid();
525 }
526
527 public boolean isSecure() {
528 return _request.isSecure();
529 }
530
531 public boolean isTriggeredByActionURL() {
532 return _triggeredByActionURL;
533 }
534
535 public boolean isUserInRole(String role) {
536 if (_remoteUserId <= 0) {
537 return false;
538 }
539 else {
540 try {
541 long companyId = PortalUtil.getCompanyId(_request);
542
543 String roleLink = _portlet.getRoleMappers().get(role);
544
545 if (Validator.isNotNull(roleLink)) {
546 return RoleLocalServiceUtil.hasUserRole(
547 _remoteUserId, companyId, roleLink, true);
548 }
549 else {
550 return RoleLocalServiceUtil.hasUserRole(
551 _remoteUserId, companyId, role, true);
552 }
553 }
554 catch (Exception e) {
555 _log.error(e);
556 }
557
558 return _request.isUserInRole(role);
559 }
560 }
561
562 public boolean isWindowStateAllowed(WindowState windowState) {
563 return PortalContextImpl.isSupportedWindowState(windowState);
564 }
565
566 public void removeAttribute(String name) {
567 if (name == null) {
568 throw new IllegalArgumentException();
569 }
570
571 _request.removeAttribute(name);
572 }
573
574 public void setAttribute(String name, Object obj) {
575 if (name == null) {
576 throw new IllegalArgumentException();
577 }
578
579 if (obj == null) {
580 removeAttribute(name);
581 }
582 else {
583 _request.setAttribute(name, obj);
584 }
585 }
586
587 public void setPortletMode(PortletMode portletMode) {
588 _portletMode = portletMode;
589 }
590
591 public void setPortletRequestDispatcherRequest(HttpServletRequest request) {
592 _portletRequestDispatcherRequest = request;
593 }
594
595 public void setWindowState(WindowState windowState) {
596 _windowState = windowState;
597 }
598
599 protected void init(
600 HttpServletRequest request, Portlet portlet,
601 InvokerPortlet invokerPortlet, PortletContext portletContext,
602 WindowState windowState, PortletMode portletMode,
603 PortletPreferences preferences, long plid) {
604
605 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
606 WebKeys.THEME_DISPLAY);
607
608 _portlet = portlet;
609 _portletName = portlet.getPortletId();
610 _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
611
612 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
613
614 boolean portalSessionShared = false;
615
616 PortletApp portletApp = portlet.getPortletApp();
617
618 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
619 portalSessionShared = true;
620 }
621
622 request = new SharedSessionServletRequest(request, portalSessionShared);
623
624 DynamicServletRequest dynamicRequest = null;
625
626 if (portlet.isPrivateRequestAttributes()) {
627 dynamicRequest = new NamespaceServletRequest(
628 request, portletNamespace, portletNamespace, false);
629 }
630 else {
631 dynamicRequest = new DynamicServletRequest(request, false);
632 }
633
634 boolean portletFocus = false;
635
636 String ppid = ParamUtil.getString(request, "p_p_id");
637
638 boolean windowStateRestoreCurrentView = ParamUtil.getBoolean(
639 request, "p_p_state_rcv");
640
641 if (_portletName.equals(ppid) &&
642 !(windowStateRestoreCurrentView &&
643 portlet.isRestoreCurrentView())) {
644
645
646
647 if (themeDisplay.isLifecycleRender() ||
648 themeDisplay.isLifecycleResource()) {
649
650
651
652 portletFocus = true;
653 }
654 else if (themeDisplay.isLifecycleAction()) {
655 _triggeredByActionURL = true;
656
657 if (getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
658
659
660
661
662 portletFocus = true;
663 }
664 }
665 }
666
667 if (portletFocus) {
668 Map<String, String[]> renderParameters =
669 new HashMap<String, String[]>();
670
671 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
672 !LiferayWindowState.isExclusive(request) &&
673 !LiferayWindowState.isPopUp(request)) {
674
675 RenderParametersPool.put(
676 request, plid, _portletName, renderParameters);
677 }
678
679 Map<String, String[]> parameters = request.getParameterMap();
680
681 for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
682 String name = entry.getKey();
683
684 if (isInvalidParameter(name)) {
685 continue;
686 }
687
688 String[] values = entry.getValue();
689
690 if (themeDisplay.isLifecycleRender()) {
691 renderParameters.put(name, values);
692 }
693
694 if (values == null) {
695 continue;
696 }
697
698 name = removePortletNamespace(
699 invokerPortlet, portletNamespace, name);
700
701 dynamicRequest.setParameterValues(name, values);
702 }
703 }
704 else {
705 Map<String, String[]> renderParameters = RenderParametersPool.get(
706 request, plid, _portletName);
707
708 for (Map.Entry<String, String[]> entry :
709 renderParameters.entrySet()) {
710
711 String name = entry.getKey();
712 String[] values = entry.getValue();
713
714 name = removePortletNamespace(
715 invokerPortlet, portletNamespace, name);
716
717 dynamicRequest.setParameterValues(name, values);
718 }
719 }
720
721 mergePublicRenderParameters(dynamicRequest, preferences, plid);
722
723 _request = dynamicRequest;
724 _originalRequest = request;
725 _wapTheme = BrowserSnifferUtil.isWap(_request);
726 _portlet = portlet;
727 _portalContext = new PortalContextImpl();
728 _portletContext = portletContext;
729 _windowState = windowState;
730 _portletMode = portletMode;
731 _preferences = preferences;
732 _session = new PortletSessionImpl(
733 _request.getSession(), _portletContext, _portletName, plid);
734
735 String remoteUser = request.getRemoteUser();
736
737 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
738
739 if (userPrincipalStrategy.equals(
740 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
741
742 try {
743 User user = PortalUtil.getUser(request);
744
745 if (user != null) {
746 _remoteUser = user.getScreenName();
747 _remoteUserId = user.getUserId();
748 _userPrincipal = new ProtectedPrincipal(_remoteUser);
749 }
750 }
751 catch (Exception e) {
752 _log.error(e);
753 }
754 }
755 else {
756 long userId = PortalUtil.getUserId(request);
757
758 if ((userId > 0) && (remoteUser == null)) {
759 _remoteUser = String.valueOf(userId);
760 _remoteUserId = userId;
761 _userPrincipal = new ProtectedPrincipal(_remoteUser);
762 }
763 else {
764 _remoteUser = remoteUser;
765 _remoteUserId = GetterUtil.getLong(remoteUser);
766 _userPrincipal = request.getUserPrincipal();
767 }
768 }
769
770 _locale = themeDisplay.getLocale();
771 _plid = plid;
772 }
773
774 protected void mergePublicRenderParameters(
775 DynamicServletRequest dynamicRequest, PortletPreferences preferences,
776 long plid) {
777
778 Enumeration<PublicRenderParameter> publicRenderParameters =
779 Collections.enumeration(_portlet.getPublicRenderParameters());
780
781 while (publicRenderParameters.hasMoreElements()) {
782 PublicRenderParameter publicRenderParameter =
783 publicRenderParameters.nextElement();
784
785 String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
786 publicRenderParameter);
787
788 boolean ignoreValue = GetterUtil.getBoolean(
789 preferences.getValue(ignoreKey, null));
790
791 if (ignoreValue) {
792 continue;
793 }
794
795 String mappingKey =
796 PublicRenderParameterConfiguration.getMappingKey(
797 publicRenderParameter);
798
799 String mappingValue = GetterUtil.getString(
800 preferences.getValue(mappingKey, null));
801
802 HttpServletRequest request =
803 (HttpServletRequest)dynamicRequest.getRequest();
804
805 String[] newValues = request.getParameterValues(mappingValue);
806
807 if ((newValues != null) && (newValues.length != 0)) {
808 newValues = ArrayUtil.remove(newValues, StringPool.NULL);
809 }
810
811 String name = publicRenderParameter.getIdentifier();
812
813 if ((newValues == null) || (newValues.length == 0)) {
814 QName qName = publicRenderParameter.getQName();
815
816 String[] values = _publicRenderParameters.get(
817 PortletQNameUtil.getPublicRenderParameterName(qName));
818
819 if ((values == null) || (values.length == 0) ||
820 Validator.isNull(values[0])) {
821
822 continue;
823 }
824
825 if (dynamicRequest.getParameter(name) == null) {
826 dynamicRequest.setParameterValues(name, values);
827 }
828 }
829 else {
830 dynamicRequest.setParameterValues(name, newValues);
831 }
832 }
833 }
834
835 protected String removePortletNamespace(
836 InvokerPortlet invokerPortlet, String portletNamespace, String name) {
837
838 if (name.startsWith(portletNamespace) &&
839 ((invokerPortlet == null) || !invokerPortlet.isFacesPortlet())) {
840
841 name = name.substring(portletNamespace.length());
842 }
843
844 return name;
845 }
846
847 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
848
849 private boolean _invalidSession;
850 private Locale _locale;
851 private HttpServletRequest _originalRequest;
852 private long _plid;
853 private PortalContext _portalContext;
854 private Portlet _portlet;
855 private PortletContext _portletContext;
856 private PortletMode _portletMode;
857 private String _portletName;
858 private HttpServletRequest _portletRequestDispatcherRequest;
859 private PortletPreferences _preferences;
860 private Profile _profile;
861 private Map<String, String[]> _publicRenderParameters;
862 private String _remoteUser;
863 private long _remoteUserId;
864 private HttpServletRequest _request;
865 private PortletSessionImpl _session;
866 private boolean _triggeredByActionURL;
867 private Principal _userPrincipal;
868 private boolean _wapTheme;
869 private WindowState _windowState;
870
871 }