1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.ccpp.PortalProfileFactory;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
29 import com.liferay.portal.kernel.portlet.LiferayWindowState;
30 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
31 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
32 import com.liferay.portal.kernel.util.ContentTypes;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.JavaConstants;
35 import com.liferay.portal.kernel.util.LocaleUtil;
36 import com.liferay.portal.kernel.util.ObjectValuePair;
37 import com.liferay.portal.kernel.util.ParamUtil;
38 import com.liferay.portal.kernel.util.StringPool;
39 import com.liferay.portal.kernel.util.Validator;
40 import com.liferay.portal.model.Portlet;
41 import com.liferay.portal.model.PortletApp;
42 import com.liferay.portal.model.PortletConstants;
43 import com.liferay.portal.model.PublicRenderParameter;
44 import com.liferay.portal.model.User;
45 import com.liferay.portal.service.RoleLocalServiceUtil;
46 import com.liferay.portal.servlet.NamespaceServletRequest;
47 import com.liferay.portal.servlet.SharedSessionUtil;
48 import com.liferay.portal.theme.ThemeDisplay;
49 import com.liferay.portal.util.PortalUtil;
50 import com.liferay.portal.util.QNameUtil;
51 import com.liferay.portal.util.WebKeys;
52 import com.liferay.util.servlet.DynamicServletRequest;
53 import com.liferay.util.servlet.SharedSessionServletRequest;
54
55 import java.security.Principal;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.Enumeration;
60 import java.util.HashMap;
61 import java.util.LinkedHashMap;
62 import java.util.List;
63 import java.util.Locale;
64 import java.util.Map;
65
66 import javax.ccpp.Profile;
67
68 import javax.portlet.PortalContext;
69 import javax.portlet.PortletConfig;
70 import javax.portlet.PortletContext;
71 import javax.portlet.PortletMode;
72 import javax.portlet.PortletPreferences;
73 import javax.portlet.PortletRequest;
74 import javax.portlet.PortletResponse;
75 import javax.portlet.PortletSession;
76 import javax.portlet.WindowState;
77
78 import javax.servlet.http.Cookie;
79 import javax.servlet.http.HttpServletRequest;
80
81 import javax.xml.namespace.QName;
82
83
91 public abstract class PortletRequestImpl implements LiferayPortletRequest {
92
93 public void defineObjects(
94 PortletConfig portletConfig, PortletResponse portletResponse) {
95
96 PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
97
98 setAttribute(WebKeys.PORTLET_ID, portletConfigImpl.getPortletId());
99 setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
100 setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
101 setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
102 setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
103 }
104
105 public Object getAttribute(String name) {
106 if (name == null) {
107 throw new IllegalArgumentException();
108 }
109
110 if (name.equals(PortletRequest.CCPP_PROFILE)) {
111 return getCCPPProfile();
112 }
113 else if (name.equals(PortletRequest.USER_INFO)) {
114 Object value = getUserInfo();
115
116 if (value != null) {
117 return value;
118 }
119 }
120
121 return _request.getAttribute(name);
122 }
123
124 public Enumeration<String> getAttributeNames() {
125 List<String> names = new ArrayList<String>();
126
127 Enumeration<String> enu = _request.getAttributeNames();
128
129 while (enu.hasMoreElements()) {
130 String name = enu.nextElement();
131
132 if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
133 names.add(name);
134 }
135 }
136
137 return Collections.enumeration(names);
138 }
139
140 public String getAuthType() {
141 return _request.getAuthType();
142 }
143
144 public Profile getCCPPProfile() {
145 if (_profile == null) {
146 _profile = PortalProfileFactory.getCCPPProfile(_request);
147 }
148
149 return _profile;
150 }
151
152 public String getContextPath() {
153 return StringPool.SLASH + _portletContext.getPortletContextName();
155 }
156
157 public Cookie[] getCookies() {
158 return _request.getCookies();
159 }
160
161 public String getETag() {
162 return null;
163 }
164
165 public HttpServletRequest getHttpServletRequest() {
166 return _request;
167 }
168
169 public abstract String getLifecycle();
170
171 public Locale getLocale() {
172 Locale locale = _locale;
173
174 if (locale == null) {
175 locale = _request.getLocale();
176 }
177
178 if (locale == null) {
179 locale = LocaleUtil.getDefault();
180 }
181
182 return locale;
183 }
184
185 public Enumeration<Locale> getLocales() {
186 return _request.getLocales();
187 }
188
189 public String getMethod() {
190 return _request.getMethod();
191 }
192
193 public HttpServletRequest getOriginalHttpServletRequest() {
194 return _originalRequest;
195 }
196
197 public String getParameter(String name) {
198 if (name == null) {
199 throw new IllegalArgumentException();
200 }
201
202 return _request.getParameter(name);
203 }
204
205 public Map<String, String[]> getParameterMap() {
206 return Collections.unmodifiableMap(_request.getParameterMap());
207 }
208
209 public Enumeration<String> getParameterNames() {
210 return _request.getParameterNames();
211 }
212
213 public String[] getParameterValues(String name) {
214 if (name == null) {
215 throw new IllegalArgumentException();
216 }
217
218 return _request.getParameterValues(name);
219 }
220
221 public PortalContext getPortalContext() {
222 return _portalContext;
223 }
224
225 public Portlet getPortlet() {
226 return _portlet;
227 }
228
229 public PortletContext getPortletContext() {
230 return _portletContext;
231 }
232
233 public PortletMode getPortletMode() {
234 return _portletMode;
235 }
236
237 public String getPortletName() {
238 return _portletName;
239 }
240
241 public PortletSession getPortletSession() {
242 return _session;
243 }
244
245 public PortletSession getPortletSession(boolean create) {
246
260
261
267
268 if (!create && _invalidSession) {
269 return null;
270 }
271
272 return _session;
273 }
274
275 public PortletPreferences getPreferences() {
276 return new PortletPreferencesWrapper(
277 getPreferencesImpl(), getLifecycle());
278 }
279
280 public PortletPreferencesImpl getPreferencesImpl() {
281 return (PortletPreferencesImpl)_prefs;
282 }
283
284 public Map<String, String[]> getPrivateParameterMap() {
285 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
286
287 Enumeration<String> enu = getParameterNames();
288
289 while (enu.hasMoreElements()) {
290 String name = enu.nextElement();
291
292 if (_portlet.getPublicRenderParameter(name) == null) {
293 parameterMap.put(name, getParameterValues(name));
294 }
295 }
296
297 return parameterMap;
298 }
299
300 public Enumeration<String> getProperties(String name) {
301 List<String> values = new ArrayList<String>();
302
303 String value = _portalContext.getProperty(name);
304
305 if (value != null) {
306 values.add(value);
307 }
308
309 return Collections.enumeration(values);
310 }
311
312 public String getProperty(String name) {
313 return _portalContext.getProperty(name);
314 }
315
316 public Enumeration<String> getPropertyNames() {
317 return _portalContext.getPropertyNames();
318 }
319
320 public Map<String, String[]> getPublicParameterMap() {
321 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
322
323 Enumeration<String> enu = getParameterNames();
324
325 while (enu.hasMoreElements()) {
326 String name = enu.nextElement();
327
328 if (_portlet.getPublicRenderParameter(name) != null) {
329 parameterMap.put(name, getParameterValues(name));
330 }
331 }
332
333 return parameterMap;
334 }
335
336 public String getRemoteUser() {
337 return _remoteUser;
338 }
339
340 public Map<String, String[]> getRenderParameters() {
341 return RenderParametersPool.get(_request, _plid, _portletName);
342 }
343
344 public String getRequestedSessionId() {
345 return _request.getSession().getId();
346 }
347
348 public String getResponseContentType() {
349 if (_wapTheme) {
350 return ContentTypes.XHTML_MP;
351 }
352 else {
353 return ContentTypes.TEXT_HTML;
354 }
355 }
356
357 public Enumeration<String> getResponseContentTypes() {
358 List<String> responseContentTypes = new ArrayList<String>();
359
360 responseContentTypes.add(getResponseContentType());
361
362 return Collections.enumeration(responseContentTypes);
363 }
364
365 public String getScheme() {
366 return _request.getScheme();
367 }
368
369 public String getServerName() {
370 return _request.getServerName();
371 }
372
373 public int getServerPort() {
374 return _request.getServerPort();
375 }
376
377 public LinkedHashMap<String, String> getUserInfo() {
378 return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
379 }
380
381 public Principal getUserPrincipal() {
382 return _userPrincipal;
383 }
384
385 public String getWindowID() {
386 StringBuilder sb = new StringBuilder();
387
388 sb.append(_portletName);
389 sb.append(PortletSessionImpl.LAYOUT_SEPARATOR);
390 sb.append(_plid);
391
392 return sb.toString();
393 }
394
395 public WindowState getWindowState() {
396 return _windowState;
397 }
398
399 public void invalidateSession() {
400 _invalidSession = true;
401 }
402
403 public boolean isPortletModeAllowed(PortletMode portletMode) {
404 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
405 return true;
406 }
407 else {
408 return _portlet.hasPortletMode(
409 getResponseContentType(), portletMode);
410 }
411 }
412
413 public boolean isPrivateRequestAttributes() {
414 return _portlet.isPrivateRequestAttributes();
415 }
416
417 public boolean isRequestedSessionIdValid() {
418 if (_session != null) {
419 return _session.isValid();
420 }
421 else {
422 return _request.isRequestedSessionIdValid();
423 }
424 }
425
426 public boolean isSecure() {
427 return _request.isSecure();
428 }
429
430 public boolean isUserInRole(String role) {
431 if (_remoteUserId <= 0) {
432 return false;
433 }
434 else {
435 try {
436 long companyId = PortalUtil.getCompanyId(_request);
437
438 String roleLink = _portlet.getRoleMappers().get(role);
439
440 if (Validator.isNotNull(roleLink)) {
441 return RoleLocalServiceUtil.hasUserRole(
442 _remoteUserId, companyId, roleLink, true);
443 }
444 else {
445 return RoleLocalServiceUtil.hasUserRole(
446 _remoteUserId, companyId, role, true);
447 }
448 }
449 catch (Exception e) {
450 _log.error(e);
451 }
452
453 return _request.isUserInRole(role);
454 }
455 }
456
457 public boolean isWindowStateAllowed(WindowState windowState) {
458 return PortalContextImpl.isSupportedWindowState(windowState);
459 }
460
461 public void removeAttribute(String name) {
462 if (name == null) {
463 throw new IllegalArgumentException();
464 }
465
466 _request.removeAttribute(name);
467 }
468
469 public void setAttribute(String name, Object obj) {
470 if (name == null) {
471 throw new IllegalArgumentException();
472 }
473
474 if (obj == null) {
475 removeAttribute(name);
476 }
477 else {
478 _request.setAttribute(name, obj);
479 }
480 }
481
482 public void setPortletMode(PortletMode portletMode) {
483 _portletMode = portletMode;
484 }
485
486 public void setWindowState(WindowState windowState) {
487 _windowState = windowState;
488 }
489
490 protected void init(
491 HttpServletRequest request, Portlet portlet,
492 InvokerPortlet invokerPortlet, PortletContext portletContext,
493 WindowState windowState, PortletMode portletMode,
494 PortletPreferences prefs, long plid) {
495
496 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
497 WebKeys.THEME_DISPLAY);
498
499 _portlet = portlet;
500 _portletName = portlet.getPortletId();
501
502 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
503
504 Map<String, Object> sharedSessionAttributes =
505 SharedSessionUtil.getSharedSessionAttributes(request);
506
507 boolean portalSessionShared = false;
508
509 PortletApp portletApp = portlet.getPortletApp();
510
511 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
512 portalSessionShared = true;
513 }
514
515 request = new SharedSessionServletRequest(
516 request, sharedSessionAttributes, portalSessionShared);
517
518 DynamicServletRequest dynamicRequest = null;
519
520 if (portlet.isPrivateRequestAttributes()) {
521 dynamicRequest = new NamespaceServletRequest(
522 request, portletNamespace, portletNamespace, false);
523 }
524 else {
525 dynamicRequest = new DynamicServletRequest(request, false);
526 }
527
528 Enumeration<String> enu = null;
529
530 boolean portletFocus = false;
531
532 String ppid = ParamUtil.getString(request, "p_p_id");
533
534 if (_portletName.equals(ppid)) {
535
536
538 if (themeDisplay.isLifecycleRender() ||
539 themeDisplay.isLifecycleResource()) {
540
541
543 portletFocus = true;
544 }
545 else if (themeDisplay.isLifecycleAction() &&
546 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
547
548
551 portletFocus = true;
552 }
553 }
554
555 Map<String, String[]> oldRenderParameters = RenderParametersPool.get(
556 request, plid, _portletName);
557
558 Map<String, String[]> newRenderParameters = null;
559
560 if (portletFocus) {
561 newRenderParameters = new HashMap<String, String[]>();
562
563 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
564 !LiferayWindowState.isExclusive(request) &&
565 !LiferayWindowState.isPopUp(request)) {
566
567 RenderParametersPool.put(
568 request, plid, _portletName, newRenderParameters);
569 }
570
571 enu = request.getParameterNames();
572 }
573 else {
574 if (!_portletName.equals(ppid)) {
575 putPublicRenderParameters(
576 request, plid, ppid, oldRenderParameters);
577 }
578
579 enu = Collections.enumeration(oldRenderParameters.keySet());
580 }
581
582 while (enu.hasMoreElements()) {
583 String name = enu.nextElement();
584
585 if (name.startsWith(portletNamespace) &&
586 !invokerPortlet.isFacesPortlet()) {
587
588 String shortName = name.substring(portletNamespace.length());
589
590 ObjectValuePair<String, String[]> ovp = getParameterValues(
591 request, themeDisplay, portletFocus, oldRenderParameters,
592 newRenderParameters, name);
593
594 dynamicRequest.setParameterValues(shortName, ovp.getValue());
595 }
596 else {
597
598
602 if (!PortalUtil.isReservedParameter(name) &&
603 Validator.isNotNull(name)) {
604
605 ObjectValuePair<String, String[]> ovp = getParameterValues(
606 request, themeDisplay, portletFocus,
607 oldRenderParameters, newRenderParameters, name);
608
609 dynamicRequest.setParameterValues(
610 ovp.getKey(), ovp.getValue());
611 }
612 }
613 }
614
615 _request = dynamicRequest;
616 _originalRequest = request;
617 _wapTheme = BrowserSnifferUtil.isWap(_request);
618 _portlet = portlet;
619 _portalContext = new PortalContextImpl();
620 _portletContext = portletContext;
621 _windowState = windowState;
622 _portletMode = portletMode;
623 _prefs = prefs;
624 _portalSessionId = _request.getRequestedSessionId();
625 _session = new PortletSessionImpl(
626 _request, _portletName, _portletContext, _portalSessionId, plid);
627
628 long userId = PortalUtil.getUserId(request);
629 String remoteUser = request.getRemoteUser();
630
631 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
632
633 if (userPrincipalStrategy.equals(
634 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
635
636 try {
637 User user = PortalUtil.getUser(request);
638
639 _remoteUser = user.getScreenName();
640 _remoteUserId = user.getUserId();
641 _userPrincipal = new ProtectedPrincipal(_remoteUser);
642 }
643 catch (Exception e) {
644 _log.error(e);
645 }
646 }
647 else {
648 if ((userId > 0) && (remoteUser == null)) {
649 _remoteUser = String.valueOf(userId);
650 _remoteUserId = userId;
651 _userPrincipal = new ProtectedPrincipal(_remoteUser);
652 }
653 else {
654 _remoteUser = remoteUser;
655 _remoteUserId = GetterUtil.getLong(remoteUser);
656 _userPrincipal = request.getUserPrincipal();
657 }
658 }
659
660 _locale = themeDisplay.getLocale();
661 _plid = plid;
662 }
663
664 protected ObjectValuePair<String, String[]> getParameterValues(
665 HttpServletRequest request, ThemeDisplay themeDisplay,
666 boolean portletFocus, Map<String, String[]> oldRenderParameters,
667 Map<String, String[]> newRenderParameters, String name) {
668
669 String[] values = null;
670
671 if (portletFocus) {
672 values = request.getParameterValues(name);
673
674 QName qName = QNameUtil.getQName(name);
675
676 if (qName != null) {
677 PublicRenderParameter publicRenderParameter =
678 _portlet.getPublicRenderParameter(
679 qName.getNamespaceURI(), qName.getLocalPart());
680
681 if (publicRenderParameter != null) {
682 name = publicRenderParameter.getIdentifier();
683 }
684 }
685
686 if (themeDisplay.isLifecycleRender()) {
687 newRenderParameters.put(name, values);
688 }
689 }
690 else {
691 values = oldRenderParameters.get(name);
692 }
693
694 return new ObjectValuePair<String, String[]>(name, values);
695 }
696
697 protected void putPublicRenderParameters(
698 HttpServletRequest request, long plid, String ppid,
699 Map<String, String[]> renderParameters) {
700
701 Enumeration<String> names = request.getParameterNames();
702
703 while (names.hasMoreElements()) {
704 String name = names.nextElement();
705
706 QName qName = QNameUtil.getQName(name);
707
708 if (qName == null) {
709 continue;
710 }
711
712 PublicRenderParameter publicRenderParameter =
713 _portlet.getPublicRenderParameter(
714 qName.getNamespaceURI(), qName.getLocalPart());
715
716 if (publicRenderParameter != null) {
717 renderParameters.put(
718 publicRenderParameter.getIdentifier(),
719 request.getParameterValues(name));
720 }
721 }
722
723 String ppidNamespace = PortalUtil.getPortletNamespace(ppid);
724
725 Map<String, String[]> ppidRenderParameters = RenderParametersPool.get(
726 request, plid, ppid);
727
728 for (Map.Entry<String, String[]> entry :
729 ppidRenderParameters.entrySet()) {
730
731 String name = entry.getKey();
732 String[] values = entry.getValue();
733
734 if (name.startsWith(ppidNamespace)) {
735 name = name.substring(ppidNamespace.length());
736 }
737
738 PublicRenderParameter publicRenderParameter =
739 _portlet.getPublicRenderParameter(name);
740
741 if (publicRenderParameter != null) {
742 renderParameters.put(
743 publicRenderParameter.getIdentifier(), values);
744 }
745 }
746 }
747
748 protected void recycle() {
749 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
750 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
751 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
752 _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
753
754 _request = null;
755 _originalRequest = null;
756 _wapTheme = false;
757 _portlet = null;
758 _portletName = null;
759 _portalContext = null;
760 _portletContext = null;
761 _windowState = null;
762 _portletMode = null;
763 _prefs = null;
764 _session = null;
765 _portalSessionId = null;
766 _remoteUser = null;
767 _userPrincipal = null;
768 _profile = null;
769 _locale = null;
770 _plid = 0;
771 }
772
773 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
774
775 private HttpServletRequest _request;
776 private HttpServletRequest _originalRequest;
777 private boolean _wapTheme;
778 private Portlet _portlet;
779 private String _portletName;
780 private PortalContext _portalContext;
781 private PortletContext _portletContext;
782 private WindowState _windowState;
783 private PortletMode _portletMode;
784 private PortletPreferences _prefs;
785 private PortletSessionImpl _session;
786 private String _portalSessionId;
787 private boolean _invalidSession;
788 private String _remoteUser;
789 private long _remoteUserId;
790 private Principal _userPrincipal;
791 private Profile _profile;
792 private Locale _locale;
793 private long _plid;
794
795 }