/**
   * Unlike Pluto, Liferay will preserve/copy request attributes that were originally set on an
   * {@link ActionRequest} into the {@link RenderRequest}. However, the Bridge Spec assumes that
   * they will not be preserved. Therefore is necessary to remove these request attributes when
   * running under Liferay.
   */
  public void removeExcludedAttributes(RenderRequest renderRequest) {

    if (isRedirectOccurred() || isPortletModeChanged()) {

      // TCK TestPage062: eventScopeNotRestoredRedirectTest
      // TCK TestPage063: eventScopeNotRestoredModeChangedTest
      @SuppressWarnings("unchecked")
      List<String> nonExcludedAttributeNames =
          (List<String>) getAttribute(BRIDGE_REQ_SCOPE_NON_EXCLUDED_ATTR_NAMES);

      if (nonExcludedAttributeNames != null) {

        for (String attributeName : nonExcludedAttributeNames) {

          renderRequest.removeAttribute(attributeName);

          if (logger.isTraceEnabled()) {

            if (isRedirectOccurred()) {
              logger.trace(
                  "Due to redirect, removed request attribute name=[{0}] that had been preserved in the ACTION_PHASE or EVENT_PHASE",
                  attributeName);
            } else {
              logger.trace(
                  "Due to PortletMode change, removed request attribute name=[{0}] that had been preserved in the ACTION_PHASE or EVENT_PHASE",
                  attributeName);
            }
          }
        }
      }
    }

    // Iterate through all of the request attributes and build up a list of those that are to be
    // removed.
    Enumeration<String> attributeNames = renderRequest.getAttributeNames();

    // TCK TestPage037: requestScopeContentsTest
    // TCK TestPage045: excludedAttributesTest
    // TCK TestPage151: requestMapRequestScopeTest
    while (attributeNames.hasMoreElements()) {
      String attributeName = attributeNames.nextElement();
      Object attributeValue = renderRequest.getAttribute(attributeName);

      if (preExistingAttributeNames.contains(attributeName)) {

        if (isExcludedRequestAttributeByConfig(attributeName, attributeValue)) {

          // TCK TestPage151 (requestMapRequestScopeTest) remove "verifyPreBridgeExclusion"
          renderRequest.removeAttribute(attributeName);
          logger.debug(
              "Removed request attribute name=[{0}] since it was specified for removal.",
              attributeName);
        } else {
          logger.debug(
              "Kept request attribute name=[{0}] since it existed prior to the FacesContext being created.",
              attributeName);
        }
      } else {

        if (isExcludedRequestAttributeByConfig(attributeName, attributeValue)
            || isExcludedRequestAttributeByAnnotation(attributeValue)
            || isExcludedRequestAttributeByInstance(attributeName, attributeValue)
            || isExcludedRequestAttributeByNamespace(attributeName)) {

          renderRequest.removeAttribute(attributeName);

          logger.debug(
              "Removed request attribute name=[{0}] that had been preserved in the ACTION_PHASE or EVENT_PHASE",
              attributeName);
        }
      }
    }
  }