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