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.LiferayPortletSession;
30 import com.liferay.portal.kernel.portlet.LiferayWindowState;
31 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
32 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
33 import com.liferay.portal.kernel.util.ContentTypes;
34 import com.liferay.portal.kernel.util.GetterUtil;
35 import com.liferay.portal.kernel.util.JavaConstants;
36 import com.liferay.portal.kernel.util.LocaleUtil;
37 import com.liferay.portal.kernel.util.ObjectValuePair;
38 import com.liferay.portal.kernel.util.ParamUtil;
39 import com.liferay.portal.kernel.util.StringPool;
40 import com.liferay.portal.kernel.util.Validator;
41 import com.liferay.portal.kernel.xml.QName;
42 import com.liferay.portal.model.Portlet;
43 import com.liferay.portal.model.PortletApp;
44 import com.liferay.portal.model.PortletConstants;
45 import com.liferay.portal.model.PublicRenderParameter;
46 import com.liferay.portal.model.User;
47 import com.liferay.portal.service.RoleLocalServiceUtil;
48 import com.liferay.portal.servlet.NamespaceServletRequest;
49 import com.liferay.portal.servlet.SharedSessionUtil;
50 import com.liferay.portal.theme.ThemeDisplay;
51 import com.liferay.portal.util.PortalUtil;
52 import com.liferay.portal.util.WebKeys;
53 import com.liferay.util.servlet.DynamicServletRequest;
54 import com.liferay.util.servlet.SharedSessionServletRequest;
55
56 import java.security.Principal;
57
58 import java.util.ArrayList;
59 import java.util.Collections;
60 import java.util.Enumeration;
61 import java.util.HashMap;
62 import java.util.LinkedHashMap;
63 import java.util.List;
64 import java.util.Locale;
65 import java.util.Map;
66
67 import javax.ccpp.Profile;
68
69 import javax.portlet.PortalContext;
70 import javax.portlet.PortletConfig;
71 import javax.portlet.PortletContext;
72 import javax.portlet.PortletMode;
73 import javax.portlet.PortletPreferences;
74 import javax.portlet.PortletRequest;
75 import javax.portlet.PortletResponse;
76 import javax.portlet.PortletSession;
77 import javax.portlet.WindowState;
78
79 import javax.servlet.http.Cookie;
80 import javax.servlet.http.HttpServletRequest;
81
82
90 public abstract class PortletRequestImpl implements LiferayPortletRequest {
91
92 public void cleanUp() {
93 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
94 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
95 _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
96 _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
97 }
98
99 public void defineObjects(
100 PortletConfig portletConfig, PortletResponse portletResponse) {
101
102 PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
103
104 setAttribute(WebKeys.PORTLET_ID, portletConfigImpl.getPortletId());
105 setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
106 setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
107 setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
108 setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
109 }
110
111 public Object getAttribute(String name) {
112 if (name == null) {
113 throw new IllegalArgumentException();
114 }
115
116 if (name.equals(PortletRequest.CCPP_PROFILE)) {
117 return getCCPPProfile();
118 }
119 else if (name.equals(PortletRequest.USER_INFO)) {
120 Object value = getUserInfo();
121
122 if (value != null) {
123 return value;
124 }
125 }
126
127 return _request.getAttribute(name);
128 }
129
130 public Enumeration<String> getAttributeNames() {
131 List<String> names = new ArrayList<String>();
132
133 Enumeration<String> enu = _request.getAttributeNames();
134
135 while (enu.hasMoreElements()) {
136 String name = enu.nextElement();
137
138 if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
139 names.add(name);
140 }
141 }
142
143 return Collections.enumeration(names);
144 }
145
146 public String getAuthType() {
147 return _request.getAuthType();
148 }
149
150 public Profile getCCPPProfile() {
151 if (_profile == null) {
152 _profile = PortalProfileFactory.getCCPPProfile(_request);
153 }
154
155 return _profile;
156 }
157
158 public String getContextPath() {
159 return StringPool.SLASH + _portletContext.getPortletContextName();
161 }
162
163 public Cookie[] getCookies() {
164 return _request.getCookies();
165 }
166
167 public String getETag() {
168 return null;
169 }
170
171 public HttpServletRequest getHttpServletRequest() {
172 return _request;
173 }
174
175 public abstract String getLifecycle();
176
177 public Locale getLocale() {
178 Locale locale = _locale;
179
180 if (locale == null) {
181 locale = _request.getLocale();
182 }
183
184 if (locale == null) {
185 locale = LocaleUtil.getDefault();
186 }
187
188 return locale;
189 }
190
191 public Enumeration<Locale> getLocales() {
192 return _request.getLocales();
193 }
194
195 public String getMethod() {
196 return _request.getMethod();
197 }
198
199 public HttpServletRequest getOriginalHttpServletRequest() {
200 return _originalRequest;
201 }
202
203 public String getParameter(String name) {
204 if (name == null) {
205 throw new IllegalArgumentException();
206 }
207
208 return _request.getParameter(name);
209 }
210
211 public Map<String, String[]> getParameterMap() {
212 return Collections.unmodifiableMap(_request.getParameterMap());
213 }
214
215 public Enumeration<String> getParameterNames() {
216 return _request.getParameterNames();
217 }
218
219 public String[] getParameterValues(String name) {
220 if (name == null) {
221 throw new IllegalArgumentException();
222 }
223
224 return _request.getParameterValues(name);
225 }
226
227 public PortalContext getPortalContext() {
228 return _portalContext;
229 }
230
231 public Portlet getPortlet() {
232 return _portlet;
233 }
234
235 public PortletContext getPortletContext() {
236 return _portletContext;
237 }
238
239 public PortletMode getPortletMode() {
240 return _portletMode;
241 }
242
243 public String getPortletName() {
244 return _portletName;
245 }
246
247 public PortletSession getPortletSession() {
248 return _session;
249 }
250
251 public PortletSession getPortletSession(boolean create) {
252
266
267
273
274 if (!create && _invalidSession) {
275 return null;
276 }
277
278 return _session;
279 }
280
281 public PortletPreferences getPreferences() {
282 return new PortletPreferencesWrapper(
283 getPreferencesImpl(), getLifecycle());
284 }
285
286 public PortletPreferencesImpl getPreferencesImpl() {
287 return (PortletPreferencesImpl)_preferences;
288 }
289
290 public Map<String, String[]> getPrivateParameterMap() {
291 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
292
293 Enumeration<String> enu = getParameterNames();
294
295 while (enu.hasMoreElements()) {
296 String name = enu.nextElement();
297
298 if (_portlet.getPublicRenderParameter(name) == null) {
299 parameterMap.put(name, getParameterValues(name));
300 }
301 }
302
303 return parameterMap;
304 }
305
306 public Enumeration<String> getProperties(String name) {
307 List<String> values = new ArrayList<String>();
308
309 String value = _portalContext.getProperty(name);
310
311 if (value != null) {
312 values.add(value);
313 }
314
315 return Collections.enumeration(values);
316 }
317
318 public String getProperty(String name) {
319 return _portalContext.getProperty(name);
320 }
321
322 public Enumeration<String> getPropertyNames() {
323 return _portalContext.getPropertyNames();
324 }
325
326 public Map<String, String[]> getPublicParameterMap() {
327 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
328
329 Enumeration<String> enu = getParameterNames();
330
331 while (enu.hasMoreElements()) {
332 String name = enu.nextElement();
333
334 if (_portlet.getPublicRenderParameter(name) != null) {
335 parameterMap.put(name, getParameterValues(name));
336 }
337 }
338
339 return parameterMap;
340 }
341
342 public String getRemoteUser() {
343 return _remoteUser;
344 }
345
346 public Map<String, String[]> getRenderParameters() {
347 return RenderParametersPool.get(_request, _plid, _portletName);
348 }
349
350 public String getRequestedSessionId() {
351 return _request.getSession().getId();
352 }
353
354 public String getResponseContentType() {
355 if (_wapTheme) {
356 return ContentTypes.XHTML_MP;
357 }
358 else {
359 return ContentTypes.TEXT_HTML;
360 }
361 }
362
363 public Enumeration<String> getResponseContentTypes() {
364 List<String> responseContentTypes = new ArrayList<String>();
365
366 responseContentTypes.add(getResponseContentType());
367
368 return Collections.enumeration(responseContentTypes);
369 }
370
371 public String getScheme() {
372 return _request.getScheme();
373 }
374
375 public String getServerName() {
376 return _request.getServerName();
377 }
378
379 public int getServerPort() {
380 return _request.getServerPort();
381 }
382
383 public LinkedHashMap<String, String> getUserInfo() {
384 return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
385 }
386
387 public Principal getUserPrincipal() {
388 return _userPrincipal;
389 }
390
391 public String getWindowID() {
392 StringBuilder sb = new StringBuilder();
393
394 sb.append(_portletName);
395 sb.append(LiferayPortletSession.LAYOUT_SEPARATOR);
396 sb.append(_plid);
397
398 return sb.toString();
399 }
400
401 public WindowState getWindowState() {
402 return _windowState;
403 }
404
405 public void invalidateSession() {
406 _invalidSession = true;
407 }
408
409 public boolean isPortletModeAllowed(PortletMode portletMode) {
410 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
411 return true;
412 }
413 else {
414 return _portlet.hasPortletMode(
415 getResponseContentType(), portletMode);
416 }
417 }
418
419 public boolean isPrivateRequestAttributes() {
420 return _portlet.isPrivateRequestAttributes();
421 }
422
423 public boolean isRequestedSessionIdValid() {
424 if (_session != null) {
425 return _session.isValid();
426 }
427 else {
428 return _request.isRequestedSessionIdValid();
429 }
430 }
431
432 public boolean isSecure() {
433 return _request.isSecure();
434 }
435
436 public boolean isUserInRole(String role) {
437 if (_remoteUserId <= 0) {
438 return false;
439 }
440 else {
441 try {
442 long companyId = PortalUtil.getCompanyId(_request);
443
444 String roleLink = _portlet.getRoleMappers().get(role);
445
446 if (Validator.isNotNull(roleLink)) {
447 return RoleLocalServiceUtil.hasUserRole(
448 _remoteUserId, companyId, roleLink, true);
449 }
450 else {
451 return RoleLocalServiceUtil.hasUserRole(
452 _remoteUserId, companyId, role, true);
453 }
454 }
455 catch (Exception e) {
456 _log.error(e);
457 }
458
459 return _request.isUserInRole(role);
460 }
461 }
462
463 public boolean isWindowStateAllowed(WindowState windowState) {
464 return PortalContextImpl.isSupportedWindowState(windowState);
465 }
466
467 public void removeAttribute(String name) {
468 if (name == null) {
469 throw new IllegalArgumentException();
470 }
471
472 _request.removeAttribute(name);
473 }
474
475 public void setAttribute(String name, Object obj) {
476 if (name == null) {
477 throw new IllegalArgumentException();
478 }
479
480 if (obj == null) {
481 removeAttribute(name);
482 }
483 else {
484 _request.setAttribute(name, obj);
485 }
486 }
487
488 public void setPortletMode(PortletMode portletMode) {
489 _portletMode = portletMode;
490 }
491
492 public void setWindowState(WindowState windowState) {
493 _windowState = windowState;
494 }
495
496 protected void init(
497 HttpServletRequest request, Portlet portlet,
498 InvokerPortlet invokerPortlet, PortletContext portletContext,
499 WindowState windowState, PortletMode portletMode,
500 PortletPreferences preferences, long plid) {
501
502 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
503 WebKeys.THEME_DISPLAY);
504
505 _portlet = portlet;
506 _portletName = portlet.getPortletId();
507
508 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
509
510 Map<String, Object> sharedSessionAttributes =
511 SharedSessionUtil.getSharedSessionAttributes(request);
512
513 boolean portalSessionShared = false;
514
515 PortletApp portletApp = portlet.getPortletApp();
516
517 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
518 portalSessionShared = true;
519 }
520
521 request = new SharedSessionServletRequest(
522 request, sharedSessionAttributes, portalSessionShared);
523
524 DynamicServletRequest dynamicRequest = null;
525
526 if (portlet.isPrivateRequestAttributes()) {
527 dynamicRequest = new NamespaceServletRequest(
528 request, portletNamespace, portletNamespace, false);
529 }
530 else {
531 dynamicRequest = new DynamicServletRequest(request, false);
532 }
533
534 Enumeration<String> enu = null;
535
536 boolean portletFocus = false;
537
538 String ppid = ParamUtil.getString(request, "p_p_id");
539
540 if (_portletName.equals(ppid)) {
541
542
544 if (themeDisplay.isLifecycleRender() ||
545 themeDisplay.isLifecycleResource()) {
546
547
549 portletFocus = true;
550 }
551 else if (themeDisplay.isLifecycleAction() &&
552 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
553
554
557 portletFocus = true;
558 }
559 }
560
561 Map<String, String[]> oldRenderParameters = RenderParametersPool.get(
562 request, plid, _portletName);
563
564 Map<String, String[]> newRenderParameters = null;
565
566 if (portletFocus) {
567 newRenderParameters = new HashMap<String, String[]>();
568
569 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
570 !LiferayWindowState.isExclusive(request) &&
571 !LiferayWindowState.isPopUp(request)) {
572
573 RenderParametersPool.put(
574 request, plid, _portletName, newRenderParameters);
575 }
576
577 enu = request.getParameterNames();
578 }
579 else {
580 if (!_portletName.equals(ppid)) {
581 putPublicRenderParameters(
582 request, plid, ppid, oldRenderParameters);
583 }
584
585 enu = Collections.enumeration(oldRenderParameters.keySet());
586 }
587
588 while (enu.hasMoreElements()) {
589 String name = enu.nextElement();
590
591 if (name.startsWith(portletNamespace) &&
592 !invokerPortlet.isFacesPortlet()) {
593
594 String shortName = name.substring(portletNamespace.length());
595
596 ObjectValuePair<String, String[]> ovp = getParameterValues(
597 request, themeDisplay, portletFocus, oldRenderParameters,
598 newRenderParameters, name);
599
600 dynamicRequest.setParameterValues(shortName, ovp.getValue());
601 }
602 else {
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 _preferences = preferences;
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 = PortletQNameUtil.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 = PortletQNameUtil.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 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
750
751 private HttpServletRequest _request;
752 private HttpServletRequest _originalRequest;
753 private boolean _wapTheme;
754 private Portlet _portlet;
755 private String _portletName;
756 private PortalContext _portalContext;
757 private PortletContext _portletContext;
758 private WindowState _windowState;
759 private PortletMode _portletMode;
760 private PortletPreferences _preferences;
761 private PortletSessionImpl _session;
762 private String _portalSessionId;
763 private boolean _invalidSession;
764 private String _remoteUser;
765 private long _remoteUserId;
766 private Principal _userPrincipal;
767 private Profile _profile;
768 private Locale _locale;
769 private long _plid;
770
771 }