Ejemplo n.º 1
0
  /**
   * @see javax.faces.view.ViewDeclarationLanguage#renderView(javax.faces.context.FacesContext,
   *     javax.faces.component.UIViewRoot)
   */
  public void renderView(FacesContext ctx, UIViewRoot viewToRender) throws IOException {

    // suppress rendering if "rendered" property on the component is
    // false
    if (!viewToRender.isRendered()) {
      return;
    }

    // log request
    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Rendering View: " + viewToRender.getViewId());
    }

    WriteBehindStateWriter stateWriter = null;
    try {
      // Only build the view if this view has not yet been built.
      if (!Util.isViewPopulated(ctx, viewToRender)) {
        this.buildView(ctx, viewToRender);
      }

      // setup writer and assign it to the ctx
      ResponseWriter origWriter = ctx.getResponseWriter();
      if (origWriter == null) {
        origWriter = createResponseWriter(ctx);
      }

      stateWriter = new WriteBehindStateWriter(origWriter, ctx, responseBufferSize);

      ResponseWriter writer = origWriter.cloneWithWriter(stateWriter);
      ctx.setResponseWriter(writer);

      // render the view to the response
      writer.startDocument();
      viewToRender.encodeAll(ctx);
      writer.endDocument();

      // finish writing
      writer.close();

      boolean writtenState = stateWriter.stateWritten();
      // flush to origWriter
      if (writtenState) {
        stateWriter.flushToWriter();
      }

    } catch (FileNotFoundException fnfe) {
      this.handleFaceletNotFound(ctx, viewToRender.getViewId(), fnfe.getMessage());
    } catch (Exception e) {
      this.handleRenderException(ctx, e);
    } finally {
      if (stateWriter != null) stateWriter.release();
    }
  }
Ejemplo n.º 2
0
  /**
   * @see javax.faces.view.ViewDeclarationLanguage#renderView(javax.faces.context.FacesContext,
   *     javax.faces.component.UIViewRoot)
   */
  public void renderView(FacesContext context, UIViewRoot view) throws IOException {

    // suppress rendering if "rendered" property on the component is
    // false
    if (!view.isRendered() || context.getResponseComplete()) {
      return;
    }

    ExternalContext extContext = context.getExternalContext();

    if (!Util.isViewPopulated(context, view)) {
      buildView(context, view);
    }

    // set up the ResponseWriter

    RenderKitFactory renderFactory =
        (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
    RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId());

    ResponseWriter oldWriter = context.getResponseWriter();

    WriteBehindStateWriter stateWriter =
        new WriteBehindStateWriter(
            extContext.getResponseOutputWriter(), context, responseBufferSize);
    ResponseWriter newWriter;
    if (null != oldWriter) {
      newWriter = oldWriter.cloneWithWriter(stateWriter);
    } else {
      newWriter =
          renderKit.createResponseWriter(
              stateWriter, null, extContext.getRequestCharacterEncoding());
    }
    context.setResponseWriter(newWriter);

    //  Don't call startDoc and endDoc on a partial response
    if (context.getPartialViewContext().isPartialRequest()) {
      doRenderView(context, view);
      try {
        extContext.getFlash().doPostPhaseActions(context);
      } catch (UnsupportedOperationException uoe) {
        if (LOGGER.isLoggable(Level.FINE)) {
          LOGGER.fine(
              "ExternalContext.getFlash() throw UnsupportedOperationException -> Flash unavailable");
        }
      }
    } else {
      // render the view to the response
      newWriter.startDocument();
      doRenderView(context, view);
      try {
        extContext.getFlash().doPostPhaseActions(context);
      } catch (UnsupportedOperationException uoe) {
        if (LOGGER.isLoggable(Level.FINE)) {
          LOGGER.fine(
              "ExternalContext.getFlash() throw UnsupportedOperationException -> Flash unavailable");
        }
      }
      newWriter.endDocument();
    }

    // replace markers in the body content and write it to response.

    // flush directly to the response
    if (stateWriter.stateWritten()) {
      stateWriter.flushToWriter();
    }

    // clear the ThreadLocal reference.
    stateWriter.release();

    if (null != oldWriter) {
      context.setResponseWriter(oldWriter);
    }

    // write any AFTER_VIEW_CONTENT to the response
    // side effect: AFTER_VIEW_CONTENT removed
    ViewHandlerResponseWrapper wrapper =
        (ViewHandlerResponseWrapper)
            RequestStateManager.remove(context, RequestStateManager.AFTER_VIEW_CONTENT);
    if (null != wrapper) {
      wrapper.flushToWriter(
          extContext.getResponseOutputWriter(), extContext.getResponseCharacterEncoding());
    }

    extContext.responseFlushBuffer();
  }