예제 #1
0
  /**
   * Perform actions that need to happen on the <code>beforePhase</code> event.
   *
   * <p>For all phases, store the current phaseId in request scope.
   *
   * <p>For before restore-view, create a sequenceId for this request and store it in request scope.
   *
   * <p>For before render-response, store the sequenceId for this request in the response.
   */
  public void beforePhase(PhaseEvent e) {
    FacesContext context = e.getFacesContext();
    ExternalContext extContext = context.getExternalContext();
    Object response = extContext.getResponse();
    Map<String, Object> requestMap = extContext.getRequestMap();
    String thisRequestSequenceString = null;
    String postbackSequenceString = null;
    ELFlash elFlash = (ELFlash) ELFlash.getFlash(context, true);

    // If we're on before-restore-view...
    if (e.getPhaseId().equals(PhaseId.RESTORE_VIEW)) {
      thisRequestSequenceString = Long.toString(getSequenceNumber());
      // Put the sequence number for the request/response pair
      // that is starting with *this particular request* in the request scope
      // so the ELFlash can access it.
      requestMap.put(Constants.FLASH_THIS_REQUEST_ATTRIBUTE_NAME, thisRequestSequenceString);

      // Make sure to restore all request scoped data
      if (null != elFlash && elFlash.isKeepMessages()) {
        elFlash.restoreAllMessages(context);
      }

      if (context.isPostback()) {
        // to a servlet JSF app...
        if (response instanceof HttpServletResponse) {
          // extract the sequence number from the cookie or portletSession
          // for the request/response pair for which this request is a postback.
          postbackSequenceString = getCookieValue(extContext);
        } else {
          /**
           * **** PortletRequest portletRequest = null; portletRequest = (PortletRequest) request;
           * // You can't retrieve a cookie in portlet. //
           * http://wiki.java.net/bin/view/Portlet/JSR168FAQ#How_can_I_set_retrieve_a_cookie
           * postbackSequenceString = (String) portletRequest.getPortletSession().
           * getAttribute(Constants.FLASH_POSTBACK_REQUEST_ATTRIBUTE_NAME,
           * PortletSession.PORTLET_SCOPE); *****
           */
        }
        if (null != postbackSequenceString) {
          // Store the sequenceNumber in the request so the
          // after-render-response event can flush the flash
          // of entries from that sequence
          requestMap.put(Constants.FLASH_POSTBACK_REQUEST_ATTRIBUTE_NAME, postbackSequenceString);
        }
      }
    }
    if (e.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
      // Set the REQUEST_ID cookie to be the sequence number
      addCookie(extContext, elFlash);
    }
  }
예제 #2
0
 private void expireEntries(FacesContext context) {
   ExternalContext extContext = context.getExternalContext();
   String postbackSequenceString = null;
   // Clear out the flash for the postback.
   if (null
       != (postbackSequenceString =
           (String)
               extContext.getRequestMap().get(Constants.FLASH_POSTBACK_REQUEST_ATTRIBUTE_NAME))) {
     ELFlash flash = (ELFlash) ELFlash.getFlash(context, false);
     if (null != flash) {
       flash.expireEntriesForSequence(postbackSequenceString);
     }
   }
 }
예제 #3
0
  /**
   * Perform actions that need to happen on the <code>afterPhase</code> event.
   *
   * <p>For after restore-view, if this is a postback, we extract the sequenceId from the request
   * and store it in the request scope.
   *
   * <p>For after render-response, we clear out the flash for the postback, while leaving the
   * current one intact.
   */
  public void afterPhase(PhaseEvent e) {
    FacesContext context = e.getFacesContext();
    ExternalContext extContext = context.getExternalContext();
    Map<String, Object> requestMap = extContext.getRequestMap();
    Object request = extContext.getRequest(), response = extContext.getResponse();
    ELFlash elFlash = ELFlash.getELFlash();

    if (e.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
      expireEntries(context);
    }

    // If this requset is ending normally...
    if (e.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
      // and the user requested we save all request scoped data...
      if (null != elFlash && elFlash.isKeepMessages()) {
        // save it all.
        elFlash.saveAllMessages(context);
      }
    }
    // Otherwise, if this request is ending early...
    else if ((context.getResponseComplete() || context.getRenderResponse())
        && elFlash.isRedirect()) {
      // and the user requested we save all request scoped data...
      if (null != elFlash && elFlash.isKeepMessages()) {
        // save it all.
        addCookie(extContext, elFlash);
        elFlash.saveAllMessages(context);
      }
    }
  }