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