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.util.ArrayUtil;
026 import com.liferay.portal.kernel.util.ContentTypes;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.JavaConstants;
029 import com.liferay.portal.kernel.util.LocaleUtil;
030 import com.liferay.portal.kernel.util.ParamUtil;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.kernel.util.Validator;
033 import com.liferay.portal.kernel.xml.QName;
034 import com.liferay.portal.model.Portlet;
035 import com.liferay.portal.model.PortletApp;
036 import com.liferay.portal.model.PortletConstants;
037 import com.liferay.portal.model.PublicRenderParameter;
038 import com.liferay.portal.model.User;
039 import com.liferay.portal.service.RoleLocalServiceUtil;
040 import com.liferay.portal.servlet.NamespaceServletRequest;
041 import com.liferay.portal.servlet.SharedSessionUtil;
042 import com.liferay.portal.theme.ThemeDisplay;
043 import com.liferay.portal.util.PortalUtil;
044 import com.liferay.portal.util.WebKeys;
045 import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
046 import com.liferay.util.servlet.DynamicServletRequest;
047 import com.liferay.util.servlet.SharedSessionServletRequest;
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 import java.util.Set;
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.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
183 return StringPool.SLASH + _portletContext.getPortletContextName();
184 }
185
186 public Cookie[] getCookies() {
187 return _request.getCookies();
188 }
189
190 public String getETag() {
191 return null;
192 }
193
194 public HttpServletRequest getHttpServletRequest() {
195 return _request;
196 }
197
198 public abstract String getLifecycle();
199
200 public Locale getLocale() {
201 Locale locale = _locale;
202
203 if (locale == null) {
204 locale = _request.getLocale();
205 }
206
207 if (locale == null) {
208 locale = LocaleUtil.getDefault();
209 }
210
211 return locale;
212 }
213
214 public Enumeration<Locale> getLocales() {
215 return _request.getLocales();
216 }
217
218 public String getMethod() {
219 return _request.getMethod();
220 }
221
222 public HttpServletRequest getOriginalHttpServletRequest() {
223 return _originalRequest;
224 }
225
226 public String getParameter(String name) {
227 if (name == null) {
228 throw new IllegalArgumentException();
229 }
230
231 return _request.getParameter(name);
232 }
233
234 public Map<String, String[]> getParameterMap() {
235 return Collections.unmodifiableMap(_request.getParameterMap());
236 }
237
238 public Enumeration<String> getParameterNames() {
239 return _request.getParameterNames();
240 }
241
242 public String[] getParameterValues(String name) {
243 if (name == null) {
244 throw new IllegalArgumentException();
245 }
246
247 return _request.getParameterValues(name);
248 }
249
250 public PortalContext getPortalContext() {
251 return _portalContext;
252 }
253
254 public Portlet getPortlet() {
255 return _portlet;
256 }
257
258 public PortletContext getPortletContext() {
259 return _portletContext;
260 }
261
262 public PortletMode getPortletMode() {
263 return _portletMode;
264 }
265
266 public String getPortletName() {
267 return _portletName;
268 }
269
270 public PortletSession getPortletSession() {
271 return _session;
272 }
273
274 public PortletSession getPortletSession(boolean create) {
275
289
290
296
297 if (!create && _invalidSession) {
298 return null;
299 }
300
301 return _session;
302 }
303
304 public PortletPreferences getPreferences() {
305 return new PortletPreferencesWrapper(
306 getPreferencesImpl(), getLifecycle());
307 }
308
309 public PortletPreferencesImpl getPreferencesImpl() {
310 return (PortletPreferencesImpl)_preferences;
311 }
312
313 public Map<String, String[]> getPrivateParameterMap() {
314 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
315
316 Enumeration<String> enu = getParameterNames();
317
318 while (enu.hasMoreElements()) {
319 String name = enu.nextElement();
320
321 if (_portlet.getPublicRenderParameter(name) == null) {
322 parameterMap.put(name, getParameterValues(name));
323 }
324 }
325
326 return parameterMap;
327 }
328
329 public Enumeration<String> getProperties(String name) {
330 List<String> values = new ArrayList<String>();
331
332 String value = _portalContext.getProperty(name);
333
334 if (value != null) {
335 values.add(value);
336 }
337
338 return Collections.enumeration(values);
339 }
340
341 public String getProperty(String name) {
342 return _portalContext.getProperty(name);
343 }
344
345 public Enumeration<String> getPropertyNames() {
346 return _portalContext.getPropertyNames();
347 }
348
349 public Map<String, String[]> getPublicParameterMap() {
350 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
351
352 Enumeration<String> enu = getParameterNames();
353
354 while (enu.hasMoreElements()) {
355 String name = enu.nextElement();
356
357 if (_portlet.getPublicRenderParameter(name) != null) {
358 parameterMap.put(name, getParameterValues(name));
359 }
360 }
361
362 return parameterMap;
363 }
364
365 public String getRemoteUser() {
366 return _remoteUser;
367 }
368
369 public Map<String, String[]> getRenderParameters() {
370 return RenderParametersPool.get(_request, _plid, _portletName);
371 }
372
373 public String getRequestedSessionId() {
374 return _request.getSession().getId();
375 }
376
377 public String getResponseContentType() {
378 if (_wapTheme) {
379 return ContentTypes.XHTML_MP;
380 }
381 else {
382 return ContentTypes.TEXT_HTML;
383 }
384 }
385
386 public Enumeration<String> getResponseContentTypes() {
387 List<String> responseContentTypes = new ArrayList<String>();
388
389 responseContentTypes.add(getResponseContentType());
390
391 return Collections.enumeration(responseContentTypes);
392 }
393
394 public String getScheme() {
395 return _request.getScheme();
396 }
397
398 public String getServerName() {
399 return _request.getServerName();
400 }
401
402 public int getServerPort() {
403 return _request.getServerPort();
404 }
405
406 public LinkedHashMap<String, String> getUserInfo() {
407 return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
408 }
409
410 public Principal getUserPrincipal() {
411 return _userPrincipal;
412 }
413
414 public String getWindowID() {
415 return _portletName.concat(
416 LiferayPortletSession.LAYOUT_SEPARATOR).concat(
417 String.valueOf(_plid));
418 }
419
420 public WindowState getWindowState() {
421 return _windowState;
422 }
423
424 public void invalidateSession() {
425 _invalidSession = true;
426 }
427
428 public boolean isInvalidParameter(String name) {
429 if (Validator.isNull(name) ||
430 name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
431 name.startsWith(
432 PortletQName.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
433 PortalUtil.isReservedParameter(name)) {
434
435 return true;
436 }
437 else {
438 return false;
439 }
440 }
441
442 public boolean isPortletModeAllowed(PortletMode portletMode) {
443 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
444 return true;
445 }
446 else {
447 return _portlet.hasPortletMode(
448 getResponseContentType(), portletMode);
449 }
450 }
451
452 public boolean isPrivateRequestAttributes() {
453 return _portlet.isPrivateRequestAttributes();
454 }
455
456 public boolean isRequestedSessionIdValid() {
457 if (_session != null) {
458 return _session.isValid();
459 }
460 else {
461 return _request.isRequestedSessionIdValid();
462 }
463 }
464
465 public boolean isSecure() {
466 return _request.isSecure();
467 }
468
469 public boolean isUserInRole(String role) {
470 if (_remoteUserId <= 0) {
471 return false;
472 }
473 else {
474 try {
475 long companyId = PortalUtil.getCompanyId(_request);
476
477 String roleLink = _portlet.getRoleMappers().get(role);
478
479 if (Validator.isNotNull(roleLink)) {
480 return RoleLocalServiceUtil.hasUserRole(
481 _remoteUserId, companyId, roleLink, true);
482 }
483 else {
484 return RoleLocalServiceUtil.hasUserRole(
485 _remoteUserId, companyId, role, true);
486 }
487 }
488 catch (Exception e) {
489 _log.error(e);
490 }
491
492 return _request.isUserInRole(role);
493 }
494 }
495
496 public boolean isWindowStateAllowed(WindowState windowState) {
497 return PortalContextImpl.isSupportedWindowState(windowState);
498 }
499
500 public void removeAttribute(String name) {
501 if (name == null) {
502 throw new IllegalArgumentException();
503 }
504
505 _request.removeAttribute(name);
506 }
507
508 public void setAttribute(String name, Object obj) {
509 if (name == null) {
510 throw new IllegalArgumentException();
511 }
512
513 if (obj == null) {
514 removeAttribute(name);
515 }
516 else {
517 _request.setAttribute(name, obj);
518 }
519 }
520
521 public void setPortletMode(PortletMode portletMode) {
522 _portletMode = portletMode;
523 }
524
525 public void setWindowState(WindowState windowState) {
526 _windowState = windowState;
527 }
528
529 protected void init(
530 HttpServletRequest request, Portlet portlet,
531 InvokerPortlet invokerPortlet, PortletContext portletContext,
532 WindowState windowState, PortletMode portletMode,
533 PortletPreferences preferences, long plid) {
534
535 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
536 WebKeys.THEME_DISPLAY);
537
538 _portlet = portlet;
539 _portletName = portlet.getPortletId();
540 _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
541
542 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
543
544 Map<String, Object> sharedSessionAttributes =
545 SharedSessionUtil.getSharedSessionAttributes(request);
546
547 boolean portalSessionShared = false;
548
549 PortletApp portletApp = portlet.getPortletApp();
550
551 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
552 portalSessionShared = true;
553 }
554
555 request = new SharedSessionServletRequest(
556 request, sharedSessionAttributes, portalSessionShared);
557
558 DynamicServletRequest dynamicRequest = null;
559
560 if (portlet.isPrivateRequestAttributes()) {
561 dynamicRequest = new NamespaceServletRequest(
562 request, portletNamespace, portletNamespace, false);
563 }
564 else {
565 dynamicRequest = new DynamicServletRequest(request, false);
566 }
567
568 boolean portletFocus = false;
569
570 String ppid = ParamUtil.getString(request, "p_p_id");
571
572 if (_portletName.equals(ppid)) {
573
574
575
576 if (themeDisplay.isLifecycleRender() ||
577 themeDisplay.isLifecycleResource()) {
578
579
580
581 portletFocus = true;
582 }
583 else if (themeDisplay.isLifecycleAction() &&
584 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
585
586
587
588
589 portletFocus = true;
590 }
591 }
592
593 Map<String, String[]> renderParameters = RenderParametersPool.get(
594 request, plid, _portletName);
595
596 if (portletFocus) {
597 renderParameters = new HashMap<String, String[]>();
598
599 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
600 !LiferayWindowState.isExclusive(request) &&
601 !LiferayWindowState.isPopUp(request)) {
602
603 RenderParametersPool.put(
604 request, plid, _portletName, renderParameters);
605 }
606
607 Enumeration<String> enu = request.getParameterNames();
608
609 while (enu.hasMoreElements()) {
610 String name = enu.nextElement();
611
612 if (isInvalidParameter(name)) {
613 continue;
614 }
615
616 String[] values = request.getParameterValues(name);
617
618 if (themeDisplay.isLifecycleRender()) {
619 renderParameters.put(name, values);
620 }
621
622 if (values == null) {
623 continue;
624 }
625
626 name = removePortletNamespace(
627 invokerPortlet, portletNamespace, name);
628
629 dynamicRequest.setParameterValues(name, values);
630 }
631 }
632 else {
633 Set<String> names = renderParameters.keySet();
634
635 for (String name : names) {
636 String[] values = renderParameters.get(name);
637
638 name = removePortletNamespace(
639 invokerPortlet, portletNamespace, name);
640
641 dynamicRequest.setParameterValues(name, values);
642 }
643 }
644
645 mergePublicRenderParameters(
646 dynamicRequest, preferences, plid, renderParameters);
647
648 _request = dynamicRequest;
649 _originalRequest = request;
650 _wapTheme = BrowserSnifferUtil.isWap(_request);
651 _portlet = portlet;
652 _portalContext = new PortalContextImpl();
653 _portletContext = portletContext;
654 _windowState = windowState;
655 _portletMode = portletMode;
656 _preferences = preferences;
657 _portalSessionId = _request.getRequestedSessionId();
658 _session = new PortletSessionImpl(
659 _request, _portletName, _portletContext, _portalSessionId, plid);
660
661 long userId = PortalUtil.getUserId(request);
662 String remoteUser = request.getRemoteUser();
663
664 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
665
666 if (userPrincipalStrategy.equals(
667 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
668
669 try {
670 User user = PortalUtil.getUser(request);
671
672 if (user != null) {
673 _remoteUser = user.getScreenName();
674 _remoteUserId = user.getUserId();
675 _userPrincipal = new ProtectedPrincipal(_remoteUser);
676 }
677 }
678 catch (Exception e) {
679 _log.error(e);
680 }
681 }
682 else {
683 if ((userId > 0) && (remoteUser == null)) {
684 _remoteUser = String.valueOf(userId);
685 _remoteUserId = userId;
686 _userPrincipal = new ProtectedPrincipal(_remoteUser);
687 }
688 else {
689 _remoteUser = remoteUser;
690 _remoteUserId = GetterUtil.getLong(remoteUser);
691 _userPrincipal = request.getUserPrincipal();
692 }
693 }
694
695 _locale = themeDisplay.getLocale();
696 _plid = plid;
697 }
698
699 protected void mergePublicRenderParameters(
700 DynamicServletRequest dynamicRequest, PortletPreferences preferences,
701 long plid, Map<String, String[]> renderParameters) {
702
703 Enumeration<PublicRenderParameter> publicRenderParameters =
704 Collections.enumeration(_portlet.getPublicRenderParameters());
705
706 while (publicRenderParameters.hasMoreElements()) {
707 PublicRenderParameter publicRenderParameter =
708 publicRenderParameters.nextElement();
709
710 String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
711 publicRenderParameter);
712
713 boolean ignoreValue = GetterUtil.getBoolean(
714 preferences.getValue(ignoreKey, null));
715
716 if (ignoreValue) {
717 continue;
718 }
719
720 String mappingKey =
721 PublicRenderParameterConfiguration.getMappingKey(
722 publicRenderParameter);
723
724 String mappingValue = GetterUtil.getString(
725 preferences.getValue(mappingKey, null));
726
727 HttpServletRequest request =
728 (HttpServletRequest)dynamicRequest.getRequest();
729
730 String[] newValues = request.getParameterValues(mappingValue);
731
732 if ((newValues != null) && (newValues.length != 0)) {
733 newValues = ArrayUtil.remove(newValues, StringPool.NULL);
734 }
735
736 String name = publicRenderParameter.getIdentifier();
737
738 if ((newValues == null) || (newValues.length == 0)) {
739 QName qName = publicRenderParameter.getQName();
740
741 String[] values = _publicRenderParameters.get(
742 PortletQNameUtil.getKey(qName));
743
744 if ((values) == null || (values.length == 0) ||
745 (Validator.isNull(values[0]))) {
746
747 continue;
748 }
749
750 if (dynamicRequest.getParameter(name) == null) {
751 dynamicRequest.setParameterValues(name, values);
752 }
753 }
754 else {
755 dynamicRequest.setParameterValues(name, newValues);
756 }
757 }
758 }
759
760 protected String removePortletNamespace(
761 InvokerPortlet invokerPortlet, String portletNamespace, String name) {
762
763 if (name.startsWith(portletNamespace) &&
764 !invokerPortlet.isFacesPortlet()) {
765
766 name = name.substring(portletNamespace.length());
767 }
768
769 return name;
770 }
771
772 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
773
774 private HttpServletRequest _request;
775 private HttpServletRequest _originalRequest;
776 private boolean _wapTheme;
777 private Portlet _portlet;
778 private String _portletName;
779 private PortalContext _portalContext;
780 private PortletContext _portletContext;
781 private WindowState _windowState;
782 private PortletMode _portletMode;
783 private PortletPreferences _preferences;
784 private PortletSessionImpl _session;
785 private String _portalSessionId;
786 private boolean _invalidSession;
787 private String _remoteUser;
788 private long _remoteUserId;
789 private Principal _userPrincipal;
790 private Profile _profile;
791 private Locale _locale;
792 private long _plid;
793 private Map<String, String[]> _publicRenderParameters;
794
795 }