public int doAfterBody() throws JspException
{
if (log.isTraceEnabled()) log.trace("entering ViewTag.doAfterBody");
try
{
BodyContent bodyContent = getBodyContent();
if (bodyContent != null)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
StateManager stateManager = facesContext.getApplication().getStateManager();
StateManager.SerializedView serializedView
= stateManager.saveSerializedView(facesContext);
if (serializedView != null)
{
//until now we have written to a buffer
ResponseWriter bufferWriter = facesContext.getResponseWriter();
bufferWriter.flush();
//now we switch to real output
ResponseWriter realWriter = bufferWriter.cloneWithWriter(getPreviousOut());
facesContext.setResponseWriter(realWriter);
String bodyStr = bodyContent.getString();
if ( stateManager.isSavingStateInClient(facesContext) )
{
int form_marker = bodyStr.indexOf(JspViewHandlerImpl.FORM_STATE_MARKER);
int url_marker = bodyStr.indexOf(HtmlLinkRendererBase.URL_STATE_MARKER);
int lastMarkerEnd = 0;
while (form_marker != -1 || url_marker != -1)
{
if (url_marker == -1 || (form_marker != -1 && form_marker < url_marker))
{
//replace form_marker
realWriter.write(bodyStr, lastMarkerEnd, form_marker - lastMarkerEnd);
stateManager.writeState(facesContext, serializedView);
lastMarkerEnd = form_marker + JspViewHandlerImpl.FORM_STATE_MARKER_LEN;
form_marker = bodyStr.indexOf(JspViewHandlerImpl.FORM_STATE_MARKER, lastMarkerEnd);
}
else
{
//replace url_marker
realWriter.write(bodyStr, lastMarkerEnd, url_marker - lastMarkerEnd);
if (stateManager instanceof MyfacesStateManager)
{
((MyfacesStateManager)stateManager).writeStateAsUrlParams(facesContext,
serializedView);
}
else
{
log.error("Current StateManager is no MyfacesStateManager and does not support saving state in url parameters.");
}
lastMarkerEnd = url_marker + HtmlLinkRendererBase.URL_STATE_MARKER_LEN;
url_marker = bodyStr.indexOf(HtmlLinkRendererBase.URL_STATE_MARKER, lastMarkerEnd);
}
}
realWriter.write(bodyStr, lastMarkerEnd, bodyStr.length() - lastMarkerEnd);
}
else
{
realWriter.write( bodyStr );
}
}
else
{
bodyContent.writeOut(getPreviousOut());
}
}
}
catch (IOException e)
{