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