001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.ccpp.PortalProfileFactory;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.portlet.LiferayPortletContext;
022    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
023    import com.liferay.portal.kernel.portlet.LiferayPortletSession;
024    import com.liferay.portal.kernel.portlet.LiferayWindowState;
025    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
026    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
027    import com.liferay.portal.kernel.servlet.ProtectedPrincipal;
028    import com.liferay.portal.kernel.servlet.ServletContextPool;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.util.ContentTypes;
031    import com.liferay.portal.kernel.util.ContextPathUtil;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.JavaConstants;
034    import com.liferay.portal.kernel.util.LocaleUtil;
035    import com.liferay.portal.kernel.util.ParamUtil;
036    import com.liferay.portal.kernel.util.StringPool;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.kernel.xml.QName;
039    import com.liferay.portal.model.Portlet;
040    import com.liferay.portal.model.PortletApp;
041    import com.liferay.portal.model.PortletConstants;
042    import com.liferay.portal.model.PublicRenderParameter;
043    import com.liferay.portal.model.User;
044    import com.liferay.portal.security.lang.DoPrivilegedBean;
045    import com.liferay.portal.security.lang.DoPrivilegedUtil;
046    import com.liferay.portal.service.PortletLocalServiceUtil;
047    import com.liferay.portal.service.RoleLocalServiceUtil;
048    import com.liferay.portal.servlet.NamespaceServletRequest;
049    import com.liferay.portal.servlet.SharedSessionServletRequest;
050    import com.liferay.portal.theme.ThemeDisplay;
051    import com.liferay.portal.util.PortalUtil;
052    import com.liferay.portal.util.WebKeys;
053    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
054    
055    import java.security.Principal;
056    import java.security.PrivilegedAction;
057    
058    import java.util.ArrayList;
059    import java.util.Collections;
060    import java.util.Enumeration;
061    import java.util.HashMap;
062    import java.util.LinkedHashMap;
063    import java.util.List;
064    import java.util.Locale;
065    import java.util.Map;
066    
067    import javax.ccpp.Profile;
068    
069    import javax.portlet.PortalContext;
070    import javax.portlet.PortletConfig;
071    import javax.portlet.PortletContext;
072    import javax.portlet.PortletMode;
073    import javax.portlet.PortletPreferences;
074    import javax.portlet.PortletRequest;
075    import javax.portlet.PortletResponse;
076    import javax.portlet.PortletSession;
077    import javax.portlet.WindowState;
078    import javax.portlet.filter.PortletRequestWrapper;
079    
080    import javax.servlet.ServletContext;
081    import javax.servlet.http.Cookie;
082    import javax.servlet.http.HttpServletRequest;
083    import javax.servlet.http.HttpSession;
084    
085    /**
086     * @author Brian Wing Shun Chan
087     * @author Brian Myunghun Kim
088     * @author Sergey Ponomarev
089     * @author Raymond Aug??
090     */
091    public abstract class PortletRequestImpl implements LiferayPortletRequest {
092    
093            public static PortletRequestImpl getPortletRequestImpl(
094                    PortletRequest portletRequest) {
095    
096                    while (!(portletRequest instanceof PortletRequestImpl)) {
097                            if (portletRequest instanceof DoPrivilegedBean) {
098                                    DoPrivilegedBean doPrivilegedBean =
099                                            (DoPrivilegedBean)portletRequest;
100    
101                                    portletRequest =
102                                            (PortletRequest)doPrivilegedBean.getActualBean();
103                            }
104                            else if (portletRequest instanceof PortletRequestWrapper) {
105                                    PortletRequestWrapper portletRequestWrapper =
106                                            (PortletRequestWrapper)portletRequest;
107    
108                                    portletRequest = portletRequestWrapper.getRequest();
109                            }
110                            else {
111                                    throw new RuntimeException(
112                                            "Unable to unwrap the portlet request from " +
113                                                    portletRequest.getClass());
114                            }
115                    }
116    
117                    return (PortletRequestImpl)portletRequest;
118            }
119    
120            public void cleanUp() {
121                    _request.removeAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
122                    _request.removeAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
123                    _request.removeAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
124                    _request.removeAttribute(PortletRequest.LIFECYCLE_PHASE);
125                    _request.removeAttribute(WebKeys.PORTLET_ID);
126                    _request.removeAttribute(WebKeys.PORTLET_CONTENT);
127            }
128    
129            public void defineObjects(
130                    PortletConfig portletConfig, PortletResponse portletResponse) {
131    
132                    LiferayPortletConfig liferayPortletConfig =
133                            (LiferayPortletConfig)portletConfig;
134    
135                    setAttribute(WebKeys.PORTLET_ID, liferayPortletConfig.getPortletId());
136                    setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG, portletConfig);
137                    setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST, this);
138                    setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE, portletResponse);
139                    setAttribute(PortletRequest.LIFECYCLE_PHASE, getLifecycle());
140            }
141    
142            @Override
143            public Object getAttribute(String name) {
144                    if (name == null) {
145                            throw new IllegalArgumentException();
146                    }
147    
148                    if (name.equals(PortletRequest.CCPP_PROFILE)) {
149                            return getCCPPProfile();
150                    }
151                    else if (name.equals(PortletRequest.USER_INFO)) {
152                            Object value = getUserInfo();
153    
154                            if (value != null) {
155                                    return value;
156                            }
157                    }
158    
159                    return _request.getAttribute(name);
160            }
161    
162            @Override
163            public Enumeration<String> getAttributeNames() {
164                    List<String> names = new ArrayList<String>();
165    
166                    Enumeration<String> enu = _request.getAttributeNames();
167    
168                    while (enu.hasMoreElements()) {
169                            String name = enu.nextElement();
170    
171                            if (!name.equals(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO)) {
172                                    names.add(name);
173                            }
174                    }
175    
176                    return Collections.enumeration(names);
177            }
178    
179            @Override
180            public String getAuthType() {
181                    return _request.getAuthType();
182            }
183    
184            public Profile getCCPPProfile() {
185                    if (_profile == null) {
186                            _profile = PortalProfileFactory.getCCPPProfile(_request);
187                    }
188    
189                    return _profile;
190            }
191    
192            @Override
193            public String getContextPath() {
194                    LiferayPortletContext liferayPortletContext =
195                            (LiferayPortletContext)_portletContext;
196    
197                    ServletContext servletContext =
198                            liferayPortletContext.getServletContext();
199    
200                    String servletContextName = servletContext.getServletContextName();
201    
202                    if (ServletContextPool.containsKey(servletContextName)) {
203                            servletContext = ServletContextPool.get(servletContextName);
204    
205                            return ContextPathUtil.getContextPath(servletContext);
206                    }
207    
208                    return StringPool.SLASH.concat(_portletContext.getPortletContextName());
209            }
210    
211            @Override
212            public Cookie[] getCookies() {
213                    return _request.getCookies();
214            }
215    
216            public String getETag() {
217                    return null;
218            }
219    
220            @Override
221            public HttpServletRequest getHttpServletRequest() {
222                    return _request;
223            }
224    
225            public abstract String getLifecycle();
226    
227            @Override
228            public Locale getLocale() {
229                    Locale locale = _locale;
230    
231                    if (locale == null) {
232                            locale = _request.getLocale();
233                    }
234    
235                    if (locale == null) {
236                            locale = LocaleUtil.getDefault();
237                    }
238    
239                    return locale;
240            }
241    
242            @Override
243            public Enumeration<Locale> getLocales() {
244                    return _request.getLocales();
245            }
246    
247            public String getMethod() {
248                    return _request.getMethod();
249            }
250    
251            public HttpServletRequest getOriginalHttpServletRequest() {
252                    return _originalRequest;
253            }
254    
255            @Override
256            public String getParameter(String name) {
257                    if (name == null) {
258                            throw new IllegalArgumentException();
259                    }
260    
261                    if (_portletRequestDispatcherRequest != null) {
262                            return _portletRequestDispatcherRequest.getParameter(name);
263                    }
264    
265                    return _request.getParameter(name);
266            }
267    
268            @Override
269            public Map<String, String[]> getParameterMap() {
270                    if (_portletRequestDispatcherRequest != null) {
271                            return Collections.unmodifiableMap(
272                                    _portletRequestDispatcherRequest.getParameterMap());
273                    }
274    
275                    return Collections.unmodifiableMap(_request.getParameterMap());
276            }
277    
278            @Override
279            public Enumeration<String> getParameterNames() {
280                    if (_portletRequestDispatcherRequest != null) {
281                            return _portletRequestDispatcherRequest.getParameterNames();
282                    }
283    
284                    return _request.getParameterNames();
285            }
286    
287            @Override
288            public String[] getParameterValues(String name) {
289                    if (name == null) {
290                            throw new IllegalArgumentException();
291                    }
292    
293                    if (_portletRequestDispatcherRequest != null) {
294                            return _portletRequestDispatcherRequest.getParameterValues(name);
295                    }
296    
297                    return _request.getParameterValues(name);
298            }
299    
300            @Override
301            public PortalContext getPortalContext() {
302                    return _portalContext;
303            }
304    
305            public Portlet getPortlet() {
306                    return _portlet;
307            }
308    
309            public PortletContext getPortletContext() {
310                    return _portletContext;
311            }
312    
313            @Override
314            public PortletMode getPortletMode() {
315                    return _portletMode;
316            }
317    
318            public String getPortletName() {
319                    return _portletName;
320            }
321    
322            @Override
323            public PortletSession getPortletSession() {
324                    return _session;
325            }
326    
327            @Override
328            public PortletSession getPortletSession(boolean create) {
329                    /*HttpSession httpSes = _req.getSession(create);
330    
331                    if (httpSes == null) {
332                            return null;
333                    }
334                    else {
335                            if (create) {
336                                    _session = new PortletSessionImpl(
337                                            _req.getSession(), _portletContext, _portletName, _plid);
338                            }
339    
340                            return _ses;
341                    }*/
342    
343                    /*if ((_session == null) && create) {
344                            _req.getSession(create);
345    
346                            _session = new PortletSessionImpl(
347                                    _req.getSession(), _portletContext, _portletName, _plid);
348                    }*/
349    
350                    if (!create && _invalidSession) {
351                            return null;
352                    }
353    
354                    return _session;
355            }
356    
357            @Override
358            public PortletPreferences getPreferences() {
359                    return DoPrivilegedUtil.wrap(new PortletPreferencesPrivilegedAction());
360            }
361    
362            public PortletPreferencesImpl getPreferencesImpl() {
363                    return (PortletPreferencesImpl)_preferences;
364            }
365    
366            @Override
367            public Map<String, String[]> getPrivateParameterMap() {
368                    Map<String, String[]> parameterMap = null;
369    
370                    if (_portletRequestDispatcherRequest != null) {
371                            parameterMap = _portletRequestDispatcherRequest.getParameterMap();
372                    }
373                    else {
374                            parameterMap = _request.getParameterMap();
375                    }
376    
377                    Map<String, String[]> privateParameterMap = null;
378    
379                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
380                            String name = entry.getKey();
381    
382                            if (_portlet.getPublicRenderParameter(name) != null) {
383                                    continue;
384                            }
385    
386                            if (privateParameterMap == null) {
387                                    privateParameterMap = new HashMap<String, String[]>(
388                                            parameterMap.size(), 1);
389                            }
390    
391                            privateParameterMap.put(name, entry.getValue());
392                    }
393    
394                    if (privateParameterMap == null) {
395                            return Collections.emptyMap();
396                    }
397    
398                    return Collections.unmodifiableMap(privateParameterMap);
399            }
400    
401            @Override
402            public Enumeration<String> getProperties(String name) {
403                    List<String> values = new ArrayList<String>();
404    
405                    String value = _portalContext.getProperty(name);
406    
407                    if (value != null) {
408                            values.add(value);
409                    }
410    
411                    return Collections.enumeration(values);
412            }
413    
414            @Override
415            public String getProperty(String name) {
416                    return _portalContext.getProperty(name);
417            }
418    
419            @Override
420            public Enumeration<String> getPropertyNames() {
421                    return _portalContext.getPropertyNames();
422            }
423    
424            @Override
425            public Map<String, String[]> getPublicParameterMap() {
426                    Map<String, String[]> parameterMap = null;
427    
428                    if (_portletRequestDispatcherRequest != null) {
429                            parameterMap = _portletRequestDispatcherRequest.getParameterMap();
430                    }
431                    else {
432                            parameterMap = _request.getParameterMap();
433                    }
434    
435                    Map<String, String[]> publicParameterMap = null;
436    
437                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
438                            String name = entry.getKey();
439    
440                            if (_portlet.getPublicRenderParameter(name) != null) {
441                                    if (publicParameterMap == null) {
442                                            publicParameterMap = new HashMap<String, String[]>(
443                                                    parameterMap.size(), 1);
444                                    }
445    
446                                    publicParameterMap.put(name, entry.getValue());
447                            }
448                    }
449    
450                    if (publicParameterMap == null) {
451                            return Collections.emptyMap();
452                    }
453                    else {
454                            return Collections.unmodifiableMap(publicParameterMap);
455                    }
456            }
457    
458            @Override
459            public String getRemoteUser() {
460                    return _remoteUser;
461            }
462    
463            @Override
464            public Map<String, String[]> getRenderParameters() {
465                    return RenderParametersPool.get(_request, _plid, _portletName);
466            }
467    
468            @Override
469            public String getRequestedSessionId() {
470                    if (_session != null) {
471                            return _session.getId();
472                    }
473    
474                    HttpSession session = _request.getSession(false);
475    
476                    if (session == null) {
477                            return StringPool.BLANK;
478                    }
479                    else {
480                            return session.getId();
481                    }
482            }
483    
484            @Override
485            public String getResponseContentType() {
486                    if (_wapTheme) {
487                            return ContentTypes.XHTML_MP;
488                    }
489                    else {
490                            return ContentTypes.TEXT_HTML;
491                    }
492            }
493    
494            @Override
495            public Enumeration<String> getResponseContentTypes() {
496                    List<String> responseContentTypes = new ArrayList<String>();
497    
498                    responseContentTypes.add(getResponseContentType());
499    
500                    return Collections.enumeration(responseContentTypes);
501            }
502    
503            @Override
504            public String getScheme() {
505                    return _request.getScheme();
506            }
507    
508            @Override
509            public String getServerName() {
510                    return _request.getServerName();
511            }
512    
513            @Override
514            public int getServerPort() {
515                    return _request.getServerPort();
516            }
517    
518            public LinkedHashMap<String, String> getUserInfo() {
519                    return UserInfoFactory.getUserInfo(_remoteUserId, _portlet);
520            }
521    
522            @Override
523            public Principal getUserPrincipal() {
524                    return _userPrincipal;
525            }
526    
527            @Override
528            public String getWindowID() {
529                    return _portletName.concat(
530                            LiferayPortletSession.LAYOUT_SEPARATOR).concat(
531                                    String.valueOf(_plid));
532            }
533    
534            @Override
535            public WindowState getWindowState() {
536                    return _windowState;
537            }
538    
539            public void invalidateSession() {
540                    _invalidSession = true;
541            }
542    
543            public boolean isInvalidParameter(String name) {
544                    if (Validator.isNull(name) ||
545                            name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
546                            name.startsWith(
547                                    PortletQName.REMOVE_PUBLIC_RENDER_PARAMETER_NAMESPACE) ||
548                            PortalUtil.isReservedParameter(name)) {
549    
550                            return true;
551                    }
552                    else {
553                            return false;
554                    }
555            }
556    
557            @Override
558            public boolean isPortletModeAllowed(PortletMode portletMode) {
559                    if ((portletMode == null) || Validator.isNull(portletMode.toString())) {
560                            return true;
561                    }
562                    else {
563                            return _portlet.hasPortletMode(
564                                    getResponseContentType(), portletMode);
565                    }
566            }
567    
568            public boolean isPrivateRequestAttributes() {
569                    return _portlet.isPrivateRequestAttributes();
570            }
571    
572            @Override
573            public boolean isRequestedSessionIdValid() {
574                    return _request.isRequestedSessionIdValid();
575            }
576    
577            @Override
578            public boolean isSecure() {
579                    return _request.isSecure();
580            }
581    
582            public boolean isTriggeredByActionURL() {
583                    return _triggeredByActionURL;
584            }
585    
586            @Override
587            public boolean isUserInRole(String role) {
588                    if (_remoteUserId <= 0) {
589                            return false;
590                    }
591    
592                    try {
593                            long companyId = PortalUtil.getCompanyId(_request);
594    
595                            String roleLink = _portlet.getRoleMappers().get(role);
596    
597                            if (Validator.isNotNull(roleLink)) {
598                                    return RoleLocalServiceUtil.hasUserRole(
599                                            _remoteUserId, companyId, roleLink, true);
600                            }
601                            else {
602                                    return RoleLocalServiceUtil.hasUserRole(
603                                            _remoteUserId, companyId, role, true);
604                            }
605                    }
606                    catch (Exception e) {
607                            _log.error(e);
608                    }
609    
610                    return _request.isUserInRole(role);
611            }
612    
613            @Override
614            public boolean isWindowStateAllowed(WindowState windowState) {
615                    return PortalContextImpl.isSupportedWindowState(windowState);
616            }
617    
618            @Override
619            public void removeAttribute(String name) {
620                    if (name == null) {
621                            throw new IllegalArgumentException();
622                    }
623    
624                    _request.removeAttribute(name);
625            }
626    
627            @Override
628            public void setAttribute(String name, Object obj) {
629                    if (name == null) {
630                            throw new IllegalArgumentException();
631                    }
632    
633                    if (obj == null) {
634                            removeAttribute(name);
635                    }
636                    else {
637                            _request.setAttribute(name, obj);
638                    }
639            }
640    
641            public void setPortletMode(PortletMode portletMode) {
642                    _portletMode = portletMode;
643            }
644    
645            public void setPortletRequestDispatcherRequest(HttpServletRequest request) {
646                    _portletRequestDispatcherRequest = request;
647            }
648    
649            public void setWindowState(WindowState windowState) {
650                    _windowState = windowState;
651            }
652    
653            protected void init(
654                    HttpServletRequest request, Portlet portlet,
655                    InvokerPortlet invokerPortlet, PortletContext portletContext,
656                    WindowState windowState, PortletMode portletMode,
657                    PortletPreferences preferences, long plid) {
658    
659                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
660                            WebKeys.THEME_DISPLAY);
661    
662                    _portlet = portlet;
663                    _portletName = portlet.getPortletId();
664                    _publicRenderParameters = PublicRenderParametersPool.get(request, plid);
665    
666                    String portletNamespace = PortalUtil.getPortletNamespace(_portletName);
667    
668                    PortletApp portletApp = portlet.getPortletApp();
669    
670                    boolean warFile = portletApp.isWARFile();
671    
672                    if (!warFile) {
673                            String portletResource = ParamUtil.getString(
674                                    request, portletNamespace.concat("portletResource"));
675    
676                            if (Validator.isNotNull(portletResource)) {
677                                    Portlet resourcePortlet = null;
678    
679                                    try {
680                                            resourcePortlet = PortletLocalServiceUtil.getPortletById(
681                                                    themeDisplay.getCompanyId(), portletResource);
682                                    }
683                                    catch (Exception e) {
684                                    }
685    
686                                    if (resourcePortlet != null) {
687                                            PortletApp resourcePortletApp =
688                                                    resourcePortlet.getPortletApp();
689    
690                                            if (resourcePortletApp.isWARFile()) {
691                                                    warFile = true;
692                                            }
693                                    }
694                            }
695                    }
696    
697                    if (warFile) {
698                            request = new SharedSessionServletRequest(
699                                    request, !portlet.isPrivateSessionAttributes());
700                    }
701    
702                    String dynamicQueryString = (String)request.getAttribute(
703                            DynamicServletRequest.DYNAMIC_QUERY_STRING);
704    
705                    if (dynamicQueryString != null) {
706                            request.removeAttribute(DynamicServletRequest.DYNAMIC_QUERY_STRING);
707    
708                            request = DynamicServletRequest.addQueryString(
709                                    request, dynamicQueryString, true);
710                    }
711    
712                    DynamicServletRequest dynamicRequest = null;
713    
714                    if (portlet.isPrivateRequestAttributes()) {
715                            dynamicRequest = new NamespaceServletRequest(
716                                    request, portletNamespace, portletNamespace, false);
717                    }
718                    else {
719                            dynamicRequest = new DynamicServletRequest(request, false);
720                    }
721    
722                    boolean portletFocus = false;
723    
724                    String ppid = ParamUtil.getString(request, "p_p_id");
725    
726                    boolean windowStateRestoreCurrentView = ParamUtil.getBoolean(
727                            request, "p_p_state_rcv");
728    
729                    if (_portletName.equals(ppid) &&
730                            !(windowStateRestoreCurrentView &&
731                              portlet.isRestoreCurrentView())) {
732    
733                            // Request was targeted to this portlet
734    
735                            if (themeDisplay.isLifecycleRender() ||
736                                    themeDisplay.isLifecycleResource()) {
737    
738                                    // Request was triggered by a render or resource URL
739    
740                                    portletFocus = true;
741                            }
742                            else if (themeDisplay.isLifecycleAction()) {
743                                    _triggeredByActionURL = true;
744    
745                                    if (getLifecycle().equals(PortletRequest.ACTION_PHASE)) {
746    
747                                            // Request was triggered by an action URL and is being
748                                            // processed by com.liferay.portlet.ActionRequestImpl
749    
750                                            portletFocus = true;
751                                    }
752                            }
753                    }
754    
755                    if (portletFocus) {
756                            Map<String, String[]> renderParameters =
757                                    new HashMap<String, String[]>();
758    
759                            if (getLifecycle().equals(PortletRequest.RENDER_PHASE) &&
760                                    !LiferayWindowState.isExclusive(request) &&
761                                    !LiferayWindowState.isPopUp(request)) {
762    
763                                    RenderParametersPool.put(
764                                            request, plid, _portletName, renderParameters);
765                            }
766    
767                            Map<String, String[]> parameters = request.getParameterMap();
768    
769                            for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
770                                    String name = entry.getKey();
771    
772                                    if (isInvalidParameter(name)) {
773                                            continue;
774                                    }
775    
776                                    String[] values = entry.getValue();
777    
778                                    if (themeDisplay.isLifecycleRender()) {
779                                            renderParameters.put(name, values);
780                                    }
781    
782                                    if (values == null) {
783                                            continue;
784                                    }
785    
786                                    if ((invokerPortlet != null) &&
787                                            invokerPortlet.isFacesPortlet()) {
788    
789                                            if (name.startsWith(portletNamespace) ||
790                                                    !portlet.isRequiresNamespacedParameters()) {
791    
792                                                    dynamicRequest.setParameterValues(name, values);
793                                            }
794                                    }
795                                    else {
796                                            String realName = removePortletNamespace(
797                                                    portletNamespace, name);
798    
799                                            if (!realName.equals(name) ||
800                                                    !portlet.isRequiresNamespacedParameters()) {
801    
802                                                    dynamicRequest.setParameterValues(realName, values);
803                                            }
804                                    }
805                            }
806                    }
807                    else {
808                            Map<String, String[]> renderParameters = RenderParametersPool.get(
809                                    request, plid, _portletName);
810    
811                            for (Map.Entry<String, String[]> entry :
812                                            renderParameters.entrySet()) {
813    
814                                    String name = entry.getKey();
815                                    String[] values = entry.getValue();
816    
817                                    if ((invokerPortlet == null) ||
818                                            !invokerPortlet.isFacesPortlet()) {
819    
820                                            name = removePortletNamespace(portletNamespace, name);
821                                    }
822    
823                                    dynamicRequest.setParameterValues(name, values);
824                            }
825                    }
826    
827                    mergePublicRenderParameters(dynamicRequest, preferences, plid);
828    
829                    _request = dynamicRequest;
830                    _originalRequest = request;
831                    _wapTheme = BrowserSnifferUtil.isWap(_request);
832                    _portlet = portlet;
833                    _portalContext = new PortalContextImpl();
834                    _portletContext = portletContext;
835                    _windowState = windowState;
836                    _portletMode = portletMode;
837                    _preferences = preferences;
838                    _session = new PortletSessionImpl(
839                            _request.getSession(), _portletContext, _portletName, plid);
840    
841                    String remoteUser = request.getRemoteUser();
842    
843                    String userPrincipalStrategy = portlet.getUserPrincipalStrategy();
844    
845                    if (userPrincipalStrategy.equals(
846                                    PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {
847    
848                            try {
849                                    User user = PortalUtil.getUser(request);
850    
851                                    if (user != null) {
852                                            _remoteUser = user.getScreenName();
853                                            _remoteUserId = user.getUserId();
854                                            _userPrincipal = new ProtectedPrincipal(_remoteUser);
855                                    }
856                            }
857                            catch (Exception e) {
858                                    _log.error(e);
859                            }
860                    }
861                    else {
862                            long userId = PortalUtil.getUserId(request);
863    
864                            if ((userId > 0) && (remoteUser == null)) {
865                                    _remoteUser = String.valueOf(userId);
866                                    _remoteUserId = userId;
867                                    _userPrincipal = new ProtectedPrincipal(_remoteUser);
868                            }
869                            else {
870                                    _remoteUser = remoteUser;
871                                    _remoteUserId = GetterUtil.getLong(remoteUser);
872                                    _userPrincipal = request.getUserPrincipal();
873                            }
874                    }
875    
876                    _locale = themeDisplay.getLocale();
877                    _plid = plid;
878            }
879    
880            protected void mergePublicRenderParameters(
881                    DynamicServletRequest dynamicRequest, PortletPreferences preferences,
882                    long plid) {
883    
884                    Enumeration<PublicRenderParameter> publicRenderParameters =
885                            Collections.enumeration(_portlet.getPublicRenderParameters());
886    
887                    while (publicRenderParameters.hasMoreElements()) {
888                            PublicRenderParameter publicRenderParameter =
889                                    publicRenderParameters.nextElement();
890    
891                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
892                                    publicRenderParameter);
893    
894                            boolean ignoreValue = GetterUtil.getBoolean(
895                                    preferences.getValue(ignoreKey, null));
896    
897                            if (ignoreValue) {
898                                    continue;
899                            }
900    
901                            String mappingKey =
902                                    PublicRenderParameterConfiguration.getMappingKey(
903                                            publicRenderParameter);
904    
905                            String mappingValue = GetterUtil.getString(
906                                    preferences.getValue(mappingKey, null));
907    
908                            HttpServletRequest request =
909                                    (HttpServletRequest)dynamicRequest.getRequest();
910    
911                            String[] newValues = request.getParameterValues(mappingValue);
912    
913                            if ((newValues != null) && (newValues.length != 0)) {
914                                    newValues = ArrayUtil.remove(newValues, StringPool.NULL);
915                            }
916    
917                            String name = publicRenderParameter.getIdentifier();
918    
919                            if (ArrayUtil.isEmpty(newValues)) {
920                                    QName qName = publicRenderParameter.getQName();
921    
922                                    String[] values = _publicRenderParameters.get(
923                                            PortletQNameUtil.getPublicRenderParameterName(qName));
924    
925                                    if (ArrayUtil.isEmpty(values) || Validator.isNull(values[0])) {
926                                            continue;
927                                    }
928    
929                                    if (dynamicRequest.getParameter(name) == null) {
930                                            dynamicRequest.setParameterValues(name, values);
931                                    }
932                            }
933                            else {
934                                    dynamicRequest.setParameterValues(name, newValues);
935                            }
936                    }
937            }
938    
939            protected String removePortletNamespace(
940                    String portletNamespace, String name) {
941    
942                    if (name.startsWith(portletNamespace)) {
943                            name = name.substring(portletNamespace.length());
944                    }
945    
946                    return name;
947            }
948    
949            private static Log _log = LogFactoryUtil.getLog(PortletRequestImpl.class);
950    
951            private boolean _invalidSession;
952            private Locale _locale;
953            private HttpServletRequest _originalRequest;
954            private long _plid;
955            private PortalContext _portalContext;
956            private Portlet _portlet;
957            private PortletContext _portletContext;
958            private PortletMode _portletMode;
959            private String _portletName;
960            private HttpServletRequest _portletRequestDispatcherRequest;
961            private PortletPreferences _preferences;
962            private Profile _profile;
963            private Map<String, String[]> _publicRenderParameters;
964            private String _remoteUser;
965            private long _remoteUserId;
966            private HttpServletRequest _request;
967            private PortletSessionImpl _session;
968            private boolean _triggeredByActionURL;
969            private Principal _userPrincipal;
970            private boolean _wapTheme;
971            private WindowState _windowState;
972    
973            private class PortletPreferencesPrivilegedAction
974                    implements PrivilegedAction<PortletPreferences> {
975    
976                    @Override
977                    public PortletPreferences run() {
978                            return new PortletPreferencesWrapper(
979                                    getPreferencesImpl(), getLifecycle());
980                    }
981    
982            }
983    
984    }