diff --git a/pebble-spring/pebble-spring-boot-starter/pom.xml b/pebble-spring/pebble-spring-boot-starter/pom.xml index 320d8b206..9c90a6551 100644 --- a/pebble-spring/pebble-spring-boot-starter/pom.xml +++ b/pebble-spring/pebble-spring-boot-starter/pom.xml @@ -14,7 +14,7 @@ http://pebbletemplates.io - 2.2.1.RELEASE + 2.2.6.RELEASE diff --git a/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/AbstractPebbleConfiguration.java b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/AbstractPebbleConfiguration.java new file mode 100644 index 000000000..b8b9b5406 --- /dev/null +++ b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/AbstractPebbleConfiguration.java @@ -0,0 +1,14 @@ +package com.mitchellbosecke.pebble.boot.autoconfigure; + +abstract class AbstractPebbleConfiguration { + + protected String stripLeadingSlash(String value) { + if (value == null) { + return null; + } + if (value.startsWith("/")) { + return value.substring(1); + } + return value; + } +} diff --git a/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfiguration.java b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfiguration.java index fa4bc3709..3e874f7c4 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfiguration.java +++ b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfiguration.java @@ -5,146 +5,58 @@ import com.mitchellbosecke.pebble.loader.ClasspathLoader; import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.spring.extension.SpringExtension; -import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver; -import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import java.util.List; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; -import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; -import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.lang.Nullable; -import java.util.List; - -@Configuration +@Configuration(proxyBeanMethods = false) @ConditionalOnClass(PebbleEngine.class) -@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class}) @EnableConfigurationProperties(PebbleProperties.class) -public class PebbleAutoConfiguration { +@Import({PebbleServletWebConfiguration.class, PebbleReactiveWebConfiguration.class}) +public class PebbleAutoConfiguration extends AbstractPebbleConfiguration { - @Configuration + @Bean @ConditionalOnMissingBean(name = "pebbleLoader") - public static class DefaultLoaderConfiguration { - - @Autowired - private PebbleProperties properties; - - @Bean - public Loader pebbleLoader() { - ClasspathLoader loader = new ClasspathLoader(); - loader.setCharset(this.properties.getCharsetName()); - // classpath loader does not like leading slashes in resource paths - loader.setPrefix(stripLeadingSlash(this.properties.getPrefix())); - loader.setSuffix(this.properties.getSuffix()); - return loader; - } - } - - @Configuration - @ConditionalOnMissingBean(name = "pebbleEngine") - public static class PebbleDefaultConfiguration { - - @Autowired - private PebbleProperties properties; - - @Autowired - private Loader pebbleLoader; - - @Autowired(required = false) - private List extensions; - - @Bean - public SpringExtension pebbleSpringExtension() { - return new SpringExtension(); - } - - @Bean - public PebbleEngine pebbleEngine() { - PebbleEngine.Builder builder = new PebbleEngine.Builder(); - builder.loader(this.pebbleLoader); - builder.extension(this.pebbleSpringExtension()); - if (this.extensions != null && !this.extensions.isEmpty()) { - builder.extension(this.extensions.toArray(new Extension[this.extensions.size()])); - } - if (!this.properties.isCache()) { - builder.cacheActive(false); - } - if (this.properties.getDefaultLocale() != null) { - builder.defaultLocale(this.properties.getDefaultLocale()); - } - builder.strictVariables(this.properties.isStrictVariables()); - builder.greedyMatchMethod(this.properties.isGreedyMatchMethod()); - return builder.build(); - } + public Loader pebbleLoader(PebbleProperties properties) { + ClasspathLoader loader = new ClasspathLoader(); + loader.setCharset(properties.getCharsetName()); + // classpath loader does not like leading slashes in resource paths + loader.setPrefix(this.stripLeadingSlash(properties.getPrefix())); + loader.setSuffix(properties.getSuffix()); + return loader; } - @Configuration - @ConditionalOnWebApplication(type = Type.SERVLET) - public static class PebbleWebMvcConfiguration { - - @Autowired - private PebbleProperties properties; - - @Autowired - private PebbleEngine pebbleEngine; - - @Bean - @ConditionalOnMissingBean(name = "pebbleViewResolver") - public PebbleViewResolver pebbleViewResolver() { - PebbleViewResolver pvr = new PebbleViewResolver(); - this.properties.applyToMvcViewResolver(pvr); - - pvr.setPebbleEngine(this.pebbleEngine); - if (this.pebbleEngine.getLoader() instanceof ClasspathLoader) { - // classpathloader doesn't like leading slashes in paths - pvr.setPrefix(stripLeadingSlash(this.properties.getPrefix())); - } - - return pvr; - } + @Bean + @ConditionalOnMissingBean + public SpringExtension pebbleSpringExtension() { + return new SpringExtension(); } - @Configuration - @ConditionalOnWebApplication(type = Type.REACTIVE) - public static class PebbleReactiveConfiguration { - - @Autowired - private PebbleProperties properties; - - @Autowired - private PebbleEngine pebbleEngine; - - @Bean - @ConditionalOnMissingBean(name = "pebbleReactiveViewResolver") - public PebbleReactiveViewResolver pebbleReactiveViewResolver() { - String prefix = this.properties.getPrefix(); - if (this.pebbleEngine.getLoader() instanceof ClasspathLoader) { - // classpathloader doesn't like leading slashes in paths - prefix = stripLeadingSlash(this.properties.getPrefix()); - } - PebbleReactiveViewResolver resolver = new PebbleReactiveViewResolver(this.pebbleEngine); - resolver.setPrefix(prefix); - resolver.setSuffix(this.properties.getSuffix()); - resolver.setViewNames(this.properties.getViewNames()); - resolver.setRequestContextAttribute(this.properties.getRequestContextAttribute()); - return resolver; + @Bean + @ConditionalOnMissingBean(name = "pebbleEngine") + public PebbleEngine pebbleEngine(PebbleProperties properties, + Loader pebbleLoader, + SpringExtension springExtension, + @Nullable List extensions) { + PebbleEngine.Builder builder = new PebbleEngine.Builder(); + builder.loader(pebbleLoader); + builder.extension(springExtension); + if (extensions != null && !extensions.isEmpty()) { + builder.extension(extensions.toArray(new Extension[extensions.size()])); } - } - - private static String stripLeadingSlash(String value) { - if (value == null) { - return null; + if (!properties.isCache()) { + builder.cacheActive(false); } - if (value.startsWith("/")) { - return value.substring(1); + if (properties.getDefaultLocale() != null) { + builder.defaultLocale(properties.getDefaultLocale()); } - return value; + builder.strictVariables(properties.isStrictVariables()); + builder.greedyMatchMethod(properties.isGreedyMatchMethod()); + return builder.build(); } - } diff --git a/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleReactiveWebConfiguration.java b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleReactiveWebConfiguration.java new file mode 100644 index 000000000..5979d6f72 --- /dev/null +++ b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleReactiveWebConfiguration.java @@ -0,0 +1,32 @@ +package com.mitchellbosecke.pebble.boot.autoconfigure; + +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.loader.ClasspathLoader; +import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@ConditionalOnWebApplication(type = Type.REACTIVE) +class PebbleReactiveWebConfiguration extends AbstractPebbleConfiguration { + + @Bean + @ConditionalOnMissingBean(name = "pebbleReactiveViewResolver") + PebbleReactiveViewResolver pebbleReactiveViewResolver(PebbleProperties properties, + PebbleEngine pebbleEngine) { + String prefix = properties.getPrefix(); + if (pebbleEngine.getLoader() instanceof ClasspathLoader) { + // classpathloader doesn't like leading slashes in paths + prefix = this.stripLeadingSlash(properties.getPrefix()); + } + PebbleReactiveViewResolver resolver = new PebbleReactiveViewResolver(pebbleEngine); + resolver.setPrefix(prefix); + resolver.setSuffix(properties.getSuffix()); + resolver.setViewNames(properties.getViewNames()); + resolver.setRequestContextAttribute(properties.getRequestContextAttribute()); + return resolver; + } +} diff --git a/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleServletWebConfiguration.java b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleServletWebConfiguration.java new file mode 100644 index 000000000..9cf619577 --- /dev/null +++ b/pebble-spring/pebble-spring-boot-starter/src/main/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleServletWebConfiguration.java @@ -0,0 +1,31 @@ +package com.mitchellbosecke.pebble.boot.autoconfigure; + +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.loader.ClasspathLoader; +import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@ConditionalOnWebApplication(type = Type.SERVLET) +class PebbleServletWebConfiguration extends AbstractPebbleConfiguration { + + @Bean + @ConditionalOnMissingBean(name = "pebbleViewResolver") + PebbleViewResolver pebbleViewResolver(PebbleProperties properties, + PebbleEngine pebbleEngine) { + PebbleViewResolver pvr = new PebbleViewResolver(); + properties.applyToMvcViewResolver(pvr); + + pvr.setPebbleEngine(pebbleEngine); + if (pebbleEngine.getLoader() instanceof ClasspathLoader) { + // classpathloader doesn't like leading slashes in paths + pvr.setPrefix(this.stripLeadingSlash(properties.getPrefix())); + } + + return pvr; + } +} diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/NonWebApplication.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/NonWebApplication.java deleted file mode 100644 index 82268aea4..000000000 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/NonWebApplication.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.mitchellbosecke.pebble.boot; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.WebApplicationType; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; - -@SpringBootApplication -public class NonWebApplication { - - public static void main(String[] args) { - SpringApplication sa = new SpringApplicationBuilder(NonWebApplication.class) - .web(WebApplicationType.NONE) - .build(); - sa.run(args); - } -} diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/ReactiveApplication.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/ReactiveApplication.java deleted file mode 100644 index b505904cc..000000000 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/ReactiveApplication.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mitchellbosecke.pebble.boot; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -import static org.springframework.boot.WebApplicationType.REACTIVE; - -@SpringBootApplication -public class ReactiveApplication { - - public static void main(String[] args) { - SpringApplication application = new SpringApplication(Application.class); - application.setWebApplicationType(REACTIVE); - application.run(args); - } -} \ No newline at end of file diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/NonWebAppTests.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/NonWebAppTests.java index e71a715c0..007e81269 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/NonWebAppTests.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/NonWebAppTests.java @@ -1,17 +1,16 @@ package com.mitchellbosecke.pebble.boot.autoconfigure; -import com.mitchellbosecke.pebble.PebbleEngine; -import com.mitchellbosecke.pebble.boot.NonWebApplication; +import static org.assertj.core.api.Assertions.assertThat; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.boot.Application; +import java.io.StringWriter; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.io.StringWriter; - -import static org.assertj.core.api.Assertions.assertThat; - -@SpringBootTest(classes = NonWebApplication.class) +@SpringBootTest(classes = Application.class, + properties = "spring.main.web-application-type=none") class NonWebAppTests { @Autowired diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfigurationTest.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfigurationTest.java index b14aacd8a..2cc79ca7c 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfigurationTest.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/PebbleAutoConfigurationTest.java @@ -1,11 +1,14 @@ package com.mitchellbosecke.pebble.boot.autoconfigure; +import static java.util.Locale.CHINESE; +import static org.assertj.core.api.Assertions.assertThat; + import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.spring.extension.SpringExtension; import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver; import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver; - +import java.util.Locale; import org.junit.jupiter.api.Test; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; @@ -14,11 +17,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import java.util.Locale; - -import static java.util.Locale.CHINESE; -import static org.assertj.core.api.Assertions.assertThat; - class PebbleAutoConfigurationTest { private static final Locale DEFAULT_LOCALE = CHINESE; @@ -37,9 +35,9 @@ void registerBeansForServletApp() { @Test void registerCompilerForServletApp() { - this.loadWithServlet(CustomCompilerConfiguration.class); + this.loadWithServlet(CustomPebbleEngineCompilerConfiguration.class); assertThat(this.webContext.getBeansOfType(Loader.class)).hasSize(1); - assertThat(this.webContext.getBeansOfType(SpringExtension.class)).isEmpty(); + assertThat(this.webContext.getBeansOfType(SpringExtension.class)).hasSize(1); assertThat(this.webContext.getBeansOfType(PebbleEngine.class)).hasSize(1); assertThat(this.webContext.getBean(PebbleEngine.class).getDefaultLocale()).isEqualTo(DEFAULT_LOCALE); assertThat(this.webContext.getBeansOfType(PebbleViewResolver.class)).hasSize(1); @@ -59,9 +57,9 @@ void registerBeansForReactiveApp() { @Test void registerCompilerForReactiveApp() { - this.loadWithReactive(CustomCompilerConfiguration.class); + this.loadWithReactive(CustomPebbleEngineCompilerConfiguration.class); assertThat(this.reactiveWebContext.getBeansOfType(Loader.class)).hasSize(1); - assertThat(this.reactiveWebContext.getBeansOfType(SpringExtension.class)).isEmpty(); + assertThat(this.reactiveWebContext.getBeansOfType(SpringExtension.class)).hasSize(1); assertThat(this.reactiveWebContext.getBeansOfType(PebbleEngine.class)).hasSize(1); assertThat(this.reactiveWebContext.getBean(PebbleEngine.class).getDefaultLocale()).isEqualTo(DEFAULT_LOCALE); assertThat(this.reactiveWebContext.getBeansOfType(PebbleReactiveViewResolver.class)).hasSize(1); @@ -95,13 +93,17 @@ protected static class BaseConfiguration { } @Configuration(proxyBeanMethods = false) - protected static class CustomCompilerConfiguration { + protected static class CustomPebbleEngineCompilerConfiguration { @Bean public PebbleEngine pebbleEngine() { return new PebbleEngine.Builder().defaultLocale(DEFAULT_LOCALE).build(); } + @Bean + public SpringExtension customSpringExtension() { + return new SpringExtension(); + } } } \ No newline at end of file diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java index 1cc068067..210b4624d 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java @@ -1,16 +1,15 @@ package com.mitchellbosecke.pebble.boot.autoconfigure; -import com.mitchellbosecke.pebble.boot.ReactiveApplication; +import static org.assertj.core.api.Assertions.assertThat; +import com.mitchellbosecke.pebble.boot.Application; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; -import static org.assertj.core.api.Assertions.assertThat; - -@SpringBootTest(classes = ReactiveApplication.class, +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive") class ReactiveAppTest { diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java index adde9964b..431142b23 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java @@ -1,7 +1,13 @@ package com.mitchellbosecke.pebble.boot.autoconfigure; -import com.mitchellbosecke.pebble.boot.Application; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.mitchellbosecke.pebble.boot.Application; +import java.util.Locale; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -12,14 +18,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.util.Locale; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - @SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) class ServletAppTest {