Search in sources :

Example 1 with FacesContextFactory

use of jakarta.faces.context.FacesContextFactory in project mojarra by eclipse-ee4j.

the class JUnitFacesTestCaseBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Set up Servlet API Objects
    servletContext = new MockServletContext();
    servletContext.addInitParameter("appParamName", "appParamValue");
    servletContext.setAttribute("appScopeName", "appScopeValue");
    config = new MockServletConfig(servletContext);
    session = new MockHttpSession();
    session.setAttribute("sesScopeName", "sesScopeValue");
    request = new MockHttpServletRequest(session);
    request.setAttribute("reqScopeName", "reqScopeValue");
    response = new MockHttpServletResponse();
    // Set up Faces API Objects
    FactoryFinder.releaseFactories();
    Method reInitializeFactoryManager = FactoryFinder.class.getDeclaredMethod("reInitializeFactoryManager", (Class<?>[]) null);
    reInitializeFactoryManager.setAccessible(true);
    reInitializeFactoryManager.invoke(null, (Object[]) null);
    // Create something to stand-in as the InitFacesContext
    new MockFacesContext(new MockExternalContext(servletContext, request, response), new MockLifecycle());
    FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY, "com.sun.faces.mock.MockFacesContextFactory");
    FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, "com.sun.faces.mock.MockLifecycleFactory");
    FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, "com.sun.faces.mock.MockApplicationFactory");
    FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY, "com.sun.faces.mock.MockRenderKitFactory");
    FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
    LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    lifecycle = (MockLifecycle) lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
    facesContext = (MockFacesContext) fcFactory.getFacesContext(servletContext, request, response, lifecycle);
    externalContext = (MockExternalContext) facesContext.getExternalContext();
    Map map = new HashMap();
    externalContext.setRequestParameterMap(map);
    ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
    application = (MockApplication) applicationFactory.getApplication();
    facesContext.setApplication(application);
}
Also used : MockFacesContext(com.sun.faces.mock.MockFacesContext) MockLifecycle(com.sun.faces.mock.MockLifecycle) MockExternalContext(com.sun.faces.mock.MockExternalContext) HashMap(java.util.HashMap) MockHttpServletRequest(com.sun.faces.mock.MockHttpServletRequest) FacesContextFactory(jakarta.faces.context.FacesContextFactory) LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory) MockServletConfig(com.sun.faces.mock.MockServletConfig) Method(java.lang.reflect.Method) MockServletContext(com.sun.faces.mock.MockServletContext) MockHttpSession(com.sun.faces.mock.MockHttpSession) ApplicationFactory(jakarta.faces.application.ApplicationFactory) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(com.sun.faces.mock.MockHttpServletResponse)

Example 2 with FacesContextFactory

use of jakarta.faces.context.FacesContextFactory in project faces by jakartaee.

the class TestServlet method initFacesContext.

private FacesContext initFacesContext(HttpServletRequest request, HttpServletResponse response) {
    LifecycleFactory lifeFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    Lifecycle lifecycle = lifeFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
    FacesContextFactory facesFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
    return facesFactory.getFacesContext(servletContext, request, response, lifecycle);
}
Also used : LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory) Lifecycle(jakarta.faces.lifecycle.Lifecycle) FacesContextFactory(jakarta.faces.context.FacesContextFactory)

Example 3 with FacesContextFactory

use of jakarta.faces.context.FacesContextFactory in project faces by jakartaee.

the class TestServlet method initFacesContext.

private FacesContext initFacesContext(HttpServletRequest request, HttpServletResponse response) {
    LifecycleFactory lifeFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
    Lifecycle lifecycle = lifeFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
    FacesContextFactory facesFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
    return facesFactory.getFacesContext(servletContext, request, response, lifecycle);
}
Also used : LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory) Lifecycle(jakarta.faces.lifecycle.Lifecycle) FacesContextFactory(jakarta.faces.context.FacesContextFactory)

Example 4 with FacesContextFactory

use of jakarta.faces.context.FacesContextFactory in project faces by jakartaee.

the class BeforeFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    Thread thread = Thread.currentThread();
    ClassLoader originalContextClassLoader = thread.getContextClassLoader();
    ClassLoader ourCustomClassLoader = new URLClassLoader(new URL[0], originalContextClassLoader);
    thread.setContextClassLoader(ourCustomClassLoader);
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    LifecycleFactory lifecycle = (LifecycleFactory) FactoryFinder.getFactory(LIFECYCLE_FACTORY);
    httpServletRequest.setAttribute(REQUEST_HAS_LIFECYCLE, lifecycle != null ? "TRUE" : "FALSE");
    FacesContext facesContext = FacesContext.getCurrentInstance();
    httpServletRequest.setAttribute(REQUEST_HAS_FACESCONTEXT, facesContext != null ? "TRUE" : "FALSE");
    boolean cdiIsTCCLReplacementResilient = true;
    if (cdiIsTCCLReplacementResilient) {
        try {
            chain.doFilter(request, response);
        } catch (Exception t) {
            throw new ServletException(t);
        } finally {
            thread.setContextClassLoader(originalContextClassLoader);
        }
    } else {
        FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FACES_CONTEXT_FACTORY);
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        PrintWriter printWriter = httpServletResponse.getWriter();
        try {
            if (facesContextFactory != null) {
                printWriter.print("<html><body><p id=\"result\">SUCCESS</p></body></html>");
            } else {
                printWriter.print("<html><body><p id=\"result\">FAILURE</p></body></html>");
            }
            httpServletResponse.setStatus(200);
            printWriter.close();
        } catch (Exception e) {
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) FacesContext(jakarta.faces.context.FacesContext) URLClassLoader(java.net.URLClassLoader) LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory) FacesContextFactory(jakarta.faces.context.FacesContextFactory) URLClassLoader(java.net.URLClassLoader) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) PrintWriter(java.io.PrintWriter)

Example 5 with FacesContextFactory

use of jakarta.faces.context.FacesContextFactory in project faces by jakartaee.

the class TestServlet method initFaces.

private void initFaces(ServletContext context, ServletRequest request, ServletResponse response) {
    FacesContextFactory facesContextFactory = (FacesContextFactory) getFactory(FACES_CONTEXT_FACTORY);
    if (facesContextFactory != null) {
        LifecycleFactory lifecycleFactory = (LifecycleFactory) getFactory(LIFECYCLE_FACTORY);
        facesContext = facesContextFactory.getFacesContext(context, request, response, lifecycleFactory.getLifecycle(DEFAULT_LIFECYCLE));
        if (facesContext == null) {
            throw new IllegalStateException("Unable to obtain FacesContext instance");
        }
        // Set up references to the application and facesContext objects
        application = facesContext.getApplication();
        facesContext.setViewRoot(createViewRoot());
    } else {
        throw new IllegalStateException("Unable to obtain FacesContextFactory instance.");
    }
}
Also used : FacesContextFactory(jakarta.faces.context.FacesContextFactory) LifecycleFactory(jakarta.faces.lifecycle.LifecycleFactory)

Aggregations

FacesContextFactory (jakarta.faces.context.FacesContextFactory)17 PrintWriter (java.io.PrintWriter)10 LifecycleFactory (jakarta.faces.lifecycle.LifecycleFactory)8 ServletException (jakarta.servlet.ServletException)7 IOException (java.io.IOException)7 FacesContext (jakarta.faces.context.FacesContext)4 Lifecycle (jakarta.faces.lifecycle.Lifecycle)4 MockHttpServletRequest (com.sun.faces.mock.MockHttpServletRequest)2 MockHttpServletResponse (com.sun.faces.mock.MockHttpServletResponse)2 MockServletContext (com.sun.faces.mock.MockServletContext)2 Application (jakarta.faces.application.Application)2 ApplicationFactory (jakarta.faces.application.ApplicationFactory)2 MockExternalContext (com.sun.faces.mock.MockExternalContext)1 MockFacesContext (com.sun.faces.mock.MockFacesContext)1 MockHttpSession (com.sun.faces.mock.MockHttpSession)1 MockLifecycle (com.sun.faces.mock.MockLifecycle)1 MockServletConfig (com.sun.faces.mock.MockServletConfig)1 TCKDecoratedContextFactory (com.sun.ts.tests.jsf.common.factories.TCKDecoratedContextFactory)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1