Skip to content

Commit 39b15af

Browse files
committed
Polish "Provide links / when using a separate management port"
See gh-17418
1 parent c108629 commit 39b15af

File tree

21 files changed

+171
-99
lines changed

21 files changed

+171
-99
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class CloudFoundryWebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointH
6060
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
6161
CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor,
6262
EndpointLinksResolver linksResolver) {
63-
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration);
63+
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, true);
6464
this.linksResolver = linksResolver;
6565
this.securityInterceptor = securityInterceptor;
6666
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryWebEndpointServletHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CloudFoundryWebEndpointServletHandlerMapping extends AbstractWebMvcEndpoin
5959
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
6060
CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor,
6161
EndpointLinksResolver linksResolver) {
62-
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration);
62+
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, true);
6363
this.securityInterceptor = securityInterceptor;
6464
this.linksResolver = linksResolver;
6565
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/jersey/JerseyWebEndpointManagementContextConfiguration.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
4545
import org.springframework.context.annotation.Bean;
4646
import org.springframework.core.env.Environment;
47+
import org.springframework.util.StringUtils;
4748

4849
/**
4950
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jersey
@@ -71,14 +72,18 @@ ResourceConfigCustomizer webEndpointRegistrar(WebEndpointsSupplier webEndpointsS
7172
return (resourceConfig) -> {
7273
JerseyEndpointResourceFactory resourceFactory = new JerseyEndpointResourceFactory();
7374
String basePath = webEndpointProperties.getBasePath();
74-
ManagementPortType type = ManagementPortType.get(environment);
75-
Boolean samePort = type == ManagementPortType.SAME;
76-
EndpointMapping endpointMapping = new EndpointMapping(basePath, samePort);
75+
EndpointMapping endpointMapping = new EndpointMapping(basePath);
7776
Collection<ExposableWebEndpoint> webEndpoints = Collections
7877
.unmodifiableCollection(webEndpointsSupplier.getEndpoints());
7978
resourceConfig.registerResources(new HashSet<>(resourceFactory.createEndpointResources(endpointMapping,
80-
webEndpoints, endpointMediaTypes, new EndpointLinksResolver(allEndpoints, basePath))));
79+
webEndpoints, endpointMediaTypes, new EndpointLinksResolver(allEndpoints, basePath),
80+
shouldRegisterLinksMapping(environment, basePath))));
8181
};
8282
}
8383

84+
private boolean shouldRegisterLinksMapping(Environment environment, String basePath) {
85+
return StringUtils.hasText(basePath)
86+
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
87+
}
88+
8489
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.context.annotation.Bean;
4444
import org.springframework.core.env.Environment;
4545
import org.springframework.http.server.reactive.HttpHandler;
46+
import org.springframework.util.StringUtils;
4647
import org.springframework.web.reactive.DispatcherHandler;
4748

4849
/**
@@ -66,16 +67,20 @@ public WebFluxEndpointHandlerMapping webEndpointReactiveHandlerMapping(WebEndpoi
6667
ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
6768
CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties,
6869
Environment environment) {
69-
ManagementPortType type = ManagementPortType.get(environment);
70-
Boolean samePort = type == ManagementPortType.SAME;
71-
EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath(), samePort);
70+
String basePath = webEndpointProperties.getBasePath();
71+
EndpointMapping endpointMapping = new EndpointMapping(basePath);
7272
Collection<ExposableWebEndpoint> endpoints = webEndpointsSupplier.getEndpoints();
7373
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
7474
allEndpoints.addAll(endpoints);
7575
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
7676
return new WebFluxEndpointHandlerMapping(endpointMapping, endpoints, endpointMediaTypes,
77-
corsProperties.toCorsConfiguration(),
78-
new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath()));
77+
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
78+
shouldRegisterLinksMapping(environment, basePath));
79+
}
80+
81+
private boolean shouldRegisterLinksMapping(Environment environment, String basePath) {
82+
return StringUtils.hasText(basePath)
83+
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
7984
}
8085

8186
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4444
import org.springframework.context.annotation.Bean;
4545
import org.springframework.core.env.Environment;
46+
import org.springframework.util.StringUtils;
4647
import org.springframework.web.servlet.DispatcherServlet;
4748

4849
/**
@@ -71,12 +72,13 @@ public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpoint
7172
allEndpoints.addAll(webEndpoints);
7273
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
7374
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
74-
ManagementPortType type = ManagementPortType.get(environment);
75-
Boolean samePort = type == ManagementPortType.SAME;
76-
EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath(), samePort);
75+
String basePath = webEndpointProperties.getBasePath();
76+
EndpointMapping endpointMapping = new EndpointMapping(basePath);
77+
boolean shouldRegisterLinksMapping = StringUtils.hasText(basePath)
78+
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
7779
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
78-
corsProperties.toCorsConfiguration(),
79-
new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath()));
80+
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
81+
shouldRegisterLinksMapping);
8082
}
8183

8284
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/JerseyEndpointRequestIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void customize(ResourceConfig config) {
133133
Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList());
134134
Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources(
135135
new EndpointMapping("/actuator"), discoverer.getEndpoints(), endpointMediaTypes,
136-
new EndpointLinksResolver(discoverer.getEndpoints()));
136+
new EndpointLinksResolver(discoverer.getEndpoints()), true);
137137
config.registerResources(new HashSet<>(resources));
138138
}
139139

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/MvcEndpointRequestIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping() {
119119
new ConversionServiceParameterValueMapper(), endpointMediaTypes,
120120
Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList());
121121
return new WebMvcEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(),
122-
endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints()));
122+
endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints()),
123+
true);
123124
}
124125

125126
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointMapping.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,12 @@ public class EndpointMapping {
2828

2929
private final String path;
3030

31-
private final Boolean samePort;
32-
3331
/**
3432
* Creates a new {@code EndpointMapping} using the given {@code path}.
3533
* @param path the path
36-
* @param samePort states true or false for same port as server
3734
*/
38-
public EndpointMapping(String path, Boolean samePort) {
39-
this.path = normalizePath(path);
40-
this.samePort = samePort;
41-
}
42-
4335
public EndpointMapping(String path) {
44-
this(path, true);
36+
this.path = normalizePath(path);
4537
}
4638

4739
/**
@@ -52,10 +44,6 @@ public String getPath() {
5244
return this.path;
5345
}
5446

55-
public boolean isSamePort() {
56-
return this.samePort;
57-
}
58-
5947
public String createSubPath(String path) {
6048
return this.path + normalizePath(path);
6149
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/jersey/JerseyEndpointResourceFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ public class JerseyEndpointResourceFactory {
7171
* @param endpoints the web endpoints
7272
* @param endpointMediaTypes media types consumed and produced by the endpoints
7373
* @param linksResolver resolver for determining links to available endpoints
74+
* @param shouldRegisterLinks should register links
7475
* @return the resources for the operations
7576
*/
7677
public Collection<Resource> createEndpointResources(EndpointMapping endpointMapping,
7778
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
78-
EndpointLinksResolver linksResolver) {
79+
EndpointLinksResolver linksResolver, boolean shouldRegisterLinks) {
7980
List<Resource> resources = new ArrayList<>();
8081
endpoints.stream().flatMap((endpoint) -> endpoint.getOperations().stream())
8182
.map((operation) -> createResource(endpointMapping, operation)).forEach(resources::add);
82-
if (StringUtils.hasText(endpointMapping.getPath()) || !endpointMapping.isSamePort()) {
83+
if (shouldRegisterLinks) {
8384
Resource resource = createEndpointLinksResource(endpointMapping.getPath(), endpointMediaTypes,
8485
linksResolver);
8586
resources.add(resource);

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/reactive/AbstractWebFluxEndpointHandlerMapping.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,25 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
9595
private final Method handleReadMethod = ReflectionUtils.findMethod(ReadOperationHandler.class, "handle",
9696
ServerWebExchange.class);
9797

98+
private final boolean shouldRegisterLinksMapping;
99+
98100
/**
99101
* Creates a new {@code AbstractWebFluxEndpointHandlerMapping} that provides mappings
100102
* for the operations of the given {@code webEndpoints}.
101103
* @param endpointMapping the base mapping for all endpoints
102104
* @param endpoints the web endpoints
103105
* @param endpointMediaTypes media types consumed and produced by the endpoints
104106
* @param corsConfiguration the CORS configuration for the endpoints
107+
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
105108
*/
106109
public AbstractWebFluxEndpointHandlerMapping(EndpointMapping endpointMapping,
107110
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
108-
CorsConfiguration corsConfiguration) {
111+
CorsConfiguration corsConfiguration, boolean shouldRegisterLinksMapping) {
109112
this.endpointMapping = endpointMapping;
110113
this.endpoints = endpoints;
111114
this.endpointMediaTypes = endpointMediaTypes;
112115
this.corsConfiguration = corsConfiguration;
116+
this.shouldRegisterLinksMapping = shouldRegisterLinksMapping;
113117
setOrder(-100);
114118
}
115119

@@ -120,7 +124,7 @@ protected void initHandlerMethods() {
120124
registerMappingForOperation(endpoint, operation);
121125
}
122126
}
123-
if (StringUtils.hasText(this.endpointMapping.getPath()) || !this.endpointMapping.isSamePort()) {
127+
if (this.shouldRegisterLinksMapping) {
124128
registerLinksMapping();
125129
}
126130
}

0 commit comments

Comments
 (0)