Search in sources :

Example 1 with ScriptException

use of org.craftercms.engine.exception.ScriptException in project engine by craftercms.

the class GroovyScript method execute.

@Override
public Object execute(Map<String, Object> variables) throws ScriptException {
    Map<String, Object> allVariables = new HashMap<String, Object>();
    if (MapUtils.isNotEmpty(globalVariables)) {
        allVariables.putAll(globalVariables);
    }
    if (MapUtils.isNotEmpty(variables)) {
        allVariables.putAll(variables);
    }
    MDC.put(SCRIPT_URL_MDC_KEY, scriptUrl);
    try {
        return scriptEngine.run(scriptUrl, new Binding(allVariables));
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (e instanceof ResourceException && cause instanceof FileNotFoundException) {
            throw new ScriptNotFoundException(cause.getMessage(), cause);
        } else {
            throw new ScriptException(e.getMessage(), e);
        }
    } finally {
        MDC.remove(SCRIPT_URL_MDC_KEY);
    }
}
Also used : Binding(groovy.lang.Binding) ScriptException(org.craftercms.engine.exception.ScriptException) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) ResourceException(groovy.util.ResourceException) ScriptNotFoundException(org.craftercms.engine.exception.ScriptNotFoundException) ScriptException(org.craftercms.engine.exception.ScriptException) ResourceException(groovy.util.ResourceException) ScriptNotFoundException(org.craftercms.engine.exception.ScriptNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with ScriptException

use of org.craftercms.engine.exception.ScriptException 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)

Aggregations

HashMap (java.util.HashMap)2 ScriptException (org.craftercms.engine.exception.ScriptException)2 Binding (groovy.lang.Binding)1 ResourceException (groovy.util.ResourceException)1 FileNotFoundException (java.io.FileNotFoundException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ScriptNotFoundException (org.craftercms.engine.exception.ScriptNotFoundException)1 Script (org.craftercms.engine.scripting.Script)1