Search in sources :

Example 1 with SiteContext

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;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ServletContext(javax.servlet.ServletContext) ContentStoreService(org.craftercms.core.service.ContentStoreService) JobContext(org.craftercms.engine.util.quartz.JobContext)

Example 2 with SiteContext

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();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobExecutionException(org.quartz.JobExecutionException) HashMap(java.util.HashMap) SiteContext(org.craftercms.engine.service.context.SiteContext) ServletContext(javax.servlet.ServletContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) JobExecutionException(org.quartz.JobExecutionException)

Example 3 with SiteContext

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;
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 4 with SiteContext

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;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with SiteContext

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();
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Lock(java.util.concurrent.locks.Lock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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