Ejemplo n.º 1
0
  /**
   * @param context the {@link FacesContext} for the current request
   * @return a {@link ResponseWriter} for processing the request
   * @throws IOException if the writer cannot be created
   */
  protected ResponseWriter createResponseWriter(FacesContext context) throws IOException {

    ExternalContext extContext = context.getExternalContext();
    RenderKit renderKit = context.getRenderKit();
    // Avoid a cryptic NullPointerException when the renderkit ID
    // is incorrectly set
    if (renderKit == null) {
      String id = context.getViewRoot().getRenderKitId();
      throw new IllegalStateException("No render kit was available for id \"" + id + "\"");
    }

    if (responseBufferSizeSet) {
      // set the buffer for content
      extContext.setResponseBufferSize(responseBufferSize);
    }

    // get our content type
    String contentType = (String) extContext.getRequestMap().get("facelets.ContentType");

    // get the encoding
    String encoding = (String) extContext.getRequestMap().get("facelets.Encoding");

    // Create a dummy ResponseWriter with a bogus writer,
    // so we can figure out what content type the ReponseWriter
    // is really going to ask for
    ResponseWriter writer =
        renderKit.createResponseWriter(NullWriter.Instance, contentType, encoding);

    contentType = getResponseContentType(context, writer.getContentType());
    encoding = getResponseEncoding(context, writer.getCharacterEncoding());

    // apply them to the response
    extContext.setResponseContentType(contentType);
    extContext.setResponseCharacterEncoding(encoding);

    // Now, clone with the real writer
    writer = writer.cloneWithWriter(extContext.getResponseOutputWriter());

    return writer;
  }
Ejemplo n.º 2
0
Archivo: App.java Proyecto: ndrew/bndr
  public void jsonStatus() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    Session session =
        (Session) context.getApplication().evaluateExpressionGet(context, "#{auth}", Session.class);

    String data = "{\"status\": \"not-logged-in\"}";

    if (session.isUserLoggedIn()) {
      if (null == this.data) {
        data = "{\"status\": \"waiting for app\"}";
        this.data = "foo";
      } else {
        data = "{\"status\": \"done\"}";
      }
    }

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/json");
    externalContext.setResponseCharacterEncoding("UTF-8");
    externalContext.getResponseOutputWriter().write(data);
    facesContext.responseComplete();
  }
Ejemplo n.º 3
0
  private PartialResponseWriter createPartialResponseWriter() {

    ExternalContext extContext = ctx.getExternalContext();
    String encoding = extContext.getRequestCharacterEncoding();
    extContext.setResponseCharacterEncoding(encoding);
    ResponseWriter responseWriter = null;
    Writer out = null;
    try {
      out = extContext.getResponseOutputWriter();
    } catch (IOException ioe) {
      if (LOGGER.isLoggable(Level.SEVERE)) {
        LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
      }
    }

    if (out != null) {
      responseWriter = ctx.getRenderKit().createResponseWriter(out, "text/xml", encoding);
    }
    if (responseWriter instanceof PartialResponseWriter) {
      return (PartialResponseWriter) responseWriter;
    } else {
      return new PartialResponseWriter(responseWriter);
    }
  }
Ejemplo n.º 4
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();
  }