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.ArrayUtil;
34 import com.liferay.portal.kernel.util.ContentTypes;
35 import com.liferay.portal.kernel.util.GetterUtil;
36 import com.liferay.portal.kernel.util.JavaConstants;
37 import com.liferay.portal.kernel.util.LocaleUtil;
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 import java.util.Set;
67
68 import javax.ccpp.Profile;
69
70 import javax.portlet.PortalContext;
71 import javax.portlet.PortletConfig;
72 import javax.portlet.PortletContext;
73 import javax.portlet.PortletMode;
74 import javax.portlet.PortletPreferences;
75 import javax.portlet.PortletRequest;
76 import javax.portlet.PortletResponse;
77 import javax.portlet.PortletSession;
78 import javax.portlet.WindowState;
79
80 import javax.servlet.http.Cookie;
81 import javax.servlet.http.HttpServletRequest;
82
83
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 _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
508
509 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
510
511 Map<String, Object> sharedSessionAttributes =
512 SharedSessionUtil.getSharedSessionAttributes(request);
513
514 boolean portalSessionShared = false;
515
516 PortletApp portletApp = portlet.getPortletApp();
517
518 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
519 portalSessionShared = true;
520 }
521
522 request = new SharedSessionServletRequest(
523 request, sharedSessionAttributes, portalSessionShared);
524
525 DynamicServletRequest dynamicRequest = null;
526
527 if (portlet.isPrivateRequestAttributes()) {
528 dynamicRequest = new NamespaceServletRequest(
529 request, portletNamespace, portletNamespace, false);
530 }
531 else {
532 dynamicRequest = new DynamicServletRequest(request, false);
533 }
534
535 boolean portletFocus = false;
536
537 String ppid = ParamUtil.getString(request, "p_p_id");
538
539 if (_portletName.equals(ppid)) {
540
541
543 if (themeDisplay.isLifecycleRender() ||
544 themeDisplay.isLifecycleResource()) {
545
546
548 portletFocus = true;
549 }
550 else if (themeDisplay.isLifecycleAction() &&
551 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
552
553
556 portletFocus = true;
557 }
558 }
559
560 Map<String, String[]> renderParameters = RenderParametersPool.get(
561 request, plid, _portletName);
562
563 if (portletFocus) {
564 renderParameters = new HashMap<String, String[]>();
565
566 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
567 !LiferayWindowState.isExclusive(request) &&
568 !LiferayWindowState.isPopUp(request)) {
569
570 RenderParametersPool.put(
571 request, plid, _portletName, renderParameters);
572 }
573
574 Enumeration<String> enu = request.getParameterNames();
575
576 while (enu.hasMoreElements()) {
577 String name = enu.nextElement();
578
579 if (Validator.isNull(name) ||
580 PortalUtil.isReservedParameter(name)) {
581
582 continue;
583 }
584
585 String[] values = getParameterValues(
586 request, themeDisplay, portletFocus, renderParameters,
587 name);
588
589 if (values == null) {
590 continue;
591 }
592
593 if (name.startsWith(portletNamespace) &&
594 !invokerPortlet.isFacesPortlet()) {
595
596 name = name.substring(portletNamespace.length());
597 }
598
599 dynamicRequest.setParameterValues(name, values);
600 }
601 }
602 else {
603 Set<String> names = renderParameters.keySet();
604
605 for (String name : names) {
606 String[] values = renderParameters.get(name);
607
608 dynamicRequest.setParameterValues(name, values);
609 }
610 }
611
612 mergePublicRenderParameters(
613 dynamicRequest, preferences, plid, renderParameters);
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 _preferences = preferences;
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 String[] getParameterValues(
665 HttpServletRequest request, ThemeDisplay themeDisplay,
666 boolean portletFocus, Map<String, String[]> renderParameters,
667 String name) {
668
669 String[] values = request.getParameterValues(name);
670
671 QName qName = PortletQNameUtil.getQName(name);
672
673 if (qName != null) {
674 PublicRenderParameter publicRenderParameter =
675 _portlet.getPublicRenderParameter(
676 qName.getNamespaceURI(), qName.getLocalPart());
677
678 if (publicRenderParameter != null) {
679 if (name.startsWith(
680 PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE)) {
681
682 _publicRenderParameters.put(
683 PortletQNameUtil.getKey(qName), values);
684 }
685 else {
686 _publicRenderParameters.remove(
687 PortletQNameUtil.getKey(qName));
688 }
689 }
690
691 return null;
692 }
693
694 if (themeDisplay.isLifecycleRender()) {
695 renderParameters.put(name, values);
696 }
697
698 return values;
699 }
700
701 protected void mergePublicRenderParameters(
702 DynamicServletRequest dynamicRequest, PortletPreferences preferences,
703 long plid, Map<String, String[]> renderParameters) {
704
705 Enumeration<PublicRenderParameter> publicRenderParameters =
706 Collections.enumeration(_portlet.getPublicRenderParameters());
707
708 while (publicRenderParameters.hasMoreElements()) {
709 PublicRenderParameter publicRenderParameter =
710 publicRenderParameters.nextElement();
711
712 String name = publicRenderParameter.getIdentifier();
713 QName qName = publicRenderParameter.getQName();
714
715 String[] values = _publicRenderParameters.get(
716 PortletQNameUtil.getKey(qName));
717
718 if ((values) == null || (values.length == 0)) {
719 continue;
720 }
721
722 String[] newValues = dynamicRequest.getParameterValues(name);
723
724 if (newValues != null) {
725 values = ArrayUtil.append(newValues, values);
726 }
727
728 dynamicRequest.setParameterValues(name, values);
729 }
730 }
731
732 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
733
734 private HttpServletRequest _request;
735 private HttpServletRequest _originalRequest;
736 private boolean _wapTheme;
737 private Portlet _portlet;
738 private String _portletName;
739 private PortalContext _portalContext;
740 private PortletContext _portletContext;
741 private WindowState _windowState;
742 private PortletMode _portletMode;
743 private PortletPreferences _preferences;
744 private PortletSessionImpl _session;
745 private String _portalSessionId;
746 private boolean _invalidSession;
747 private String _remoteUser;
748 private long _remoteUserId;
749 private Principal _userPrincipal;
750 private Profile _profile;
751 private Locale _locale;
752 private long _plid;
753 private Map<String, String[]> _publicRenderParameters;
754
755 }