示例#1
0
  /*
   * This method provides custom logic of wrapping viewRoot instances.
   * This is done, because different environments casts current viewRoot
   * instance to their own implementation of UIViewRoot class.
   * So, we need to comply with inheritance hierarchy.
   */
  private UIViewRoot getAjaxViewRoot(UIViewRoot root, FacesContext context) {
    UIViewRoot riRoot;
    Class ajax4jsfViewRootClass = null;
    try {
      ajax4jsfViewRootClass = Class.forName("org.ajax4jsf.framework.ajax.AjaxViewRoot");
    } catch (ClassNotFoundException e) {
      // absence of the Ajax4jsf library is a valid case
    }
    boolean isAjax4jsf = (ajax4jsfViewRootClass != null);
    if (isAjax4jsf)
      throw new IllegalArgumentException(
          "OpenFaces warning: The old Ajax4jsf framework is not supported. Use RichFaces that now incorporates this framework instead.");

    Class richFacesAjaxViewRootClass = null;
    try {
      richFacesAjaxViewRootClass = Class.forName("org.ajax4jsf.component.AjaxViewRoot");
    } catch (ClassNotFoundException e) {
      // absence of the RichFaces library is a valid case
    }

    if (Environment.isExoPortal()) riRoot = createExoAjaxViewRoot(root);
    else if (richFacesAjaxViewRootClass == null) riRoot = new AjaxViewRoot();
    else if (Environment.isGateInPortal(context)) riRoot = createGateInAjaxViewRoot(root);
    else riRoot = createA4jAjaxViewRoot(root);
    // fill properties from default.
    riRoot.setViewId(root.getViewId());
    riRoot.setLocale(root.getLocale());
    String renderKitId = root.getRenderKitId();
    // Fix facelets bug - for debug requests renderKitId is null !
    if (null == renderKitId) {
      renderKitId = calculateRenderKitId(context);
    }
    riRoot.setRenderKitId(renderKitId);
    return riRoot;
  }
示例#2
0
  public String getRenderResult3() {
    try {
      FacesContext currentContext = FacesContext.getCurrentInstance();
      ExternalContextImpl externalContext =
          new ExternalContextImpl(
              (ServletContext) currentContext.getExternalContext().getContext(),
              (HttpServletRequest) currentContext.getExternalContext().getRequest(),
              (HttpServletResponse) currentContext.getExternalContext().getResponse());
      LifecycleImpl lifecycle = new LifecycleImpl();
      FacesContextImpl context = new FacesContextImpl(externalContext, lifecycle);

      /*
       * Actual test.
       */
      Application application = context.getApplication();
      UIViewRoot root = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE);

      // if no UIViewRoot then null should be returned
      assertTrue(context.getRenderKit() == null);

      // if UIViewRoot is present but has no RenderKitID, null
      // should be rendered
      context.setViewRoot(root);
      assertTrue(context.getRenderKit() == null);

      // UIViewRoot is present, and has an ID for a non existent
      // RenderKit - null should be returned
      root.setRenderKitId("nosuchkit");
      assertTrue(context.getRenderKit() == null);

      // UIViewRoot with valid RenderKit id should return a RenderKit
      root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
      assertTrue(context.getRenderKit() != null);

    } catch (Exception exception) {
      exception.printStackTrace();
      fail();
    }
    return "PASSED";
  }
  /**
   * @param context
   * @param resource
   * @throws IOException
   */
  public void send(ResourceContext resourceContext, InternetResource resource) throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (null != facesContext) {
      Lifecycle facesLifecycle = getFacesLifecycle();
      PhaseListener[] phaseListeners = facesLifecycle.getPhaseListeners();
      PhaseEvent restoreViewEvent = new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, this);
      processPhaseListeners(phaseListeners, restoreViewEvent, true);
      // Fix for a http://jira.jboss.org/jira/browse/RF-1056
      if (facesContext.getResponseComplete()) {
        return;
      }
      // fix for a http://jira.jboss.com/jira/browse/RF-1064 .
      // viewRoot can be created outside.
      UIViewRoot savedViewRoot = facesContext.getViewRoot();
      try {
        // create "dummy" viewRoot, to avoid problems in phase listeners.
        UIViewRoot root = new UIViewRoot();
        String key = resource.getKey();
        if (null != key && !key.startsWith("/")) {
          key = "/" + key;
        }
        root.setViewId(key);
        root.setLocale(Locale.getDefault());
        root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
        facesContext.setViewRoot(root);
        // We do not simulate other phases.
        facesContext.renderResponse();
        // Invoke after restore view phase listeners
        processPhaseListeners(phaseListeners, restoreViewEvent, false);
        // Fix for a http://jira.jboss.org/jira/browse/RF-1056
        if (!facesContext.getResponseComplete()) {
          // Invoke before render view phase listeners
          PhaseEvent renderViewEvent = new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, this);

          try {
            processPhaseListeners(phaseListeners, renderViewEvent, true);
            sendResource(resourceContext, resource);
          } finally {
            processPhaseListeners(phaseListeners, renderViewEvent, false);
          }
        }

      } finally {
        if (null != savedViewRoot) {
          facesContext.setViewRoot(savedViewRoot);
        }
      }
    } else {
      sendResource(resourceContext, resource);
    }
  }
示例#4
0
  @Override
  public void execute(FacesContext context) throws FacesException {
    /*
     * 1. Find the bean + method that matches the correct @RequestMapping.
     */
    Set<Bean<?>> beans = getBeanManager().getBeans(Object.class, new AnnotationLiteral<Any>() {});
    Iterator<Bean<?>> beanIterator = beans.iterator();
    RequestMappingInfo current = null;

    while (beanIterator.hasNext()) {
      Bean<?> bean = beanIterator.next();
      RequestMappingInfo info = findMethodRequestMapping(context, bean);

      if (current == null) {
        current = info;
      } else if (info != null && info.getLength() > current.getLength()) {
        current = info;
      }
    }

    String viewId = null;

    if (current != null) {
      /*
       * 2. Get an instance of that bean.
       */
      Instance instance =
          CDI.current().select(current.getBean().getBeanClass(), new AnnotationLiteral<Any>() {});

      try {
        /*
         * 3. Call the required method and capture its result.
         *
         * Currently assuming String invoke() signature, but that obviously
         * needs to be expanded.
         */
        viewId = (String) current.getMethod().invoke(instance.get(), new Object[0]);
      } catch (Throwable throwable) {
        throw new FacesException(throwable);
      }
      if (context.getViewRoot() == null) {
        UIViewRoot viewRoot = new UIViewRoot();
        viewRoot.setRenderKitId("HTML_BASIC");
        /*
         * 4. Set the resulting view id on the viewroot.
         */
        viewRoot.setViewId(viewId);
        context.setViewRoot(viewRoot);
      }
    }
  }