@Override
  public void doDecode(FacesContext context, UIComponent component) {
    final Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String newToggleState = map.get(component.getClientId(context) + NEW_NODE_TOGGLE_STATE);
    if (newToggleState != null) {

      AbstractTreeNode treeNode = (AbstractTreeNode) component;

      boolean initialState = treeNode.isExpanded();
      boolean newState = Boolean.valueOf(newToggleState);
      if (initialState ^ newState) {
        new TreeToggleEvent(treeNode, newState).queue();
      }

      PartialViewContext pvc = context.getPartialViewContext();
      if (pvc.isAjaxRequest()
          && map.get(component.getClientId(context) + TRIGGER_NODE_AJAX_UPDATE) != null) {
        pvc.getRenderIds()
            .add(
                component.getClientId(context)
                    + MetaComponentResolver.META_COMPONENT_SEPARATOR_CHAR
                    + AbstractTreeNode.SUBTREE_META_COMPONENT_ID);

        context.getAttributes().put(AJAX_TOGGLED_NODE_ATTRIBUTE, component.getClientId(context));
        context
            .getAttributes()
            .put(
                AJAX_TOGGLED_NODE_STATE_ATTRIBUTE,
                initialState ? TreeNodeState.expanded : TreeNodeState.collapsed);
      }
    }
  }
示例#2
0
  public void execute(FacesContext facesContext) throws FacesException {

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Entering RenderResponsePhase");
    }
    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("About to render view " + facesContext.getViewRoot().getViewId());
    }
    // For requests intended to produce a partial response, we need prohibit
    // writing any content outside of the view itself (f:view).
    PartialViewContext partialViewContext = facesContext.getPartialViewContext();
    if (partialViewContext.isAjaxRequest()) {
      OnOffResponseWrapper onOffResponse = new OnOffResponseWrapper(facesContext);
      onOffResponse.setEnabled(false);
    }

    try {

      ViewHandler vh = facesContext.getApplication().getViewHandler();

      ViewDeclarationLanguage vdl =
          vh.getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
      if (vdl != null) {
        vdl.buildView(facesContext, facesContext.getViewRoot());
      }

      boolean viewIdsUnchanged;
      do {
        String beforePublishViewId = facesContext.getViewRoot().getViewId();
        // the before render event on the view root is a special case to keep door open for
        // navigation
        // this must be called *after* PDL.buildView() and before VH.renderView()
        facesContext
            .getApplication()
            .publishEvent(facesContext, PreRenderViewEvent.class, facesContext.getViewRoot());
        String afterPublishViewId = facesContext.getViewRoot().getViewId();
        viewIdsUnchanged =
            beforePublishViewId == null && afterPublishViewId == null
                || (beforePublishViewId != null && afterPublishViewId != null)
                    && beforePublishViewId.equals(afterPublishViewId);
      } while (!viewIdsUnchanged);

      // render the view
      vh.renderView(facesContext, facesContext.getViewRoot());

    } catch (IOException e) {
      throw new FacesException(e.getMessage(), e);
    }

    if (LOGGER.isLoggable(Level.FINEST)) {
      LOGGER.log(
          Level.FINEST,
          "+=+=+=+=+=+= View structure printout for " + facesContext.getViewRoot().getViewId());
      DebugUtil.printTree(facesContext.getViewRoot(), LOGGER, Level.FINEST);
    }

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Exiting RenderResponsePhase");
    }
  }
示例#3
0
  public String getThrowExceptionOnAjax() {
    FacesContext context = FacesContext.getCurrentInstance();
    PartialViewContext partialContext = context.getPartialViewContext();
    if (null != partialContext) {
      if (partialContext.isAjaxRequest()) {
        throw new RuntimeException("Intentionally throwing exception on ajax request");
      }
    }

    String result = "not an ajax request";

    return result;
  }
示例#4
0
  /**
   * Cria um relatorio
   *
   * @param dataSource lista de objetos que criarao o data source do relatorio
   * @param parameters map de parametros a serem adicionados no relatorio
   * @param path caminho do relatorio
   * @param fileName Nome do Arquivo gerado
   * @param template Caminho do template (ao informar o template o reltorio sera inserido como
   *     subrelatorio do template)
   * @param usuario usuario que sera passado por parametro ao relatorio
   * @param entityManager
   */
  public static void createJasperReport(
      List dataSource,
      Map parameters,
      String path,
      String fileName,
      String template,
      Usuario usuario,
      EntityManager entityManager) {
    PartialViewContext partialViewContext =
        FacesContext.getCurrentInstance().getPartialViewContext();

    // verificar se eh requisicao ajax, caso seja, informar warning
    if (partialViewContext != null && partialViewContext.isAjaxRequest()) {
      logger.warning(
          "Relatorio sendo gerado a partir de uma requisicao ajax. Uma requisicao Ajax nao gera download de arquivos, para isso coloque ajax='false'.");
    }

    if (parameters == null) {
      parameters = new HashMap();
    }

    String logo = FacesContext.getCurrentInstance().getExternalContext().getRealPath(LOGO);
    if (new File(logo).exists()) {
      parameters.put("logo", logo);
    } else {
      logger.warning("Logo do nao encontrado no caminho " + LOGO);
      parameters.put("logo", null);
    }

    // parametros padrao do relatorio
    parameters.put(
        REPORT_PATH, FacesContext.getCurrentInstance().getExternalContext().getRealPath(path));
    parameters.put("usuario", usuario.getNome());
    parameters.put("ip", FacesUtils.getIP());
    parameters.put(JRParameter.REPORT_LOCALE, I18N.getLocale());
    parameters.put("titulo", TITULO);
    parameters.put("subtitulo", SUBTITULO);

    try {
      // criar do template deve ser inserido o data source nos parametros
      if (template != null && !template.trim().isEmpty()) {
        parameters.put(
            "REPORT_DATA_SOURCE_CLONE", new JRBeanCollectionDataSource(dataSource, entityManager));
        FacesJasper.createJasperReport(null, parameters, template, fileName, entityManager);
      } else {
        FacesJasper.createJasperReport(dataSource, parameters, path, fileName, entityManager);
      }
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
示例#5
0
  /**
   * RichFaces PartialViewContext implementation does not extend from PartialViewContextWrapper. So
   * a hack wherin the exact fully qualified class name needs to be known has to be used to properly
   * extract it from the {@link FacesContext#getPartialViewContext()}.
   *
   * @return The RichFaces PartialViewContext implementation.
   */
  public static PartialViewContext getRichFacesPartialViewContext() {
    PartialViewContext context = Ajax.getContext();

    while (!context.getClass().getName().equals(Hacks.RICHFACES_PVC_CLASS_NAME)
        && context instanceof PartialViewContextWrapper) {
      context = ((PartialViewContextWrapper) context).getWrapped();
    }

    if (context.getClass().getName().equals(Hacks.RICHFACES_PVC_CLASS_NAME)) {
      return context;
    } else {
      return null;
    }
  }
示例#6
0
  private void renderAll(FacesContext context, UIViewRoot viewRoot) throws IOException {
    // If this is a "render all via ajax" request,
    // make sure to wrap the entire page in a <render> elemnt
    // with the special viewStateId of VIEW_ROOT_ID.  This is how the client
    // JavaScript knows how to replace the entire document with
    // this response.
    PartialViewContext pvc = context.getPartialViewContext();
    PartialResponseWriter writer = pvc.getPartialResponseWriter();
    writer.startUpdate(PartialResponseWriter.RENDER_ALL_MARKER);

    if (viewRoot.getChildCount() > 0) {
      for (UIComponent uiComponent : viewRoot.getChildren()) {
        uiComponent.encodeAll(context);
      }
    }

    writer.endUpdate();
  }
示例#7
0
  private void renderState(FacesContext context) throws IOException {

    if (!context.getViewRoot().isTransient()) {
      // Get the view state and write it to the response..
      PartialViewContext pvc = context.getPartialViewContext();
      PartialResponseWriter writer = pvc.getPartialResponseWriter();
      String viewStateId = Util.getViewStateId(context);

      writer.startUpdate(viewStateId);
      String state = context.getApplication().getStateManager().getViewState(context);
      writer.write(state);
      writer.endUpdate();

      ClientWindow window = context.getExternalContext().getClientWindow();
      if (null != window) {
        String windowIdId = Util.getWindowIdId(context);
        writer.startUpdate(windowIdId);
        writer.write(window.getId());
        writer.endUpdate();
      }
    }
  }
  @Override
  protected void doDecode(FacesContext context, UIComponent component) {
    AbstractAutocomplete autocomplete = (AbstractAutocomplete) component;
    if (InputUtils.isDisabled(autocomplete)) {
      return;
    }
    Map<String, String> requestParameters = context.getExternalContext().getRequestParameterMap();
    String value = requestParameters.get(component.getClientId(context) + "Input");
    if (value != null) {
      autocomplete.setSubmittedValue(value);
    }

    if (requestParameters.get(component.getClientId(context) + ".ajax") != null) {
      PartialViewContext pvc = context.getPartialViewContext();
      pvc.getRenderIds()
          .add(
              component.getClientId(context)
                  + MetaComponentResolver.META_COMPONENT_SEPARATOR_CHAR
                  + AbstractAutocomplete.ITEMS_META_COMPONENT_ID);

      context.renderResponse();
    }
  }
示例#9
0
  /** @see javax.faces.context.PartialViewContext#processPartial(javax.faces.event.PhaseId)) */
  @Override
  public void processPartial(PhaseId phaseId) {
    updateFacesContext();
    PartialViewContext pvc = ctx.getPartialViewContext();
    Collection<String> executeIds = pvc.getExecuteIds();
    Collection<String> renderIds = pvc.getRenderIds();
    UIViewRoot viewRoot = ctx.getViewRoot();

    if (phaseId == PhaseId.APPLY_REQUEST_VALUES
        || phaseId == PhaseId.PROCESS_VALIDATIONS
        || phaseId == PhaseId.UPDATE_MODEL_VALUES) {

      // Skip this processing if "none" is specified in the render list,
      // or there were no execute phase client ids.

      if (executeIds == null || executeIds.isEmpty()) {
        if (LOGGER.isLoggable(Level.FINE)) {
          LOGGER.log(
              Level.FINE,
              "No execute and render identifiers specified.  Skipping component processing.");
        }
        return;
      }

      try {
        processComponents(viewRoot, phaseId, executeIds, ctx);
      } catch (Exception e) {
        if (LOGGER.isLoggable(Level.INFO)) {
          LOGGER.log(Level.INFO, e.toString(), e);
        }
      }

      // If we have just finished APPLY_REQUEST_VALUES phase, install the
      // partial response writer.  We want to make sure that any content
      // or errors generated in the other phases are written using the
      // partial response writer.
      //
      if (phaseId == PhaseId.APPLY_REQUEST_VALUES) {
        PartialResponseWriter writer = pvc.getPartialResponseWriter();
        ctx.setResponseWriter(writer);
      }

    } else if (phaseId == PhaseId.RENDER_RESPONSE) {

      try {
        //
        // We re-enable response writing.
        //
        PartialResponseWriter writer = pvc.getPartialResponseWriter();
        ResponseWriter orig = ctx.getResponseWriter();
        ctx.getAttributes().put(ORIGINAL_WRITER, orig);
        ctx.setResponseWriter(writer);

        ExternalContext exContext = ctx.getExternalContext();
        exContext.setResponseContentType("text/xml");
        exContext.addResponseHeader("Cache-Control", "no-cache");
        writer.startDocument();
        if (isRenderAll()) {
          renderAll(ctx, viewRoot);
          renderState(ctx);
          writer.endDocument();
          return;
        }

        // Skip this processing if "none" is specified in the render list,
        // or there were no render phase client ids.
        if (renderIds == null || renderIds.isEmpty()) {
        } else {
          processComponents(viewRoot, phaseId, renderIds, ctx);
        }

        renderState(ctx);

        writer.endDocument();
      } catch (IOException ex) {
        this.cleanupAfterView();
      } catch (RuntimeException ex) {
        this.cleanupAfterView();
        // Throw the exception
        throw ex;
      }
    }
  }
示例#10
0
 public static boolean isAjaxRequest() {
   PartialViewContext context = FacesUtils.context().getPartialViewContext();
   return context.isAjaxRequest();
 }