mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
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
This commit is contained in:
+1
-1
@@ -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
@@ -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();
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
+20
-6
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
+16
-4
@@ -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;
|
||||
|
||||
+17
-3
@@ -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;
|
||||
|
||||
+17
-4
@@ -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();
|
||||
|
||||
+17
-3
@@ -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);
|
||||
|
||||
+4
-3
@@ -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");
|
||||
|
||||
+1
-1
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user