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