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