use of org.craftercms.commons.lang.Callback 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.craftercms.commons.lang.Callback in project engine by craftercms.
the class ConfigAwareConnectionFactoryLocator method getCurrentConnectionFactoryLocator.
protected ConnectionFactoryLocator getCurrentConnectionFactoryLocator() {
Callback<ConnectionFactoryLocator> callback = new Callback<ConnectionFactoryLocator>() {
@Override
public ConnectionFactoryLocator execute() {
HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
ConnectionFactoryRegistry registry = null;
if (config != null) {
try {
HierarchicalConfiguration socialConnectionsConfig = config.configurationAt(SOCIAL_CONNECTIONS_KEY);
for (ConfigurationParser<?> parser : configParsers) {
ConnectionFactory<?> factory = (ConnectionFactory<?>) parser.parse(socialConnectionsConfig);
if (factory != null) {
if (registry == null) {
registry = new ConnectionFactoryRegistry();
}
registry.addConnectionFactory(factory);
}
}
} catch (IllegalArgumentException e) {
// Ignore if != 1
}
}
if (registry != null) {
return registry;
} else {
return defaultLocator;
}
}
};
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
return cacheTemplate.getObject(siteContext.getContext(), callback, CONNECTION_FACTORY_LOCATOR_CACHE_KEY);
} else {
return defaultLocator;
}
}
use of org.craftercms.commons.lang.Callback 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);
}
}
}
}
return mappings;
}
};
return cacheTemplate.getObject(siteContext.getContext(), callback, FILTER_MAPPINGS_CACHE_KEY);
} else {
return null;
}
}
Aggregations