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