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