From 6cc272ecf5d481ba3111265d028fe6327a0718db Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 4 Oct 2018 11:51:13 +0200 Subject: [PATCH] Polish "Make sure cache busting works with error pages" Closes gh-14583 --- .../FreeMarkerServletWebConfiguration.java | 9 +++++++-- .../thymeleaf/ThymeleafAutoConfiguration.java | 9 ++++----- ...rAutoConfigurationServletIntegrationTests.java | 15 +++++++++++---- .../ThymeleafServletAutoConfigurationTests.java | 6 ++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.java index 93d76b1185be..142fd4cee6b2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.freemarker; +import javax.servlet.DispatcherType; import javax.servlet.Servlet; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -25,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; @@ -74,8 +76,11 @@ public FreeMarkerViewResolver freeMarkerViewResolver() { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledResourceChain - public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { - return new ResourceUrlEncodingFilter(); + public FilterRegistrationBean resourceUrlEncodingFilter() { + FilterRegistrationBean registration = new FilterRegistrationBean<>( + new ResourceUrlEncodingFilter()); + registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ERROR); + return registration; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index e7deacdcbe07..7475e0f45d12 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -169,12 +169,11 @@ static class ThymeleafWebMvcConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledResourceChain - public FilterRegistrationBean resourceUrlEncodingFilter() { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( + public FilterRegistrationBean resourceUrlEncodingFilter() { + FilterRegistrationBean registration = new FilterRegistrationBean<>( new ResourceUrlEncodingFilter()); - filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, - DispatcherType.ERROR); - return filterRegistrationBean; + registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ERROR); + return registration; } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationServletIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationServletIntegrationTests.java index 83df1fbb09c4..f6dad0c0d247 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationServletIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationServletIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,8 +17,10 @@ package org.springframework.boot.autoconfigure.freemarker; import java.io.StringWriter; +import java.util.EnumSet; import java.util.Locale; +import javax.servlet.DispatcherType; import javax.servlet.http.HttpServletRequest; import org.junit.After; @@ -26,6 +28,7 @@ import org.junit.Test; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; @@ -153,14 +156,18 @@ public void renderTemplate() throws Exception { @Test public void registerResourceHandlingFilterDisabledByDefault() { registerAndRefreshContext(); - assertThat(this.context.getBeansOfType(ResourceUrlEncodingFilter.class)) - .isEmpty(); + assertThat(this.context.getBeansOfType(FilterRegistrationBean.class)).isEmpty(); } @Test public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() { registerAndRefreshContext("spring.resources.chain.enabled:true"); - assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull(); + FilterRegistrationBean registration = this.context + .getBean(FilterRegistrationBean.class); + assertThat(registration.getFilter()) + .isInstanceOf(ResourceUrlEncodingFilter.class); + assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes", + EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR)); } private void registerAndRefreshContext(String... env) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index 7a226aaef6eb..07720e8823da 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -51,6 +51,7 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.support.RequestContext; import static org.assertj.core.api.Assertions.assertThat; @@ -217,7 +218,8 @@ public void registerResourceHandlingFilterOnlyIfResourceChainIsEnabled() { load(BaseConfiguration.class, "spring.resources.chain.enabled:true"); FilterRegistrationBean registration = this.context .getBean(FilterRegistrationBean.class); - assertThat(registration).isNotNull(); + assertThat(registration.getFilter()) + .isInstanceOf(ResourceUrlEncodingFilter.class); assertThat(registration).hasFieldOrPropertyWithValue("dispatcherTypes", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR)); }