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));
}
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);
}
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"));
}
Aggregations