TemplateEngineFreemarker implements TemplateEngine { @Override public void invoke(Context context, Result result) { Object object = result.getRenderable(); Map map; if (object == null) { map = Maps.newHashMap(); } else if (object instanceof Map) { map = (Map) object; } else { String realClassNameLowerCamelCase = CaseFormat.UPPER_CAMEL.to( CaseFormat.LOWER_CAMEL, object.getClass().getSimpleName()); map = Maps.newHashMap(); map.put(realClassNameLowerCamelCase, object); } Optional<String> language = lang.getLanguage(context, Optional.of(result)); if (language.isPresent()) { map.put("lang", language.get()); } if (!context.getSession().isEmpty()) { map.put("session", context.getSession().getData()); } map.put("contextPath", context.getContextPath()); map.put("validation", context.getValidation()); map.put("i18n", new TemplateEngineFreemarkerI18nMethod(messages, context, result)); Optional<String> requestLang = lang.getLanguage(context, Optional.of(result)); Locale locale = lang.getLocaleFromStringOrDefault(requestLang); map.put("prettyTime", new TemplateEngineFreemarkerPrettyTimeMethod(locale)); map.put("reverseRoute", templateEngineFreemarkerReverseRouteMethod); map.put("assetsAt", templateEngineFreemarkerAssetsAtMethod); map.put("webJarsAt", templateEngineFreemarkerWebJarsAtMethod); map.put("authenticityToken", new TemplateEngineFreemarkerAuthenticityTokenDirective(context)); map.put("authenticityForm", new TemplateEngineFreemarkerAuthenticityFormDirective(context)); Map<String, String> translatedFlashCookieMap = Maps.newHashMap(); for (Entry<String, String> entry : context.getFlashScope().getCurrentFlashCookieData().entrySet()) { String messageValue = null; Optional<String> messageValueOptional = messages.get(entry.getValue(), context, Optional.of(result)); if (!messageValueOptional.isPresent()) { messageValue = entry.getValue(); } else { messageValue = messageValueOptional.get(); } translatedFlashCookieMap.put(entry.getKey(), messageValue); } map.put("flash", translatedFlashCookieMap); String templateName = templateEngineHelper.getTemplateForResult( context.getRoute(), result, this.fileSuffix); Template freemarkerTemplate = null; try { freemarkerTemplate = cfg.getTemplate(templateName); StringWriter buffer = new StringWriter(64 * 1024); freemarkerTemplate.process(map, buffer); ResponseStreams responseStreams = context.finalizeHeaders(result); try (Writer writer = responseStreams.getWriter()) { writer.write(buffer.toString()); } } catch (Exception cause) { throwRenderingException(context, result, cause, templateName); } } @Inject  TemplateEngineFreemarker(Messages messages,
                                    Lang lang,
                                    Logger logger,
                                    TemplateEngineHelper templateEngineHelper,
                                    TemplateEngineManager templateEngineManager,
                                    TemplateEngineFreemarkerReverseRouteMethod templateEngineFreemarkerReverseRouteMethod,
                                    TemplateEngineFreemarkerAssetsAtMethod templateEngineFreemarkerAssetsAtMethod,
                                    TemplateEngineFreemarkerWebJarsAtMethod templateEngineFreemarkerWebJarsAtMethod,
                                    NinjaProperties ninjaProperties); @Override void invoke(Context context, Result result); void throwRenderingException(
            Context context,
            Result result,
            Exception cause,
            String knownTemplateSourcePath); @Override String getContentType(); @Override String getSuffixOfTemplatingEngine(); Configuration getConfiguration(); final static String FREEMARKER_CONFIGURATION_FILE_SUFFIX; final static String FREEMARKER_CONFIGURATION_OLDVERSION; }
@Test public void testThatWhenNotProdModeThrowsRenderingException() { when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/broken.ftl.html"); try { templateEngineFreemarker.invoke(context, Results.ok()); fail("exception expected"); } catch (RenderingException e) { } }
@Test(expected = RuntimeException.class) public void testThatProdModeThrowsTemplateException() throws RuntimeException { when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/broken.ftl.html"); templateEngineFreemarker.invoke(context, Results.ok()); }