Search in sources :

Example 1 with StandardScriptEvalException

use of org.springframework.scripting.support.StandardScriptEvalException in project spring-framework by spring-projects.

the class ScriptTemplateView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        ScriptEngine engine = getEngine();
        Invocable invocable = (Invocable) engine;
        String url = getUrl();
        String template = getTemplate(url);
        Function<String, String> templateLoader = path -> {
            try {
                return getTemplate(path);
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }
        };
        RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
        Object html;
        if (this.renderObject != null) {
            Object thiz = engine.eval(this.renderObject);
            html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
        } else {
            html = invocable.invokeFunction(this.renderFunction, template, model, context);
        }
        response.getWriter().write(String.valueOf(html));
    } catch (ScriptException ex) {
        throw new ServletException("Failed to render script template", new StandardScriptEvalException(ex));
    }
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) HashMap(java.util.HashMap) ApplicationContextException(org.springframework.context.ApplicationContextException) Function(java.util.function.Function) StandardScriptUtils(org.springframework.scripting.support.StandardScriptUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) Charset(java.nio.charset.Charset) Locale(java.util.Locale) Map(java.util.Map) ScriptException(javax.script.ScriptException) Resource(org.springframework.core.io.Resource) ResourceLoader(org.springframework.core.io.ResourceLoader) ObjectUtils(org.springframework.util.ObjectUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) StandardCharsets(java.nio.charset.StandardCharsets) NamedThreadLocal(org.springframework.core.NamedThreadLocal) Invocable(javax.script.Invocable) AbstractUrlBasedView(org.springframework.web.servlet.view.AbstractUrlBasedView) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ScriptEngine(javax.script.ScriptEngine) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) FileCopyUtils(org.springframework.util.FileCopyUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) ServletException(javax.servlet.ServletException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 2 with StandardScriptEvalException

use of org.springframework.scripting.support.StandardScriptEvalException in project spring-framework by spring-projects.

the class ScriptTemplateView method renderInternal.

@Override
protected Mono<Void> renderInternal(Map<String, Object> model, @Nullable MediaType contentType, ServerWebExchange exchange) {
    return exchange.getResponse().writeWith(Mono.fromCallable(() -> {
        try {
            ScriptEngine engine = getEngine();
            String url = getUrl();
            Assert.state(url != null, "'url' not set");
            String template = getTemplate(url);
            Function<String, String> templateLoader = path -> {
                try {
                    return getTemplate(path);
                } catch (IOException ex) {
                    throw new IllegalStateException(ex);
                }
            };
            Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());
            RenderingContext context = new RenderingContext(obtainApplicationContext(), locale, templateLoader, url);
            Object html;
            if (this.renderFunction == null) {
                SimpleBindings bindings = new SimpleBindings();
                bindings.putAll(model);
                model.put("renderingContext", context);
                html = engine.eval(template, bindings);
            } else if (this.renderObject != null) {
                Object thiz = engine.eval(this.renderObject);
                html = ((Invocable) engine).invokeMethod(thiz, this.renderFunction, template, model, context);
            } else {
                html = ((Invocable) engine).invokeFunction(this.renderFunction, template, model, context);
            }
            byte[] bytes = String.valueOf(html).getBytes(StandardCharsets.UTF_8);
            // just wrapping, no allocation
            return exchange.getResponse().bufferFactory().wrap(bytes);
        } catch (ScriptException ex) {
            throw new IllegalStateException("Failed to render script template", new StandardScriptEvalException(ex));
        } catch (Exception ex) {
            throw new IllegalStateException("Failed to render script template", ex);
        }
    }));
}
Also used : Locale(java.util.Locale) Function(java.util.function.Function) ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) ApplicationContextException(org.springframework.context.ApplicationContextException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException)

Aggregations

IOException (java.io.IOException)2 Locale (java.util.Locale)2 Function (java.util.function.Function)2 ScriptEngine (javax.script.ScriptEngine)2 ScriptException (javax.script.ScriptException)2 BeansException (org.springframework.beans.BeansException)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 ApplicationContextException (org.springframework.context.ApplicationContextException)2 StandardScriptEvalException (org.springframework.scripting.support.StandardScriptEvalException)2 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Invocable (javax.script.Invocable)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 SimpleBindings (javax.script.SimpleBindings)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1