diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java index 453810dff8e..b007395cb51 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java @@ -78,7 +78,7 @@ public final class MustacheAutoConfiguration { MustacheResourceTemplateLoader mustacheTemplateLoader() { MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader(this.mustache.getPrefix(), this.mustache.getSuffix()); - loader.setCharset(this.mustache.getCharsetName()); + loader.setCharset(this.mustache.getCharset()); return loader; } diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java index 558d90b9e49..c65b8f0f141 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java @@ -124,6 +124,12 @@ public class MustacheProperties { return this.charset; } + /** + * Return name of the charset. + * @return the charset name + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of {@link #getCharset()} + */ + @Deprecated(since = "4.1.0", forRemoval = true) public String getCharsetName() { return this.charset.name(); } diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java index d40e59f0f15..31ea690d5a4 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java @@ -40,7 +40,7 @@ class MustacheReactiveWebConfiguration { map.from(mustache::getSuffix).to(resolver::setSuffix); map.from(mustache::getViewNames).to(resolver::setViewNames); map.from(mustache::getRequestContextAttribute).to(resolver::setRequestContextAttribute); - map.from(mustache::getCharsetName).to(resolver::setCharset); + map.from(mustache::getCharset).to(resolver::setCharset); map.from(mustache.getReactive()::getMediaTypes).to(resolver::setSupportedMediaTypes); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); return resolver; diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java index b388ff0b33c..7a1be36ea2d 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java @@ -18,6 +18,8 @@ package org.springframework.boot.mustache.autoconfigure; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; @@ -45,7 +47,7 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL private String suffix = ""; - private String charSet = "UTF-8"; + private Charset charset = StandardCharsets.UTF_8; private ResourceLoader resourceLoader = new DefaultResourceLoader(null); @@ -58,11 +60,23 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL } /** - * Set the charset. - * @param charSet the charset + * Set the {@link Charset} to use. + * @param charset the charset + * @since 4.1.0 */ - public void setCharset(String charSet) { - this.charSet = charSet; + public void setCharset(Charset charset) { + this.charset = charset; + } + + /** + * Set the name of the charset to use. + * @param charset the charset + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of + * {@link #setCharset(Charset)} + */ + @Deprecated(since = "4.1.0", forRemoval = true) + public void setCharset(String charset) { + this.charset = Charset.forName(charset); } /** @@ -77,7 +91,7 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL @Override public Reader getTemplate(String name) throws Exception { return new InputStreamReader(this.resourceLoader.getResource(this.prefix + name + this.suffix).getInputStream(), - this.charSet); + this.charset); } } diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java index a327223c0eb..1910d078089 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java @@ -49,7 +49,7 @@ class MustacheServletWebConfiguration { resolver.setExposeSessionAttributes(mustache.getServlet().isExposeSessionAttributes()); resolver.setExposeSpringMacroHelpers(mustache.getServlet().isExposeSpringMacroHelpers()); resolver.setRequestContextAttribute(mustache.getRequestContextAttribute()); - resolver.setCharset(mustache.getCharsetName()); + resolver.setCharset(mustache.getCharset()); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); return resolver; } diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java index d200249319a..3be5191c4e9 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java @@ -53,7 +53,7 @@ public class MustacheView extends AbstractUrlBasedView { private @Nullable Compiler compiler; - private @Nullable String charset; + private @Nullable Charset charset; /** * Set the JMustache compiler to be used by this view. Typically this property is not @@ -66,13 +66,25 @@ public class MustacheView extends AbstractUrlBasedView { } /** - * Set the charset used for reading Mustache template files. - * @param charset the charset to use for reading template files + * Set the {@link Charset} used for reading Mustache template files. + * @param charset the charset + * @since 4.1.0 */ - public void setCharset(@Nullable String charset) { + public void setCharset(@Nullable Charset charset) { this.charset = charset; } + /** + * Set the name of the charset used for reading Mustache template files. + * @param charset the charset + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of + * {@link #setCharset(Charset)} + */ + @Deprecated(since = "4.1.0", forRemoval = true) + public void setCharset(@Nullable String charset) { + setCharset((charset != null) ? Charset.forName(charset) : null); + } + @Override public boolean checkResourceExists(Locale locale) throws Exception { return resolveResource() != null; diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java index 40188df7eaf..0c870d977b6 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java @@ -16,6 +16,8 @@ package org.springframework.boot.mustache.reactive.view; +import java.nio.charset.Charset; + import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; import org.jspecify.annotations.Nullable; @@ -35,7 +37,7 @@ public class MustacheViewResolver extends UrlBasedViewResolver { private final Compiler compiler; - private @Nullable String charset; + private @Nullable Charset charset; /** * Create a {@code MustacheViewResolver} backed by a default instance of a @@ -57,13 +59,25 @@ public class MustacheViewResolver extends UrlBasedViewResolver { } /** - * Set the charset. + * Set the {@link Charset} to use. * @param charset the charset + * @since 4.1.0 */ - public void setCharset(String charset) { + public void setCharset(@Nullable Charset charset) { this.charset = charset; } + /** + * Set the name of the charset to use. + * @param charset the charset + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of + * {@link #setCharset(Charset)} + */ + @Deprecated(since = "4.1.0", forRemoval = true) + public void setCharset(@Nullable String charset) { + setCharset((charset != null) ? Charset.forName(charset) : null); + } + @Override protected Class requiredViewClass() { return MustacheView.class; diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java index 57b7e76a528..2378e404f79 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java @@ -19,6 +19,7 @@ package org.springframework.boot.mustache.servlet.view; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.Charset; import java.util.Locale; import java.util.Map; @@ -46,7 +47,7 @@ public class MustacheView extends AbstractTemplateView { private @Nullable Compiler compiler; - private @Nullable String charset; + private @Nullable Charset charset; /** * Set the Mustache compiler to be used by this view. @@ -61,13 +62,25 @@ public class MustacheView extends AbstractTemplateView { } /** - * Set the charset used for reading Mustache template files. - * @param charset the charset to use for reading template files + * Set the {@link Charset} used for reading Mustache template files. + * @param charset the charset + * @since 4.1.0 */ - public void setCharset(@Nullable String charset) { + public void setCharset(@Nullable Charset charset) { this.charset = charset; } + /** + * Set the name of the charset used for reading Mustache template files. + * @param charset the charset + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of + * {@link #setCharset(Charset)} + */ + @Deprecated(since = "4.1.0", forRemoval = true) + public void setCharset(@Nullable String charset) { + setCharset((charset != null) ? Charset.forName(charset) : null); + } + @Override public boolean checkResource(Locale locale) throws Exception { Resource resource = getResource(); diff --git a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java index a856adff0c8..b0ea99843f7 100644 --- a/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java +++ b/module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java @@ -16,6 +16,8 @@ package org.springframework.boot.mustache.servlet.view; +import java.nio.charset.Charset; + import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; import org.jspecify.annotations.Nullable; @@ -34,7 +36,7 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver { private final Mustache.Compiler compiler; - private @Nullable String charset; + private @Nullable Charset charset; /** * Create a {@code MustacheViewResolver} backed by a default instance of a @@ -61,13 +63,25 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver { } /** - * Set the charset. + * Set the {@link Charset} to use. * @param charset the charset + * @since 4.1.0 */ - public void setCharset(@Nullable String charset) { + public void setCharset(@Nullable Charset charset) { this.charset = charset; } + /** + * Set the name of the charset to use. + * @param charset the charset + * @deprecated since 4.1.0 for removal in 4.3.0 in favor of + * {@link #setCharset(Charset)} + */ + @Deprecated(since = "4.1.0", forRemoval = true) + public void setCharset(@Nullable String charset) { + setCharset((charset != null) ? Charset.forName(charset) : null); + } + @Override protected AbstractUrlBasedView buildView(String viewName) throws Exception { MustacheView view = (MustacheView) super.buildView(viewName); diff --git a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java index 4d0a9e7d87f..2c95a4048c6 100644 --- a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java +++ b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.mustache.autoconfigure; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.function.Supplier; @@ -96,7 +97,7 @@ class MustacheAutoConfigurationTests { assertThat(viewResolver).extracting("allowRequestOverride", InstanceOfAssertFactories.BOOLEAN).isFalse(); assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse(); assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse(); - assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8"); + assertThat(viewResolver).extracting("charset").isEqualTo(StandardCharsets.UTF_8); assertThat(viewResolver).extracting("contentType").isEqualTo("text/html;charset=UTF-8"); assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse(); assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse(); @@ -112,7 +113,7 @@ class MustacheAutoConfigurationTests { configure(new ReactiveWebApplicationContextRunner()).run((context) -> { org.springframework.boot.mustache.reactive.view.MustacheViewResolver viewResolver = context .getBean(org.springframework.boot.mustache.reactive.view.MustacheViewResolver.class); - assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8"); + assertThat(viewResolver).extracting("charset").isEqualTo(StandardCharsets.UTF_8); assertThat(viewResolver).extracting("prefix").isEqualTo("classpath:/templates/"); assertThat(viewResolver).extracting("requestContextAttribute").isNull(); assertThat(viewResolver).extracting("suffix").isEqualTo(".mustache"); @@ -141,7 +142,7 @@ class MustacheAutoConfigurationTests { @ParameterizedTest @EnumSource void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) { - assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16"); + assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", StandardCharsets.UTF_16); if (kind == ViewResolverKind.SERVLET) { assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "contentType", "text/html;charset=UTF-16"); diff --git a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java index f45679f560d..f51d92584f0 100644 --- a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java +++ b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java @@ -47,7 +47,7 @@ class MustacheViewTests { MustacheView view = new MustacheView(); view.setCompiler(Mustache.compiler()); view.setUrl("classpath:template.html"); - view.setCharset(StandardCharsets.UTF_8.displayName()); + view.setCharset(StandardCharsets.UTF_8); view.setApplicationContext(this.context); view.render(Collections.singletonMap("World", "Spring"), MediaType.TEXT_HTML, exchange) .block(Duration.ofSeconds(30));