Skip to content

Commit 65cd92f

Browse files
committed
Polish "Use Charset instead of String for Mustache templates support"
This replaces the use of charset name for our Mustache support. See spring-projectsgh-48347
1 parent c0f5809 commit 65cd92f

File tree

11 files changed

+92
-37
lines changed

11 files changed

+92
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Mustache.Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) {
7878
MustacheResourceTemplateLoader mustacheTemplateLoader() {
7979
MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader(this.mustache.getPrefix(),
8080
this.mustache.getSuffix());
81-
loader.setCharset(this.mustache.getCharsetName());
81+
loader.setCharset(this.mustache.getCharset());
8282
return loader;
8383
}
8484

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ public Charset getCharset() {
125125
}
126126

127127
/**
128-
* Get the charset name.
128+
* Return name of the charset.
129129
* @return the charset name
130-
* @deprecated since 4.1.0 in favor of {@link #getCharset()}
130+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of {@link #getCharset()}
131131
*/
132-
@Deprecated(since = "4.1.0")
132+
@Deprecated(since = "4.1.0", forRemoval = true)
133133
public String getCharsetName() {
134134
return this.charset.name();
135135
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustachePro
4040
map.from(mustache::getSuffix).to(resolver::setSuffix);
4141
map.from(mustache::getViewNames).to(resolver::setViewNames);
4242
map.from(mustache::getRequestContextAttribute).to(resolver::setRequestContextAttribute);
43-
map.from(mustache::getCharsetName).to(resolver::setCharset);
43+
map.from(mustache::getCharset).to(resolver::setCharset);
4444
map.from(mustache.getReactive()::getMediaTypes).to(resolver::setSupportedMediaTypes);
4545
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
4646
return resolver;

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL
4747

4848
private String suffix = "";
4949

50-
private Charset charSet = StandardCharsets.UTF_8;
50+
private Charset charset = StandardCharsets.UTF_8;
5151

5252
private ResourceLoader resourceLoader = new DefaultResourceLoader(null);
5353

@@ -60,22 +60,23 @@ public MustacheResourceTemplateLoader(String prefix, String suffix) {
6060
}
6161

6262
/**
63-
* Set the charset.
64-
* @param charSet the charset
65-
* @deprecated since 4.1.0 in favor of {@link #setCharset(Charset)}
63+
* Set the {@link Charset} to use.
64+
* @param charset the charset
65+
* @since 4.1.0
6666
*/
67-
@Deprecated(since = "4.1.0")
68-
public void setCharset(String charSet) {
69-
this.charSet = Charset.forName(charSet);
67+
public void setCharset(Charset charset) {
68+
this.charset = charset;
7069
}
7170

7271
/**
73-
* Set the charset.
74-
* @param charSet the charset
75-
* @since 4.1.0
72+
* Set the name of the charset to use.
73+
* @param charset the charset
74+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
75+
* {@link #setCharset(Charset)}
7676
*/
77-
public void setCharset(Charset charSet) {
78-
this.charSet = charSet;
77+
@Deprecated(since = "4.1.0", forRemoval = true)
78+
public void setCharset(String charset) {
79+
this.charset = Charset.forName(charset);
7980
}
8081

8182
/**
@@ -90,7 +91,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
9091
@Override
9192
public Reader getTemplate(String name) throws Exception {
9293
return new InputStreamReader(this.resourceLoader.getResource(this.prefix + name + this.suffix).getInputStream(),
93-
this.charSet);
94+
this.charset);
9495
}
9596

9697
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustachePro
4949
resolver.setExposeSessionAttributes(mustache.getServlet().isExposeSessionAttributes());
5050
resolver.setExposeSpringMacroHelpers(mustache.getServlet().isExposeSpringMacroHelpers());
5151
resolver.setRequestContextAttribute(mustache.getRequestContextAttribute());
52-
resolver.setCharset(mustache.getCharsetName());
52+
resolver.setCharset(mustache.getCharset());
5353
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
5454
return resolver;
5555
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class MustacheView extends AbstractUrlBasedView {
5353

5454
private @Nullable Compiler compiler;
5555

56-
private @Nullable String charset;
56+
private @Nullable Charset charset;
5757

5858
/**
5959
* Set the JMustache compiler to be used by this view. Typically this property is not
@@ -66,13 +66,25 @@ public void setCompiler(Compiler compiler) {
6666
}
6767

6868
/**
69-
* Set the charset used for reading Mustache template files.
70-
* @param charset the charset to use for reading template files
69+
* Set the {@link Charset} used for reading Mustache template files.
70+
* @param charset the charset
71+
* @since 4.1.0
7172
*/
72-
public void setCharset(@Nullable String charset) {
73+
public void setCharset(@Nullable Charset charset) {
7374
this.charset = charset;
7475
}
7576

77+
/**
78+
* Set the name of the charset used for reading Mustache template files.
79+
* @param charset the charset
80+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
81+
* {@link #setCharset(Charset)}
82+
*/
83+
@Deprecated(since = "4.1.0", forRemoval = true)
84+
public void setCharset(@Nullable String charset) {
85+
setCharset((charset != null) ? Charset.forName(charset) : null);
86+
}
87+
7688
@Override
7789
public boolean checkResourceExists(Locale locale) throws Exception {
7890
return resolveResource() != null;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.mustache.reactive.view;
1818

19+
import java.nio.charset.Charset;
20+
1921
import com.samskivert.mustache.Mustache;
2022
import com.samskivert.mustache.Mustache.Compiler;
2123
import org.jspecify.annotations.Nullable;
@@ -35,7 +37,7 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
3537

3638
private final Compiler compiler;
3739

38-
private @Nullable String charset;
40+
private @Nullable Charset charset;
3941

4042
/**
4143
* Create a {@code MustacheViewResolver} backed by a default instance of a
@@ -57,13 +59,25 @@ public MustacheViewResolver(Compiler compiler) {
5759
}
5860

5961
/**
60-
* Set the charset.
62+
* Set the {@link Charset} to use.
6163
* @param charset the charset
64+
* @since 4.1.0
6265
*/
63-
public void setCharset(String charset) {
66+
public void setCharset(@Nullable Charset charset) {
6467
this.charset = charset;
6568
}
6669

70+
/**
71+
* Set the name of the charset to use.
72+
* @param charset the charset
73+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
74+
* {@link #setCharset(Charset)}
75+
*/
76+
@Deprecated(since = "4.1.0", forRemoval = true)
77+
public void setCharset(@Nullable String charset) {
78+
setCharset((charset != null) ? Charset.forName(charset) : null);
79+
}
80+
6781
@Override
6882
protected Class<?> requiredViewClass() {
6983
return MustacheView.class;

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStreamReader;
2121
import java.io.Reader;
22+
import java.nio.charset.Charset;
2223
import java.util.Locale;
2324
import java.util.Map;
2425

@@ -46,7 +47,7 @@ public class MustacheView extends AbstractTemplateView {
4647

4748
private @Nullable Compiler compiler;
4849

49-
private @Nullable String charset;
50+
private @Nullable Charset charset;
5051

5152
/**
5253
* Set the Mustache compiler to be used by this view.
@@ -61,13 +62,25 @@ public void setCompiler(Compiler compiler) {
6162
}
6263

6364
/**
64-
* Set the charset used for reading Mustache template files.
65-
* @param charset the charset to use for reading template files
65+
* Set the {@link Charset} used for reading Mustache template files.
66+
* @param charset the charset
67+
* @since 4.1.0
6668
*/
67-
public void setCharset(@Nullable String charset) {
69+
public void setCharset(@Nullable Charset charset) {
6870
this.charset = charset;
6971
}
7072

73+
/**
74+
* Set the name of the charset used for reading Mustache template files.
75+
* @param charset the charset
76+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
77+
* {@link #setCharset(Charset)}
78+
*/
79+
@Deprecated(since = "4.1.0", forRemoval = true)
80+
public void setCharset(@Nullable String charset) {
81+
setCharset((charset != null) ? Charset.forName(charset) : null);
82+
}
83+
7184
@Override
7285
public boolean checkResource(Locale locale) throws Exception {
7386
Resource resource = getResource();

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.mustache.servlet.view;
1818

19+
import java.nio.charset.Charset;
20+
1921
import com.samskivert.mustache.Mustache;
2022
import com.samskivert.mustache.Mustache.Compiler;
2123
import org.jspecify.annotations.Nullable;
@@ -34,7 +36,7 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver {
3436

3537
private final Mustache.Compiler compiler;
3638

37-
private @Nullable String charset;
39+
private @Nullable Charset charset;
3840

3941
/**
4042
* Create a {@code MustacheViewResolver} backed by a default instance of a
@@ -61,13 +63,25 @@ protected Class<?> requiredViewClass() {
6163
}
6264

6365
/**
64-
* Set the charset.
66+
* Set the {@link Charset} to use.
6567
* @param charset the charset
68+
* @since 4.1.0
6669
*/
67-
public void setCharset(@Nullable String charset) {
70+
public void setCharset(@Nullable Charset charset) {
6871
this.charset = charset;
6972
}
7073

74+
/**
75+
* Set the name of the charset to use.
76+
* @param charset the charset
77+
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
78+
* {@link #setCharset(Charset)}
79+
*/
80+
@Deprecated(since = "4.1.0", forRemoval = true)
81+
public void setCharset(@Nullable String charset) {
82+
setCharset((charset != null) ? Charset.forName(charset) : null);
83+
}
84+
7185
@Override
7286
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
7387
MustacheView view = (MustacheView) super.buildView(viewName);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.mustache.autoconfigure;
1818

19+
import java.nio.charset.StandardCharsets;
1920
import java.util.Arrays;
2021
import java.util.function.Supplier;
2122

@@ -96,7 +97,7 @@ void defaultServletViewResolverConfiguration() {
9697
assertThat(viewResolver).extracting("allowRequestOverride", InstanceOfAssertFactories.BOOLEAN).isFalse();
9798
assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse();
9899
assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse();
99-
assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8");
100+
assertThat(viewResolver).extracting("charset").isEqualTo(StandardCharsets.UTF_8);
100101
assertThat(viewResolver).extracting("contentType").isEqualTo("text/html;charset=UTF-8");
101102
assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
102103
assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
@@ -112,7 +113,7 @@ void defaultReactiveViewResolverConfiguration() {
112113
configure(new ReactiveWebApplicationContextRunner()).run((context) -> {
113114
org.springframework.boot.mustache.reactive.view.MustacheViewResolver viewResolver = context
114115
.getBean(org.springframework.boot.mustache.reactive.view.MustacheViewResolver.class);
115-
assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8");
116+
assertThat(viewResolver).extracting("charset").isEqualTo(StandardCharsets.UTF_8);
116117
assertThat(viewResolver).extracting("prefix").isEqualTo("classpath:/templates/");
117118
assertThat(viewResolver).extracting("requestContextAttribute").isNull();
118119
assertThat(viewResolver).extracting("suffix").isEqualTo(".mustache");
@@ -141,7 +142,7 @@ void cacheCanBeCustomizedOnServletViewResolver() {
141142
@ParameterizedTest
142143
@EnumSource
143144
void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
144-
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16");
145+
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", StandardCharsets.UTF_16);
145146
if (kind == ViewResolverKind.SERVLET) {
146147
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "contentType",
147148
"text/html;charset=UTF-16");

0 commit comments

Comments
 (0)