Search in sources :

Example 1 with Callback

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;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with Callback

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;
    }
}
Also used : ConnectionFactory(org.springframework.social.connect.ConnectionFactory) Callback(org.craftercms.commons.lang.Callback) SiteContext(org.craftercms.engine.service.context.SiteContext) ConnectionFactoryLocator(org.springframework.social.connect.ConnectionFactoryLocator) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) ConnectionFactoryRegistry(org.springframework.social.connect.support.ConnectionFactoryRegistry)

Example 3 with Callback

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;
    }
}
Also used : ContentStoreService(org.craftercms.core.service.ContentStoreService) SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ArrayList(java.util.ArrayList) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList) List(java.util.List) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList)

Aggregations

HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)3 Callback (org.craftercms.commons.lang.Callback)3 SiteContext (org.craftercms.engine.service.context.SiteContext)3 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ContentStoreService (org.craftercms.core.service.ContentStoreService)1 CachingAwareList (org.craftercms.core.util.cache.impl.CachingAwareList)1 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)1 Expression (org.springframework.expression.Expression)1 ExpressionParser (org.springframework.expression.ExpressionParser)1 ParseException (org.springframework.expression.ParseException)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 ConnectionFactory (org.springframework.social.connect.ConnectionFactory)1 ConnectionFactoryLocator (org.springframework.social.connect.ConnectionFactoryLocator)1 ConnectionFactoryRegistry (org.springframework.social.connect.support.ConnectionFactoryRegistry)1