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