Auto-configure ResourceUrlEncodingFilter when using FreeMarker
Closes gh-5126
This commit is contained in:
committed by
Andy Wilkinson
parent
1f106ddf8c
commit
4e177eeed7
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebAppli
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.template.TemplateLocation;
|
||||
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -42,6 +43,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
|
||||
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
@@ -51,6 +53,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Dave Syer
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -149,5 +152,12 @@ public class FreeMarkerAutoConfiguration {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledResourceChain
|
||||
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
|
||||
return new ResourceUrlEncodingFilter();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
@@ -36,6 +36,7 @@ import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
@@ -48,6 +49,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||
* Tests for {@link FreeMarkerAutoConfiguration}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Kazuki Shimizu
|
||||
*/
|
||||
public class FreeMarkerAutoConfigurationTests {
|
||||
|
||||
@@ -189,6 +191,20 @@ public class FreeMarkerAutoConfigurationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerResourceHandlingFilterDisabledByDefault() throws Exception {
|
||||
registerAndRefreshContext();
|
||||
assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class))
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled()
|
||||
throws Exception {
|
||||
registerAndRefreshContext("spring.resources.chain.enabled:true");
|
||||
assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull();
|
||||
}
|
||||
|
||||
private void registerAndRefreshContext(String... env) {
|
||||
EnvironmentTestUtils.addEnvironment(this.context, env);
|
||||
this.context.register(FreeMarkerAutoConfiguration.class);
|
||||
|
||||
Reference in New Issue
Block a user