diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java index a5bf1bdf9c1..809095d5621 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java @@ -28,10 +28,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration; import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.servlet.MustacheViewResolver; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java index d7bc9566381..90cffe03543 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java @@ -20,7 +20,6 @@ import javax.annotation.PostConstruct; import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Collector; -import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Mustache.TemplateLoader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,15 +27,12 @@ import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; -import org.springframework.boot.autoconfigure.mustache.servlet.MustacheViewResolver; import org.springframework.boot.autoconfigure.template.TemplateLocation; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; +import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; /** @@ -49,6 +45,7 @@ import org.springframework.core.env.Environment; @Configuration @ConditionalOnClass(Mustache.class) @EnableConfigurationProperties(MustacheProperties.class) +@Import({ MustacheServletWebConfiguration.class, MustacheReactiveWebConfiguration.class }) public class MustacheAutoConfiguration { private static final Log logger = LogFactory.getLog(MustacheAutoConfiguration.class); @@ -101,56 +98,4 @@ public class MustacheAutoConfiguration { return loader; } - @Configuration - @ConditionalOnWebApplication(type = Type.SERVLET) - protected static class MustacheWebConfiguration { - - private final MustacheProperties mustache; - - protected MustacheWebConfiguration(MustacheProperties mustache) { - this.mustache = mustache; - } - - @Bean - @ConditionalOnMissingBean(MustacheViewResolver.class) - public MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler) { - MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler); - this.mustache.applyToViewResolver(resolver); - resolver.setCharset(this.mustache.getCharsetName()); - resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); - return resolver; - } - - } - - @Configuration - @ConditionalOnWebApplication(type = Type.REACTIVE) - protected static class MustacheReactiveWebConfiguration { - - private final MustacheProperties mustache; - - protected MustacheReactiveWebConfiguration(MustacheProperties mustache) { - this.mustache = mustache; - } - - @Bean - @ConditionalOnMissingBean(org.springframework.boot.autoconfigure - .mustache.reactive.MustacheViewResolver.class) - public org.springframework.boot.autoconfigure - .mustache.reactive.MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler) { - org.springframework.boot.autoconfigure - .mustache.reactive.MustacheViewResolver resolver - = new org.springframework.boot.autoconfigure - .mustache.reactive.MustacheViewResolver(mustacheCompiler); - resolver.setPrefix(this.mustache.getPrefix()); - resolver.setSuffix(this.mustache.getSuffix()); - resolver.setViewNames(this.mustache.getViewNames()); - resolver.setRequestContextAttribute(this.mustache.getRequestContextAttribute()); - resolver.setCharset(this.mustache.getCharsetName()); - resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); - return resolver; - } - - } - } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java new file mode 100644 index 00000000000..55502245f00 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.mustache; + +import com.samskivert.mustache.Mustache.Compiler; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.boot.web.reactive.result.view.MustacheViewResolver; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; + +@Configuration +@ConditionalOnWebApplication(type = Type.REACTIVE) +class MustacheReactiveWebConfiguration { + + private final MustacheProperties mustache; + + protected MustacheReactiveWebConfiguration(MustacheProperties mustache) { + this.mustache = mustache; + } + + @Bean + @ConditionalOnMissingBean(MustacheViewResolver.class) + public MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler) { + MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler); + resolver.setPrefix(this.mustache.getPrefix()); + resolver.setSuffix(this.mustache.getSuffix()); + resolver.setViewNames(this.mustache.getViewNames()); + resolver.setRequestContextAttribute(this.mustache.getRequestContextAttribute()); + resolver.setCharset(this.mustache.getCharsetName()); + resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); + return resolver; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheResourceTemplateLoader.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheResourceTemplateLoader.java index 8070b5325e4..d2144b30427 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheResourceTemplateLoader.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheResourceTemplateLoader.java @@ -20,6 +20,7 @@ import java.io.InputStreamReader; import java.io.Reader; import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Mustache.TemplateLoader; import org.springframework.context.ResourceLoaderAware; @@ -30,8 +31,8 @@ import org.springframework.core.io.ResourceLoader; /** * Mustache TemplateLoader implementation that uses a prefix, suffix and the Spring * Resource abstraction to load a template from a file, classpath, URL etc. A - * TemplateLoader is needed in the Compiler when you want to render partials (i.e. - * tiles-like features). + * {@link TemplateLoader} is needed in the {@link Compiler} when you want to render + * partials (i.e. tiles-like features). * * @author Dave Syer * @since 1.2.2 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java new file mode 100644 index 00000000000..b3a41d0a9bc --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.mustache; + +import com.samskivert.mustache.Mustache.Compiler; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.boot.web.servlet.view.MustacheViewResolver; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; + +@Configuration +@ConditionalOnWebApplication(type = Type.SERVLET) +class MustacheServletWebConfiguration { + + private final MustacheProperties mustache; + + protected MustacheServletWebConfiguration(MustacheProperties mustache) { + this.mustache = mustache; + } + + @Bean + @ConditionalOnMissingBean(MustacheViewResolver.class) + public MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler) { + MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler); + this.mustache.applyToViewResolver(resolver); + resolver.setCharset(this.mustache.getCharsetName()); + resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); + return resolver; + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfigurationTests.java index e6e4cb971e6..3c8409a2573 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfigurationTests.java @@ -34,10 +34,10 @@ import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfigura import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.servlet.MustacheViewResolver; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.core.Ordered; import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; import org.springframework.mock.web.MockServletContext; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheWebIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationReactiveIntegrationTests.java similarity index 83% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheWebIntegrationTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationReactiveIntegrationTests.java index 34f2bddec72..1e636aa1ccd 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheWebIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationReactiveIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.autoconfigure.mustache; import java.util.Date; @@ -26,13 +26,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader; import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.reactive.result.view.MustacheView; +import org.springframework.boot.web.reactive.result.view.MustacheViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -51,34 +51,30 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Brian Clozel */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, - properties = "spring.main.web-application-type=reactive") -public class MustacheWebIntegrationTests { +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive") +public class MustacheAutoConfigurationReactiveIntegrationTests { @Autowired private WebTestClient client; @Test public void testHomePage() throws Exception { - String result = (String) this.client.get().uri("/").exchange() - .expectStatus().isOk() + String result = this.client.get().uri("/").exchange().expectStatus().isOk() .expectBody(String.class).returnResult().getResponseBody(); assertThat(result).contains("Hello App").contains("Hello World"); } @Test public void testPartialPage() throws Exception { - String result = (String) this.client.get().uri("/partial").exchange() - .expectStatus().isOk() + String result = this.client.get().uri("/partial").exchange().expectStatus().isOk() .expectBody(String.class).returnResult().getResponseBody(); assertThat(result).contains("Hello App").contains("Hello World"); } @Configuration - @Import({ReactiveWebServerAutoConfiguration.class, - WebFluxAutoConfiguration.class, + @Import({ ReactiveWebServerAutoConfiguration.class, WebFluxAutoConfiguration.class, HttpHandlerAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class}) + PropertyPlaceholderAutoConfiguration.class }) @Controller public static class Application { @@ -101,7 +97,8 @@ public class MustacheWebIntegrationTests { @Bean public MustacheViewResolver viewResolver() { Mustache.Compiler compiler = Mustache.compiler().withLoader( - new MustacheResourceTemplateLoader("classpath:/mustache-templates/", ".html")); + new MustacheResourceTemplateLoader("classpath:/mustache-templates/", + ".html")); MustacheViewResolver resolver = new MustacheViewResolver(compiler); resolver.setPrefix("classpath:/mustache-templates/"); resolver.setSuffix(".html"); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheWebIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationServletIntegrationTests.java similarity index 89% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheWebIntegrationTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationServletIntegrationTests.java index 7b0a45c425e..9f75618d568 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheWebIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationServletIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.autoconfigure.mustache; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -34,14 +34,14 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; +import org.springframework.boot.web.servlet.view.MustacheView; +import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -61,7 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @DirtiesContext @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -public class MustacheWebIntegrationTests { +public class MustacheAutoConfigurationServletIntegrationTests { @Autowired private ServletWebServerApplicationContext context; @@ -79,8 +79,7 @@ public class MustacheWebIntegrationTests { Template tmpl = Mustache.compiler().compile(source); Map context = new HashMap<>(); context.put("arg", "world"); - assertThat(tmpl.execute(context)).isEqualTo("Hello world!"); // returns "Hello - // world!" + assertThat(tmpl.execute(context)).isEqualTo("Hello world!"); } @Test @@ -120,8 +119,8 @@ public class MustacheWebIntegrationTests { @Bean public MustacheViewResolver viewResolver() { - Mustache.Compiler compiler = Mustache.compiler() - .withLoader(new MustacheResourceTemplateLoader("classpath:/mustache-templates/", + Mustache.Compiler compiler = Mustache.compiler().withLoader( + new MustacheResourceTemplateLoader("classpath:/mustache-templates/", ".html")); MustacheViewResolver resolver = new MustacheViewResolver(compiler); resolver.setPrefix("classpath:/mustache-templates/"); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java index 1e71ed027fd..aba0adf3acb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java @@ -19,9 +19,9 @@ package org.springframework.boot.autoconfigure.mustache; import com.samskivert.mustache.Mustache; import org.junit.Test; -import org.springframework.boot.autoconfigure.mustache.servlet.MustacheViewResolver; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; +import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -64,7 +64,7 @@ public class MustacheAutoConfigurationTests { assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); assertThat(this.reactiveWebContext - .getBeansOfType(org.springframework.boot.autoconfigure.mustache.reactive.MustacheViewResolver.class) + .getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class) ).hasSize(1); } @@ -76,7 +76,7 @@ public class MustacheAutoConfigurationTests { assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); assertThat(this.reactiveWebContext - .getBeansOfType(org.springframework.boot.autoconfigure.mustache.reactive.MustacheViewResolver.class) + .getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class) ).hasSize(1); assertThat(this.reactiveWebContext.getBean(Mustache.Compiler.class).standardsMode).isTrue(); } diff --git a/spring-boot-parent/src/checkstyle/import-control.xml b/spring-boot-parent/src/checkstyle/import-control.xml index e267727f8c6..db8c4a8f5da 100644 --- a/spring-boot-parent/src/checkstyle/import-control.xml +++ b/spring-boot-parent/src/checkstyle/import-control.xml @@ -50,6 +50,9 @@ + + + @@ -63,6 +66,11 @@ + + + + + diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 41d187c1945..64a92657137 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -54,6 +54,11 @@ jackson-databind true + + com.samskivert + jmustache + true + com.sendgrid sendgrid-java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheView.java b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheView.java similarity index 79% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheView.java rename to spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheView.java index d40f7fb5ddc..5abe18c8dea 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheView.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheView.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.web.reactive.result.view; import java.io.IOException; import java.io.InputStreamReader; @@ -26,7 +26,7 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; -import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Template; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -46,18 +46,17 @@ import org.springframework.web.server.ServerWebExchange; */ public class MustacheView extends AbstractUrlBasedView { - private Mustache.Compiler compiler; + private Compiler compiler; private String charset; /** - * Set the JMustache compiler to be used by this view. - *

Typically this property is not set directly. Instead a single - * {@link Mustache.Compiler} is expected in the Spring application context - * which is used to compile Mustache templates. + * Set the JMustache compiler to be used by this view. Typically this property is not + * set directly. Instead a single {@link Compiler} is expected in the Spring + * application context which is used to compile Mustache templates. * @param compiler the Mustache compiler */ - public void setCompiler(Mustache.Compiler compiler) { + public void setCompiler(Compiler compiler) { this.compiler = compiler; } @@ -74,27 +73,20 @@ public class MustacheView extends AbstractUrlBasedView { return resolveResource() != null; } - private Resource resolveResource() { - Resource resource = getApplicationContext().getResource(getUrl()); - if (resource == null || !resource.exists()) { - return null; - } - return resource; - } - @Override - protected Mono renderInternal(Map model, - MediaType contentType, ServerWebExchange exchange) { + protected Mono renderInternal(Map model, MediaType contentType, + ServerWebExchange exchange) { Resource resource = resolveResource(); if (resource == null) { - return Mono.error(new IllegalStateException("Could not find Mustache template with URL [" - + getUrl() + "]")); + return Mono.error(new IllegalStateException( + "Could not find Mustache template with URL [" + getUrl() + "]")); } DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer(); try (Reader reader = getReader(resource)) { Template template = this.compiler.compile(reader); Charset charset = getCharset(contentType).orElse(getDefaultCharset()); - try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset)) { + try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), + charset)) { template.execute(model, writer); writer.flush(); } @@ -105,6 +97,14 @@ public class MustacheView extends AbstractUrlBasedView { return exchange.getResponse().writeWith(Flux.just(dataBuffer)); } + private Resource resolveResource() { + Resource resource = getApplicationContext().getResource(getUrl()); + if (resource == null || !resource.exists()) { + return null; + } + return resource; + } + private Reader getReader(Resource resource) throws IOException { if (this.charset != null) { return new InputStreamReader(resource.getInputStream(), this.charset); @@ -113,6 +113,7 @@ public class MustacheView extends AbstractUrlBasedView { } private Optional getCharset(MediaType mediaType) { - return (mediaType != null ? Optional.ofNullable(mediaType.getCharset()) : Optional.empty()); + return Optional.ofNullable(mediaType != null ? mediaType.getCharset() : null); } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolver.java b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolver.java similarity index 82% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolver.java rename to spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolver.java index 2a60f8881b5..1886e46e382 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolver.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolver.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.web.reactive.result.view; import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Mustache.Compiler; import org.springframework.web.reactive.result.view.AbstractUrlBasedView; import org.springframework.web.reactive.result.view.UrlBasedViewResolver; @@ -30,13 +31,13 @@ import org.springframework.web.reactive.result.view.ViewResolver; */ public class MustacheViewResolver extends UrlBasedViewResolver { - private final Mustache.Compiler compiler; + private final Compiler compiler; private String charset; /** - * Create a {@code MustacheViewResolver} backed by a default - * instance of a {@link Mustache.Compiler}. + * Create a {@code MustacheViewResolver} backed by a default instance of a + * {@link Compiler}. */ public MustacheViewResolver() { this.compiler = Mustache.compiler(); @@ -44,11 +45,11 @@ public class MustacheViewResolver extends UrlBasedViewResolver { } /** - * Create a {@code MustacheViewResolver} backed by a custom - * instance of a {@link Mustache.Compiler}. + * Create a {@code MustacheViewResolver} backed by a custom instance of a + * {@link Compiler}. * @param compiler the Mustache compiler used to compile templates */ - public MustacheViewResolver(Mustache.Compiler compiler) { + public MustacheViewResolver(Compiler compiler) { this.compiler = compiler; setViewClass(requiredViewClass()); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/package-info.java b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/package-info.java similarity index 79% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/package-info.java rename to spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/package-info.java index e89db28df66..354fb3c005b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/reactive/package-info.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/package-info.java @@ -15,6 +15,7 @@ */ /** - * Auto-configuration for Mustache with Spring WebFlux. + * Additional {@link org.springframework.web.reactive.result.view.View Views} for use with + * WebFlux. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.web.reactive.result.view; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheView.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheView.java similarity index 81% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheView.java rename to spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheView.java index 7fbe995aca8..b82db8825d2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheView.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheView.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.web.servlet.view; import java.io.IOException; import java.io.InputStreamReader; @@ -25,7 +25,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Template; import org.springframework.core.io.Resource; @@ -42,18 +42,19 @@ import org.springframework.web.servlet.view.AbstractTemplateView; */ public class MustacheView extends AbstractTemplateView { - private Mustache.Compiler compiler; + private Compiler compiler; private String charset; /** * Set the Mustache compiler to be used by this view. - *

Typically this property is not set directly. Instead a single - * {@link Mustache.Compiler} is expected in the Spring application context - * which is used to compile Mustache templates. + *

+ * Typically this property is not set directly. Instead a single {@link Compiler} is + * expected in the Spring application context which is used to compile Mustache + * templates. * @param compiler the Mustache compiler */ - public void setCompiler(Mustache.Compiler compiler) { + public void setCompiler(Compiler compiler) { this.compiler = compiler; } @@ -72,9 +73,10 @@ public class MustacheView extends AbstractTemplateView { } @Override - protected void renderMergedTemplateModel(Map model, HttpServletRequest request, - HttpServletResponse response) throws Exception { - Template template = createTemplate(getApplicationContext().getResource(this.getUrl())); + protected void renderMergedTemplateModel(Map model, + HttpServletRequest request, HttpServletResponse response) throws Exception { + Template template = createTemplate( + getApplicationContext().getResource(this.getUrl())); if (template != null) { template.execute(model, response.getWriter()); } @@ -96,4 +98,5 @@ public class MustacheView extends AbstractTemplateView { } return new InputStreamReader(resource.getInputStream()); } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolver.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheViewResolver.java similarity index 84% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolver.java rename to spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheViewResolver.java index 1c66279268f..73f60aeae87 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolver.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/MustacheViewResolver.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.web.servlet.view; import com.samskivert.mustache.Mustache; +import com.samskivert.mustache.Mustache.Compiler; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.AbstractTemplateViewResolver; @@ -35,8 +36,8 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver { private String charset; /** - * Create a {@code MustacheViewResolver} backed by a default - * instance of a {@link Mustache.Compiler}. + * Create a {@code MustacheViewResolver} backed by a default instance of a + * {@link Compiler}. */ public MustacheViewResolver() { this.compiler = Mustache.compiler(); @@ -44,11 +45,11 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver { } /** - * Create a {@code MustacheViewResolver} backed by a custom - * instance of a {@link Mustache.Compiler}. + * Create a {@code MustacheViewResolver} backed by a custom instance of a + * {@link Compiler}. * @param compiler the Mustache compiler used to compile templates */ - public MustacheViewResolver(Mustache.Compiler compiler) { + public MustacheViewResolver(Compiler compiler) { this.compiler = compiler; setViewClass(requiredViewClass()); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/package-info.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/package-info.java similarity index 82% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/package-info.java rename to spring-boot/src/main/java/org/springframework/boot/web/servlet/view/package-info.java index 3d137e0afd2..7fce11b6322 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/servlet/package-info.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/package-info.java @@ -15,6 +15,6 @@ */ /** - * Auto-configuration for Mustache with Spring MVC. + * Additional {@link org.springframework.web.servlet.View Views} for use with Web MVC. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.web.servlet.view; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolverTests.java b/spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolverTests.java similarity index 80% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolverTests.java rename to spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolverTests.java index 3f84133b1b6..4875c5ac195 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewResolverTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewResolverTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.web.reactive.result.view; import org.junit.Before; import org.junit.Test; @@ -24,12 +24,15 @@ import org.springframework.context.support.GenericApplicationContext; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link org.springframework.boot.autoconfigure.mustache.reactive.MustacheViewResolver}. + * Tests for {@link MustacheViewResolver}. * * @author Brian Clozel */ public class MustacheViewResolverTests { + private final String prefix = "classpath:/" + + getClass().getPackage().getName().replace(".", "/") + "/"; + private MustacheViewResolver resolver = new MustacheViewResolver(); @Before @@ -37,7 +40,7 @@ public class MustacheViewResolverTests { GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh(); this.resolver.setApplicationContext(applicationContext); - this.resolver.setPrefix("classpath:/mustache-templates/"); + this.resolver.setPrefix(this.prefix); this.resolver.setSuffix(".html"); } @@ -48,7 +51,7 @@ public class MustacheViewResolverTests { @Test public void resolveExisting() throws Exception { - assertThat(this.resolver.resolveViewName("foo", null).block()).isNotNull(); + assertThat(this.resolver.resolveViewName("template", null).block()).isNotNull(); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewTests.java b/spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewTests.java similarity index 86% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewTests.java rename to spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewTests.java index ebb05c81534..dcb94fd8158 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/reactive/MustacheViewTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/reactive/result/view/MustacheViewTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.reactive; +package org.springframework.boot.web.reactive.result.view; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -37,6 +37,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class MustacheViewTests { + private final String templateUrl = "classpath:/" + + getClass().getPackage().getName().replace(".", "/") + "/template.html"; + private GenericApplicationContext context = new GenericApplicationContext(); private MockServerWebExchange exchange; @@ -51,11 +54,13 @@ public class MustacheViewTests { this.exchange = MockServerHttpRequest.get("/test").toExchange(); MustacheView view = new MustacheView(); view.setCompiler(Mustache.compiler()); - view.setUrl("classpath:/mustache-templates/foo.html"); + view.setUrl(this.templateUrl); view.setCharset(StandardCharsets.UTF_8.displayName()); view.setApplicationContext(this.context); - view.render(Collections.singletonMap("World", "Spring"), MediaType.TEXT_HTML, this.exchange).block(); - assertThat(this.exchange.getResponse().getBodyAsString().block()).isEqualTo("Hello Spring"); + view.render(Collections.singletonMap("World", "Spring"), MediaType.TEXT_HTML, + this.exchange).block(); + assertThat(this.exchange.getResponse().getBodyAsString().block()) + .isEqualTo("Hello Spring"); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolverTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewResolverTests.java similarity index 84% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolverTests.java rename to spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewResolverTests.java index 36a4ad24e9a..eaf5bef3f34 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewResolverTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewResolverTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.web.servlet.view; import org.junit.Before; import org.junit.Test; @@ -33,6 +33,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class MustacheViewResolverTests { + private final String prefix = "classpath:/" + + getClass().getPackage().getName().replace(".", "/") + "/"; + private MustacheViewResolver resolver = new MustacheViewResolver(); @Before @@ -41,7 +44,7 @@ public class MustacheViewResolverTests { applicationContext.refresh(); this.resolver.setApplicationContext(applicationContext); this.resolver.setServletContext(new MockServletContext()); - this.resolver.setPrefix("classpath:/mustache-templates/"); + this.resolver.setPrefix(this.prefix); this.resolver.setSuffix(".html"); } @@ -52,13 +55,13 @@ public class MustacheViewResolverTests { @Test public void resolveExisting() throws Exception { - assertThat(this.resolver.resolveViewName("foo", null)).isNotNull(); + assertThat(this.resolver.resolveViewName("template", null)).isNotNull(); } @Test public void setsContentType() throws Exception { this.resolver.setContentType("application/octet-stream"); - View view = this.resolver.resolveViewName("foo", null); + View view = this.resolver.resolveViewName("template", null); assertThat(view.getContentType()).isEqualTo("application/octet-stream"); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewTests.java similarity index 89% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewTests.java rename to spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewTests.java index a1801eb2cdd..c47c59f34e2 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/servlet/MustacheViewTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/view/MustacheViewTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache.servlet; +package org.springframework.boot.web.servlet.view; import java.util.Collections; @@ -37,6 +37,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class MustacheViewTests { + private final String templateUrl = "classpath:/" + + getClass().getPackage().getName().replace(".", "/") + "/template.html"; + private MockHttpServletRequest request = new MockHttpServletRequest(); private MockHttpServletResponse response = new MockHttpServletResponse(); @@ -57,9 +60,10 @@ public class MustacheViewTests { public void viewResolvesHandlebars() throws Exception { MustacheView view = new MustacheView(); view.setCompiler(Mustache.compiler()); - view.setUrl("classpath:/mustache-templates/foo.html"); + view.setUrl(this.templateUrl); view.setApplicationContext(this.context); - view.render(Collections.singletonMap("World", "Spring"), this.request, this.response); + view.render(Collections.singletonMap("World", "Spring"), this.request, + this.response); assertThat(this.response.getContentAsString()).isEqualTo("Hello Spring"); } diff --git a/spring-boot/src/test/resources/org/springframework/boot/web/reactive/result/view/template.html b/spring-boot/src/test/resources/org/springframework/boot/web/reactive/result/view/template.html new file mode 100644 index 00000000000..18624afa99b --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/web/reactive/result/view/template.html @@ -0,0 +1 @@ +Hello {{World}} \ No newline at end of file diff --git a/spring-boot/src/test/resources/org/springframework/boot/web/servlet/view/template.html b/spring-boot/src/test/resources/org/springframework/boot/web/servlet/view/template.html new file mode 100644 index 00000000000..18624afa99b --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/web/servlet/view/template.html @@ -0,0 +1 @@ +Hello {{World}} \ No newline at end of file