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 isInvalidParameter(String name) {
410 if (Validator.isNull(name) ||
411 name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
412 name.startsWith(
413 PortletQName.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
414 PortalUtil.isReservedParameter(name)) {
415
416 return true;
417 }
418 else {
419 return false;
420 }
421 }
422
423 public boolean isPortletModeAllowed(PortletMode portletMode) {
424 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
425 return true;
426 }
427 else {
428 return _portlet.hasPortletMode(
429 getResponseContentType(), portletMode);
430 }
431 }
432
433 public boolean isPrivateRequestAttributes() {
434 return _portlet.isPrivateRequestAttributes();
435 }
436
437 public boolean isRequestedSessionIdValid() {
438 if (_session != null) {
439 return _session.isValid();
440 }
441 else {
442 return _request.isRequestedSessionIdValid();
443 }
444 }
445
446 public boolean isSecure() {
447 return _request.isSecure();
448 }
449
450 public boolean isUserInRole(String role) {
451 if (_remoteUserId <= 0) {
452 return false;
453 }
454 else {
455 try {
456 long companyId = PortalUtil.getCompanyId(_request);
457
458 String roleLink = _portlet.getRoleMappers().get(role);
459
460 if (Validator.isNotNull(roleLink)) {
461 return RoleLocalServiceUtil.hasUserRole(
462 _remoteUserId, companyId, roleLink, true);
463 }
464 else {
465 return RoleLocalServiceUtil.hasUserRole(
466 _remoteUserId, companyId, role, true);
467 }
468 }
469 catch (Exception e) {
470 _log.error(e);
471 }
472
473 return _request.isUserInRole(role);
474 }
475 }
476
477 public boolean isWindowStateAllowed(WindowState windowState) {
478 return PortalContextImpl.isSupportedWindowState(windowState);
479 }
480
481 public void removeAttribute(String name) {
482 if (name == null) {
483 throw new IllegalArgumentException();
484 }
485
486 _request.removeAttribute(name);
487 }
488
489 public void setAttribute(String name, Object obj) {
490 if (name == null) {
491 throw new IllegalArgumentException();
492 }
493
494 if (obj == null) {
495 removeAttribute(name);
496 }
497 else {
498 _request.setAttribute(name, obj);
499 }
500 }
501
502 public void setPortletMode(PortletMode portletMode) {
503 _portletMode = portletMode;
504 }
505
506 public void setWindowState(WindowState windowState) {
507 _windowState = windowState;
508 }
509
510 protected void init(
511 HttpServletRequest request, Portlet portlet,
512 InvokerPortlet invokerPortlet, PortletContext portletContext,
513 WindowState windowState, PortletMode portletMode,
514 PortletPreferences preferences, long plid) {
515
516 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
517 WebKeys.THEME_DISPLAY);
518
519 _portlet = portlet;
520 _portletName = portlet.getPortletId();
521 _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
522
523 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
524
525 Map<String, Object> sharedSessionAttributes =
526 SharedSessionUtil.getSharedSessionAttributes(request);
527
528 boolean portalSessionShared = false;
529
530 PortletApp portletApp = portlet.getPortletApp();
531
532 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
533 portalSessionShared = true;
534 }
535
536 request = new SharedSessionServletRequest(
537 request, sharedSessionAttributes, portalSessionShared);
538
539 DynamicServletRequest dynamicRequest = null;
540
541 if (portlet.isPrivateRequestAttributes()) {
542 dynamicRequest = new NamespaceServletRequest(
543 request, portletNamespace, portletNamespace, false);
544 }
545 else {
546 dynamicRequest = new DynamicServletRequest(request, false);
547 }
548
549 boolean portletFocus = false;
550
551 String ppid = ParamUtil.getString(request, "p_p_id");
552
553 if (_portletName.equals(ppid)) {
554
555
557 if (themeDisplay.isLifecycleRender() ||
558 themeDisplay.isLifecycleResource()) {
559
560
562 portletFocus = true;
563 }
564 else if (themeDisplay.isLifecycleAction() &&
565 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
566
567
570 portletFocus = true;
571 }
572 }
573
574 Map<String, String[]> renderParameters = RenderParametersPool.get(
575 request, plid, _portletName);
576
577 if (portletFocus) {
578 renderParameters = new HashMap<String, String[]>();
579
580 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
581 !LiferayWindowState.isExclusive(request) &&
582 !LiferayWindowState.isPopUp(request)) {
583
584 RenderParametersPool.put(
585 request, plid, _portletName, renderParameters);
586 }
587
588 Enumeration<String> enu = request.getParameterNames();
589
590 while (enu.hasMoreElements()) {
591 String name = enu.nextElement();
592
593 if (isInvalidParameter(name)) {
594 continue;
595 }
596
597 String[] values = request.getParameterValues(name);
598
599 if (themeDisplay.isLifecycleRender()) {
600 renderParameters.put(name, values);
601 }
602
603 if (values == null) {
604 continue;
605 }
606
607 if (name.startsWith(portletNamespace) &&
608 !invokerPortlet.isFacesPortlet()) {
609
610 name = name.substring(portletNamespace.length());
611 }
612
613 dynamicRequest.setParameterValues(name, values);
614 }
615 }
616 else {
617 Set<String> names = renderParameters.keySet();
618
619 for (String name : names) {
620 String[] values = renderParameters.get(name);
621
622 dynamicRequest.setParameterValues(name, values);
623 }
624 }
625
626 mergePublicRenderParameters(
627 dynamicRequest, preferences, plid, renderParameters);
628
629 _request = dynamicRequest;
630 _originalRequest = request;
631 _wapTheme = BrowserSnifferUtil.isWap(_request);
632 _portlet = portlet;
633 _portalContext = new PortalContextImpl();
634 _portletContext = portletContext;
635 _windowState = windowState;
636 _portletMode = portletMode;
637 _preferences = preferences;
638 _portalSessionId = _request.getRequestedSessionId();
639 _session = new PortletSessionImpl(
640 _request, _portletName, _portletContext, _portalSessionId, plid);
641
642 long userId = PortalUtil.getUserId(request);
643 String remoteUser = request.getRemoteUser();
644
645 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
646
647 if (userPrincipalStrategy.equals(
648 PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
649
650 try {
651 User user = PortalUtil.getUser(request);
652
653 _remoteUser = user.getScreenName();
654 _remoteUserId = user.getUserId();
655 _userPrincipal = new ProtectedPrincipal(_remoteUser);
656 }
657 catch (Exception e) {
658 _log.error(e);
659 }
660 }
661 else {
662 if ((userId > 0) && (remoteUser == null)) {
663 _remoteUser = String.valueOf(userId);
664 _remoteUserId = userId;
665 _userPrincipal = new ProtectedPrincipal(_remoteUser);
666 }
667 else {
668 _remoteUser = remoteUser;
669 _remoteUserId = GetterUtil.getLong(remoteUser);
670 _userPrincipal = request.getUserPrincipal();
671 }
672 }
673
674 _locale = themeDisplay.getLocale();
675 _plid = plid;
676 }
677
678 protected void mergePublicRenderParameters(
679 DynamicServletRequest dynamicRequest, PortletPreferences preferences,
680 long plid, Map<String, String[]> renderParameters) {
681
682 Enumeration<PublicRenderParameter> publicRenderParameters =
683 Collections.enumeration(_portlet.getPublicRenderParameters());
684
685 while (publicRenderParameters.hasMoreElements()) {
686 PublicRenderParameter publicRenderParameter =
687 publicRenderParameters.nextElement();
688
689 String name = publicRenderParameter.getIdentifier();
690 QName qName = publicRenderParameter.getQName();
691
692 String[] values = _publicRenderParameters.get(
693 PortletQNameUtil.getKey(qName));
694
695 if ((values) == null || (values.length == 0)) {
696 continue;
697 }
698
699 String[] newValues = dynamicRequest.getParameterValues(name);
700
701 if (newValues != null) {
702 values = ArrayUtil.append(newValues, values);
703 }
704
705 dynamicRequest.setParameterValues(name, values);
706 }
707 }
708
709 private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
710
711 private HttpServletRequest _request;
712 private HttpServletRequest _originalRequest;
713 private boolean _wapTheme;
714 private Portlet _portlet;
715 private String _portletName;
716 private PortalContext _portalContext;
717 private PortletContext _portletContext;
718 private WindowState _windowState;
719 private PortletMode _portletMode;
720 private PortletPreferences _preferences;
721 private PortletSessionImpl _session;
722 private String _portalSessionId;
723 private boolean _invalidSession;
724 private String _remoteUser;
725 private long _remoteUserId;
726 private Principal _userPrincipal;
727 private Profile _profile;
728 private Locale _locale;
729 private long _plid;
730 private Map<String, String[]> _publicRenderParameters;
731
732 }