protected void processPublicRenderParameters(
      HttpServletRequest request, Layout layout, Portlet portlet) {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    Map<String, String[]> publicRenderParameters =
        PublicRenderParametersPool.get(request, layout.getPlid());

    Map<String, String[]> parameters = request.getParameterMap();

    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
      String name = entry.getKey();

      QName qName = PortletQNameUtil.getQName(name);

      if (qName == null) {
        continue;
      }

      PublicRenderParameter publicRenderParameter =
          portlet.getPublicRenderParameter(qName.getNamespaceURI(), qName.getLocalPart());

      if (publicRenderParameter == null) {
        continue;
      }

      String publicRenderParameterName = PortletQNameUtil.getPublicRenderParameterName(qName);

      if (name.startsWith(PortletQName.PUBLIC_RENDER_PARAMETER_NAMESPACE)) {

        String[] values = entry.getValue();

        if (themeDisplay.isLifecycleAction()) {
          String[] oldValues = publicRenderParameters.get(publicRenderParameterName);

          if ((oldValues != null) && (oldValues.length != 0)) {
            values = ArrayUtil.append(values, oldValues);
          }
        }

        publicRenderParameters.put(publicRenderParameterName, values);
      } else {
        publicRenderParameters.remove(publicRenderParameterName);
      }
    }
  }
  protected void init(
      HttpServletRequest request,
      Portlet portlet,
      InvokerPortlet invokerPortlet,
      PortletContext portletContext,
      WindowState windowState,
      PortletMode portletMode,
      PortletPreferences preferences,
      long plid) {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    _portlet = portlet;
    _portletName = portlet.getPortletId();
    _publicRenderParameters = PublicRenderParametersPool.get(request, plid);

    String portletNamespace = PortalUtil.getPortletNamespace(_portletName);

    boolean portalSessionShared = false;

    PortletApp portletApp = portlet.getPortletApp();

    if (portletApp.isWARFile() && !portlet.isPrivateSessionAttributes()) {
      portalSessionShared = true;
    }

    request = new SharedSessionServletRequest(request, portalSessionShared);

    DynamicServletRequest dynamicRequest = null;

    if (portlet.isPrivateRequestAttributes()) {
      dynamicRequest =
          new NamespaceServletRequest(request, portletNamespace, portletNamespace, false);
    } else {
      dynamicRequest = new DynamicServletRequest(request, false);
    }

    boolean portletFocus = false;

    String ppid = ParamUtil.getString(request, "p_p_id");

    boolean windowStateRestoreCurrentView = ParamUtil.getBoolean(request, "p_p_state_rcv");

    if (_portletName.equals(ppid)
        && !(windowStateRestoreCurrentView && portlet.isRestoreCurrentView())) {

      // Request was targeted to this portlet

      if (themeDisplay.isLifecycleRender() || themeDisplay.isLifecycleResource()) {

        // Request was triggered by a render or resource URL

        portletFocus = true;
      } else if (themeDisplay.isLifecycleAction()) {
        _triggeredByActionURL = true;

        if (getLifecycle().equals(PortletRequest.ACTION_PHASE)) {

          // Request was triggered by an action URL and is being
          // processed by com.liferay.portlet.ActionRequestImpl

          portletFocus = true;
        }
      }
    }

    Map<String, String[]> renderParameters = RenderParametersPool.get(request, plid, _portletName);

    if (portletFocus) {
      renderParameters = new HashMap<String, String[]>();

      if (getLifecycle().equals(PortletRequest.RENDER_PHASE)
          && !LiferayWindowState.isExclusive(request)
          && !LiferayWindowState.isPopUp(request)) {

        RenderParametersPool.put(request, plid, _portletName, renderParameters);
      }

      Map<String, String[]> parameters = request.getParameterMap();

      for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
        String name = entry.getKey();

        if (isInvalidParameter(name)) {
          continue;
        }

        String[] values = entry.getValue();

        if (themeDisplay.isLifecycleRender()) {
          renderParameters.put(name, values);
        }

        if (values == null) {
          continue;
        }

        name = removePortletNamespace(invokerPortlet, portletNamespace, name);

        dynamicRequest.setParameterValues(name, values);
      }
    } else {
      for (Map.Entry<String, String[]> entry : renderParameters.entrySet()) {

        String name = entry.getKey();
        String[] values = entry.getValue();

        name = removePortletNamespace(invokerPortlet, portletNamespace, name);

        dynamicRequest.setParameterValues(name, values);
      }
    }

    mergePublicRenderParameters(dynamicRequest, preferences, plid);

    _request = dynamicRequest;
    _originalRequest = request;
    _wapTheme = BrowserSnifferUtil.isWap(_request);
    _portlet = portlet;
    _portalContext = new PortalContextImpl();
    _portletContext = portletContext;
    _windowState = windowState;
    _portletMode = portletMode;
    _preferences = preferences;
    _portalSessionId = _request.getRequestedSessionId();
    _session =
        new PortletSessionImpl(_request, _portletName, _portletContext, _portalSessionId, plid);

    String remoteUser = request.getRemoteUser();

    String userPrincipalStrategy = portlet.getUserPrincipalStrategy();

    if (userPrincipalStrategy.equals(PortletConstants.USER_PRINCIPAL_STRATEGY_SCREEN_NAME)) {

      try {
        User user = PortalUtil.getUser(request);

        if (user != null) {
          _remoteUser = user.getScreenName();
          _remoteUserId = user.getUserId();
          _userPrincipal = new ProtectedPrincipal(_remoteUser);
        }
      } catch (Exception e) {
        _log.error(e);
      }
    } else {
      long userId = PortalUtil.getUserId(request);

      if ((userId > 0) && (remoteUser == null)) {
        _remoteUser = String.valueOf(userId);
        _remoteUserId = userId;
        _userPrincipal = new ProtectedPrincipal(_remoteUser);
      } else {
        _remoteUser = remoteUser;
        _remoteUserId = GetterUtil.getLong(remoteUser);
        _userPrincipal = request.getUserPrincipal();
      }
    }

    _locale = themeDisplay.getLocale();
    _plid = plid;
  }
  protected ActionForward processLayout(
      ActionMapping mapping, HttpServletRequest request, HttpServletResponse response, long plid)
      throws Exception {

    HttpSession session = request.getSession();

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    try {
      Layout layout = themeDisplay.getLayout();

      boolean resetLayout =
          ParamUtil.getBoolean(request, "p_l_reset", PropsValues.LAYOUT_DEFAULT_P_L_RESET);

      String portletId = ParamUtil.getString(request, "p_p_id");

      Layout previousLayout = (Layout) session.getAttribute(WebKeys.PREVIOUS_LAYOUT);

      if ((previousLayout == null) || (layout.getPlid() != previousLayout.getPlid())) {

        session.setAttribute(WebKeys.PREVIOUS_LAYOUT, layout);

        if (themeDisplay.isSignedIn()
            && PropsValues.AUDIT_MESSAGE_COM_LIFERAY_PORTAL_MODEL_LAYOUT_VIEW
            && MessageBusUtil.hasMessageListener(DestinationNames.AUDIT)) {

          User user = themeDisplay.getUser();

          AuditMessage auditMessage =
              new AuditMessage(
                  ActionKeys.VIEW,
                  user.getCompanyId(),
                  user.getUserId(),
                  user.getFullName(),
                  Layout.class.getName(),
                  String.valueOf(layout.getPlid()));

          AuditRouterUtil.route(auditMessage);
        }
      }

      if (!PropsValues.TCK_URL
          && resetLayout
          && (Validator.isNull(portletId)
              || ((previousLayout != null) && (layout.getPlid() != previousLayout.getPlid())))) {

        // Always clear render parameters on a layout url, but do not
        // clear on portlet urls invoked on the same layout

        RenderParametersPool.clear(request, plid);
      }

      if (themeDisplay.isLifecycleAction()) {
        Portlet portlet = processPortletRequest(request, response, PortletRequest.ACTION_PHASE);

        if (portlet != null) {
          ActionResponseImpl actionResponseImpl =
              (ActionResponseImpl) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);

          String redirectLocation = actionResponseImpl.getRedirectLocation();

          if (Validator.isNotNull(redirectLocation)) {
            response.sendRedirect(redirectLocation);

            return null;
          }

          if (portlet.isActionURLRedirect()) {
            redirectActionURL(request, response, actionResponseImpl, portlet);

            return null;
          }
        }
      } else if (themeDisplay.isLifecycleRender()) {
        processPortletRequest(request, response, PortletRequest.RENDER_PHASE);
      }

      if (themeDisplay.isLifecycleResource()) {
        processPortletRequest(request, response, PortletRequest.RESOURCE_PHASE);

        return null;
      } else {
        if (response.isCommitted()) {
          return null;
        }

        if (layout != null) {

          // Include layout content before the page loads because
          // portlets on the page can set the page title and page
          // subtitle

          includeLayoutContent(request, response, themeDisplay, layout);

          if (themeDisplay.isStateExclusive()) {
            renderExclusive(request, response, themeDisplay);

            return null;
          }
        }

        return mapping.findForward("portal.layout");
      }
    } catch (Exception e) {
      PortalUtil.sendError(e, request, response);

      return null;
    } finally {
      if (!ServerDetector.isResin()) {
        PortletRequest portletRequest =
            (PortletRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

        if (portletRequest != null) {
          PortletRequestImpl portletRequestImpl = (PortletRequestImpl) portletRequest;

          portletRequestImpl.cleanUp();
        }
      }
    }
  }