use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.
the class ConfigurationScriptJobResolver method getJobsUnderFolder.
protected List<JobContext> getJobsUnderFolder(SiteContext siteContext, HierarchicalConfiguration jobFolderConfig) {
List<JobContext> jobContexts = null;
String folderPath = jobFolderConfig.getString(PATH_KEY);
String cronExpression = jobFolderConfig.getString(CRON_EXPRESSION_KEY);
ContentStoreService storeService = siteContext.getStoreService();
Context context = siteContext.getContext();
if (StringUtils.isNotEmpty(folderPath) && StringUtils.isNotEmpty(cronExpression)) {
List<String> scriptPaths = ContentStoreUtils.findChildrenUrl(storeService, context, folderPath);
if (CollectionUtils.isNotEmpty(scriptPaths)) {
for (String scriptPath : scriptPaths) {
if (scriptPath.endsWith(scriptSuffix)) {
if (jobContexts == null) {
jobContexts = new ArrayList<>();
}
jobContexts.add(SchedulingUtils.createJobContext(siteContext, scriptPath, cronExpression, servletContext));
}
}
}
}
return jobContexts;
}
use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.
the class ScriptJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String scriptUrl = dataMap.getString(SCRIPT_URL_DATA_KEY);
SiteContext siteContext = (SiteContext) dataMap.get(SITE_CONTEXT_DATA_KEY);
ServletContext servletContext = (ServletContext) dataMap.get(SERVLET_CONTEXT_DATA_KEY);
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new JobExecutionException("No script factory associate to site context '" + siteContext.getSiteName() + "'");
}
SiteContext.setCurrent(siteContext);
try {
Map<String, Object> variables = new HashMap<>();
GroovyScriptUtils.addJobScriptVariables(variables, servletContext);
scriptFactory.getScript(scriptUrl).execute(variables);
} catch (Exception e) {
throw new JobExecutionException("Error executing script job at " + scriptUrl, e);
} finally {
SiteContext.clear();
}
}
use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.
the class SiteItemScriptResolverImpl method getScriptUrls.
@Override
public List<String> getScriptUrls(SiteItem item) {
List<String> scriptUrls = null;
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
String contentType = item.getItem().queryDescriptorValue(contentTypeXPathQuery);
if (StringUtils.isNotEmpty(contentType)) {
String scriptUrl = getScriptUrlForContentType(contentType);
if (StringUtils.isNotEmpty(scriptUrl)) {
try {
// Check that the script exists. If not, ignore.
if (storeService.exists(siteContext.getContext(), scriptUrl)) {
if (logger.isDebugEnabled()) {
logger.debug("Script for content type '" + contentType + "' found at " + scriptUrl);
}
scriptUrls = new ArrayList<>();
scriptUrls.add(scriptUrl);
}
if (logger.isDebugEnabled()) {
logger.debug("No script for content type '" + contentType + "' found at " + scriptUrl);
}
} catch (CrafterException e) {
logger.error("Error retrieving script for content type '" + contentType + "' at " + scriptUrl, e);
}
}
}
if (CollectionUtils.isNotEmpty(scriptUrls)) {
List<String> additionalUrls = item.getItem().queryDescriptorValues(scriptsXPathQuery);
if (scriptUrls == null) {
scriptUrls = new ArrayList<>();
}
if (CollectionUtils.isNotEmpty(additionalUrls)) {
scriptUrls.addAll(additionalUrls);
}
} else {
scriptUrls = item.getItem().queryDescriptorValues(scriptsXPathQuery);
}
}
return scriptUrls;
}
use of org.craftercms.engine.service.context.SiteContext 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;
}
}
use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.
the class SiteContextRestController method rebuild.
@RequestMapping(value = URL_REBUILD, method = RequestMethod.GET)
@ResponseBody
public Map<String, String> rebuild() {
Lock lock = contextRegistry.getLock();
SiteContext siteContext = SiteContext.getCurrent();
String siteName = siteContext.getSiteName();
boolean fallback = siteContext.isFallback();
lock.lock();
try {
contextRegistry.destroyContext(siteName);
siteContext = contextRegistry.getContext(siteName, fallback);
SiteContext.setCurrent(siteContext);
return Collections.singletonMap(RestControllerBase.MESSAGE_MODEL_ATTRIBUTE_NAME, "Site context for '" + siteName + "' rebuilt");
} finally {
lock.unlock();
}
}
Aggregations