1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.ccpp.EmptyProfile;
26 import com.liferay.portal.kernel.portlet.LiferayWindowState;
27 import com.liferay.portal.kernel.servlet.BrowserSniffer;
28 import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
29 import com.liferay.portal.kernel.util.ContentTypes;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.JavaConstants;
32 import com.liferay.portal.kernel.util.LocaleUtil;
33 import com.liferay.portal.kernel.util.ObjectValuePair;
34 import com.liferay.portal.kernel.util.ParamUtil;
35 import com.liferay.portal.kernel.util.StringMaker;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.model.Portlet;
39 import com.liferay.portal.model.PortletApp;
40 import com.liferay.portal.model.PublicRenderParameter;
41 import com.liferay.portal.model.User;
42 import com.liferay.portal.model.impl.PortletImpl;
43 import com.liferay.portal.service.RoleLocalServiceUtil;
44 import com.liferay.portal.servlet.NamespaceServletRequest;
45 import com.liferay.portal.servlet.SharedSessionUtil;
46 import com.liferay.portal.theme.ThemeDisplay;
47 import com.liferay.portal.util.PortalUtil;
48 import com.liferay.portal.util.QNameUtil;
49 import com.liferay.portal.util.WebKeys;
50 import com.liferay.util.servlet.DynamicServletRequest;
51 import com.liferay.util.servlet.SharedSessionServletRequest;
52
53 import com.sun.ccpp.ProfileFactoryImpl;
54
55 import java.security.Principal;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.Enumeration;
60 import java.util.HashMap;
61 import java.util.LinkedHashMap;
62 import java.util.List;
63 import java.util.Locale;
64 import java.util.Map;
65
66 import javax.ccpp.Profile;
67 import javax.ccpp.ProfileFactory;
68 import javax.ccpp.ValidationMode;
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 import javax.xml.namespace.QName;
84
85 import org.apache.commons.logging.Log;
86 import org.apache.commons.logging.LogFactory;
87 import org.apache.struts.Globals;
88
89
97 public abstract class PortletRequestImpl implements PortletRequest {
98
99 public void defineObjects(
100 PortletConfig portletConfig, PortletResponse res) {
101
102 setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
103 setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
104 setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, res);
105 setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
106 }
107
108 public Object getAttribute(String name) {
109 if (name == null) {
110 throw new IllegalArgumentException();
111 }
112
113 if (name.equals(PortletRequest.CCPP_PROFILE)) {
114 return getCCPPProfile();
115 }
116 else if (name.equals(PortletRequest.USER_INFO)) {
117 Object value = getUserInfo();
118
119 if (value != null) {
120 return value;
121 }
122 }
123
124 return _req.getAttribute(name);
125 }
126
127 public Enumeration<String> getAttributeNames() {
128 List<String> names = new ArrayList<String>();
129
130 Enumeration<String> enu = _req.getAttributeNames();
131
132 while (enu.hasMoreElements()) {
133 String name = enu.nextElement();
134
135 if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
136 names.add(name);
137 }
138 }
139
140 return Collections.enumeration(names);
141 }
142
143 public String getAuthType() {
144 return _req.getAuthType();
145 }
146
147 public Profile getCCPPProfile() {
148 if (_profile == null) {
149 ProfileFactory profileFactory = ProfileFactory.getInstance();
150
151 if (profileFactory == null) {
152 profileFactory = ProfileFactoryImpl.getInstance();
153
154 ProfileFactory.setInstance(profileFactory);
155 }
156
157 _profile = profileFactory.newProfile(
158 _req, ValidationMode.VALIDATIONMODE_NONE);
159
160 if (_profile == null) {
161 _profile = _EMPTY_PROFILE;
162 }
163 }
164
165 return _profile;
166 }
167
168 public String getContextPath() {
169 return StringPool.SLASH + _portletCtx.getPortletContextName();
171 }
172
173 public Cookie[] getCookies() {
174 return _req.getCookies();
175 }
176
177 public String getETag() {
178 return null;
179 }
180
181 public HttpServletRequest getHttpServletRequest() {
182 return _req;
183 }
184
185 public abstract String getLifecycle();
186
187 public Locale getLocale() {
188 Locale locale = _locale;
189
190 if (locale == null) {
191 locale = _req.getLocale();
192 }
193
194 if (locale == null) {
195 locale = LocaleUtil.getDefault();
196 }
197
198 return locale;
199 }
200
201 public Enumeration<Locale> getLocales() {
202 return _req.getLocales();
203 }
204
205 public String getMethod() {
206 return _req.getMethod();
207 }
208
209 public String getParameter(String name) {
210 if (name == null) {
211 throw new IllegalArgumentException();
212 }
213
214 return _req.getParameter(name);
215 }
216
217 public Map<String, String[]> getParameterMap() {
218 return Collections.unmodifiableMap(_req.getParameterMap());
219 }
220
221 public Enumeration<String> getParameterNames() {
222 return _req.getParameterNames();
223 }
224
225 public String[] getParameterValues(String name) {
226 if (name == null) {
227 throw new IllegalArgumentException();
228 }
229
230 return _req.getParameterValues(name);
231 }
232
233 public PortalContext getPortalContext() {
234 return _portalCtx;
235 }
236
237 public Portlet getPortlet() {
238 return _portlet;
239 }
240
241 public PortletContext getPortletContext() {
242 return _portletCtx;
243 }
244
245 public PortletMode getPortletMode() {
246 return _portletMode;
247 }
248
249 public String getPortletName() {
250 return _portletName;
251 }
252
253 public PortletSession getPortletSession() {
254 return _ses;
255 }
256
257 public PortletSession getPortletSession(boolean create) {
258
272
273
279
280 if (!create && _invalidSession) {
281 return null;
282 }
283
284 return _ses;
285 }
286
287 public PortletPreferences getPreferences() {
288 return new PortletPreferencesWrapper(
289 getPreferencesImpl(), getLifecycle());
290 }
291
292 public PortletPreferencesImpl getPreferencesImpl() {
293 return (PortletPreferencesImpl)_prefs;
294 }
295
296 public Map<String, String[]> getPrivateParameterMap() {
297 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
298
299 Enumeration<String> enu = getParameterNames();
300
301 while (enu.hasMoreElements()) {
302 String name = enu.nextElement();
303
304 if (_portlet.getPublicRenderParameter(name) == null) {
305 parameterMap.put(name, getParameterValues(name));
306 }
307 }
308
309 return parameterMap;
310 }
311
312 public Enumeration<String> getProperties(String name) {
313 List<String> values = new ArrayList<String>();
314
315 String value = _portalCtx.getProperty(name);
316
317 if (value != null) {
318 values.add(value);
319 }
320
321 return Collections.enumeration(values);
322 }
323
324 public String getProperty(String name) {
325 return _portalCtx.getProperty(name);
326 }
327
328 public Enumeration<String> getPropertyNames() {
329 return _portalCtx.getPropertyNames();
330 }
331
332 public Map<String, String[]> getPublicParameterMap() {
333 Map<String, String[]> parameterMap = new HashMap<String, String[]>();
334
335 Enumeration<String> enu = getParameterNames();
336
337 while (enu.hasMoreElements()) {
338 String name = enu.nextElement();
339
340 if (_portlet.getPublicRenderParameter(name) != null) {
341 parameterMap.put(name, getParameterValues(name));
342 }
343 }
344
345 return parameterMap;
346 }
347
348 public String getRemoteUser() {
349 return _remoteUser;
350 }
351
352 public Map<String, String[]> getRenderParameters() {
353 return RenderParametersPool.get(_req, _plid, _portletName);
354 }
355
356 public String getRequestedSessionId() {
357 return _req.getSession().getId();
358 }
359
360 public String getResponseContentType() {
361 if (_wapTheme) {
362 return ContentTypes.XHTML_MP;
363 }
364 else {
365 return ContentTypes.TEXT_HTML;
366 }
367 }
368
369 public Enumeration<String> getResponseContentTypes() {
370 List<String> responseContentTypes = new ArrayList<String>();
371
372 responseContentTypes.add(getResponseContentType());
373
374 return Collections.enumeration(responseContentTypes);
375 }
376
377 public String getScheme() {
378 return _req.getScheme();
379 }
380
381 public String getServerName() {
382 return _req.getServerName();
383 }
384
385 public int getServerPort() {
386 return _req.getServerPort();
387 }
388
389 public Object getUserInfo() {
390 if (getRemoteUser() == null) {
391 return null;
392 }
393
394 LinkedHashMap<String, String> userInfo =
395 new LinkedHashMap<String, String>();
396
397 PortletApp portletApp = _portlet.getPortletApp();
398
399
401 try {
402 User user = PortalUtil.getUser(_req);
403
404 UserAttributes userAttributes = new UserAttributes(user);
405
406
408 userInfo.put(
409 UserAttributes.LIFERAY_COMPANY_ID,
410 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
411
412 userInfo.put(
413 UserAttributes.LIFERAY_USER_ID,
414 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
415
416
418 for (String attrName : portletApp.getUserAttributes()) {
419 String attrValue = userAttributes.getValue(attrName);
420
421 if (attrValue != null) {
422 userInfo.put(attrName, attrValue);
423 }
424 }
425 }
426 catch (Exception e) {
427 _log.error(e, e);
428 }
429
430 Map<String, String> unmodifiableUserInfo =
431 Collections.unmodifiableMap((Map<String, String>)userInfo.clone());
432
433
435 Map<String, CustomUserAttributes> cuaInstances =
436 new HashMap<String, CustomUserAttributes>();
437
438 for (Map.Entry<String, String> entry :
439 portletApp.getCustomUserAttributes().entrySet()) {
440
441 String attrName = entry.getKey();
442 String attrCustomClass = entry.getValue();
443
444 CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
445
446 if (cua == null) {
447 if (portletApp.isWARFile()) {
448 PortletContextBag portletContextBag =
449 PortletContextBagPool.get(
450 portletApp.getServletContextName());
451
452 cua = portletContextBag.getCustomUserAttributes().get(
453 attrCustomClass);
454
455 cua = (CustomUserAttributes)cua.clone();
456 }
457 else {
458 try {
459 cua = (CustomUserAttributes)Class.forName(
460 attrCustomClass).newInstance();
461 }
462 catch (Exception e) {
463 _log.error(e, e);
464 }
465 }
466
467 cuaInstances.put(attrCustomClass, cua);
468 }
469
470 if (cua != null) {
471 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
472
473 if (attrValue != null) {
474 userInfo.put(attrName, attrValue);
475 }
476 }
477 }
478
479 return userInfo;
480 }
481
482 public Principal getUserPrincipal() {
483 return _userPrincipal;
484 }
485
486 public String getWindowID() {
487 StringMaker sm = new StringMaker();
488
489 sm.append(_portletName);
490 sm.append(PortletSessionImpl.LAYOUT_SEPARATOR);
491 sm.append(_plid);
492
493 return sm.toString();
494 }
495
496 public WindowState getWindowState() {
497 return _windowState;
498 }
499
500 public void invalidateSession() {
501 _invalidSession = true;
502 }
503
504 public boolean isPortletModeAllowed(PortletMode portletMode) {
505 if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
506 return true;
507 }
508 else {
509 return _portlet.hasPortletMode(
510 getResponseContentType(), portletMode);
511 }
512 }
513
514 public boolean isPrivateRequestAttributes() {
515 return _portlet.isPrivateRequestAttributes();
516 }
517
518 public boolean isRequestedSessionIdValid() {
519 if (_ses != null) {
520 return _ses.isValid();
521 }
522 else {
523 return _req.isRequestedSessionIdValid();
524 }
525 }
526
527 public boolean isSecure() {
528 return _req.isSecure();
529 }
530
531 public boolean isUserInRole(String role) {
532 if (_remoteUserId <= 0) {
533 return false;
534 }
535 else {
536 try {
537 long companyId = PortalUtil.getCompanyId(_req);
538
539 return RoleLocalServiceUtil.hasUserRole(
540 _remoteUserId, companyId, role, true);
541 }
542 catch (Exception e) {
543 _log.error(e);
544 }
545
546 return _req.isUserInRole(role);
547 }
548 }
549
550 public boolean isWindowStateAllowed(WindowState windowState) {
551 return PortalContextImpl.isSupportedWindowState(windowState);
552 }
553
554 public void removeAttribute(String name) {
555 if (name == null) {
556 throw new IllegalArgumentException();
557 }
558
559 _req.removeAttribute(name);
560 }
561
562 public void setAttribute(String name, Object obj) {
563 if (name == null) {
564 throw new IllegalArgumentException();
565 }
566
567 if (obj == null) {
568 removeAttribute(name);
569 }
570 else {
571 _req.setAttribute(name, obj);
572 }
573 }
574
575 public void setPortletMode(PortletMode portletMode) {
576 _portletMode = portletMode;
577 }
578
579 public void setWindowState(WindowState windowState) {
580 _windowState = windowState;
581 }
582
583 protected void init(
584 HttpServletRequest req, Portlet portlet, InvokerPortlet invokerPortlet,
585 PortletContext portletCtx, WindowState windowState,
586 PortletMode portletMode, PortletPreferences prefs, long plid) {
587
588 ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
589 WebKeys.THEME_DISPLAY);
590
591 _portlet = portlet;
592 _portletName = portlet.getPortletId();
593
594 String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
595
596 Map<String, Object> sharedSessionAttributes =
597 SharedSessionUtil.getSharedSessionAttributes(req);
598
599 boolean portalSessionShared = false;
600
601 PortletApp portletApp = portlet.getPortletApp();
602
603 if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
604 portalSessionShared = true;
605 }
606
607 req = new SharedSessionServletRequest(
608 req, sharedSessionAttributes, portalSessionShared);
609
610 DynamicServletRequest dynamicReq = null;
611
612 if (portlet.isPrivateRequestAttributes()) {
613 dynamicReq = new NamespaceServletRequest(
614 req, portletNamespace, portletNamespace, false);
615 }
616 else {
617 dynamicReq = new DynamicServletRequest(req, false);
618 }
619
620 Enumeration<String> enu = null;
621
622 boolean portletFocus = false;
623
624 String ppid = ParamUtil.getString(req, "p_p_id");
625
626 if (_portletName.equals(ppid)) {
627
628
630 if (themeDisplay.isLifecycleRender() ||
631 themeDisplay.isLifecycleResource()) {
632
633
635 portletFocus = true;
636 }
637 else if (themeDisplay.isLifecycleAction() &&
638 getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
639
640
643 portletFocus = true;
644 }
645 }
646
647 Map<String, String[]> oldRenderParameters = RenderParametersPool.get(
648 req, plid, _portletName);
649
650 Map<String, String[]> newRenderParameters = null;
651
652 if (portletFocus) {
653 newRenderParameters = new HashMap<String, String[]>();
654
655 if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
656 !LiferayWindowState.isExclusive(req) &&
657 !LiferayWindowState.isPopUp(req)) {
658
659 RenderParametersPool.put(
660 req, plid, _portletName, newRenderParameters);
661 }
662
663 enu = req.getParameterNames();
664 }
665 else {
666 if (!_portletName.equals(ppid)) {
667 putPublicRenderParameters(req, plid, ppid, oldRenderParameters);
668 }
669
670 enu = Collections.enumeration(oldRenderParameters.keySet());
671 }
672
673 while (enu.hasMoreElements()) {
674 String name = enu.nextElement();
675
676 if (name.startsWith(portletNamespace) &&
677 !invokerPortlet.isFacesPortlet()) {
678
679 String shortName = name.substring(portletNamespace.length());
680
681 ObjectValuePair<String, String[]> ovp = getParameterValues(
682 req, themeDisplay, portletFocus, oldRenderParameters,
683 newRenderParameters, name);
684
685 dynamicReq.setParameterValues(shortName, ovp.getValue());
686 }
687 else {
688
689
693 if (!PortalUtil.isReservedParameter(name) &&
694 Validator.isNotNull(name)) {
695
696 ObjectValuePair<String, String[]> ovp = getParameterValues(
697 req, themeDisplay, portletFocus, oldRenderParameters,
698 newRenderParameters, name);
699
700 dynamicReq.setParameterValues(ovp.getKey(), ovp.getValue());
701 }
702 }
703 }
704
705 _req = dynamicReq;
706 _wapTheme = BrowserSniffer.is_wap_xhtml(_req);
707 _portlet = portlet;
708 _portalCtx = new PortalContextImpl();
709 _portletCtx = portletCtx;
710 _windowState = windowState;
711 _portletMode = portletMode;
712 _prefs = prefs;
713 _portalSessionId = _req.getRequestedSessionId();
714 _ses = new PortletSessionImpl(
715 _req, _portletName, _portletCtx, _portalSessionId, plid);
716
717 long userId = PortalUtil.getUserId(req);
718 String remoteUser = req.getRemoteUser();
719
720 String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
721
722 if (userPrincipalStrategy.equals(
723 PortletImpl.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
724
725 try {
726 User user = PortalUtil.getUser(req);
727
728 _remoteUser = user.getScreenName();
729 _remoteUserId = user.getUserId();
730 _userPrincipal = new ProtectedPrincipal(_remoteUser);
731 }
732 catch (Exception e) {
733 _log.error(e);
734 }
735 }
736 else {
737 if ((userId > 0) && (remoteUser == null)) {
738 _remoteUser = String.valueOf(userId);
739 _remoteUserId = userId;
740 _userPrincipal = new ProtectedPrincipal(_remoteUser);
741 }
742 else {
743 _remoteUser = remoteUser;
744 _remoteUserId = GetterUtil.getLong(remoteUser);
745 _userPrincipal = req.getUserPrincipal();
746 }
747 }
748
749 _locale = (Locale)_req.getSession().getAttribute(Globals.LOCALE_KEY);
750 _plid = plid;
751 }
752
753 protected ObjectValuePair<String, String[]> getParameterValues(
754 HttpServletRequest req, ThemeDisplay themeDisplay, boolean portletFocus,
755 Map<String, String[]> oldRenderParameters,
756 Map<String, String[]> newRenderParameters, String name) {
757
758 String[] values = null;
759
760 if (portletFocus) {
761 values = req.getParameterValues(name);
762
763 QName qName = QNameUtil.getQName(name);
764
765 if (qName != null) {
766 PublicRenderParameter publicRenderParameter =
767 _portlet.getPublicRenderParameter(
768 qName.getNamespaceURI(), qName.getLocalPart());
769
770 if (publicRenderParameter != null) {
771 name = publicRenderParameter.getIdentifier();
772 }
773 }
774
775 if (themeDisplay.isLifecycleRender()) {
776 newRenderParameters.put(name, values);
777 }
778 }
779 else {
780 values = oldRenderParameters.get(name);
781 }
782
783 return new ObjectValuePair<String, String[]>(name, values);
784 }
785
786 protected void putPublicRenderParameters(
787 HttpServletRequest req, long plid, String ppid,
788 Map<String, String[]> renderParameters) {
789
790 Enumeration<String> names = req.getParameterNames();
791
792 while (names.hasMoreElements()) {
793 String name = names.nextElement();
794
795 QName qName = QNameUtil.getQName(name);
796
797 if (qName == null) {
798 continue;
799 }
800
801 PublicRenderParameter publicRenderParameter =
802 _portlet.getPublicRenderParameter(
803 qName.getNamespaceURI(), qName.getLocalPart());
804
805 if (publicRenderParameter != null) {
806 renderParameters.put(
807 publicRenderParameter.getIdentifier(),
808 req.getParameterValues(name));
809 }
810 }
811
812 String ppidNamespace = PortalUtil.getPortletNamespace(ppid);
813
814 Map<String, String[]> ppidRenderParameters = RenderParametersPool.get(
815 req, plid, ppid);
816
817 for (Map.Entry<String, String[]> entry :
818 ppidRenderParameters.entrySet()) {
819
820 String name = entry.getKey();
821 String[] values = entry.getValue();
822
823 if (name.startsWith(ppidNamespace)) {
824 name = name.substring(ppidNamespace.length());
825 }
826
827 PublicRenderParameter publicRenderParameter =
828 _portlet.getPublicRenderParameter(name);
829
830 if (publicRenderParameter != null) {
831 renderParameters.put(
832 publicRenderParameter.getIdentifier(), values);
833 }
834 }
835 }
836
837 protected void recycle() {
838 _req.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
839 _req.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
840 _req.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
841 _req.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
842
843 _req = null;
844 _wapTheme = false;
845 _portlet = null;
846 _portletName = null;
847 _portalCtx = null;
848 _portletCtx = null;
849 _windowState = null;
850 _portletMode = null;
851 _prefs = null;
852 _ses = null;
853 _portalSessionId = null;
854 _remoteUser = null;
855 _userPrincipal = null;
856 _profile = null;
857 _locale = null;
858 _plid = 0;
859 }
860
861 private static Log _log = LogFactory.getLog(PortletRequestImpl.class);
862
863 private DynamicServletRequest _req;
864 private boolean _wapTheme;
865 private Portlet _portlet;
866 private String _portletName;
867 private PortalContext _portalCtx;
868 private PortletContext _portletCtx;
869 private WindowState _windowState;
870 private PortletMode _portletMode;
871 private PortletPreferences _prefs;
872 private PortletSessionImpl _ses;
873 private String _portalSessionId;
874 private boolean _invalidSession;
875 private String _remoteUser;
876 private long _remoteUserId;
877 private Principal _userPrincipal;
878 private Profile _profile;
879 private Locale _locale;
880 private long _plid;
881
882 private static final Profile _EMPTY_PROFILE = new EmptyProfile();
883
884 }