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