Пример #1
0
  /**
   * Obtain and return the {@link ResponseStateManager} for the specified #renderKitId.
   *
   * @param context the {@link FacesContext} of the current request
   * @param renderKitId {@link RenderKit} ID
   * @return the {@link ResponseStateManager} for the specified #renderKitId
   * @throws FacesException if an exception occurs while trying to obtain the <code>
   *     ResponseStateManager</code>
   */
  public static ResponseStateManager getResponseStateManager(
      FacesContext context, String renderKitId) throws FacesException {

    assert (null != renderKitId);
    assert (null != context);

    RenderKit renderKit = context.getRenderKit();
    if (renderKit == null) {
      // check request scope for a RenderKitFactory implementation
      RenderKitFactory factory =
          (RenderKitFactory)
              RequestStateManager.get(context, RequestStateManager.RENDER_KIT_IMPL_REQ);
      if (factory != null) {
        renderKit = factory.getRenderKit(context, renderKitId);
      } else {
        factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
        if (factory == null) {
          throw new IllegalStateException();
        } else {
          RequestStateManager.set(context, RequestStateManager.RENDER_KIT_IMPL_REQ, factory);
        }
        renderKit = factory.getRenderKit(context, renderKitId);
      }
    }
    return renderKit.getResponseStateManager();
  }
Пример #2
0
 static Renderer getRenderer(final FacesContext context, final UIComponent component) {
   final String rendererType = component.getRendererType();
   if (rendererType == null) {
     return null;
   }
   final RenderKit renderKit = RenderKitUtil.getRenderKit(context);
   return renderKit.getRenderer(component.getFamily(), rendererType);
 }
Пример #3
0
  private void restoreView(FacesContext context) throws FacesException {
    Application app = context.getApplication();

    if (app instanceof ApplicationImpl) ((ApplicationImpl) app).initRequest();

    ViewHandler view = app.getViewHandler();

    view.initView(context);

    UIViewRoot viewRoot = context.getViewRoot();

    if (viewRoot != null) {
      ExternalContext extContext = context.getExternalContext();

      viewRoot.setLocale(extContext.getRequestLocale());

      doSetBindings(context.getELContext(), viewRoot);

      return;
    }

    String viewId = calculateViewId(context);

    String renderKitId = view.calculateRenderKitId(context);

    RenderKitFactory renderKitFactory =
        (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);

    RenderKit renderKit = renderKitFactory.getRenderKit(context, renderKitId);

    ResponseStateManager stateManager = renderKit.getResponseStateManager();

    if (stateManager.isPostback(context)) {
      viewRoot = view.restoreView(context, viewId);

      if (viewRoot != null) {
        doSetBindings(context.getELContext(), viewRoot);
      } else {
        // XXX: backward compat issues with ViewHandler and StateManager

        // throw new ViewExpiredException(L.l("{0} is an expired view", viewId));

        context.renderResponse();

        viewRoot = view.createView(context, viewId);

        context.setViewRoot(viewRoot);
      }

      context.setViewRoot(viewRoot);
    } else {
      context.renderResponse();

      viewRoot = view.createView(context, viewId);

      context.setViewRoot(viewRoot);
    }
  }
Пример #4
0
  /** @see javax.faces.context.FacesContext#isPostback() */
  @Override
  public boolean isPostback() {

    assertNotReleased();
    Boolean postback = (Boolean) this.getAttributes().get(POST_BACK_MARKER);
    if (postback == null) {
      RenderKit rk = this.getRenderKit();
      if (rk != null) {
        postback = rk.getResponseStateManager().isPostback(this);
      } else {
        // ViewRoot hasn't been set yet, so calculate the RK
        ViewHandler vh = this.getApplication().getViewHandler();
        String rkId = vh.calculateRenderKitId(this);
        postback = RenderKitUtils.getResponseStateManager(this, rkId).isPostback(this);
      }
      this.getAttributes().put(POST_BACK_MARKER, postback);
    }

    return postback;
  }
Пример #5
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;
  }
  @Override
  public void encodeChildren(FacesContext facesContext, UIComponent uiComponent)
      throws IOException {

    // Encode each of the children using a writer that can capture the child markup as a string.
    ResponseWriter originalResponseWriter = facesContext.getResponseWriter();
    RenderKit renderKit = facesContext.getRenderKit();
    StringWriter bufferedChildrenMarkupWriter = new StringWriter();
    ResponseWriter stringResponseWriter =
        renderKit.createResponseWriter(bufferedChildrenMarkupWriter, null, StringPool.UTF8);
    facesContext.setResponseWriter(stringResponseWriter);

    List<UIComponent> children = uiComponent.getChildren();

    for (UIComponent child : children) {
      child.encodeAll(facesContext);
    }

    facesContext.setResponseWriter(originalResponseWriter);

    // Get the output of the JSP tag (and all child JSP tags).
    Map<String, Object> componentAttributes = uiComponent.getAttributes();
    Tag tag = (Tag) componentAttributes.get(CORRESPONDING_JSP_TAG);
    PortalTagOutput portalTagOutput;

    try {
      portalTagOutput = getPortalTagOutput(facesContext, uiComponent, tag);

      String preChildMarkup = portalTagOutput.getMarkup();
      String postChildMarkup = null;

      // Determine the point at which children should be inserted into the markup.
      String childInsertionMarker = getChildInsertionMarker();

      if (childInsertionMarker != null) {

        int pos = preChildMarkup.indexOf(childInsertionMarker);

        if (pos > 0) {
          postChildMarkup = preChildMarkup.substring(pos).trim();
          preChildMarkup = preChildMarkup.substring(0, pos).trim();
        }
      }

      // Encode the output of the JSP tag up until the point at which children should be inserted.
      StringBuilder markup = new StringBuilder(3);

      markup.append(preChildMarkup);

      // Ensure that scripts are rendered at the bottom of the page.
      String scripts = portalTagOutput.getScripts();

      if (scripts != null) {
        RendererUtil.renderScript(scripts, null);
      }

      // Encode the children markup.
      String childrenMarkup = bufferedChildrenMarkupWriter.toString();

      if (childrenMarkup != null) {
        markup.append(childrenMarkup);
      }

      // Encode the output of the JSP tag that is to appear after the children.
      if (postChildMarkup != null) {
        markup.append(postChildMarkup);
      }

      ResponseWriter responseWriter = facesContext.getResponseWriter();
      logger.debug("Markup before transformation:{0}", markup);
      StringBuilder processedMarkup = getMarkup(uiComponent, markup);
      logger.debug("Markup after transformation:{0}", processedMarkup);
      responseWriter.write(processedMarkup.toString());
    } catch (Exception e) {
      throw new IOException(e);
    }
  }
Пример #7
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();
  }