super.renderView(context, viewToRender);
return;
}
ExternalContext extContext = context.getExternalContext();
MimeResponse mimeResponse = (MimeResponse) extContext.getResponse();
// If first time -- Get the renderPolicy from the context init parameter
if (mRenderPolicy == null)
{
PortletContext pCtx = (PortletContext) context.getExternalContext().getContext();
String policy = pCtx.getInitParameter(Bridge.RENDER_POLICY);
if (policy != null)
{
mRenderPolicy = Bridge.BridgeRenderPolicy.valueOf(policy);
}
else
{
mRenderPolicy = Bridge.BridgeRenderPolicy.DEFAULT;
}
}
if (mRenderPolicy == Bridge.BridgeRenderPolicy.ALWAYS_DELEGATE)
{
super.renderView(context, viewToRender);
return;
}
else if (mRenderPolicy == Bridge.BridgeRenderPolicy.DEFAULT)
{
try
{
super.renderView(context, viewToRender);
return;
}
catch (Throwable t)
{
// catch all throws and swallow -- falling through to our own
// render -- Note because delegate may have set the Response object and
// not cleaned it up on the exception -- reset to ensure a good object
if (!mimeResponse.equals(extContext.getResponse()))
{
extContext.setResponse(mimeResponse);
}
}
}
// suppress rendering if "rendered" property on the component is
// false
if (!viewToRender.isRendered())
{
return;
}
try
{
extContext.getRequestMap().remove("javax.servlet.include.servlet_path");
dispatchPageToBuildView(context, extContext, viewToRender);
}
catch (IOException e)
{
throw new FacesException(e);
}
// If a redirect occurred -- merely return
// check here to see if a redirect occurred -- if so rerun doFacesRequest
// for this new view
QueryString redirectParams = (QueryString) context.getExternalContext()
.getRequestMap().get(BridgeImpl.REDIRECT_VIEWPARAMS);
if ((redirectParams != null))
{
// redirect must have occurred during dispatch
// set a flag indicating we are in a renderRedirect so the ViewHandler will know to dispatch.include instead of forward
// mark it as such so the next dispatch will use an include
extContext.getRequestMap().put(BridgeImpl.HAS_RENDER_REDIRECTED_AFTER_FORWARD, Boolean.TRUE);
return;
}
// set up the ResponseWriter
RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder
.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
RenderKit renderKit = renderFactory.getRenderKit(context, viewToRender.getRenderKitId());
ResponseWriter oldWriter = context.getResponseWriter();
StringBuilderWriter strWriter = new StringBuilderWriter(context, 4096);
ResponseWriter newWriter;
if (null != oldWriter)
{
newWriter = oldWriter.cloneWithWriter(strWriter);
}
else
{
newWriter = renderKit.createResponseWriter(strWriter, null,
mimeResponse.getCharacterEncoding());
}
context.setResponseWriter(newWriter);
newWriter.startDocument();
doRenderView(context, viewToRender);
newWriter.endDocument();
// Check again to see if the processing of the Faces view triggered a redirect
redirectParams = (QueryString) context.getExternalContext()
.getRequestMap().get(BridgeImpl.REDIRECT_VIEWPARAMS);
if ((redirectParams != null))
{
// set a flag indicating we are in a renderRedirect so the ViewHandler will know to dispatch.include instead of forward
// mark it as such so the next dispatch will use an include
extContext.getRequestMap().put(BridgeImpl.HAS_RENDER_REDIRECTED_AFTER_FORWARD, Boolean.TRUE);
return;
}
// replace markers in the body content and write it to response.
ResponseWriter responseWriter;
// Dispatch may have output to an OutputStream instead of a Writer
Writer renderResponseWriter = null;
try {
renderResponseWriter = mimeResponse.getWriter();
}
catch (IllegalStateException ise) {
// got this exception because we've called getOutputStream() previously
renderResponseWriter = new BufferedWriter(
new OutputStreamWriter(
mimeResponse.getPortletOutputStream(),
mimeResponse.getCharacterEncoding()));
}
if (null != oldWriter)
{
responseWriter = oldWriter.cloneWithWriter(renderResponseWriter);
}
else
{
responseWriter = newWriter.cloneWithWriter(renderResponseWriter);
}
context.setResponseWriter(responseWriter);
strWriter.write(responseWriter);
renderResponseWriter.flush();
if (null != oldWriter)
{
context.setResponseWriter(oldWriter);
}
Object content = extContext.getRequestMap().get(Bridge.AFTER_VIEW_CONTENT);
if (content != null)
{
try
{
if (content instanceof char[])
{
mimeResponse.getWriter().write(new String((char[]) content));
}
else if (content instanceof byte[])
{
mimeResponse.getWriter().write(new String((byte[]) content));
}
else
{
throw new IOException("PortletViewHandlerImpl: invalid" + "AFTER_VIEW_CONTENT buffer type");
}
}
finally
{
// So it doesn't geet reused accidentaly
extContext.getRequestMap().remove(Bridge.AFTER_VIEW_CONTENT);
}
}
mimeResponse.flushBuffer();
}