Search in sources :

Example 1 with ConfigurationException

use of org.craftercms.engine.exception.ConfigurationException in project engine by craftercms.

the class ConfigAwareUrlAccessRestrictionCheckingProcessor method getUrlRestrictions.

@Override
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.configuration.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 ConfigurationException

use of org.craftercms.engine.exception.ConfigurationException in project engine by craftercms.

the class ConfigAwareAccessTokenIdResolver method getAccessTokenId.

@Override
public String getAccessTokenId() {
    String accessTokenId = null;
    HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
    if (config != null) {
        accessTokenId = config.getString(ACCESS_TOKEN_ID_KEY);
    }
    if (StringUtils.isNotEmpty(accessTokenId)) {
        return accessTokenId;
    } else {
        throw new ConfigurationException("Current config for site '" + SiteContext.getCurrent().getSiteName() + "' doesn't contain required property " + ACCESS_TOKEN_ID_KEY);
    }
}
Also used : ConfigurationException(org.craftercms.engine.exception.ConfigurationException) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Example 3 with ConfigurationException

use of org.craftercms.engine.exception.ConfigurationException in project engine by craftercms.

the class ScriptFilter method getFilterMappings.

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;
    }
}
Also used : ContentStoreService(org.craftercms.core.service.ContentStoreService) SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration.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.configuration.HierarchicalConfiguration)3 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)3 Callback (org.craftercms.commons.lang.Callback)2 SiteContext (org.craftercms.engine.service.context.SiteContext)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