@Override public boolean isRenderable(View view) { return FILE_PATTERN.matcher(view.getTemplateName()).find(); }
@Override public boolean isRenderable(View view) { return FILE_PATTERN.matcher(view.getTemplateName()).find(); }
@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); } }
@Override public void render(View view, Locale locale, OutputStream output) throws IOException { final Configuration configuration = configurationCache.get(view.getClass()); if (configuration == null) { throw new ViewRenderException("Couldn't find view class " + view.getClass()); } try { 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); } }
@Override public void writeTo(View t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { final Timer.Context context = metricRegistry.timer(name(t.getClass(), "rendering")).time(); try { for (ViewRenderer renderer : renderers) { if (renderer.isRenderable(t)) { renderer.render(t, detectLocale(requireNonNull(headers)), entityStream); return; } } throw new ViewRenderException("Unable to find a renderer for " + t.getTemplateName()); } catch (ViewRenderException e) { throw new WebApplicationException(e); } finally { context.stop(); } }
/** * {@inheritDoc} * * <p/>Templates ending with ".hbs" are rendered by this. */ public boolean isRenderable(View view) { return view.getTemplateName().endsWith(".hbs"); }
@Override public boolean isRenderable(View view) { return FILE_PATTERN.matcher(view.getTemplateName()).find(); }
@Override public boolean isRenderable(View view) { return FILE_PATTERN.matcher(view.getTemplateName()).find(); }
@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); } }
@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); } }
@Override public void writeTo(View t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { final Timer.Context context = metricRegistry.timer(name(t.getClass(), "rendering")).time(); try { for (ViewRenderer renderer : renderers) { if (renderer.isRenderable(t)) { renderer.render(t, detectLocale(requireNonNull(headers)), entityStream); return; } } throw new ViewRenderException("Unable to find a renderer for " + t.getTemplateName()); } catch (ViewRenderException e) { throw new WebApplicationException(e); } finally { context.stop(); } }
/** {@inheritDoc} */ public void render(View view, Locale locale, OutputStream output) throws IOException, WebApplicationException { try(Writer writer = new OutputStreamWriter(output, view.getCharset().or(Charsets.UTF_8))) { if (view instanceof Layout) { Writer buffer = new StringWriter(); Layout layout = (Layout) view; Template layoutTemplate = loadTemplate(layout.getLayoutTemplateName()); Template viewTemplate = loadTemplate(layout.getTemplateName()); viewTemplate.apply(view, buffer); layout.setBody(buffer.toString()); layoutTemplate.apply(layout, writer); } else { loadTemplate(view.getTemplateName()).apply(view, writer); } } catch (IOException | WebApplicationException e) { // The exception handling in dropwizard view renderer is so bad. So rethrowing this as an illegal state // - system exception. throw new IllegalStateException(e); } }