Merge branch '1.2.x'

This commit is contained in:
Phillip Webb 2015-06-30 15:04:06 -07:00
commit df3ced1a45
5 changed files with 81 additions and 24 deletions

View File

@ -30,20 +30,44 @@ import com.samskivert.mustache.Template;
* Spring MVC {@link View} using the Mustache template engine. * Spring MVC {@link View} using the Mustache template engine.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb
* @since 1.2.2 * @since 1.2.2
*/ */
public class MustacheView extends AbstractTemplateView { 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) { public MustacheView(Template template) {
this.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 @Override
protected void renderMergedTemplateModel(Map<String, Object> model, protected void renderMergedTemplateModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletRequest request, HttpServletResponse response) throws Exception {
if (this.template != null) {
this.template.execute(model, response.getWriter()); this.template.execute(model, response.getWriter());
} }
}
} }

View File

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.mustache.web;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Locale; import java.util.Locale;
import org.springframework.beans.propertyeditors.LocaleEditor; import org.springframework.beans.propertyeditors.LocaleEditor;
@ -35,6 +36,7 @@ import com.samskivert.mustache.Template;
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Phillip Webb
* @since 1.2.2 * @since 1.2.2
*/ */
public class MustacheViewResolver extends UrlBasedViewResolver { public class MustacheViewResolver extends UrlBasedViewResolver {
@ -44,7 +46,12 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
private String charset; private String charset;
public MustacheViewResolver() { 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) { if (resource == null) {
return null; return null;
} }
MustacheView view = new MustacheView(createTemplate(resource)); MustacheView mustacheView = (MustacheView) super.loadView(viewName, locale);
view.setApplicationContext(getApplicationContext()); mustacheView.setTemplate(createTemplate(resource));
view.setServletContext(getServletContext()); return mustacheView;
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));
} }
private Resource resolveResource(String viewName, Locale locale) { private Resource resolveResource(String viewName, Locale locale) {
return resolveFromLocale(viewName, getLocale(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) { private Resource resolveFromLocale(String viewName, String locale) {
Resource resource = getApplicationContext().getResource( Resource resource = getApplicationContext().getResource(
getPrefix() + viewName + locale + getSuffix()); getPrefix() + viewName + locale + getSuffix());
@ -105,4 +96,24 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
return resource; 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());
}
} }

View File

@ -20,12 +20,14 @@ import java.util.Locale;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.StaticWebApplicationContext; 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.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link MustacheViewResolver}. * Tests for {@link MustacheViewResolver}.
@ -74,4 +76,12 @@ public class MustacheViewResolverTests {
assertNotNull(this.resolver.resolveViewName("foo", new Locale("de"))); 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"));
}
} }

View File

@ -252,7 +252,7 @@ public class LoggingApplicationListener implements GenericApplicationListener {
name = null; name = null;
} }
level = environment.resolvePlaceholders(level); level = environment.resolvePlaceholders(level);
system.setLogLevel(name, LogLevel.valueOf(level)); system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase()));
} }
catch (RuntimeException ex) { catch (RuntimeException ex) {
this.logger.error("Cannot set level: " + level + " for '" + name + "'"); this.logger.error("Cannot set level: " + level + " for '" + name + "'");

View File

@ -224,6 +224,18 @@ public class LoggingApplicationListenerTests {
assertThat(this.outputCapture.toString(), containsString("testattrace")); assertThat(this.outputCapture.toString(), containsString("testattrace"));
} }
@Test
public void parseLevelsCaseInsensitive() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.level.org.springframework.boot=TrAcE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
this.logger.trace("testattrace");
assertThat(this.outputCapture.toString(), containsString("testatdebug"));
assertThat(this.outputCapture.toString(), containsString("testattrace"));
}
@Test @Test
public void parseLevelsWithPlaceholder() throws Exception { public void parseLevelsWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "foo=TRACE", EnvironmentTestUtils.addEnvironment(this.context, "foo=TRACE",