From 87a515f6a0be669ad5b93194011c880e9cf168b8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 2 Oct 2015 15:39:43 -0700 Subject: [PATCH] Fix TemplateAvailabilityProvider binding issues Update all TemplateAvailabilityProvider implementations to use the relaxed property binder. Also fix FreeMarkerTemplateAvailabilityProvider to use `template-loader-path` rather than `path`. Fixes gh-4085 --- .../FreeMarkerTemplateAvailabilityProvider.java | 11 +++++++---- .../GroovyTemplateAvailabilityProvider.java | 10 +++++++--- .../MustacheTemplateAvailabilityProvider.java | 8 ++++++-- .../ThymeleafTemplateAvailabilityProvider.java | 10 +++++++--- .../VelocityTemplateAvailabilityProvider.java | 13 ++++++++----- .../web/JspTemplateAvailabilityProvider.java | 10 +++++++--- ...FreeMarkerTemplateAvailabilityProviderTests.java | 7 ++----- .../VelocityTemplateAvailabilityProviderTests.java | 5 +---- 8 files changed, 45 insertions(+), 29 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProvider.java index b04081ba777..a5877d3a28d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.freemarker; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -35,11 +36,13 @@ public class FreeMarkerTemplateAvailabilityProvider implements public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("freemarker.template.Configuration", classLoader)) { - String loaderPath = environment.getProperty("spring.freemarker.path", + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.freemarker."); + String loaderPath = resolver.getProperty("template-loader-path", FreeMarkerProperties.DEFAULT_TEMPLATE_LOADER_PATH); - String prefix = environment.getProperty("spring.freemarker.prefix", + String prefix = resolver.getProperty("prefix", FreeMarkerProperties.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.freemarker.suffix", + String suffix = resolver.getProperty("suffix", FreeMarkerProperties.DEFAULT_SUFFIX); return resourceLoader.getResource(loaderPath + prefix + view + suffix) .exists(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAvailabilityProvider.java index c089b6d0aed..7e5fc8edc68 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.springframework.boot.autoconfigure.groovy.template; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -34,9 +36,11 @@ public class GroovyTemplateAvailabilityProvider implements TemplateAvailabilityP public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("groovy.text.TemplateEngine", classLoader)) { - String prefix = environment.getProperty("spring.groovy.template.prefix", + PropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.groovy.template."); + String prefix = resolver.getProperty("prefix", GroovyTemplateProperties.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.groovy.template.suffix", + String suffix = resolver.getProperty("suffix", GroovyTemplateProperties.DEFAULT_SUFFIX); return resourceLoader.getResource(prefix + view + suffix).exists(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheTemplateAvailabilityProvider.java index 7ca6386db09..fd325e0f7b7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheTemplateAvailabilityProvider.java @@ -17,7 +17,9 @@ package org.springframework.boot.autoconfigure.mustache; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -34,9 +36,11 @@ public class MustacheTemplateAvailabilityProvider implements TemplateAvailabilit public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("com.samskivert.mustache.Template", classLoader)) { - String prefix = environment.getProperty("spring.mustache.prefix", + PropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.mustache."); + String prefix = resolver.getProperty("prefix", MustacheProperties.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.mustache.suffix", + String suffix = resolver.getProperty("suffix", MustacheProperties.DEFAULT_SUFFIX); return resourceLoader.getResource(prefix + view + suffix).exists(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafTemplateAvailabilityProvider.java index 5ff9fd55309..54c7a0d4fcc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.springframework.boot.autoconfigure.thymeleaf; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -36,9 +38,11 @@ public class ThymeleafTemplateAvailabilityProvider implements ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("org.thymeleaf.spring4.SpringTemplateEngine", classLoader)) { - String prefix = environment.getProperty("spring.thymeleaf.prefix", + PropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.thymeleaf."); + String prefix = resolver.getProperty("prefix", ThymeleafProperties.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.thymeleaf.suffix", + String suffix = resolver.getProperty("suffix", ThymeleafProperties.DEFAULT_SUFFIX); return resourceLoader.getResource(prefix + view + suffix).exists(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProvider.java index 79aa84de469..904783c7c9e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.springframework.boot.autoconfigure.velocity; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -34,12 +36,13 @@ public class VelocityTemplateAvailabilityProvider implements TemplateAvailabilit public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("org.apache.velocity.app.VelocityEngine", classLoader)) { - String loaderPath = environment.getProperty( - "spring.velocity.resourceLoaderPath", + PropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.velocity."); + String loaderPath = resolver.getProperty("resource-loader-path", VelocityProperties.DEFAULT_RESOURCE_LOADER_PATH); - String prefix = environment.getProperty("spring.velocity.prefix", + String prefix = resolver.getProperty("prefix", VelocityProperties.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.velocity.suffix", + String suffix = resolver.getProperty("suffix", VelocityProperties.DEFAULT_SUFFIX); return resourceLoader.getResource(loaderPath + prefix + view + suffix) .exists(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java index 2193231fb54..7570d71c9ef 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/JspTemplateAvailabilityProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.springframework.boot.autoconfigure.web; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; @@ -34,9 +36,11 @@ public class JspTemplateAvailabilityProvider implements TemplateAvailabilityProv public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) { if (ClassUtils.isPresent("org.apache.jasper.compiler.JspConfig", classLoader)) { - String prefix = environment.getProperty("spring.view.prefix", + PropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.view."); + String prefix = resolver.getProperty("prefix", WebMvcAutoConfiguration.DEFAULT_PREFIX); - String suffix = environment.getProperty("spring.view.suffix", + String suffix = resolver.getProperty("suffix", WebMvcAutoConfiguration.DEFAULT_SUFFIX); return resourceLoader.getResource(prefix + view + suffix).exists(); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProviderTests.java index 8eb02a76494..2a7401426d7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerTemplateAvailabilityProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,9 +52,8 @@ public class FreeMarkerTemplateAvailabilityProviderTests { @Test public void availabilityOfTemplateWithCustomLoaderPath() { - this.environment.setProperty("spring.freemarker.path", + this.environment.setProperty("spring.freemarker.template-loader-path", "classpath:/custom-templates/"); - assertTrue(this.provider.isTemplateAvailable("custom", this.environment, getClass().getClassLoader(), this.resourceLoader)); } @@ -62,7 +61,6 @@ public class FreeMarkerTemplateAvailabilityProviderTests { @Test public void availabilityOfTemplateWithCustomPrefix() { this.environment.setProperty("spring.freemarker.prefix", "prefix/"); - assertTrue(this.provider.isTemplateAvailable("prefixed", this.environment, getClass().getClassLoader(), this.resourceLoader)); } @@ -70,7 +68,6 @@ public class FreeMarkerTemplateAvailabilityProviderTests { @Test public void availabilityOfTemplateWithCustomSuffix() { this.environment.setProperty("spring.freemarker.suffix", ".freemarker"); - assertTrue(this.provider.isTemplateAvailable("suffixed", this.environment, getClass().getClassLoader(), this.resourceLoader)); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProviderTests.java index 1804a6c9872..70696072c88 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityTemplateAvailabilityProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,6 @@ public class VelocityTemplateAvailabilityProviderTests { public void availabilityOfTemplateWithCustomLoaderPath() { this.environment.setProperty("spring.velocity.resourceLoaderPath", "classpath:/custom-templates/"); - assertTrue(this.provider.isTemplateAvailable("custom", this.environment, getClass().getClassLoader(), this.resourceLoader)); } @@ -62,7 +61,6 @@ public class VelocityTemplateAvailabilityProviderTests { @Test public void availabilityOfTemplateWithCustomPrefix() { this.environment.setProperty("spring.velocity.prefix", "prefix/"); - assertTrue(this.provider.isTemplateAvailable("prefixed", this.environment, getClass().getClassLoader(), this.resourceLoader)); } @@ -70,7 +68,6 @@ public class VelocityTemplateAvailabilityProviderTests { @Test public void availabilityOfTemplateWithCustomSuffix() { this.environment.setProperty("spring.velocity.suffix", ".freemarker"); - assertTrue(this.provider.isTemplateAvailable("suffixed", this.environment, getClass().getClassLoader(), this.resourceLoader)); }