Search in sources :

Example 1 with ProjectStage

use of jakarta.faces.application.ProjectStage in project org.openntf.xsp.jakartaee by OpenNTF.

the class NSFJsfServlet method doInit.

public void doInit(HttpServletRequest req, ServletConfig config) throws ServletException {
    try {
        CDI<Object> cdi = ContainerUtil.getContainer(NotesContext.getCurrent().getNotesDatabase());
        ServletContext context = config.getServletContext();
        // $NON-NLS-1$
        context.setAttribute("jakarta.enterprise.inject.spi.BeanManager", ContainerUtil.getBeanManager(cdi));
        // TODO investigate why partial state saving doesn't work with a basic form
        // $NON-NLS-1$ //$NON-NLS-2$
        context.setInitParameter("jakarta.faces.PARTIAL_STATE_SAVING", "false");
        Properties props = LibraryUtil.getXspProperties(module);
        // $NON-NLS-1$
        String projectStage = props.getProperty(ProjectStage.PROJECT_STAGE_PARAM_NAME, "");
        context.setInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, projectStage);
        Bundle b = FrameworkUtil.getBundle(FacesServlet.class);
        {
            ServletContainerInitializer initializer = new MyFacesContainerInitializer();
            Set<Class<?>> classes = null;
            HandlesTypes types = initializer.getClass().getAnnotation(HandlesTypes.class);
            if (types != null) {
                classes = buildMatchingClasses(types, b);
            }
            initializer.onStartup(classes, getServletContext());
        }
        {
            // Re-wrap the ServletContext to provide the context path
            javax.servlet.ServletContext oldCtx = ServletUtil.newToOld(getServletContext());
            ServletContext ctx = ServletUtil.oldToNew(req.getContextPath(), oldCtx, 5, 0);
            ctx.addListener(StartupServletContextListener.class);
            ServletUtil.getListeners(ctx, ServletContextListener.class).forEach(l -> l.contextInitialized(new ServletContextEvent(ctx)));
        }
        this.delegate = new FacesServlet();
        delegate.init(config);
    } catch (NotesAPIException e) {
        throw new ServletException(e);
    }
}
Also used : StartupServletContextListener(org.apache.myfaces.webapp.StartupServletContextListener) ProjectStage(jakarta.faces.application.ProjectStage) ComponentModule(com.ibm.designer.runtime.domino.adapter.ComponentModule) CDI(jakarta.enterprise.inject.spi.CDI) XSPErrorPage(com.ibm.designer.runtime.domino.adapter.util.XSPErrorPage) ServletConfig(jakarta.servlet.ServletConfig) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) URL(java.net.URL) FacesServlet(jakarta.faces.webapp.FacesServlet) DelegatingClassLoader(org.openntf.xsp.jakartaee.DelegatingClassLoader) FacesContext(jakarta.faces.context.FacesContext) ServletException(jakarta.servlet.ServletException) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) HttpSessionListener(jakarta.servlet.http.HttpSessionListener) HashSet(java.util.HashSet) NotesAPIException(com.ibm.designer.domino.napi.NotesAPIException) HttpSession(jakarta.servlet.http.HttpSession) MyFacesContainerInitializer(org.apache.myfaces.webapp.MyFacesContainerInitializer) ServletUtil(org.openntf.xsp.jakartaee.servlet.ServletUtil) ModuleUtil(org.openntf.xsp.jakartaee.util.ModuleUtil) ServletRequestListener(jakarta.servlet.ServletRequestListener) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) LibraryUtil(org.openntf.xsp.jakartaee.util.LibraryUtil) HandlesTypes(jakarta.servlet.annotation.HandlesTypes) PrintWriter(java.io.PrintWriter) Properties(java.util.Properties) AbstractProxyingContext(org.openntf.xsp.cdi.context.AbstractProxyingContext) Set(java.util.Set) IOException(java.io.IOException) NSFComponentModule(com.ibm.domino.xsp.module.nsf.NSFComponentModule) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) HttpServlet(jakarta.servlet.http.HttpServlet) List(java.util.List) HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) StringUtil(com.ibm.commons.util.StringUtil) ServletContextEvent(jakarta.servlet.ServletContextEvent) Annotation(java.lang.annotation.Annotation) NotesContext(com.ibm.domino.xsp.module.nsf.NotesContext) DiscoveryUtil(org.openntf.xsp.cdi.util.DiscoveryUtil) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AccessController(java.security.AccessController) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ContainerUtil(org.openntf.xsp.cdi.util.ContainerUtil) ServletContextListener(jakarta.servlet.ServletContextListener) HashSet(java.util.HashSet) Set(java.util.Set) Bundle(org.osgi.framework.Bundle) FacesServlet(jakarta.faces.webapp.FacesServlet) Properties(java.util.Properties) MyFacesContainerInitializer(org.apache.myfaces.webapp.MyFacesContainerInitializer) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) ServletException(jakarta.servlet.ServletException) ServletContext(jakarta.servlet.ServletContext) NotesAPIException(com.ibm.designer.domino.napi.NotesAPIException) HandlesTypes(jakarta.servlet.annotation.HandlesTypes) ServletContextEvent(jakarta.servlet.ServletContextEvent) StartupServletContextListener(org.apache.myfaces.webapp.StartupServletContextListener)

Example 2 with ProjectStage

use of jakarta.faces.application.ProjectStage in project mojarra by eclipse-ee4j.

the class ResourceImpl method getRequestPath.

/**
 * @see jakarta.faces.application.Resource#getRequestPath()
 */
@Override
public String getRequestPath() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletMapping mapping = getFacesMapping(context);
    String uri = null;
    // Check for exact mapping first
    if (mapping.getMappingMatch() == EXACT) {
        String resource = RESOURCE_IDENTIFIER + '/' + getResourceName();
        // Check if the FacesServlet is exact mapped to the resource
        if (isResourceExactMappedToFacesServlet(context.getExternalContext(), resource)) {
            uri = resource;
        } else {
            // No exact mapping for the requested resource, see if Facelets servlet is mapped to
            // e.g. /faces/* or *.xhtml and take that mapping
            mapping = getFirstWildCardMappingToFacesServlet(context.getExternalContext());
            if (mapping == null) {
                throw new IllegalStateException("No suitable mapping for FacesServlet found. To serve resources " + "FacesServlet should have at least one prefix or suffix mapping.");
            }
        }
    }
    if (uri == null) {
        if (mapping.getMappingMatch() == PATH) {
            // If it is prefix/path mapped, e.g /faces/* -> /faces/jakarta.faces.resource/name
            uri = mapping.getPattern().replace("/*", RESOURCE_IDENTIFIER) + '/' + getResourceName();
        } else {
            // If it is prefix/path mapped, e.g *.xhtml -> /jakarta.faces.resource/name.xhtml
            uri = RESOURCE_IDENTIFIER + '/' + mapping.getPattern().replace("*", getResourceName());
        }
    }
    boolean queryStarted = false;
    if (getLibraryName() != null) {
        queryStarted = true;
        uri += "?ln=" + getLibraryName();
    }
    String version = "";
    initResourceInfo();
    if (resourceInfo.getLibraryInfo() != null && resourceInfo.getLibraryInfo().getVersion() != null) {
        version += resourceInfo.getLibraryInfo().getVersion().toString();
    }
    if (resourceInfo.getVersion() != null) {
        version += resourceInfo.getVersion().toString();
    }
    if (version.length() > 0) {
        uri += (queryStarted ? "&v=" : "?v=") + version;
        queryStarted = true;
    }
    String localePrefix = resourceInfo.getLocalePrefix();
    if (localePrefix != null) {
        uri += (queryStarted ? "&loc=" : "?loc=") + localePrefix;
        queryStarted = true;
    }
    String contract = resourceInfo.getContract();
    if (contract != null) {
        uri += (queryStarted ? "&con=" : "?con=") + contract;
        queryStarted = true;
    }
    if (FACES_SCRIPT_RESOURCE_NAME.equals(getResourceName()) && FACES_SCRIPT_LIBRARY_NAME.equals(getLibraryName())) {
        ProjectStage stage = context.getApplication().getProjectStage();
        switch(stage) {
            case Development:
                uri += queryStarted ? "&stage=Development" : "?stage=Development";
                break;
            case SystemTest:
                uri += queryStarted ? "&stage=SystemTest" : "?stage=SystemTest";
                break;
            case UnitTest:
                uri += queryStarted ? "&stage=UnitTest" : "?stage=UnitTest";
                break;
            default:
                assert stage.equals(Production);
        }
    }
    return context.getApplication().getViewHandler().getResourceURL(context, uri);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ProjectStage(jakarta.faces.application.ProjectStage) HttpServletMapping(jakarta.servlet.http.HttpServletMapping)

Example 3 with ProjectStage

use of jakarta.faces.application.ProjectStage in project myfaces by apache.

the class FacesInitializerImpl method initFaces.

/**
 * Performs all necessary initialization tasks like configuring this JSF
 * application.
 *
 * @param servletContext The current {@link ServletContext}
 */
@Override
public void initFaces(ServletContext servletContext) {
    if (Boolean.TRUE.equals(servletContext.getAttribute(INITIALIZED))) {
        if (log.isLoggable(Level.FINEST)) {
            log.finest("MyFaces already initialized");
        }
        return;
    }
    try {
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Initializing MyFaces");
        }
        long start = System.currentTimeMillis();
        // Some parts of the following configuration tasks have been implemented
        // by using an ExternalContext. However, that's no problem as long as no
        // one tries to call methods depending on either the ServletRequest or
        // the ServletResponse.
        // JSF 2.0: FacesInitializer now has some new methods to
        // use proper startup FacesContext and ExternalContext instances.
        FacesContext facesContext = initStartupFacesContext(servletContext);
        ExternalContext externalContext = facesContext.getExternalContext();
        dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_PREINIT);
        // Setup ServiceProviderFinder
        ServiceProviderFinder spf = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext);
        Map<String, List<String>> spfConfig = spf.calculateKnownServiceProviderMapInfo(externalContext, ServiceProviderFinder.KNOWN_SERVICES);
        if (spfConfig != null) {
            spf.initKnownServiceProviderMapInfo(externalContext, spfConfig);
        }
        if (!WebConfigParamUtils.getBooleanInitParameter(externalContext, MyfacesConfig.INITIALIZE_ALWAYS_STANDALONE, false)) {
            FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration = FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext);
            if (facesServletRegistration == null || facesServletRegistration.getMappings() == null || facesServletRegistration.getMappings().length == 0) {
                // check to see if the FacesServlet was found by MyFacesContainerInitializer
                Boolean mappingAdded = (Boolean) servletContext.getAttribute(MyFacesContainerInitializer.FACES_SERVLET_FOUND);
                if (mappingAdded == null || !mappingAdded) {
                    // check if the FacesServlet has been added dynamically
                    // in a Servlet 3.0 environment by MyFacesContainerInitializer
                    mappingAdded = (Boolean) servletContext.getAttribute(MyFacesContainerInitializer.FACES_SERVLET_ADDED_ATTRIBUTE);
                    if (mappingAdded == null || !mappingAdded) {
                        if (log.isLoggable(Level.WARNING)) {
                            log.warning("No mappings of FacesServlet found. Abort initializing MyFaces.");
                        }
                        return;
                    }
                }
            }
        }
        initCDIIntegration(servletContext, externalContext);
        initContainerIntegration(servletContext, externalContext);
        // log environment integrations
        ExternalSpecifications.isBeanValidationAvailable();
        ExternalSpecifications.isCDIAvailable(externalContext);
        ExternalSpecifications.isEL3Available();
        ExternalSpecifications.isServlet4Available();
        String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION);
        if ("false".equals(useEncryption)) {
            log.warning(StateUtils.USE_ENCRYPTION + " is set to false. " + "This is unsecure and should only be used for local or intranet applications!");
        } else {
            StateUtils.initSecret(servletContext);
        }
        _dispatchApplicationEvent(servletContext, PostConstructApplicationEvent.class);
        initWebsocketIntegration(servletContext, externalContext);
        WebConfigParamsLogger.logWebContextParams(facesContext);
        // Start ViewPoolProcessor if necessary
        ViewPoolProcessor.initialize(facesContext);
        MyfacesConfig config = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
        if (config.isAutomaticExtensionlessMapping()) {
            initAutomaticExtensionlessMapping(facesContext, servletContext);
        }
        // publish resourceBundleControl to applicationMap, to make it available to the API
        ResourceBundle.Control resourceBundleControl = config.getResourceBundleControl();
        if (resourceBundleControl != null) {
            facesContext.getExternalContext().getApplicationMap().put(MyfacesConfig.RESOURCE_BUNDLE_CONTROL, resourceBundleControl);
        }
        // print out a very prominent log message if the project stage is != Production
        if (!facesContext.isProjectStage(ProjectStage.Production) && !facesContext.isProjectStage(ProjectStage.UnitTest)) {
            ProjectStage projectStage = facesContext.getApplication().getProjectStage();
            StringBuilder message = new StringBuilder("\n\n");
            message.append("********************************************************************\n");
            message.append("*** WARNING: Apache MyFaces Core is running in ");
            message.append(projectStage.name().toUpperCase());
            message.append(" mode.");
            int length = projectStage.name().length();
            for (int i = 0; i < 11 - length; i++) {
                message.append(' ');
            }
            message.append(" ***\n");
            message.append("***                                            ");
            for (int i = 0; i < length; i++) {
                message.append('^');
            }
            for (int i = 0; i < 18 - length; i++) {
                message.append(' ');
            }
            message.append("***\n");
            message.append("*** Do NOT deploy to your live server(s) without changing this.  ***\n");
            message.append("*** See Application#getProjectStage() for more information.      ***\n");
            message.append("********************************************************************\n");
            message.append("\n");
            log.log(Level.WARNING, message.toString());
        }
        cleanupAfterStartup(facesContext);
        dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_POSTINIT);
        destroyStartupFacesContext(facesContext);
        servletContext.setAttribute(INITIALIZED, Boolean.TRUE);
        log.log(Level.INFO, "MyFaces Core has started, it took [" + (System.currentTimeMillis() - start) + "] ms.");
    } catch (Exception ex) {
        log.log(Level.SEVERE, "An error occured while initializing MyFaces: " + ex.getMessage(), ex);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FacesServletMappingUtils(org.apache.myfaces.application.FacesServletMappingUtils) ProjectStage(jakarta.faces.application.ProjectStage) FacesException(jakarta.faces.FacesException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectionProviderException(org.apache.myfaces.spi.InjectionProviderException) DeploymentException(jakarta.websocket.DeploymentException) ExternalContext(jakarta.faces.context.ExternalContext) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResourceBundle(java.util.ResourceBundle) MyfacesConfig(org.apache.myfaces.config.MyfacesConfig) ServiceProviderFinder(org.apache.myfaces.spi.ServiceProviderFinder)

Example 4 with ProjectStage

use of jakarta.faces.application.ProjectStage in project mojarra by eclipse-ee4j.

the class AbstractConfigProcessor method getProjectStage.

private ProjectStage getProjectStage(ServletContext sc, FacesContext facesContext) {
    final String projectStageKey = AbstractConfigProcessor.class.getName() + ".PROJECTSTAGE";
    ProjectStage projectStage = (ProjectStage) sc.getAttribute(projectStageKey);
    if (projectStage == null) {
        WebConfiguration webConfig = WebConfiguration.getInstance(facesContext.getExternalContext());
        String value = webConfig.getEnvironmentEntry(WebConfiguration.WebEnvironmentEntry.ProjectStage);
        if (value != null) {
            if (LOGGER.isLoggable(FINE)) {
                LOGGER.log(FINE, "ProjectStage configured via JNDI: {0}", value);
            }
        } else {
            value = webConfig.getOptionValue(JakartaFacesProjectStage);
            if (value != null) {
                if (LOGGER.isLoggable(FINE)) {
                    LOGGER.log(FINE, "ProjectStage configured via servlet context init parameter: {0}", value);
                }
            }
        }
        if (value != null) {
            try {
                projectStage = ProjectStage.valueOf(value);
            } catch (IllegalArgumentException iae) {
                if (LOGGER.isLoggable(INFO)) {
                    LOGGER.log(INFO, "Unable to discern ProjectStage for value {0}.", value);
                }
            }
        }
        if (projectStage == null) {
            projectStage = Production;
        }
        sc.setAttribute(projectStageKey, projectStage);
    }
    return projectStage;
}
Also used : ProjectStage(jakarta.faces.application.ProjectStage) JakartaFacesProjectStage(com.sun.faces.config.WebConfiguration.WebContextInitParameter.JakartaFacesProjectStage) WebConfiguration(com.sun.faces.config.WebConfiguration)

Aggregations

ProjectStage (jakarta.faces.application.ProjectStage)4 FacesContext (jakarta.faces.context.FacesContext)3 List (java.util.List)2 StringUtil (com.ibm.commons.util.StringUtil)1 NotesAPIException (com.ibm.designer.domino.napi.NotesAPIException)1 ComponentModule (com.ibm.designer.runtime.domino.adapter.ComponentModule)1 XSPErrorPage (com.ibm.designer.runtime.domino.adapter.util.XSPErrorPage)1 NSFComponentModule (com.ibm.domino.xsp.module.nsf.NSFComponentModule)1 NotesContext (com.ibm.domino.xsp.module.nsf.NotesContext)1 WebConfiguration (com.sun.faces.config.WebConfiguration)1 JakartaFacesProjectStage (com.sun.faces.config.WebConfiguration.WebContextInitParameter.JakartaFacesProjectStage)1 CDI (jakarta.enterprise.inject.spi.CDI)1 FacesException (jakarta.faces.FacesException)1 ExternalContext (jakarta.faces.context.ExternalContext)1 FacesServlet (jakarta.faces.webapp.FacesServlet)1 ServletConfig (jakarta.servlet.ServletConfig)1 ServletContainerInitializer (jakarta.servlet.ServletContainerInitializer)1 ServletContext (jakarta.servlet.ServletContext)1 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 ServletContextListener (jakarta.servlet.ServletContextListener)1