From c219f5e57fd3992fc6047c6ea608be2c4951e206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 12 Nov 2015 17:17:28 -0500 Subject: [PATCH 1/2] Don't create ApplicationContextHeaderFilter when header is disabled See gh-4320 Closes gh-4454 --- .../actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java | 3 +++ .../autoconfigure/EndpointWebMvcAutoConfigurationTests.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index 1b86940cc90..9024aa7027c 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -42,6 +42,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; @@ -87,6 +88,7 @@ import org.springframework.web.servlet.DispatcherServlet; * @author Christian Dupuis * @author Andy Wilkinson * @author Johannes Edmeier + * @author Eddú Meléndez */ @Configuration @ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) @@ -198,6 +200,7 @@ public class EndpointWebMvcAutoConfiguration // Put Servlets and Filters in their own nested class so they don't force early // instantiation of ManagementServerProperties. @Configuration + @ConditionalOnProperty(prefix = "management", name = "addApplicationContextHeader", matchIfMissing = true, havingValue = "true") protected static class ApplicationContextFilterConfiguration { @Bean diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java index b99db580a9c..f920953f4ea 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java @@ -98,6 +98,7 @@ import static org.mockito.Mockito.mock; * @author Phillip Webb * @author Greg Turnquist * @author Andy Wilkinson + * @author Eddú Meléndez */ public class EndpointWebMvcAutoConfigurationTests { @@ -136,6 +137,7 @@ public class EndpointWebMvcAutoConfigurationTests { assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, null); assertTrue(hasHeader("/endpoint", ports.get().server, "X-Application-Context")); + assertTrue(this.applicationContext.containsBean("applicationContextIdFilter")); this.applicationContext.close(); assertAllClosed(); } @@ -149,6 +151,7 @@ public class EndpointWebMvcAutoConfigurationTests { EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertFalse(hasHeader("/endpoint", ports.get().server, "X-Application-Context")); + assertFalse(this.applicationContext.containsBean("applicationContextIdFilter")); this.applicationContext.close(); assertAllClosed(); } From f5b973a242e435987a4cdb1c20d7afff6ddb41c6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 15 Jan 2016 17:44:04 +0000 Subject: [PATCH 2/2] Polish contribution - Update copyright headers - Use kebab-case for the property name - Set the header unconditionally when the filter's registered Closes gh-4320 --- .../EndpointWebMvcAutoConfiguration.java | 15 +++------------ .../EndpointWebMvcAutoConfigurationTests.java | 2 +- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index 9024aa7027c..1687b696c47 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -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. @@ -200,7 +200,7 @@ public class EndpointWebMvcAutoConfiguration // Put Servlets and Filters in their own nested class so they don't force early // instantiation of ManagementServerProperties. @Configuration - @ConditionalOnProperty(prefix = "management", name = "addApplicationContextHeader", matchIfMissing = true, havingValue = "true") + @ConditionalOnProperty(prefix = "management", name = "add-application-context-header", matchIfMissing = true, havingValue = "true") protected static class ApplicationContextFilterConfiguration { @Bean @@ -225,8 +225,6 @@ public class EndpointWebMvcAutoConfiguration private final ApplicationContext applicationContext; - private ManagementServerProperties properties; - ApplicationContextHeaderFilter(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @@ -235,14 +233,7 @@ public class EndpointWebMvcAutoConfiguration protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - if (this.properties == null) { - this.properties = this.applicationContext - .getBean(ManagementServerProperties.class); - } - if (this.properties.getAddApplicationContextHeader()) { - response.addHeader("X-Application-Context", - this.applicationContext.getId()); - } + response.addHeader("X-Application-Context", this.applicationContext.getId()); filterChain.doFilter(request, response); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java index f920953f4ea..f43a17bb266 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java @@ -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.