Search in sources :

Example 26 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class RenderComponentDirective method executeScripts.

protected Map<String, Object> executeScripts(SiteItem component, Map<String, Object> additionalModel, Environment env) throws TemplateException {
    List<String> scriptUrls = scriptResolver.getScriptUrls(component);
    if (CollectionUtils.isNotEmpty(scriptUrls)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Scripts associated to component " + component.getStoreUrl() + ": " + scriptUrls);
        }
        Map<String, Object> templateModel = new HashMap<>();
        Map<String, Object> scriptVariables = createScriptVariables(component, templateModel, additionalModel);
        SiteContext siteContext = SiteContext.getCurrent();
        if (siteContext != null) {
            ScriptFactory scriptFactory = siteContext.getScriptFactory();
            if (scriptFactory == null) {
                throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
            }
            for (String scriptUrl : scriptUrls) {
                Script script;
                try {
                    script = scriptFactory.getScript(scriptUrl);
                } catch (Exception e) {
                    throw new TemplateException("Unable to load script at '" + scriptUrl + "'", e, env);
                }
                executeScript(script, scriptVariables, env);
            }
        } else {
            throw new IllegalStateException("No current site context found");
        }
        return templateModel;
    } else {
        return null;
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 27 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class SiteCacheRestController method clear.

@RequestMapping(value = URL_CLEAR, method = RequestMethod.GET)
@ResponseBody
public Map<String, String> clear() {
    SiteContext siteContext = SiteContext.getCurrent();
    String siteName = siteContext.getSiteName();
    // Clear content cache
    cacheService.clearScope(siteContext.getContext());
    // Clear Freemarker cache
    siteContext.getFreeMarkerConfig().getConfiguration().clearTemplateCache();
    String msg = "Content cache and Freemarker cache have been cleared for site '" + siteName + "'";
    logger.info(msg);
    return Collections.singletonMap(RestControllerBase.MESSAGE_MODEL_ATTRIBUTE_NAME, msg);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 28 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class CrafterFreeMarkerTemplateLoader method findTemplateSource.

@Override
public Object findTemplateSource(String name) throws IOException {
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        String path = getTemplatePath(siteContext, name);
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for FreeMarker template at [context=" + siteContext + ", path='" + path + "']");
        }
        Content content = contentStoreService.findContent(siteContext.getContext(), path);
        if (content == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to find FreeMarker template at [context=" + siteContext + ", path='" + path + "']");
            }
        }
        return content;
    } else {
        return null;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Content(org.craftercms.core.service.Content)

Example 29 with SiteContext

use of org.craftercms.engine.service.context.SiteContext 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)

Example 30 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class ScriptUrlTemplateScannerImpl method scan.

@Override
public List<UriTemplate> scan(SiteContext siteContext) {
    Context context = siteContext.getContext();
    ContentStoreService storeService = siteContext.getStoreService();
    ScriptFactory scriptFactory = siteContext.getScriptFactory();
    List<String> scriptUrls = new ArrayList<>();
    List<UriTemplate> urlTemplates = new ArrayList<>();
    findScripts(context, storeService, scriptFactory, scriptsFolder, scriptUrls);
    if (CollectionUtils.isNotEmpty(scriptUrls)) {
        for (String scriptUrl : scriptUrls) {
            Matcher matcher = urlVariablePlaceholderPattern.matcher(scriptUrl);
            if (matcher.find()) {
                urlTemplates.add(new UriTemplate(scriptUrl));
            }
        }
    }
    return urlTemplates;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ContentStoreService(org.craftercms.core.service.ContentStoreService) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) UriTemplate(org.springframework.web.util.UriTemplate) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Aggregations

SiteContext (org.craftercms.engine.service.context.SiteContext)35 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)8 Context (org.craftercms.core.service.Context)7 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)6 HashMap (java.util.HashMap)4 ServletContext (javax.servlet.ServletContext)4 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)4 RequestContext (org.craftercms.commons.http.RequestContext)4 Callback (org.craftercms.commons.lang.Callback)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Script (org.craftercms.engine.scripting.Script)3 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Configuration (org.apache.commons.configuration.Configuration)2 CrafterException (org.craftercms.core.exception.CrafterException)2 Content (org.craftercms.core.service.Content)2 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2