}
ResponseWriter oldResponseWriter = responseWriter;
StateMarkerAwareWriter stateAwareWriter = null;
StateManager stateManager = facesContext.getApplication().getStateManager();
if (stateManager.isSavingStateInClient(facesContext))
{
stateAwareWriter = new StateMarkerAwareWriter();
// Create a new response-writer using as an underlying writer the stateAwareWriter
// Effectively, all output will be buffered in the stateAwareWriter so that later
// this writer can replace the state-markers with the actual state.
responseWriter = hookInStateAwareWriter(
oldResponseWriter, stateAwareWriter, renderKit, externalContext);
facesContext.setResponseWriter(responseWriter);
}
actuallyRenderView(facesContext, viewToRender);
facesContext.setResponseWriter(oldResponseWriter);
//We're done with the document - now we can write all content
//to the response, properly replacing the state-markers on the way out
//by using the stateAwareWriter
if (stateManager.isSavingStateInClient(facesContext))
{
stateAwareWriter.flushToWriter(response.getWriter());
}
else
{
stateManager.saveView(facesContext);
}
// Final step - we output any content in the wrappedResponse response from above to the response,
// removing the wrappedResponse response from the request, we don't need it anymore
ViewResponseWrapper afterViewTagResponse = (ViewResponseWrapper) externalContext.getRequestMap().get(