Search in sources :

Example 1 with ContractMapping

use of org.apache.myfaces.config.element.ContractMapping in project myfaces by apache.

the class FacesConfigUnmarshallerImplTest method testContracts.

public void testContracts() throws Exception {
    FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream("csrf-and-contracts.xml"), "csrf-and-contracts.xml");
    Assert.assertNotNull(cfg);
    Application app = cfg.getApplications().get(0);
    Assert.assertNotNull(app);
    Assert.assertEquals(1, app.getResourceLibraryContractMappings().size());
    ContractMapping mapping = app.getResourceLibraryContractMappings().get(0);
    Assert.assertEquals("/files/*.xhtml", mapping.getUrlPatternList().get(0));
    Assert.assertEquals("contractA contractB", mapping.getContractList().get(0));
}
Also used : FacesConfig(org.apache.myfaces.config.element.FacesConfig) Application(org.apache.myfaces.config.element.Application) ContractMapping(org.apache.myfaces.config.element.ContractMapping)

Example 2 with ContractMapping

use of org.apache.myfaces.config.element.ContractMapping in project myfaces by apache.

the class FacesConfigurator method configureApplication.

private void configureApplication() {
    Application application = ((ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY)).getApplication();
    FacesConfigData dispenser = getDispenser();
    ActionListener actionListener = ClassUtils.buildApplicationObject(ActionListener.class, dispenser.getActionListenerIterator(), null);
    _callInjectAndPostConstruct(actionListener);
    application.setActionListener(actionListener);
    if (dispenser.getDefaultLocale() != null) {
        application.setDefaultLocale(LocaleUtils.toLocale(dispenser.getDefaultLocale()));
    }
    if (dispenser.getDefaultRenderKitId() != null) {
        application.setDefaultRenderKitId(dispenser.getDefaultRenderKitId());
    }
    if (dispenser.getMessageBundle() != null) {
        application.setMessageBundle(dispenser.getMessageBundle());
    }
    // First build the object
    NavigationHandler navigationHandler = ClassUtils.buildApplicationObject(NavigationHandler.class, ConfigurableNavigationHandler.class, null, dispenser.getNavigationHandlerIterator(), application.getNavigationHandler());
    // Invoke inject and post construct
    _callInjectAndPostConstruct(navigationHandler);
    // Finally wrap the object with the BackwardsCompatibleNavigationHandlerWrapper if it is not assignable
    // from ConfigurableNavigationHandler
    navigationHandler = ClassUtils.wrapBackwardCompatible(NavigationHandler.class, ConfigurableNavigationHandler.class, BackwardsCompatibleNavigationHandlerWrapper.class, application.getNavigationHandler(), navigationHandler);
    application.setNavigationHandler(navigationHandler);
    StateManager stateManager = ClassUtils.buildApplicationObject(StateManager.class, dispenser.getStateManagerIterator(), application.getStateManager());
    _callInjectAndPostConstruct(stateManager);
    application.setStateManager(stateManager);
    ResourceHandler resourceHandler = ClassUtils.buildApplicationObject(ResourceHandler.class, dispenser.getResourceHandlerIterator(), application.getResourceHandler());
    _callInjectAndPostConstruct(resourceHandler);
    application.setResourceHandler(resourceHandler);
    List<Locale> locales = new ArrayList<>();
    for (String locale : dispenser.getSupportedLocalesIterator()) {
        locales.add(LocaleUtils.toLocale(locale));
    }
    application.setSupportedLocales(locales);
    application.setViewHandler(ClassUtils.buildApplicationObject(ViewHandler.class, dispenser.getViewHandlerIterator(), application.getViewHandler()));
    application.setSearchExpressionHandler(ClassUtils.buildApplicationObject(SearchExpressionHandler.class, dispenser.getSearchExpressionHandlerIterator(), application.getSearchExpressionHandler()));
    for (SystemEventListener systemEventListener : dispenser.getSystemEventListeners()) {
        try {
            // note here used to be an instantiation to deal with the explicit source type in the registration,
            // that cannot work because all system events need to have the source being passed in the constructor
            // instead we now  rely on the standard system event types and map them to their appropriate
            // constructor types
            Class eventClass = ClassUtils.classForName((systemEventListener.getSystemEventClass() != null) ? systemEventListener.getSystemEventClass() : SystemEvent.class.getName());
            jakarta.faces.event.SystemEventListener listener = (jakarta.faces.event.SystemEventListener) ClassUtils.newInstance(systemEventListener.getSystemEventListenerClass());
            _callInjectAndPostConstruct(listener);
            if (systemEventListener.getSourceClass() != null && systemEventListener.getSourceClass().length() > 0) {
                application.subscribeToEvent((Class<? extends SystemEvent>) eventClass, ClassUtils.classForName(systemEventListener.getSourceClass()), listener);
            } else {
                application.subscribeToEvent((Class<? extends SystemEvent>) eventClass, listener);
            }
        } catch (ClassNotFoundException e) {
            log.log(Level.SEVERE, "System event listener could not be initialized, reason:", e);
        }
    }
    for (Map.Entry<String, Component> entry : dispenser.getComponentsByType().entrySet()) {
        application.addComponent(entry.getKey(), entry.getValue().getComponentClass());
    }
    for (Map.Entry<String, String> entry : dispenser.getConverterClassesById().entrySet()) {
        application.addConverter(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, String> entry : dispenser.getConverterClassesByClass().entrySet()) {
        try {
            application.addConverter(ClassUtils.classForName(entry.getKey()), entry.getValue());
        } catch (Exception ex) {
            log.log(Level.SEVERE, "Converter could not be added. Reason:", ex);
        }
    }
    for (Map.Entry<String, String> entry : dispenser.getValidatorClassesById().entrySet()) {
        application.addValidator(entry.getKey(), entry.getValue());
    }
    // programmatically add the BeanValidator if the following requirements are met:
    // - bean validation has not been disabled
    // - bean validation is available in the classpath
    String beanValidatorDisabled = _externalContext.getInitParameter(BeanValidator.DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME);
    final boolean defaultBeanValidatorDisabled = (beanValidatorDisabled != null && beanValidatorDisabled.toLowerCase().equals("true"));
    boolean beanValidatorInstalledProgrammatically = false;
    if (!defaultBeanValidatorDisabled && ExternalSpecifications.isBeanValidationAvailable()) {
        // add the BeanValidator as default validator
        application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
        beanValidatorInstalledProgrammatically = true;
    }
    // add the default-validators from the config files
    for (String validatorId : dispenser.getDefaultValidatorIds()) {
        application.addDefaultValidatorId(validatorId);
    }
    // default-validator programmatically, but via a config file.
    if (!beanValidatorInstalledProgrammatically && application.getDefaultValidatorInfo().containsKey(BeanValidator.VALIDATOR_ID)) {
        if (!ExternalSpecifications.isBeanValidationAvailable()) {
            // the BeanValidator was installed via a config file,
            // but bean validation is not available
            log.log(Level.WARNING, "The BeanValidator was installed as a " + "default-validator from a faces-config file, but bean " + "validation is not available on the classpath, " + "thus it will not work!");
        } else if (defaultBeanValidatorDisabled) {
            // the user disabled the default bean validator in web.xml,
            // but a config file added it, which is ok with the spec
            // (section 11.1.3: "though manual installation is still possible")
            // --> inform the user about this scenario
            log.log(Level.INFO, "The BeanValidator was disabled as a " + "default-validator via the config parameter " + BeanValidator.DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME + " in web.xml, but a faces-config file added it, " + "thus it actually was installed as a default-validator.");
        }
    }
    for (Behavior behavior : dispenser.getBehaviors()) {
        application.addBehavior(behavior.getBehaviorId(), behavior.getBehaviorClass());
    }
    // JSF 2.2 set FlowHandler from factory.
    FlowHandlerFactory flowHandlerFactory = (FlowHandlerFactory) FactoryFinder.getFactory(FactoryFinder.FLOW_HANDLER_FACTORY);
    FlowHandler flowHandler = flowHandlerFactory.createFlowHandler(getFacesContext());
    application.setFlowHandler(flowHandler);
    for (ContractMapping mapping : dispenser.getResourceLibraryContractMappings()) {
        List<String> urlMappingsList = mapping.getUrlPatternList();
        for (String urlPattern : urlMappingsList) {
            for (String contract : mapping.getContractList()) {
                String[] contracts = StringUtils.trim(StringUtils.splitShortString(contract, ' '));
                _runtimeConfig.addContractMapping(urlPattern, contracts);
            }
        }
    }
    this.setApplication(application);
}
Also used : Locale(java.util.Locale) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ResourceHandler(jakarta.faces.application.ResourceHandler) ContractMapping(org.apache.myfaces.config.element.ContractMapping) StateManager(jakarta.faces.application.StateManager) SearchExpressionHandler(jakarta.faces.component.search.SearchExpressionHandler) Behavior(org.apache.myfaces.config.element.Behavior) Component(org.apache.myfaces.config.element.Component) FacesConfigData(org.apache.myfaces.config.element.FacesConfigData) ViewHandler(jakarta.faces.application.ViewHandler) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) NavigationHandler(jakarta.faces.application.NavigationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectionProviderException(org.apache.myfaces.spi.InjectionProviderException) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) FlowHandlerFactory(jakarta.faces.flow.FlowHandlerFactory) SystemEventListener(org.apache.myfaces.config.element.SystemEventListener) ActionListener(jakarta.faces.event.ActionListener) ApplicationFactory(jakarta.faces.application.ApplicationFactory) FlowHandler(jakarta.faces.flow.FlowHandler) Application(jakarta.faces.application.Application) BackwardsCompatibleNavigationHandlerWrapper(org.apache.myfaces.application.BackwardsCompatibleNavigationHandlerWrapper) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ContractMapping

use of org.apache.myfaces.config.element.ContractMapping in project myfaces by apache.

the class FacesConfigUnmarshallerImplTest method testContracts2.

public void testContracts2() throws Exception {
    FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream("contracts2.xml"), "contracts2.xml");
    Assert.assertNotNull(cfg);
    Application app = cfg.getApplications().get(0);
    Assert.assertNotNull(app);
    Assert.assertEquals(1, app.getResourceLibraryContractMappings().size());
    ContractMapping mapping = app.getResourceLibraryContractMappings().get(0);
    Assert.assertTrue(mapping.getUrlPatternList().contains("/files/*.xhtml"));
    Assert.assertTrue(mapping.getUrlPatternList().contains("/files2/*.xhtml"));
    Assert.assertTrue(mapping.getContractList().contains("contractA"));
    Assert.assertTrue(mapping.getContractList().contains("contractB"));
}
Also used : FacesConfig(org.apache.myfaces.config.element.FacesConfig) Application(org.apache.myfaces.config.element.Application) ContractMapping(org.apache.myfaces.config.element.ContractMapping)

Aggregations

ContractMapping (org.apache.myfaces.config.element.ContractMapping)3 Application (org.apache.myfaces.config.element.Application)2 FacesConfig (org.apache.myfaces.config.element.FacesConfig)2 FacesException (jakarta.faces.FacesException)1 Application (jakarta.faces.application.Application)1 ApplicationFactory (jakarta.faces.application.ApplicationFactory)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 ResourceHandler (jakarta.faces.application.ResourceHandler)1 StateManager (jakarta.faces.application.StateManager)1 ViewHandler (jakarta.faces.application.ViewHandler)1 SearchExpressionHandler (jakarta.faces.component.search.SearchExpressionHandler)1 ActionListener (jakarta.faces.event.ActionListener)1 FlowHandler (jakarta.faces.flow.FlowHandler)1 FlowHandlerFactory (jakarta.faces.flow.FlowHandlerFactory)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1