001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.portlet.LiferayRenderRequest;
024: import com.liferay.portal.kernel.portlet.LiferayWindowState;
025: import com.liferay.portal.kernel.servlet.BrowserSniffer;
026: import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
027: import com.liferay.portal.kernel.util.ContentTypes;
028: import com.liferay.portal.kernel.util.GetterUtil;
029: import com.liferay.portal.kernel.util.JavaConstants;
030: import com.liferay.portal.kernel.util.LocaleUtil;
031: import com.liferay.portal.kernel.util.ParamUtil;
032: import com.liferay.portal.kernel.util.StringPool;
033: import com.liferay.portal.kernel.util.Validator;
034: import com.liferay.portal.model.Portlet;
035: import com.liferay.portal.model.User;
036: import com.liferay.portal.model.impl.PortletImpl;
037: import com.liferay.portal.service.RoleLocalServiceUtil;
038: import com.liferay.portal.servlet.NamespaceServletRequest;
039: import com.liferay.portal.servlet.PortletContextPool;
040: import com.liferay.portal.servlet.PortletContextWrapper;
041: import com.liferay.portal.servlet.SharedSessionUtil;
042: import com.liferay.portal.util.PortalUtil;
043: import com.liferay.util.CollectionFactory;
044: import com.liferay.util.servlet.DynamicServletRequest;
045: import com.liferay.util.servlet.SharedSessionServletRequest;
046:
047: import java.security.Principal;
048:
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.Enumeration;
052: import java.util.HashMap;
053: import java.util.Iterator;
054: import java.util.LinkedHashMap;
055: import java.util.List;
056: import java.util.Locale;
057: import java.util.Map;
058:
059: import javax.portlet.PortalContext;
060: import javax.portlet.PortletConfig;
061: import javax.portlet.PortletContext;
062: import javax.portlet.PortletMode;
063: import javax.portlet.PortletPreferences;
064: import javax.portlet.PortletSession;
065: import javax.portlet.RenderRequest;
066: import javax.portlet.RenderResponse;
067: import javax.portlet.WindowState;
068:
069: import javax.servlet.http.HttpServletRequest;
070:
071: import org.apache.commons.logging.Log;
072: import org.apache.commons.logging.LogFactory;
073: import org.apache.struts.Globals;
074:
075: /**
076: * <a href="RenderRequestImpl.java.html"><b><i>View Source</i></b></a>
077: *
078: * @author Brian Wing Shun Chan
079: * @author Brian Myunghun Kim
080: * @author Sergey Ponomarev
081: *
082: */
083: public class RenderRequestImpl implements LiferayRenderRequest {
084:
085: public WindowState getWindowState() {
086: return _windowState;
087: }
088:
089: public void setWindowState(WindowState windowState) {
090: _windowState = windowState;
091: }
092:
093: public boolean isWindowStateAllowed(WindowState windowState) {
094: return PortalContextImpl.isSupportedWindowState(windowState);
095: }
096:
097: public PortletMode getPortletMode() {
098: return _portletMode;
099: }
100:
101: public void setPortletMode(PortletMode portletMode) {
102: _portletMode = portletMode;
103: }
104:
105: public boolean isPortletModeAllowed(PortletMode portletMode) {
106: if ((portletMode == null)
107: || Validator.isNull(portletMode.toString())) {
108: return true;
109: } else {
110: return _portlet.hasPortletMode(getResponseContentType(),
111: portletMode);
112: }
113: }
114:
115: public PortletPreferences getPreferences() {
116: return new PortletPreferencesWrapper(getPreferencesImpl(),
117: false);
118: }
119:
120: public PortletPreferencesImpl getPreferencesImpl() {
121: return (PortletPreferencesImpl) _prefs;
122: }
123:
124: public PortletSession getPortletSession() {
125: return _ses;
126: }
127:
128: public PortletSession getPortletSession(boolean create) {
129: /*HttpSession httpSes = _req.getSession(create);
130:
131: if (httpSes == null) {
132: return null;
133: }
134: else {
135: if (create) {
136: _ses = new PortletSessionImpl(
137: _req, _portletName, _portletCtx, _portalSessionId,
138: _plid);
139: }
140:
141: return _ses;
142: }*/
143:
144: /*if ((_ses == null) && create) {
145: _req.getSession(create);
146:
147: _ses = new PortletSessionImpl(
148: _req, _portletName, _portletCtx, _portalSessionId, _plid);
149: }*/
150:
151: return _ses;
152: }
153:
154: public String getProperty(String name) {
155: return _portalCtx.getProperty(name);
156: }
157:
158: public Enumeration getProperties(String name) {
159: List values = new ArrayList();
160:
161: String value = _portalCtx.getProperty(name);
162:
163: if (value != null) {
164: values.add(value);
165: }
166:
167: return Collections.enumeration(values);
168: }
169:
170: public Enumeration getPropertyNames() {
171: return _portalCtx.getPropertyNames();
172: }
173:
174: public PortalContext getPortalContext() {
175: return _portalCtx;
176: }
177:
178: public String getAuthType() {
179: return _req.getAuthType();
180: }
181:
182: public String getContextPath() {
183: //return StringPool.SLASH + _req.getContextPath();
184: return StringPool.SLASH + _portletCtx.getPortletContextName();
185: }
186:
187: public String getRemoteUser() {
188: return _remoteUser;
189: }
190:
191: public Principal getUserPrincipal() {
192: return _userPrincipal;
193: }
194:
195: public boolean isUserInRole(String role) {
196: if (_remoteUserId <= 0) {
197: return false;
198: } else {
199: try {
200: long companyId = PortalUtil.getCompanyId(_req);
201:
202: return RoleLocalServiceUtil.hasUserRole(_remoteUserId,
203: companyId, role, true);
204: } catch (Exception e) {
205: _log.error(e);
206: }
207:
208: return _req.isUserInRole(role);
209: }
210: }
211:
212: public Object getAttribute(String name) {
213: if (name == null) {
214: throw new IllegalArgumentException();
215: }
216:
217: if (name.equals(RenderRequest.USER_INFO)) {
218: if (getRemoteUser() != null) {
219: LinkedHashMap userInfo = new LinkedHashMap();
220:
221: // Liferay user attributes
222:
223: try {
224: User user = PortalUtil.getUser(_req);
225:
226: UserAttributes userAttributes = new UserAttributes(
227: user);
228:
229: // Mandatory user attributes
230:
231: userInfo
232: .put(
233: UserAttributes.LIFERAY_COMPANY_ID,
234: userAttributes
235: .getValue(UserAttributes.LIFERAY_COMPANY_ID));
236:
237: userInfo
238: .put(
239: UserAttributes.LIFERAY_USER_ID,
240: userAttributes
241: .getValue(UserAttributes.LIFERAY_USER_ID));
242:
243: // Portlet user attributes
244:
245: Iterator itr = _portlet.getUserAttributes()
246: .iterator();
247:
248: while (itr.hasNext()) {
249: String attrName = (String) itr.next();
250: String attrValue = userAttributes
251: .getValue(attrName);
252:
253: if (attrValue != null) {
254: userInfo.put(attrName, attrValue);
255: }
256: }
257: } catch (Exception e) {
258: e.printStackTrace();
259: }
260:
261: Map unmodifiableUserInfo = Collections
262: .unmodifiableMap((Map) userInfo.clone());
263:
264: // Custom user attributes
265:
266: Map cuaInstances = CollectionFactory.getHashMap();
267:
268: Iterator itr = _portlet.getCustomUserAttributes()
269: .entrySet().iterator();
270:
271: while (itr.hasNext()) {
272: Map.Entry entry = (Map.Entry) itr.next();
273:
274: String attrName = (String) entry.getKey();
275: String attrCustomClass = (String) entry.getValue();
276:
277: CustomUserAttributes cua = (CustomUserAttributes) cuaInstances
278: .get(attrCustomClass);
279:
280: if (cua == null) {
281: if (_portlet.isWARFile()) {
282: PortletContextWrapper pcw = (PortletContextWrapper) PortletContextPool
283: .get(_portlet.getRootPortletId());
284:
285: cua = (CustomUserAttributes) pcw
286: .getCustomUserAttributes().get(
287: attrCustomClass);
288:
289: cua = (CustomUserAttributes) cua.clone();
290: } else {
291: try {
292: cua = (CustomUserAttributes) Class
293: .forName(attrCustomClass)
294: .newInstance();
295: } catch (Exception e) {
296: e.printStackTrace();
297: }
298: }
299:
300: cuaInstances.put(attrCustomClass, cua);
301: }
302:
303: if (cua != null) {
304: String attrValue = cua.getValue(attrName,
305: unmodifiableUserInfo);
306:
307: if (attrValue != null) {
308: userInfo.put(attrName, attrValue);
309: }
310: }
311: }
312:
313: return userInfo;
314: }
315: }
316:
317: return _req.getAttribute(name);
318: }
319:
320: public void setAttribute(String name, Object obj) {
321: if (name == null) {
322: throw new IllegalArgumentException();
323: }
324:
325: if (obj == null) {
326: removeAttribute(name);
327: } else {
328: _req.setAttribute(name, obj);
329: }
330: }
331:
332: public void removeAttribute(String name) {
333: if (name == null) {
334: throw new IllegalArgumentException();
335: }
336:
337: _req.removeAttribute(name);
338: }
339:
340: public Enumeration getAttributeNames() {
341: List names = new ArrayList();
342:
343: Enumeration enu = _req.getAttributeNames();
344:
345: while (enu.hasMoreElements()) {
346: String name = (String) enu.nextElement();
347:
348: if (!name
349: .equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
350: names.add(name);
351: }
352: }
353:
354: return Collections.enumeration(names);
355: }
356:
357: public String getParameter(String name) {
358: if (name == null) {
359: throw new IllegalArgumentException();
360: }
361:
362: return _req.getParameter(name);
363: }
364:
365: public Enumeration getParameterNames() {
366: return _req.getParameterNames();
367: }
368:
369: public String[] getParameterValues(String name) {
370: if (name == null) {
371: throw new IllegalArgumentException();
372: }
373:
374: return _req.getParameterValues(name);
375: }
376:
377: public Map getParameterMap() {
378: return _req.getParameterMap();
379: }
380:
381: public boolean isSecure() {
382: return _req.isSecure();
383: }
384:
385: public String getRequestedSessionId() {
386: return _req.getSession().getId();
387: }
388:
389: public boolean isRequestedSessionIdValid() {
390: if (_ses != null) {
391: return _ses.isValid();
392: } else {
393: return _req.isRequestedSessionIdValid();
394: }
395: }
396:
397: public String getResponseContentType() {
398: if (_wapTheme) {
399: return ContentTypes.XHTML_MP;
400: } else {
401: return ContentTypes.TEXT_HTML;
402: }
403: }
404:
405: public Enumeration getResponseContentTypes() {
406: List responseContentTypes = new ArrayList();
407:
408: responseContentTypes.add(getResponseContentType());
409:
410: return Collections.enumeration(responseContentTypes);
411: }
412:
413: public Locale getLocale() {
414: Locale locale = _locale;
415:
416: if (locale == null) {
417: locale = _req.getLocale();
418: }
419:
420: if (locale == null) {
421: locale = LocaleUtil.getDefault();
422: }
423:
424: return locale;
425: }
426:
427: public Enumeration getLocales() {
428: return _req.getLocales();
429: }
430:
431: public String getScheme() {
432: return _req.getScheme();
433: }
434:
435: public String getServerName() {
436: return _req.getServerName();
437: }
438:
439: public int getServerPort() {
440: return _req.getServerPort();
441: }
442:
443: public HttpServletRequest getHttpServletRequest() {
444: return _req;
445: }
446:
447: public String getPortletName() {
448: return _portletName;
449: }
450:
451: public Portlet getPortlet() {
452: return _portlet;
453: }
454:
455: public boolean isPrivateRequestAttributes() {
456: return _portlet.isPrivateRequestAttributes();
457: }
458:
459: public Map getRenderParameters() {
460: return RenderParametersPool.get(_req, _plid, _portletName);
461: }
462:
463: public void defineObjects(PortletConfig portletConfig,
464: RenderResponse res) {
465: setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
466: setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this );
467: setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, res);
468: }
469:
470: public boolean isAction() {
471: return false;
472: }
473:
474: protected RenderRequestImpl() {
475: if (_log.isDebugEnabled()) {
476: _log.debug("Creating new instance " + hashCode());
477: }
478: }
479:
480: protected void init(HttpServletRequest req, Portlet portlet,
481: CachePortlet cachePortlet, PortletContext portletCtx,
482: WindowState windowState, PortletMode portletMode,
483: PortletPreferences prefs, long plid) {
484:
485: _portletName = portlet.getPortletId();
486:
487: String portletNamespace = PortalUtil
488: .getPortletNamespace(_portletName);
489:
490: Map sharedSessionAttributes = SharedSessionUtil
491: .getSharedSessionAttributes(req);
492:
493: boolean portalSessionShared = false;
494:
495: if (portlet.isWARFile()
496: && !portlet.isPrivateSessionAttributes()) {
497: portalSessionShared = true;
498: }
499:
500: req = new SharedSessionServletRequest(req,
501: sharedSessionAttributes, portalSessionShared);
502:
503: DynamicServletRequest dynamicReq = null;
504:
505: if (portlet.isPrivateRequestAttributes()) {
506: dynamicReq = new NamespaceServletRequest(req,
507: portletNamespace, portletNamespace, false);
508: } else {
509: dynamicReq = new DynamicServletRequest(req, false);
510: }
511:
512: Enumeration enu = null;
513:
514: Map renderParameters = null;
515:
516: boolean portletFocus = false;
517:
518: if (_portletName.equals(req.getParameter("p_p_id"))) {
519:
520: // Request was targeted to this portlet
521:
522: boolean action = ParamUtil.getBoolean(req, "p_p_action");
523:
524: if (!action) {
525:
526: // Request was triggered by a render URL
527:
528: portletFocus = true;
529: } else if (action && isAction()) {
530:
531: // Request was triggered by an action URL and is being processed
532: // by com.liferay.portlet.ActionRequestImpl
533:
534: portletFocus = true;
535: }
536: }
537:
538: if (portletFocus) {
539: renderParameters = new HashMap();
540:
541: if (!isAction() && !LiferayWindowState.isExclusive(req)
542: && !LiferayWindowState.isPopUp(req)) {
543:
544: RenderParametersPool.put(req, plid, _portletName,
545: renderParameters);
546: }
547:
548: enu = req.getParameterNames();
549: } else {
550: renderParameters = RenderParametersPool.get(req, plid,
551: _portletName);
552:
553: if (!_portletName.equals(req.getParameter("p_p_id"))) {
554: _putNamespaceParams(req, portletNamespace, plid,
555: renderParameters);
556: }
557:
558: enu = Collections.enumeration(renderParameters.keySet());
559: }
560:
561: while (enu.hasMoreElements()) {
562: String param = (String) enu.nextElement();
563:
564: if (param.startsWith(portletNamespace)
565: && !cachePortlet.isFacesPortlet()) {
566:
567: String newParam = param.substring(portletNamespace
568: .length(), param.length());
569: String[] values = null;
570:
571: if (portletFocus) {
572: values = req.getParameterValues(param);
573:
574: renderParameters.put(param, values);
575: } else {
576: values = (String[]) renderParameters.get(param);
577: }
578:
579: dynamicReq.setParameterValues(newParam, values);
580: } else {
581:
582: // Do not allow reserved or null parameters to pass through.
583: // Jetty has a bug that adds an additional null parameter
584: // the enumeration of parameter names.
585:
586: if (!PortalUtil.isReservedParameter(param)
587: && Validator.isNotNull(param)) {
588:
589: String[] values = null;
590:
591: if (portletFocus) {
592: values = req.getParameterValues(param);
593:
594: renderParameters.put(param, values);
595: } else {
596: values = (String[]) renderParameters.get(param);
597: }
598:
599: dynamicReq.setParameterValues(param, values);
600: }
601: }
602: }
603:
604: _req = dynamicReq;
605: _wapTheme = BrowserSniffer.is_wap_xhtml(_req);
606: _portlet = portlet;
607: _portalCtx = new PortalContextImpl();
608: _portletCtx = portletCtx;
609: _windowState = windowState;
610: _portletMode = portletMode;
611: _prefs = prefs;
612: _portalSessionId = _req.getRequestedSessionId();
613: _ses = new PortletSessionImpl(_req, _portletName, _portletCtx,
614: _portalSessionId, plid);
615:
616: long userId = PortalUtil.getUserId(req);
617: String remoteUser = req.getRemoteUser();
618:
619: String userPrincipalStrategy = portlet
620: .getUserPrincipalStrategy();
621:
622: if (userPrincipalStrategy
623: .equals(PortletImpl.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
624:
625: try {
626: User user = PortalUtil.getUser(req);
627:
628: _remoteUser = user.getScreenName();
629: _remoteUserId = user.getUserId();
630: _userPrincipal = new ProtectedPrincipal(_remoteUser);
631: } catch (Exception e) {
632: _log.error(e);
633: }
634: } else {
635: if ((userId > 0) && (remoteUser == null)) {
636: _remoteUser = String.valueOf(userId);
637: _remoteUserId = userId;
638: _userPrincipal = new ProtectedPrincipal(_remoteUser);
639: } else {
640: _remoteUser = remoteUser;
641: _remoteUserId = GetterUtil.getLong(remoteUser);
642: _userPrincipal = req.getUserPrincipal();
643: }
644: }
645:
646: _locale = (Locale) _req.getSession().getAttribute(
647: Globals.LOCALE_KEY);
648: _plid = plid;
649: }
650:
651: protected void recycle() {
652: if (_log.isDebugEnabled()) {
653: _log.debug("Recycling instance " + hashCode());
654: }
655:
656: _req.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
657: _req.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
658: _req.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
659:
660: _req = null;
661: _wapTheme = false;
662: _portlet = null;
663: _portletName = null;
664: _portalCtx = null;
665: _portletCtx = null;
666: _windowState = null;
667: _portletMode = null;
668: _prefs = null;
669: _ses = null;
670: _portalSessionId = null;
671: _remoteUser = null;
672: _userPrincipal = null;
673: _locale = null;
674: _plid = 0;
675: }
676:
677: private void _putNamespaceParams(HttpServletRequest req,
678: String prefix, long plid, Map renderParameters) {
679:
680: // Adds params that are prefixed with given prefix to parameters pool.
681: // Functionality added by Sergey Ponomarev to allow passing parameters
682: // to multiple portlets in one portlet URL.
683:
684: Enumeration names = req.getParameterNames();
685:
686: while (names.hasMoreElements()) {
687: String key = (String) (names.nextElement());
688:
689: if (key.startsWith(prefix)) {
690: renderParameters.put(key, req.getParameterValues(key));
691: }
692: }
693:
694: RenderParametersPool.put(req, plid, _portletName,
695: renderParameters);
696: }
697:
698: private static Log _log = LogFactory
699: .getLog(RenderRequestImpl.class);
700:
701: private DynamicServletRequest _req;
702: private boolean _wapTheme;
703: private Portlet _portlet;
704: private String _portletName;
705: private PortalContext _portalCtx;
706: private PortletContext _portletCtx;
707: private WindowState _windowState;
708: private PortletMode _portletMode;
709: private PortletPreferences _prefs;
710: private PortletSessionImpl _ses;
711: private String _portalSessionId;
712: private String _remoteUser;
713: private long _remoteUserId;
714: private Principal _userPrincipal;
715: private Locale _locale;
716: private long _plid;
717:
718: }
|