use of org.apache.commons.configuration2.HierarchicalConfiguration in project engine by craftercms.
the class ConfigAwareUrlAccessRestrictionCheckingProcessor method getUrlRestrictions.
@Override
@SuppressWarnings("unchecked")
protected Map<String, Expression> getUrlRestrictions() {
Callback<Map<String, Expression>> callback = new Callback<Map<String, Expression>>() {
@Override
public Map<String, Expression> execute() {
HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
Map<String, Expression> customRestrictions = null;
if (config != null) {
List<HierarchicalConfiguration> restrictionsConfig = config.configurationsAt(URL_RESTRICTION_KEY);
if (CollectionUtils.isNotEmpty(restrictionsConfig)) {
customRestrictions = new LinkedHashMap<>(restrictionsConfig.size());
ExpressionParser parser = new SpelExpressionParser();
for (HierarchicalConfiguration restrictionConfig : restrictionsConfig) {
String url = restrictionConfig.getString(URL_RESTRICTION_URL_KEY);
String expression = restrictionConfig.getString(URL_RESTRICTION_EXPRESSION_KEY);
if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(expression)) {
try {
customRestrictions.put(url, parser.parseExpression(expression));
} catch (ParseException e) {
throw new ConfigurationException(expression + " is not a valid SpEL expression", e);
}
}
}
}
}
if (customRestrictions != null) {
return customRestrictions;
} else {
return urlRestrictions;
}
}
};
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
return cacheTemplate.getObject(siteContext.getContext(), callback, URL_RESTRICTIONS_CACHE_KEY);
} else {
return urlRestrictions;
}
}
use of org.apache.commons.configuration2.HierarchicalConfiguration in project engine by craftercms.
the class SiteContextFactory method createContext.
public SiteContext createContext(String siteName) {
Map<String, String> macroValues = Collections.singletonMap(siteNameMacroName, siteName);
String resolvedRootFolderPath = macroResolver.resolveMacros(rootFolderPath, macroValues);
Context context = storeService.createContext(storeType, storeServerUrl, username, password, resolvedRootFolderPath, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
try {
SiteContext siteContext = new SiteContext();
siteContext.setStoreService(storeService);
siteContext.setSiteName(siteName);
siteContext.setContext(context);
siteContext.setStaticAssetsPath(staticAssetsPath);
siteContext.setTemplatesPath(templatesPath);
siteContext.setFreeMarkerConfig(freeMarkerConfigFactory.getObject());
siteContext.setUrlTransformationEngine(urlTransformationEngine);
siteContext.setOverlayCallback(overlayCallback);
siteContext.setRestScriptsPath(restScriptsPath);
siteContext.setControllerScriptsPath(controllerScriptsPath);
String[] resolvedConfigPaths = new String[configPaths.length];
for (int i = 0; i < configPaths.length; i++) {
resolvedConfigPaths[i] = macroResolver.resolveMacros(configPaths[i], macroValues);
}
String[] resolvedAppContextPaths = new String[applicationContextPaths.length];
for (int i = 0; i < applicationContextPaths.length; i++) {
resolvedAppContextPaths[i] = macroResolver.resolveMacros(applicationContextPaths[i], macroValues);
}
ResourceLoader resourceLoader = new ContentStoreResourceLoader(siteContext);
HierarchicalConfiguration<?> config = getConfig(siteContext, resolvedConfigPaths, resourceLoader);
URLClassLoader classLoader = getClassLoader(siteContext);
ScriptFactory scriptFactory = getScriptFactory(siteContext, classLoader);
ConfigurableApplicationContext appContext = getApplicationContext(siteContext, classLoader, config, resolvedAppContextPaths, resourceLoader);
siteContext.setConfigPaths(resolvedConfigPaths);
siteContext.setApplicationContextPaths(resolvedAppContextPaths);
siteContext.setGroovyClassesPath(groovyClassesPath);
siteContext.setScriptFactory(scriptFactory);
siteContext.setConfig(config);
siteContext.setGlobalApplicationContext(globalApplicationContext);
siteContext.setApplicationContext(appContext);
siteContext.setClassLoader(classLoader);
executeInitScript(siteContext, scriptFactory);
Scheduler scheduler = scheduleJobs(siteContext);
siteContext.setScheduler(scheduler);
return siteContext;
} catch (Exception e) {
// Destroy context if the site context creation failed
storeService.destroyContext(context);
throw e;
}
}
use of org.apache.commons.configuration2.HierarchicalConfiguration in project engine by craftercms.
the class SiteContextFactory method getApplicationContext.
protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext, URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths, ResourceLoader resourceLoader) {
try {
List<Resource> resources = new ArrayList<>();
for (String path : applicationContextPaths) {
Resource resource = resourceLoader.getResource(path);
if (resource.exists()) {
resources.add(resource);
}
}
if (CollectionUtils.isNotEmpty(resources)) {
String siteName = siteContext.getSiteName();
logger.info("--------------------------------------------------");
logger.info("<Loading application context for site: " + siteName + ">");
logger.info("--------------------------------------------------");
GenericApplicationContext appContext = new GenericApplicationContext(globalApplicationContext);
appContext.setClassLoader(classLoader);
if (config != null) {
MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
propertySources.addFirst(new ApacheCommonsConfiguration2PropertySource("siteConfig", config));
}
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
for (Resource resource : resources) {
reader.loadBeanDefinitions(resource);
}
appContext.refresh();
logger.info("--------------------------------------------------");
logger.info("</Loading application context for site: " + siteName + ">");
logger.info("--------------------------------------------------");
return appContext;
} else {
return null;
}
} catch (Exception e) {
throw new SiteContextCreationException("Unable to load application context for site '" + siteContext.getSiteName() + "'", e);
}
}
use of org.apache.commons.configuration2.HierarchicalConfiguration in project engine by craftercms.
the class ToTargetedUrlTransformerTest method setUpCurrentSiteContext.
private void setUpCurrentSiteContext() {
HierarchicalConfiguration config = mock(HierarchicalConfiguration.class);
when(config.getBoolean(TARGETING_ENABLED_CONFIG_KEY, false)).thenReturn(true);
when(config.getStringArray(ROOT_FOLDERS_CONFIG_KEY)).thenReturn(ROOT_FOLDERS);
when(config.getStringArray(EXCLUDE_PATTERNS_CONFIG_KEY)).thenReturn(new String[] { "/site/website/index\\.xml" });
SiteContext siteContext = new SiteContext();
siteContext.setSiteName(SITE_NAME);
siteContext.setConfig(config);
SiteContext.setCurrent(siteContext);
}
use of org.apache.commons.configuration2.HierarchicalConfiguration in project engine by craftercms.
the class ScriptFilter method getFilterMappings.
@SuppressWarnings("unchecked")
protected List<FilterMapping> getFilterMappings() {
final SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
Callback<List<FilterMapping>> callback = new Callback<List<FilterMapping>>() {
@Override
public List<FilterMapping> execute() {
HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
CachingAwareList<FilterMapping> mappings = new CachingAwareList<>();
if (config != null) {
List<HierarchicalConfiguration> filtersConfig = config.configurationsAt(FILTER_KEY);
if (CollectionUtils.isNotEmpty(filtersConfig)) {
for (HierarchicalConfiguration filterConfig : filtersConfig) {
String scriptUrl = filterConfig.getString(SCRIPT_KEY);
String[] includes = filterConfig.getStringArray(INCLUDE_MAPPINGS_KEY);
String[] excludes = filterConfig.getStringArray(EXCLUDE_MAPPINGS_KEY);
if (StringUtils.isNotEmpty(scriptUrl) && ArrayUtils.isNotEmpty(includes)) {
ContentStoreService storeService = siteContext.getStoreService();
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (!storeService.exists(siteContext.getContext(), scriptUrl)) {
throw new ConfigurationException("No filter script found at " + scriptUrl);
}
FilterMapping mapping = new FilterMapping();
mapping.script = scriptFactory.getScript(scriptUrl);
mapping.includes = includes;
mapping.excludes = excludes;
mappings.add(mapping);
mappings.addDependencyKey(mapping.script.getKey());
}
}
}
}
return mappings;
}
};
return cacheTemplate.getObject(siteContext.getContext(), callback, FILTER_MAPPINGS_CACHE_KEY);
} else {
return null;
}
}
Aggregations