From dc4dbd1bf2c4568cf18e29df551fe215fba35d53 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 17 Feb 2025 14:34:04 +0100 Subject: [PATCH] Rename management.server.accesslog.prefix Closes gh-44196 --- ...veManagementChildContextConfiguration.java | 28 ++++++----- .../server/ManagementServerProperties.java | 49 +++++++++++++++++-- ...etManagementChildContextConfiguration.java | 28 ++++++----- ...agementChildContextConfigurationTests.java | 49 +++++++++++++++++++ .../ManagementServerPropertiesTests.java | 5 +- ...agementChildContextConfigurationTests.java | 49 +++++++++++++++++++ 6 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java index 0631b84119..36dd324bcf 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java @@ -56,6 +56,7 @@ import org.springframework.web.server.adapter.WebHttpHandlerBuilder; * * @author Andy Wilkinson * @author Phillip Webb + * @author Moritz Halbritter * @since 2.0.0 */ @EnableWebFlux @@ -99,18 +100,23 @@ public class ReactiveManagementChildContextConfiguration { abstract static class AccessLogCustomizer implements Ordered { - private final ManagementServerProperties properties; + private final String prefix; - AccessLogCustomizer(ManagementServerProperties properties) { - this.properties = properties; + AccessLogCustomizer(String prefix) { + this.prefix = prefix; } - protected String customizePrefix(String prefix) { - prefix = (prefix != null) ? prefix : ""; - if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) { - return prefix; + protected String customizePrefix(String existingPrefix) { + if (this.prefix == null) { + return existingPrefix; } - return this.properties.getAccesslog().getPrefix() + prefix; + if (existingPrefix == null) { + return this.prefix; + } + if (existingPrefix.startsWith(this.prefix)) { + return existingPrefix; + } + return this.prefix + existingPrefix; } @Override @@ -124,7 +130,7 @@ public class ReactiveManagementChildContextConfiguration { implements WebServerFactoryCustomizer { TomcatAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getTomcat().getAccesslog().getPrefix()); } @Override @@ -151,7 +157,7 @@ public class ReactiveManagementChildContextConfiguration { implements WebServerFactoryCustomizer { UndertowAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getUndertow().getAccesslog().getPrefix()); } @Override @@ -165,7 +171,7 @@ public class ReactiveManagementChildContextConfiguration { implements WebServerFactoryCustomizer { JettyAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getJetty().getAccesslog().getPrefix()); } @Override diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java index 9fe8f639e4..c5e7ca7321 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java @@ -30,6 +30,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Stephane Nicoll * @author Vedran Pavic + * @author Moritz Halbritter * @since 2.0.0 * @see ServerProperties */ @@ -57,7 +58,11 @@ public class ManagementServerProperties { @NestedConfigurationProperty private Ssl ssl; - private final Accesslog accesslog = new Accesslog(); + private final Jetty jetty = new Jetty(); + + private final Tomcat tomcat = new Tomcat(); + + private final Undertow undertow = new Undertow(); /** * Returns the management port or {@code null} if the @@ -103,6 +108,18 @@ public class ManagementServerProperties { this.ssl = ssl; } + public Jetty getJetty() { + return this.jetty; + } + + public Tomcat getTomcat() { + return this.tomcat; + } + + public Undertow getUndertow() { + return this.undertow; + } + private String cleanBasePath(String basePath) { String candidate = null; if (StringUtils.hasLength(basePath)) { @@ -119,8 +136,34 @@ public class ManagementServerProperties { return candidate; } - public Accesslog getAccesslog() { - return this.accesslog; + public static class Jetty { + + private final Accesslog accesslog = new Accesslog(); + + public Accesslog getAccesslog() { + return this.accesslog; + } + + } + + public static class Tomcat { + + private final Accesslog accesslog = new Accesslog(); + + public Accesslog getAccesslog() { + return this.accesslog; + } + + } + + public static class Undertow { + + private final Accesslog accesslog = new Accesslog(); + + public Accesslog getAccesslog() { + return this.accesslog; + } + } public static class Accesslog { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java index fa5cb85249..fefe79cb1a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java @@ -63,6 +63,7 @@ import org.springframework.util.StringUtils; * @author Andy Wilkinson * @author EddĂș MelĂ©ndez * @author Phillip Webb + * @author Moritz Halbritter */ @ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SERVLET) @@ -136,18 +137,23 @@ class ServletManagementChildContextConfiguration { abstract static class AccessLogCustomizer implements Ordered { - private final ManagementServerProperties properties; + private final String prefix; - AccessLogCustomizer(ManagementServerProperties properties) { - this.properties = properties; + AccessLogCustomizer(String prefix) { + this.prefix = prefix; } - protected String customizePrefix(String prefix) { - prefix = (prefix != null) ? prefix : ""; - if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) { - return prefix; + protected String customizePrefix(String existingPrefix) { + if (this.prefix == null) { + return existingPrefix; } - return this.properties.getAccesslog().getPrefix() + prefix; + if (existingPrefix == null) { + return this.prefix; + } + if (existingPrefix.startsWith(this.prefix)) { + return existingPrefix; + } + return this.prefix + existingPrefix; } @Override @@ -161,7 +167,7 @@ class ServletManagementChildContextConfiguration { implements WebServerFactoryCustomizer { TomcatAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getTomcat().getAccesslog().getPrefix()); } @Override @@ -188,7 +194,7 @@ class ServletManagementChildContextConfiguration { implements WebServerFactoryCustomizer { UndertowAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getUndertow().getAccesslog().getPrefix()); } @Override @@ -202,7 +208,7 @@ class ServletManagementChildContextConfiguration { implements WebServerFactoryCustomizer { JettyAccessLogCustomizer(ManagementServerProperties properties) { - super(properties); + super(properties.getJetty().getAccesslog().getPrefix()); } @Override diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java new file mode 100644 index 0000000000..7bfbd7153a --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2025 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.web.reactive; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration.AccessLogCustomizer; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ReactiveManagementChildContextConfiguration}. + * + * @author Moritz Halbritter + */ +class ReactiveManagementChildContextConfigurationTests { + + @Test + void accessLogCustomizer() { + AccessLogCustomizer customizer = new AccessLogCustomizer("prefix") { + }; + assertThat(customizer.customizePrefix(null)).isEqualTo("prefix"); + assertThat(customizer.customizePrefix("existing")).isEqualTo("prefixexisting"); + assertThat(customizer.customizePrefix("prefixexisting")).isEqualTo("prefixexisting"); + } + + @Test + void accessLogCustomizerWithNullPrefix() { + AccessLogCustomizer customizer = new AccessLogCustomizer(null) { + }; + assertThat(customizer.customizePrefix(null)).isEqualTo(null); + assertThat(customizer.customizePrefix("existing")).isEqualTo("existing"); + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java index 37d46b3db9..46edd410c2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java @@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Stephane Nicoll + * @author Moritz Halbritter */ class ManagementServerPropertiesTests { @@ -71,7 +72,9 @@ class ManagementServerPropertiesTests { @Test void accessLogsArePrefixedByDefault() { ManagementServerProperties properties = new ManagementServerProperties(); - assertThat(properties.getAccesslog().getPrefix()).isEqualTo("management_"); + assertThat(properties.getTomcat().getAccesslog().getPrefix()).isEqualTo("management_"); + assertThat(properties.getJetty().getAccesslog().getPrefix()).isEqualTo("management_"); + assertThat(properties.getUndertow().getAccesslog().getPrefix()).isEqualTo("management_"); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java new file mode 100644 index 0000000000..a46dca62ca --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2025 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.web.servlet; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration.AccessLogCustomizer; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ServletManagementChildContextConfiguration}. + * + * @author Moritz Halbritter + */ +class ServletManagementChildContextConfigurationTests { + + @Test + void accessLogCustomizer() { + AccessLogCustomizer customizer = new AccessLogCustomizer("prefix") { + }; + assertThat(customizer.customizePrefix(null)).isEqualTo("prefix"); + assertThat(customizer.customizePrefix("existing")).isEqualTo("prefixexisting"); + assertThat(customizer.customizePrefix("prefixexisting")).isEqualTo("prefixexisting"); + } + + @Test + void accessLogCustomizerWithNullPrefix() { + AccessLogCustomizer customizer = new AccessLogCustomizer(null) { + }; + assertThat(customizer.customizePrefix(null)).isEqualTo(null); + assertThat(customizer.customizePrefix("existing")).isEqualTo("existing"); + } + +}