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;
}
}
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;
}
}
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;
}
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;
}
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;
}
}
Aggregations