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
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.http.Cookie;
075 import javax.servlet.http.HttpServletRequest;
076
077
082 public abstract class PortletRequestImpl implements LiferayPortletRequest {
083
084 public static PortletRequestImpl getPortletRequestImpl(
085 PortletRequest portletRequest) {
086
087 PortletRequestImpl portletRequestImpl = null;
088
089 if (portletRequest instanceof PortletRequestImpl) {
090 portletRequestImpl = (PortletRequestImpl)portletRequest;
091 }
092 else {
093
094
095
096 try {
097 Method method = portletRequest.getClass().getMethod(
098 "getRequest");
099
100 Object obj = method.invoke(portletRequest, (Object[])null);
101
102 portletRequestImpl = getPortletRequestImpl((PortletRequest)obj);
103 }
104 catch (Exception e) {
105 throw new RuntimeException(
106 "Unable to get the portlet request from " +
107 portletRequest.getClass().getName());
108 }
109 }
110
111 return portletRequestImpl;
112 }
113
114 public void cleanUp() {
115 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
116 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
117 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
118 _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
119 }
120
121 public void defineObjects(
122 PortletConfig portletConfig, PortletResponse portletResponse) {
123
124 PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
125
126 setAttribute(WebKeys.PORTLET_ID, portletConfigImpl.getPortletId());
127 setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
128 setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
129 setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
130 setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
131 }
132
133 public Object getAttribute(String name) {
134 if (name == null) {
135 throw new IllegalArgumentException();
136 }
137
138 if (name.equals(PortletRequest.CCPP_PROFILE)) {
139 return getCCPPProfile();
140 }
141 else if (name.equals(PortletRequest.USER_INFO)) {
142 Object value = getUserInfo();
143
144 if (value != null) {
145 return value;
146 }
147 }
148
149 return _request.getAttribute(name);
150 }
151
152 public Enumeration<String> getAttributeNames() {
153 List<String> names = new ArrayList<String>();
154
155 Enumeration<String> enu = _request.getAttributeNames();
156
157 while (enu.hasMoreElements()) {
158 String name = enu.nextElement();
159
160 if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
161 names.add(name);
162 }
163 }
164
165 return Collections.enumeration(names);
166 }
167
168 public String getAuthType() {
169 return _request.getAuthType();
170 }
171
172 public Profile getCCPPProfile() {
173 if (_profile == null) {
174 _profile = PortalProfileFactory.getCCPPProfile(_request);
175 }
176
177 return _profile;
178 }
179
180 public String getContextPath() {
181
182 return StringPool.SLASH + _portletContext.getPortletContextName();
183 }
184
185 public Cookie[] getCookies() {
186 return _request.getCookies();
187 }
188
189 public String getETag() {
190 return null;
191 }
192
193 public HttpServletRequest getHttpServletRequest() {
194 return _request;
195 }
196
197 public abstract String getLifecycle();
198
199 public Locale getLocale() {
200 Locale locale = _locale;
201
202 if (locale == null) {
203 locale = _request.getLocale();
204 }
205
206 if (locale == null) {
207 locale = LocaleUtil.getDefault();
208 }
209
210 return locale;
211 }
212
213 public Enumeration<Locale> getLocales() {
214 return _request.getLocales();
215 }
216
217 public String getMethod() {
218 return _request.getMethod();
219 }
220
221 public HttpServletRequest getOriginalHttpServletRequest() {
222 return _originalRequest;
223 }
224
225 public String getParameter(String name) {
226 if (name == null) {
227 throw new IllegalArgumentException();
228 }
229
230 return _request.getParameter(name);
231 }
232
233 public Map<String, String[]> getParameterMap() {
234 return Collections.unmodifiableMap(_request.getParameterMap());
235 }
236
237 public Enumeration<String> getParameterNames() {
238 return _request.getParameterNames();
239 }
240
241 public String[] getParameterValues(String name) {
242 if (name == null) {
243 throw new IllegalArgumentException();
244 }
245
246 return _request.getParameterValues(name);
247 }
248
249 public PortalContext getPortalContext() {
250 return _portalContext;
251 }
252
253 public Portlet getPortlet() {
254 return _portlet;
255 }
256
257 public PortletContext getPortletContext() {
258 return _portletContext;
259 }
260
261 public PortletMode getPortletMode() {
262 return _portletMode;
263 }
264
265 public String getPortletName() {
266 return _portletName;
267 }
268
269 public PortletSession getPortletSession() {
270 return _session;
271 }
272
273 public PortletSession getPortletSession(boolean create) {
274
288
289
295
296 if (!create && _invalidSession) {
297 return null;
298 }
299
300 return _session;
301 }
302
303 public PortletPreferences getPreferences() {
304 return new PortletPreferencesWrapper(
305 getPreferencesImpl(), getLifecycle());
306 }
307
308 public PortletPreferencesImpl getPreferencesImpl() {
309 return (PortletPreferencesImpl)_preferences;
310 }
311
312 public Map<String, String[]> getPrivateParameterMap() {
313 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
314
315 Enumeration<String> enu = getParameterNames();
316
317 while (enu.hasMoreElements()) {
318 String name = enu.nextElement();
319
320 if (_portlet.getPublicRenderParameter(name) == null) {
321 parameterMap.put(name, getParameterValues(name));
322 }
323 }
324
325 return parameterMap;
326 }
327
328 public Enumeration<String> getProperties(String name) {
329 List<String> values = new ArrayList<String>();
330
331 String value = _portalContext.getProperty(name);
332
333 if (value != null) {
334 values.add(value);
335 }
336
337 return Collections.enumeration(values);
338 }
339
340 public String getProperty(String name) {
341 return _portalContext.getProperty(name);
342 }
343
344 public Enumeration<String> getPropertyNames() {
345 return _portalContext.getPropertyNames();
346 }
347
348 public Map<String, String[]> getPublicParameterMap() {
349 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
350
351 Enumeration<String> enu = getParameterNames();
352
353 while (enu.hasMoreElements()) {
354 String name = enu.nextElement();
355
356 if (_portlet.getPublicRenderParameter(name) != null) {
357 parameterMap.put(name, getParameterValues(name));
358 }
359 }
360
361 return parameterMap;
362 }
363
364 public String getRemoteUser() {
365 return _remoteUser;
366 }
367
368 public Map<String, String[]> getRenderParameters() {
369 return RenderParametersPool.get(_request, _plid, _portletName);
370 }
371
372 public String getRequestedSessionId() {
373 return _request.getSession().getId();
374 }
375
376 public String getResponseContentType() {
377 if (_wapTheme) {
378 return ContentTypes.XHTML_MP;
379 }
380 else {
381 return ContentTypes.TEXT_HTML;
382 }
383 }
384
385 public Enumeration<String> getResponseContentTypes() {
386 List<String> responseContentTypes = new ArrayList<String>();
387
388 responseContentTypes.add(getResponseContentType());
389
390 return Collections.enumeration(responseContentTypes);
391 }
392
393 public String getScheme() {
394 return _request.getScheme();
395 }
396
397 public String getServerName() {
398 return _request.getServerName();
399 }
400
401 public int getServerPort() {
402 return _request.getServerPort();
403 }
404
405 public LinkedHashMap<String, String> getUserInfo() {
406 return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
407 }
408
409 public Principal getUserPrincipal() {
410 return _userPrincipal;
411 }
412
413 public String getWindowID() {
414 return _portletName.concat(
415 LiferayPortletSession.LAYOUT_SEPARATOR).concat(
416 String.valueOf(_plid));
417 }
418
419 public WindowState getWindowState() {
420 return _windowState;
421 }
422
423 public void invalidateSession() {
424 _invalidSession = true;
425 }
426
427 public boolean isInvalidParameter(String name) {
428 if (Validator.isNull(name) ||
429 name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
430 name.startsWith(
431 PortletQName.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
432 PortalUtil.isReservedParameter(name)) {
433
434 return true;
435 }
436 else {
437 return false;
438 }
439 }
440
441 public boolean isPortletModeAllowed(PortletMode portletMode) {
442 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
443 return true;
444 }
445 else {
446 return _portlet.hasPortletMode(
447 getResponseContentType(), portletMode);
448 }
449 }
450
451 public boolean isPrivateRequestAttributes() {
452 return _portlet.isPrivateRequestAttributes();
453 }
454
455 public boolean isRequestedSessionIdValid() {
456 if (_session != null) {
457 return _session.isValid();
458 }
459 else {
460 return _request.isRequestedSessionIdValid();
461 }
462 }
463
464 public boolean isSecure() {
465 return _request.isSecure();
466 }
467
468 public boolean isUserInRole(String role) {
469 if (_remoteUserId <= 0) {
470 return false;
471 }
472 else {
473 try {
474 long companyId = PortalUtil.getCompanyId(_request);
475
476 String roleLink = _portlet.getRoleMappers().get(role);
477
478 if (Validator.isNotNull(roleLink)) {
479 return RoleLocalServiceUtil.hasUserRole(
480 _remoteUserId, companyId, roleLink, true);
481 }
482 else {
483 return RoleLocalServiceUtil.hasUserRole(
484 _remoteUserId, companyId, role, true);
485 }
486 }
487 catch (Exception e) {
488 _log.error(e);
489 }
490
491 return _request.isUserInRole(role);
492 }
493 }
494
495 public boolean isWindowStateAllowed(WindowState windowState) {
496 return PortalContextImpl.isSupportedWindowState(windowState);
497 }
498
499 public void removeAttribute(String name) {
500 if (name == null) {
501 throw new IllegalArgumentException();
502 }
503
504 _request.removeAttribute(name);
505 }
506
507 public void setAttribute(String name, Object obj) {
508 if (name == null) {
509 throw new IllegalArgumentException();
510 }
511
512 if (obj == null) {
513 removeAttribute(name);
514 }
515 else {
516 _request.setAttribute(name, obj);
517 }
518 }
519
520 public void setPortletMode(PortletMode portletMode) {
521 _portletMode = portletMode;
522 }
523
524 public void setWindowState(WindowState windowState) {
525 _windowState = windowState;
526 }
527
528 protected void init(
529 HttpServletRequest request, Portlet portlet,
530 InvokerPortlet invokerPortlet, PortletContext portletContext,
531 WindowState windowState, PortletMode portletMode,
532 PortletPreferences preferences, long plid) {
533
534 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
535 WebKeys.THEME_DISPLAY);
536
537 _portlet = portlet;
538 _portletName = portlet.getPortletId();
539 _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
540
541 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
542
543 Map<String, Object> sharedSessionAttributes =
544 SharedSessionUtil.getSharedSessionAttributes(request);
545
546 boolean portalSessionShared = false;
547
548 PortletApp portletApp = portlet.getPortletApp();
549
550 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
551 portalSessionShared = true;
552 }
553
554 request = new SharedSessionServletRequest(
555 request, sharedSessionAttributes, portalSessionShared);
556
557 DynamicServletRequest dynamicRequest = null;
558
559 if (portlet.isPrivateRequestAttributes()) {
560 dynamicRequest = new NamespaceServletRequest(
561 request, portletNamespace, portletNamespace, false);
562 }
563 else {
564 dynamicRequest = new DynamicServletRequest(request, false);
565 }
566
567 boolean portletFocus = false;
568
569 String ppid = ParamUtil.getString(request, "p_p_id");
570
571 if (_portletName.equals(ppid)) {
572
573
574
575 if (themeDisplay.isLifecycleRender() ||
576 themeDisplay.isLifecycleResource()) {
577
578
579
580 portletFocus = true;
581 }
582 else if (themeDisplay.isLifecycleAction() &&
583 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
584
585
586
587
588 portletFocus = true;
589 }
590 }
591
592 Map<String, String[]> renderParameters = RenderParametersPool.get(
593 request, plid, _portletName);
594
595 if (portletFocus) {
596 renderParameters = new HashMap<String, String[]>();
597
598 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
599 !LiferayWindowState.isExclusive(request) &&
600 !LiferayWindowState.isPopUp(request)) {
601
602 RenderParametersPool.put(
603 request, plid, _portletName, renderParameters);
604 }
605
606 Enumeration<String> enu = request.getParameterNames();
607
608 while (enu.hasMoreElements()) {
609 String name = enu.nextElement();
610
611 if (isInvalidParameter(name)) {
612 continue;
613 }
614
615 String[] values = request.getParameterValues(name);
616
617 if (themeDisplay.isLifecycleRender()) {
618 renderParameters.put(name, values);
619 }
620
621 if (values == null) {
622 continue;
623 }
624
625 name = removePortletNamespace(
626 invokerPortlet, portletNamespace, name);
627
628 dynamicRequest.setParameterValues(name, values);
629 }
630 }
631 else {
632 for (Map.Entry<String, String[]> entry :
633 renderParameters.entrySet()) {
634
635 String name = entry.getKey();
636 String[] values = entry.getValue();
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 }