Search in sources :

Example 6 with ScriptFactory

use of org.craftercms.engine.scripting.ScriptFactory 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;
    }
}
Also used : JobContext(org.craftercms.engine.util.quartz.JobContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Context(org.craftercms.core.service.Context) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ServletContext(javax.servlet.ServletContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContentStoreGroovyResourceLoader(org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) Scheduler(org.quartz.Scheduler) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) BeansException(org.springframework.beans.BeansException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) URLClassLoader(java.net.URLClassLoader) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 7 with ScriptFactory

use of org.craftercms.engine.scripting.ScriptFactory 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 8 with ScriptFactory

use of org.craftercms.engine.scripting.ScriptFactory 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)

Example 9 with ScriptFactory

use of org.craftercms.engine.scripting.ScriptFactory in project engine by craftercms.

the class RestScriptsController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    SiteContext siteContext = SiteContext.getCurrent();
    ScriptFactory scriptFactory = siteContext.getScriptFactory();
    if (scriptFactory == null) {
        throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
    }
    String serviceUrl = getServiceUrl(request);
    String scriptUrl = getScriptUrl(scriptFactory, siteContext, request, serviceUrl);
    Map<String, Object> scriptVariables = createScriptVariables(request, response);
    scriptUrl = parseScriptUrlForVariables(siteContext, scriptUrl, scriptVariables);
    Object responseBody = executeScript(scriptFactory, scriptVariables, response, scriptUrl);
    if (response.isCommitted()) {
        // If response has been already committed by the script, just return null
        logger.debug("Response already committed by script " + scriptUrl);
        return null;
    }
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject(responseBodyModelAttributeName, responseBody);
    return modelAndView;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) ModelAndView(org.springframework.web.servlet.ModelAndView) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 10 with ScriptFactory

use of org.craftercms.engine.scripting.ScriptFactory 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;
    }
}
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

ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)10 SiteContext (org.craftercms.engine.service.context.SiteContext)8 ServletContext (javax.servlet.ServletContext)4 Context (org.craftercms.core.service.Context)4 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RequestContext (org.craftercms.commons.http.RequestContext)2 ContentStoreService (org.craftercms.core.service.ContentStoreService)2 Script (org.craftercms.engine.scripting.Script)2 GroovyScriptFactory (org.craftercms.engine.scripting.impl.GroovyScriptFactory)2 TemplateModelException (freemarker.template.TemplateModelException)1 URLClassLoader (java.net.URLClassLoader)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)1 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1