public void setUp() {
   super.setUp();
   UIViewRoot page = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null);
   page.setViewId("viewId");
   page.setLocale(Locale.US);
   getFacesContext().setViewRoot(page);
 }
Example #2
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;
  }
Example #3
0
  /**
   * Build the view.
   *
   * @param ctx the {@link FacesContext} for the current request
   * @param view the {@link UIViewRoot} to populate based of the Facelet template
   * @throws IOException if an error occurs building the view.
   */
  @Override
  public void buildView(FacesContext ctx, UIViewRoot view) throws IOException {

    if (Util.isViewPopulated(ctx, view)) {
      return;
    }
    updateStateSavingType(ctx, view.getViewId());
    view.setViewId(view.getViewId());

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Building View: " + view.getViewId());
    }
    if (faceletFactory == null) {
      ApplicationAssociate associate = ApplicationAssociate.getInstance(ctx.getExternalContext());
      faceletFactory = associate.getFaceletFactory();
      assert (faceletFactory != null);
    }
    RequestStateManager.set(ctx, RequestStateManager.FACELET_FACTORY, faceletFactory);
    Facelet f = faceletFactory.getFacelet(view.getViewId());

    // populate UIViewRoot
    f.apply(ctx, view);
    doPostBuildActions(view);
    Util.setViewPopulated(ctx, view);
  }
Example #4
0
  /** Reload the current active page. */
  public static void reloadPage() {
    FacesContext context = FacesContext.getCurrentInstance();

    String viewId = context.getViewRoot().getViewId();

    ViewHandler handler = context.getApplication().getViewHandler();

    UIViewRoot root = handler.createView(context, viewId);
    root.setViewId(viewId);
    context.setViewRoot(root);
  }
Example #5
0
 //
 // Methods from TestCase
 //
 public void setUp() {
   super.setUp();
   ApplicationFactory aFactory =
       (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
   application = aFactory.getApplication();
   UIViewRoot viewRoot =
       Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null);
   viewRoot.setViewId("viewId");
   viewRoot.setLocale(Locale.US);
   getFacesContext().setViewRoot(viewRoot);
 }
Example #6
0
  /**
   * @see javax.faces.view.ViewDeclarationLanguage#createView(javax.faces.context.FacesContext,
   *     String)
   * @return
   */
  @Override
  public UIViewRoot createView(FacesContext ctx, String viewId) {

    if (UIDebug.debugRequest(ctx)) {
      UIViewRoot root =
          (UIViewRoot) ctx.getApplication().createComponent(UIViewRoot.COMPONENT_TYPE);
      root.setViewId(viewId);
      return root;
    }

    return super.createView(ctx, viewId);
  }
  /**
   * @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);
    }
  }
Example #8
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);
      }
    }
  }
  public void testImplicitObjects() {
    VariableResolver variableResolver = getFacesContext().getApplication().getVariableResolver();
    Object result = null;

    //
    // test scope maps
    //

    // ApplicationMap
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "applicationScope")
            == getFacesContext().getExternalContext().getApplicationMap());

    // SessionMap
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "sessionScope")
            == getFacesContext().getExternalContext().getSessionMap());

    // RequestMap
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "requestScope")
            == getFacesContext().getExternalContext().getRequestMap());

    //
    // test request objects
    //

    // cookie
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "cookie")
            == getFacesContext().getExternalContext().getRequestCookieMap());

    // header
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "header")
            == getFacesContext().getExternalContext().getRequestHeaderMap());

    // headerValues
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "headerValues")
            == getFacesContext().getExternalContext().getRequestHeaderValuesMap());

    // parameter
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "param")
            == getFacesContext().getExternalContext().getRequestParameterMap());

    // parameterValues
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "paramValues")
            == getFacesContext().getExternalContext().getRequestParameterValuesMap());

    //
    // misc
    //

    // initParameter
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "initParam")
            == getFacesContext().getExternalContext().getInitParameterMap());

    // facesContext
    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "facesContext") == getFacesContext());

    // tree
    // create a dummy root for the tree.
    UIViewRoot page = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null);
    page.setId("root");
    page.setViewId("newTree");
    page.setLocale(Locale.US);
    getFacesContext().setViewRoot(page);

    assertTrue(
        variableResolver.resolveVariable(getFacesContext(), "view")
            == getFacesContext().getViewRoot());
  }