protected PortletWindowData getOrCreateDefaultPortletWindowData(
      HttpServletRequest request,
      IPortletEntityId portletEntityId,
      IPortletWindowId portletWindowId,
      IPortletWindowId delegationParentId) {
    // Sync on session map to make sure duplicate PortletWindowData is never created
    final PortletWindowCache<PortletWindowData> portletWindowDataMap =
        getPortletWindowDataMap(request);
    // Check if there portlet window data cached in the session
    PortletWindowData portletWindowData = portletWindowDataMap.getWindow(portletWindowId);
    if (portletWindowData != null) {
      logger.trace(
          "Found PortletWindowData {} in session cache", portletWindowData.getPortletWindowId());
      return portletWindowData;
    }

    // Create new window data for and initialize
    portletWindowData = new PortletWindowData(portletWindowId, portletEntityId, delegationParentId);
    this.initializePortletWindowData(request, portletWindowData);

    // Store in the session cache
    portletWindowData = portletWindowDataMap.storeIfAbsentWindow(portletWindowData);
    logger.trace(
        "Created PortletWindowData {} and stored session cache, wrapping as IPortletWindow and returning",
        portletWindowData.getPortletWindowId());

    return portletWindowData;
  }
  /**
   * @param request
   * @param portletEntityId
   * @param portletWindows
   * @param portletWindowMap
   * @param portletWindowDataMap
   */
  protected void addPortletWindowData(
      HttpServletRequest request,
      IPortletEntityId portletEntityId,
      final Set<IPortletWindow> portletWindows,
      final PortletWindowCache<IPortletWindow> portletWindowMap,
      final PortletWindowCache<PortletWindowData> portletWindowDataMap) {

    final Set<PortletWindowData> windows = portletWindowDataMap.getWindows(portletEntityId);
    if (windows == null) {
      return;
    }

    for (final PortletWindowData portletWindowData : windows) {
      final IPortletWindowId portletWindowId = portletWindowData.getPortletWindowId();

      // Skip data windows that aren't for this entity and for windows that are already in the
      // request cache
      if (!portletEntityId.equals(portletWindowData.getPortletEntityId())
          || portletWindowMap.containsWindow(portletWindowId)) {
        continue;
      }

      // Wrap the data in a window and stick it in the request cache
      IPortletWindow portletWindow = this.wrapPortletWindowData(request, portletWindowData);
      portletWindow = portletWindowMap.storeIfAbsentWindow(portletWindow);

      portletWindows.add(portletWindow);
    }
  }
  protected IPortletWindow wrapPortletWindowData(
      HttpServletRequest request, PortletWindowData portletWindowData) {
    final IPortletEntityId portletEntityId = portletWindowData.getPortletEntityId();
    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
    if (portletEntity == null) {
      return null;
    }

    final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
    final PortletDefinition portletDescriptor =
        this.portletDefinitionRegistry.getParentPortletDescriptor(
            portletDefinition.getPortletDefinitionId());
    if (portletDescriptor == null) {
      return null;
    }

    final IPortletWindow portletWindow =
        new PortletWindowImpl(portletDescriptor, portletEntity, portletWindowData);

    logger.trace(
        "Wrapping PortletWindowData {} as IPortletWindow", portletWindow.getPortletWindowId());

    return portletWindow;
  }
  protected PortletWindowData getPortletWindowData(
      HttpServletRequest request, IPortletWindowId portletWindowId) {
    final PortletWindowCache<PortletWindowData> portletWindowDataMap =
        getPortletWindowDataMap(request, false);
    if (portletWindowDataMap == null) {
      return null;
    }

    final PortletWindowData portletWindowData = portletWindowDataMap.getWindow(portletWindowId);
    if (portletWindowData == null) {
      logger.trace("No PortletWindowData {} in session cache", portletWindowId);
      return null;
    }

    logger.trace(
        "Found PortletWindowData {} in session cache", portletWindowData.getPortletWindowId());
    return portletWindowData;
  }
 /**
  * Initializes a newly created {@link PortletWindow}, the default implementation sets up the
  * appropriate {@link WindowState} and {@link javax.portlet.PortletMode}
  */
 protected void initializePortletWindowData(
     HttpServletRequest request, PortletWindowData portletWindowData) {
   final IStylesheetDescriptor stylesheetDescriptor = getThemeStylesheetDescriptor(request);
   final IPortletEntityId portletEntityId = portletWindowData.getPortletEntityId();
   final IPortletEntity portletEntity =
       this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
   final WindowState entityWindowState = portletEntity.getWindowState(stylesheetDescriptor);
   if (persistentWindowStates.contains(entityWindowState)) {
     portletWindowData.setWindowState(entityWindowState);
   } else if (entityWindowState != null) {
     // Set of persistent window states must have changed, nuke the old value
     this.logger.warn(
         "PortletEntity.windowState="
             + entityWindowState
             + " but that state is not in the set of persistent WindowStates. PortletEntity.windowState will be set to null");
     portletEntity.setWindowState(stylesheetDescriptor, null);
     this.portletEntityRegistry.storePortletEntity(request, portletEntity);
   }
 }
  @Override
  public IPortletWindow getOrCreateStatelessPortletWindow(
      HttpServletRequest request, IPortletWindowId basePortletWindowId) {
    // Need the basePortletWindowId to be an instance of PortletWindowIdImpl so that we can extract
    // the entity ID
    if (!(basePortletWindowId instanceof PortletWindowIdImpl)) {
      final String basePortletWindowIdStr = basePortletWindowId.getStringId();
      basePortletWindowId = this.getPortletWindowId(request, basePortletWindowIdStr);
    }

    // Get the entity ID for the portlet window
    final IPortletEntityId portletEntityId =
        ((PortletWindowIdImpl) basePortletWindowId).getPortletEntityId();

    // Create the stateless ID
    final PortletWindowIdImpl statelessPortletWindowId =
        this.createPortletWindowId(STATELESS_PORTLET_WINDOW_ID, portletEntityId);

    // See if there is already a request cached stateless window
    IPortletWindow statelessPortletWindow =
        this.getPortletWindow(request, statelessPortletWindowId);
    if (statelessPortletWindow != null) {
      return statelessPortletWindow;
    }

    // Lookup the base portlet window to clone the stateless from
    final IPortletWindow basePortletWindow = this.getPortletWindow(request, basePortletWindowId);

    final PortletWindowCache<PortletWindowData> statelessPortletWindowDataMap =
        this.getStatelessPortletWindowDataMap(request, true);

    // If no base to clone from lookup the entity and pluto definition data
    if (basePortletWindow == null) {
      final IPortletEntity portletEntity =
          this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
      if (portletEntity == null) {
        throw new IllegalArgumentException(
            "No IPortletEntity could be found for "
                + portletEntity
                + " while creating stateless portlet window for "
                + basePortletWindowId);
      }

      final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
      final IPortletDefinitionId portletDefinitionId = portletDefinition.getPortletDefinitionId();
      final PortletDefinition portletDescriptor =
          this.portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId);

      final PortletWindowData portletWindowData =
          new PortletWindowData(statelessPortletWindowId, portletEntityId);
      statelessPortletWindowDataMap.storeWindow(portletWindowData);

      statelessPortletWindow =
          new StatelessPortletWindowImpl(portletWindowData, portletEntity, portletDescriptor);
    }
    // Clone the existing base window
    else {
      final PortletWindowData portletWindowData =
          new PortletWindowData(statelessPortletWindowId, portletEntityId);
      portletWindowData.setExpirationCache(basePortletWindow.getExpirationCache());
      portletWindowData.setPortletMode(basePortletWindow.getPortletMode());
      portletWindowData.setWindowState(basePortletWindow.getWindowState());
      portletWindowData.setPublicRenderParameters(basePortletWindow.getPublicRenderParameters());
      portletWindowData.setRenderParameters(basePortletWindow.getRenderParameters());

      statelessPortletWindowDataMap.storeWindow(portletWindowData);

      final IPortletEntity portletEntity = basePortletWindow.getPortletEntity();
      final PortletDefinition portletDescriptor =
          basePortletWindow.getPlutoPortletWindow().getPortletDefinition();
      statelessPortletWindow =
          new StatelessPortletWindowImpl(portletWindowData, portletEntity, portletDescriptor);
    }

    // Cache the stateless window in the request
    final PortletWindowCache<IPortletWindow> portletWindowMap = this.getPortletWindowMap(request);
    portletWindowMap.storeWindow(statelessPortletWindow);

    return statelessPortletWindow;
  }