private void sendWindowHandlerHtml(ExternalContext externalContext, String windowId) {
    HttpServletResponse httpResponse = (HttpServletResponse) externalContext.getResponse();

    try {
      httpResponse.setStatus(HttpServletResponse.SC_OK);
      httpResponse.setContentType("text/html");

      String windowHandlerHtml = clientWindowConfig.getClientWindowHtml();

      if (windowId == null) {
        windowId = UNINITIALIZED_WINDOW_ID_VALUE;
      }

      // set the windowId value in the javascript code
      windowHandlerHtml = windowHandlerHtml.replace(WINDOW_ID_REPLACE_PATTERN, windowId);

      // set the noscript-URL for users with no JavaScript
      windowHandlerHtml =
          windowHandlerHtml.replace(NOSCRIPT_URL_REPLACE_PATTERN, getNoscriptUrl(externalContext));

      OutputStream os = httpResponse.getOutputStream();
      try {
        os.write(windowHandlerHtml.getBytes());
      } finally {
        os.close();
      }
    } catch (IOException ioe) {
      throw new FacesException(ioe);
    }
  }
  @Override
  public boolean isClientWindowRenderModeEnabled(FacesContext facesContext) {
    ClientWindowRenderMode clientWindowRenderMode =
        clientWindowConfig.getClientWindowRenderMode(facesContext);

    if (ClientWindowRenderMode.LAZY.equals(clientWindowRenderMode)) {
      Map<Object, Object> attrMap = facesContext.getAttributes();
      return !attrMap.containsKey(PER_USE_CLIENT_WINDOW_URL_QUERY_PARAMETER_DISABLED_KEY);
    }

    return false;
  }
  @Override
  public void enableClientWindowRenderMode(FacesContext facesContext) {
    ClientWindowRenderMode clientWindowRenderMode =
        clientWindowConfig.getClientWindowRenderMode(facesContext);

    if (ClientWindowRenderMode.DELEGATED.equals(clientWindowRenderMode)) {
      facesContext
          .getExternalContext()
          .getClientWindow()
          .enableClientWindowRenderMode(facesContext);
    } else if (ClientWindowRenderMode.LAZY.equals(clientWindowRenderMode)) {
      Map<Object, Object> attrMap = facesContext.getAttributes();
      attrMap.remove(PER_USE_CLIENT_WINDOW_URL_QUERY_PARAMETER_DISABLED_KEY);
    }
  }
  @Override
  public Map<String, String> getQueryURLParameters(FacesContext facesContext) {
    ClientWindowRenderMode clientWindowRenderMode =
        clientWindowConfig.getClientWindowRenderMode(facesContext);

    if (ClientWindowRenderMode.LAZY.equals(clientWindowRenderMode)) {
      String windowId = getWindowId(facesContext);
      if (windowId != null) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(DELTASPIKE_WINDOW_ID_URL_PARAM, getWindowId(facesContext));
        return params;
      }
    }

    return null;
  }
  @Override
  public String getWindowId(FacesContext facesContext) {
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();

    // try to lookup from cache
    String windowId = (String) requestMap.get(WINDOW_ID_REQUEST_MAP_KEY);
    if (windowId != null) {
      return windowId;
    }

    ClientWindowRenderMode clientWindowRenderMode =
        clientWindowConfig.getClientWindowRenderMode(facesContext);
    if (ClientWindowRenderMode.NONE.equals(clientWindowRenderMode)) {
      // if this request should not get any window detection then we are done
      windowId = DEFAULT_WINDOW_ID;
    } else if (ClientWindowRenderMode.DELEGATED.equals(clientWindowRenderMode)) {
      windowId = ClientWindowAdapter.getWindowIdFromJsf(facesContext);
    } else if (ClientWindowRenderMode.LAZY.equals(clientWindowRenderMode)) {
      windowId = ClientWindowHelper.getInitialRedirectWindowId(facesContext);

      if (StringUtils.isEmpty(windowId)) {
        windowId = externalContext.getRequestParameterMap().get(DELTASPIKE_WINDOW_ID_URL_PARAM);
      }

      if (StringUtils.isEmpty(windowId)) {
        if (this.jsfModuleConfig.isInitialRedirectEnabled()) {
          ClientWindowHelper.handleInitialRedirect(facesContext, generateNewWindowId());
          facesContext.responseComplete();
          windowId = null;
        } else {
          windowId = generateNewWindowId();
        }
      }
    } else if (ClientWindowRenderMode.CLIENTWINDOW.equals(clientWindowRenderMode)) {
      if (facesContext.isPostback()) {
        windowId = getPostBackWindowId(facesContext);
      } else if (isNoscriptRequest(externalContext)) {
        // the client has JavaScript disabled
        clientWindowConfig.setJavaScriptEnabled(false);

        windowId = DEFAULT_WINDOW_ID;
      } else {
        windowId = getVerifiedWindowIdFromCookie(externalContext);

        boolean newWindowIdRequested = false;
        if (AUTOMATED_ENTRY_POINT_PARAMETER_KEY.equals(windowId)) {
          // this is a marker for generating a new windowId
          windowId = generateNewWindowId();
          newWindowIdRequested = true;
        }

        if (windowId == null || newWindowIdRequested) {
          // GET request without windowId - send windowhandlerfilter.html to get the windowId
          sendWindowHandlerHtml(externalContext, windowId);
          facesContext.responseComplete();
        }
      }
    }

    // we have a valid windowId - set it and continue with the request
    if (windowId != null) {
      requestMap.put(WINDOW_ID_REQUEST_MAP_KEY, windowId);
    }

    return windowId;
  }