diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheView.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheView.java index 8c2d72b273..4d8b9ff0f4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheView.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheView.java @@ -30,20 +30,44 @@ import com.samskivert.mustache.Template; * Spring MVC {@link View} using the Mustache template engine. * * @author Dave Syer + * @author Phillip Webb * @since 1.2.2 */ public class MustacheView extends AbstractTemplateView { - private final Template template; + private Template template; + /** + * Create a new {@link MustacheView} instance. + * @see #setTemplate(Template) + * @since 1.2.5 + */ + public MustacheView() { + } + + /** + * Create a new {@link MustacheView} with the specified template. + * @param template the source template + */ public MustacheView(Template template) { this.template = template; } + /** + * Set the Mustache template that should actually be rendered. + * @param template the mustache template + * @since 1.2.5 + */ + public void setTemplate(Template template) { + this.template = template; + } + @Override protected void renderMergedTemplateModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { - this.template.execute(model, response.getWriter()); + if (this.template != null) { + this.template.execute(model, response.getWriter()); + } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java index 022c55c8cf..da4a705ee4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.mustache.web; import java.io.IOException; import java.io.InputStreamReader; +import java.io.Reader; import java.util.Locale; import org.springframework.beans.propertyeditors.LocaleEditor; @@ -35,6 +36,7 @@ import com.samskivert.mustache.Template; * * @author Dave Syer * @author Andy Wilkinson + * @author Phillip Webb * @since 1.2.2 */ public class MustacheViewResolver extends UrlBasedViewResolver { @@ -44,7 +46,12 @@ public class MustacheViewResolver extends UrlBasedViewResolver { private String charset; public MustacheViewResolver() { - setViewClass(MustacheView.class); + setViewClass(requiredViewClass()); + } + + @Override + protected Class requiredViewClass() { + return MustacheView.class; } /** @@ -67,31 +74,15 @@ public class MustacheViewResolver extends UrlBasedViewResolver { if (resource == null) { return null; } - MustacheView view = new MustacheView(createTemplate(resource)); - view.setApplicationContext(getApplicationContext()); - view.setServletContext(getServletContext()); - return view; - } - - private Template createTemplate(Resource resource) throws IOException { - return this.charset == null ? this.compiler.compile(new InputStreamReader( - resource.getInputStream())) : this.compiler - .compile(new InputStreamReader(resource.getInputStream(), this.charset)); + MustacheView mustacheView = (MustacheView) super.loadView(viewName, locale); + mustacheView.setTemplate(createTemplate(resource)); + return mustacheView; } private Resource resolveResource(String viewName, Locale locale) { return resolveFromLocale(viewName, getLocale(locale)); } - private String getLocale(Locale locale) { - if (locale == null) { - return ""; - } - LocaleEditor localeEditor = new LocaleEditor(); - localeEditor.setValue(locale); - return "_" + localeEditor.getAsText(); - } - private Resource resolveFromLocale(String viewName, String locale) { Resource resource = getApplicationContext().getResource( getPrefix() + viewName + locale + getSuffix()); @@ -105,4 +96,24 @@ public class MustacheViewResolver extends UrlBasedViewResolver { return resource; } + private String getLocale(Locale locale) { + if (locale == null) { + return ""; + } + LocaleEditor localeEditor = new LocaleEditor(); + localeEditor.setValue(locale); + return "_" + localeEditor.getAsText(); + } + + private Template createTemplate(Resource resource) throws IOException { + return this.compiler.compile(getReader(resource)); + } + + private Reader getReader(Resource resource) throws IOException { + if (this.charset != null) { + return new InputStreamReader(resource.getInputStream(), this.charset); + } + return new InputStreamReader(resource.getInputStream()); + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java index 8df86ea06d..2d065f20eb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java @@ -20,12 +20,14 @@ import java.util.Locale; import org.junit.Before; import org.junit.Test; -import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.support.StaticWebApplicationContext; +import org.springframework.web.servlet.View; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; /** * Tests for {@link MustacheViewResolver}. @@ -74,4 +76,12 @@ public class MustacheViewResolverTests { assertNotNull(this.resolver.resolveViewName("foo", new Locale("de"))); } + @Test + public void setsContentType() throws Exception { + this.resolver.setContentType("application/octet-stream"); + View view = this.resolver.resolveViewName("foo", null); + assertThat(view.getContentType(), equalTo("application/octet-stream")); + + } + }