Search in sources :

Example 1 with ViewRenderException

use of io.dropwizard.views.ViewRenderException in project dropwizard by dropwizard.

the class FreemarkerViewRenderer method render.

@Override
public void render(View view, Locale locale, OutputStream output) throws IOException {
    try {
        final Configuration configuration = configurationCache.getUnchecked(view.getClass());
        final Charset charset = view.getCharset().orElseGet(() -> Charset.forName(configuration.getEncoding(locale)));
        final Template template = configuration.getTemplate(view.getTemplateName(), locale, charset.name());
        template.process(view, new OutputStreamWriter(output, template.getEncoding()));
    } catch (Exception e) {
        throw new ViewRenderException(e);
    }
}
Also used : Configuration(freemarker.template.Configuration) ViewRenderException(io.dropwizard.views.ViewRenderException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) ViewRenderException(io.dropwizard.views.ViewRenderException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 2 with ViewRenderException

use of io.dropwizard.views.ViewRenderException in project dropwizard by dropwizard.

the class MustacheViewRenderer method render.

@Override
public void render(View view, Locale locale, OutputStream output) throws IOException {
    try {
        final MustacheFactory mustacheFactory = useCache ? factories.get(view.getClass()) : createNewMustacheFactory(view.getClass());
        final Mustache template = mustacheFactory.compile(view.getTemplateName());
        final Charset charset = view.getCharset().orElse(StandardCharsets.UTF_8);
        try (OutputStreamWriter writer = new OutputStreamWriter(output, charset)) {
            template.execute(writer, view);
        }
    } catch (Throwable e) {
        throw new ViewRenderException("Mustache template error: " + view.getTemplateName(), e);
    }
}
Also used : ViewRenderException(io.dropwizard.views.ViewRenderException) Mustache(com.github.mustachejava.Mustache) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Aggregations

ViewRenderException (io.dropwizard.views.ViewRenderException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Charset (java.nio.charset.Charset)2 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)1 Mustache (com.github.mustachejava.Mustache)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 IOException (java.io.IOException)1