From d9cf8245b862a5771eeb53664f47ea664bb9e76c Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 30 Apr 2019 00:19:06 +0800 Subject: [PATCH] Predefine freemarker configuration --- .../app/config/WebMvcAutoConfiguration.java | 20 ++++++++++++++----- .../freemarker/tag/PostTagDirective.java | 1 + .../app/service/impl/AdminServiceImpl.java | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java index b0fce101b..07430b10f 100644 --- a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java @@ -1,6 +1,8 @@ package run.halo.app.config; import com.fasterxml.jackson.databind.ObjectMapper; +import freemarker.template.TemplateException; +import freemarker.template.TemplateExceptionHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.jackson.JsonComponentModule; import org.springframework.context.annotation.Bean; @@ -20,11 +22,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import run.halo.app.config.properties.HaloProperties; +import run.halo.app.controller.support.PageJacksonSerializer; import run.halo.app.factory.StringToEnumConverterFactory; import run.halo.app.model.support.HaloConst; import run.halo.app.security.resolver.AuthenticationArgumentResolver; -import run.halo.app.controller.support.PageJacksonSerializer; +import java.io.IOException; import java.util.List; /** @@ -111,13 +114,20 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { * @return new FreeMarkerConfigurer */ @Bean - public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) { + public FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties) throws IOException, TemplateException { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/"); configurer.setDefaultEncoding("UTF-8"); -// if (haloProperties.isProductionEnv()) { -// configurer.getConfiguration().setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); -// } + + // Predefine configuration + freemarker.template.Configuration configuration = configurer.createConfiguration(); + if (haloProperties.isProductionEnv()) { + configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + } + + // Set predefined freemarker configuration + configurer.setConfiguration(configuration); + return configurer; } diff --git a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java index 6a9feee8c..a84e3ff47 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java @@ -41,6 +41,7 @@ public class PostTagDirective implements TemplateDirectiveModel { final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); if (params.containsKey(HaloConst.METHOD_KEY)) { String method = params.get(HaloConst.METHOD_KEY).toString(); + // TODO NullPointerException Integer categoryId = Integer.parseInt(params.get("categoryId").toString()); Integer tagId = Integer.parseInt(params.get("tagId").toString()); int top = Integer.parseInt(params.get("top").toString()); diff --git a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java index cd0bf3cd6..d005570a8 100644 --- a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java @@ -154,7 +154,7 @@ public class AdminServiceImpl implements AdminService { Assert.hasText(refreshToken, "Refresh token must not be blank"); Integer userId = cacheStore.getAny(SecurityUtils.buildTokenRefreshKey(refreshToken), Integer.class) - .orElseThrow(() -> new BadRequestException("The refresh token may have been expired already")); + .orElseThrow(() -> new BadRequestException("The refresh token may have been expired already").setErrorData(refreshToken)); // Get user info User user = userService.getById(userId);