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