Search in sources :

Example 1 with Script

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

the class ScriptFilterChainImpl method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if (scriptIterator.hasNext()) {
        Script script = scriptIterator.next();
        if (logger.isDebugEnabled()) {
            logger.debug("Executing filter script at " + script.getUrl());
        }
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        Map<String, Object> variables = new HashMap<>();
        GroovyScriptUtils.addFilterScriptVariables(variables, httpRequest, httpResponse, servletContext, this);
        try {
            script.execute(variables);
        } catch (ScriptException e) {
            Throwable cause = e.getCause();
            if (cause instanceof ServletException) {
                throw (ServletException) cause;
            } else {
                throw new ServletException("Error executing filter script at " + script.getUrl(), cause);
            }
        }
    } else {
        delegateChain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Script(org.craftercms.engine.scripting.Script) ScriptException(org.craftercms.engine.exception.ScriptException) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 2 with Script

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

the class ExecuteControllerDirective method executeController.

protected void executeController(String path, Environment env) throws TemplateException {
    Map<String, Object> scriptVariables = createScriptVariables(env);
    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() + "'");
        }
        Script script;
        try {
            script = scriptFactory.getScript(path);
        } catch (Exception e) {
            throw new TemplateException("Unable to load controller at '" + path + "'", e, env);
        }
        executeController(script, scriptVariables, env);
    } else {
        throw new IllegalStateException("No current site context found");
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) TemplateException(freemarker.template.TemplateException) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 3 with Script

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

the class CrafterPageViewResolver method loadScripts.

protected void loadScripts(ScriptFactory scriptFactory, SiteItem page, CrafterPageView view) {
    if (scriptFactory != null) {
        List<String> scriptUrls = scriptResolver.getScriptUrls(page);
        if (CollectionUtils.isNotEmpty(scriptUrls)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Scripts associated to page " + page.getStoreUrl() + ": " + scriptUrls);
            }
            List<Script> scripts = new ArrayList<Script>(scriptUrls.size());
            for (String scriptUrl : scriptUrls) {
                Script script = scriptFactory.getScript(scriptUrl);
                scripts.add(script);
                view.addDependencyKey(script.getKey());
            }
            view.setScripts(scripts);
        }
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) ArrayList(java.util.ArrayList)

Example 4 with Script

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

the class CrafterPageView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setAttribute(PAGE_URL_ATTRIBUTE_NAME, page.getStoreUrl());
    Map<String, Object> scriptVariables = createScriptVariables(request, response, model);
    if (CollectionUtils.isNotEmpty(scripts)) {
        for (Script script : scripts) {
            executeScript(script, scriptVariables);
            // If the response has been already committed by one of the scripts, stop and don't render the view
            if (response.isCommitted()) {
                logger.debug("Response already committed by script");
                return;
            }
        }
    }
    model.put(KEY_MODEL, page);
    model.put(KEY_CONTENT_MODEL, page);
    renderActualView(getPageViewName(), model, request, response);
}
Also used : Script(org.craftercms.engine.scripting.Script) CachingAwareObject(org.craftercms.core.util.cache.CachingAwareObject)

Example 5 with Script

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

Aggregations

Script (org.craftercms.engine.scripting.Script)7 HashMap (java.util.HashMap)3 SiteContext (org.craftercms.engine.service.context.SiteContext)3 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)2 TemplateModelException (freemarker.template.TemplateModelException)1 ServletException (javax.servlet.ServletException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CachingAwareObject (org.craftercms.core.util.cache.CachingAwareObject)1 ScriptException (org.craftercms.engine.exception.ScriptException)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1