Browse Source

Merge pull request #48347 from ddongjunn

* pr/48347:
  Polish "Use Charset instead of String for Mustache templates support"
  Use Charset instead of String for Mustache templates support

Closes gh-48347
pull/48596/head
Stéphane Nicoll 1 month ago
parent
commit
a1ed3bffaa
  1. 2
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java
  2. 6
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java
  3. 2
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java
  4. 26
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java
  5. 2
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java
  6. 20
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java
  7. 20
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java
  8. 21
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheView.java
  9. 20
      module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java
  10. 7
      module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java
  11. 2
      module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java

2
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfiguration.java

@ -78,7 +78,7 @@ public final class MustacheAutoConfiguration { @@ -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;
}

6
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheProperties.java

@ -124,6 +124,12 @@ public class MustacheProperties { @@ -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();
}

2
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheReactiveWebConfiguration.java

@ -40,7 +40,7 @@ class MustacheReactiveWebConfiguration { @@ -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;

26
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheResourceTemplateLoader.java

@ -18,6 +18,8 @@ package org.springframework.boot.mustache.autoconfigure; @@ -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 @@ -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 @@ -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 @@ -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);
}
}

2
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/autoconfigure/MustacheServletWebConfiguration.java

@ -49,7 +49,7 @@ class MustacheServletWebConfiguration { @@ -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;
}

20
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheView.java

@ -53,7 +53,7 @@ public class MustacheView extends AbstractUrlBasedView { @@ -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 { @@ -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;

20
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/reactive/view/MustacheViewResolver.java

@ -16,6 +16,8 @@ @@ -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 { @@ -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 { @@ -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;

21
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; @@ -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 { @@ -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 { @@ -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();

20
module/spring-boot-mustache/src/main/java/org/springframework/boot/mustache/servlet/view/MustacheViewResolver.java

@ -16,6 +16,8 @@ @@ -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 { @@ -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 { @@ -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);

7
module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationTests.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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 { @@ -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 { @@ -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");

2
module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/reactive/view/MustacheViewTests.java

@ -47,7 +47,7 @@ class MustacheViewTests { @@ -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));

Loading…
Cancel
Save