throw new FacesException("FacesContext is null");
}
// get some required Objects
Application application = facesContext.getApplication();
ViewHandler viewHandler = application.getViewHandler();
UIViewRoot viewRoot = facesContext.getViewRoot();
RestoreViewSupport restoreViewSupport = getRestoreViewSupport();
// Examine the FacesContext instance for the current request. If it already contains a UIViewRoot
if (viewRoot != null)
{
if (log.isLoggable(Level.FINEST))
{
log.finest("View already exists in the FacesContext");
}
// Set the locale on this UIViewRoot to the value returned by the getRequestLocale() method on the
// ExternalContext for this request
viewRoot.setLocale(facesContext.getExternalContext().getRequestLocale());
restoreViewSupport.processComponentBinding(facesContext, viewRoot);
// invoke the afterPhase MethodExpression of UIViewRoot
_invokeViewRootAfterPhaseListener(facesContext);
return false;
}
String viewId = restoreViewSupport.calculateViewId(facesContext);
// Determine if the current request is an attempt by the
// servlet container to display an error page.
// If the request is an error page request, the servlet container
// is required to set the request parameter "javax.servlet.error.message".
final boolean errorPageRequest = facesContext.getExternalContext().getRequestMap()
.get("javax.servlet.error.message") != null;
// Determine if this request is a postback or an initial request.
// But if it is an error page request, do not treat it as a postback (since 2.0)
if (!errorPageRequest && restoreViewSupport.isPostback(facesContext))
{ // If the request is a postback
if (log.isLoggable(Level.FINEST))
{
log.finest("Request is a postback");
}
try
{
facesContext.setProcessingEvents(false);
// call ViewHandler.restoreView(), passing the FacesContext instance for the current request and the
// view identifier, and returning a UIViewRoot for the restored view.
viewRoot = viewHandler.restoreView(facesContext, viewId);
if (viewRoot == null)
{
// If the return from ViewHandler.restoreView() is null, throw a ViewExpiredException with an
// appropriate error message.
throw new ViewExpiredException("No saved view state could be found for the view identifier: "
+ viewId, viewId);
}
// Store the restored UIViewRoot in the FacesContext.
facesContext.setViewRoot(viewRoot);
}
finally
{
facesContext.setProcessingEvents(true);
}
// Restore binding
// See https://javaserverfaces-spec-public.dev.java.net/issues/show_bug.cgi?id=806
restoreViewSupport.processComponentBinding(facesContext, viewRoot);
}
else
{ // If the request is a non-postback
if (log.isLoggable(Level.FINEST))
{
log.finest("Request is not a postback. New UIViewRoot will be created");
}
//viewHandler.deriveViewId(facesContext, viewId)
//restoreViewSupport.deriveViewId(facesContext, viewId)
ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(facesContext,
viewHandler.deriveLogicalViewId(facesContext, viewId));
// viewHandler.deriveLogicalViewId() could trigger an InvalidViewIdException, which
// it is handled internally sending a 404 error code set the response as complete.
if (facesContext.getResponseComplete())
{
return true;
}
if (vdl != null)
{
ViewMetadata metadata = vdl.getViewMetadata(facesContext, viewId);
Collection<UIViewParameter> viewParameters = null;
if (metadata != null)
{
viewRoot = metadata.createMetadataView(facesContext);
if (viewRoot != null)
{
viewParameters = ViewMetadata.getViewParameters(viewRoot);
}
else if(facesContext.getResponseComplete())
{
// this can happen if the current request is a debug request,
// in this case no further processing is necessary
return true;
}
}
// If viewParameters is not an empty collection DO NOT call renderResponse
if ( !(viewParameters != null && !viewParameters.isEmpty()) )
{
// Call renderResponse() on the FacesContext.
facesContext.renderResponse();
}
}
else
{
// Call renderResponse
facesContext.renderResponse();
}
// viewRoot can be null here, if ...
// - we don't have a ViewDeclarationLanguage (e.g. when using facelets-1.x)
// - there is no view metadata or metadata.createMetadataView() returned null
if (viewRoot == null)
{
// call ViewHandler.createView(), passing the FacesContext instance for the current request and
// the view identifier
viewRoot = viewHandler.createView(facesContext, viewId);
}
// Subscribe the newly created UIViewRoot instance to the AfterAddToParent event, passing the
// UIViewRoot instance itself as the listener.
// -= Leonardo Uribe =- This line it is not necessary because it was