public void execute(FacesContext facesContext) throws FacesException {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Entering RestoreViewPhase");
}
if (null == facesContext) {
throw new FacesException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_CONTEXT_ERROR_MESSAGE_ID));
}
// If an app had explicitely set the tree in the context, use that;
//
UIViewRoot viewRoot = facesContext.getViewRoot();
if (viewRoot != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Found a pre created view in FacesContext");
}
facesContext.getViewRoot().setLocale(
facesContext.getExternalContext().getRequestLocale());
doPerComponentActions(facesContext, viewRoot);
if (!isPostback(facesContext)) {
facesContext.responseComplete();
}
return;
}
// Reconstitute or create the request tree
Map requestMap = facesContext.getExternalContext().getRequestMap();
String viewId = (String)
requestMap.get("javax.servlet.include.path_info");
if (viewId == null) {
viewId = facesContext.getExternalContext().getRequestPathInfo();
}
// It could be that this request was mapped using
// a prefix mapping in which case there would be no
// path_info. Query the servlet path.
if (viewId == null) {
viewId = (String)
requestMap.get("javax.servlet.include.servlet_path");
}
if (viewId == null) {
Object request = facesContext.getExternalContext().getRequest();
if (request instanceof HttpServletRequest) {
viewId = ((HttpServletRequest) request).getServletPath();
}
}
if (viewId == null) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("viewId is null");
}
throw new FacesException(MessageUtils.getExceptionMessageString(
MessageUtils.NULL_REQUEST_VIEW_ERROR_MESSAGE_ID));
}
boolean isPostBack = (isPostback(facesContext) && !isErrorPage(facesContext));
if (isPostBack) {
// try to restore the view
ViewHandler viewHandler = Util.getViewHandler(facesContext);
viewRoot = viewHandler.restoreView(facesContext, viewId);
if (viewRoot == null) {
if (is11CompatEnabled(facesContext)) {
// 1.1 - > create a new view and flag that the response should
// be immediately rendered
viewRoot = viewHandler.createView(facesContext, viewId);
facesContext.renderResponse();
} else {
Object[] params = {viewId};
throw new ViewExpiredException(
MessageUtils.getExceptionMessageString(
MessageUtils.RESTORE_VIEW_ERROR_MESSAGE_ID,
params),
viewId);
}
}
facesContext.setViewRoot(viewRoot);
doPerComponentActions(facesContext, viewRoot);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Postback: Restored view for " + viewId);
}
} else {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("New request: creating a view for " + viewId);
}
// if that fails, create one
viewRoot = (Util.getViewHandler(facesContext)).
createView(facesContext, viewId);
facesContext.setViewRoot(viewRoot);
facesContext.renderResponse();
}
assert(null != viewRoot);
if (isPostBack && LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "+=+=+=+=+=+= Restored View Printout for " + viewId);
DebugUtil.printTree(viewRoot, LOGGER, Level.FINEST);
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Exiting RestoreViewPhase");
}
}
PRECONDITION: the necessary factories have been installed in the
ServletContext attr set.
POSTCONDITION: The facesContext has been initialized with a tree. |