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