/** * 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); } }
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); } } }