}
ResponseWriter oldResponseWriter = responseWriter;
StringWriter stateAwareWriter = null;
StateManager stateManager = context.getApplication().getStateManager();
if (stateManager.isSavingStateInClient(context))
{
stateAwareWriter = new StringWriter();
// 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 = oldResponseWriter.cloneWithWriter(stateAwareWriter);
context.setResponseWriter(responseWriter);
}
actuallyRenderView(context, view);
if(oldResponseWriter != null)
{
context.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(context))
{
flushBufferToWriter(stateAwareWriter.getBuffer(), externalContext.getResponseOutputWriter());
}
else
{
stateManager.saveView(context);
}
// 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()