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