Merge branch '1.2.x'

pull/3370/head
Phillip Webb 10 years ago
commit df3ced1a45

@ -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<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
this.template.execute(model, response.getWriter());
if (this.template != null) {
this.template.execute(model, response.getWriter());
}
}
}

@ -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());
}
}

@ -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"));
}
}

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

@ -224,6 +224,18 @@ public class LoggingApplicationListenerTests {
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
public void parseLevelsWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "foo=TRACE",

Loading…
Cancel
Save