Commit 39b15af5 authored by Madhura Bhave's avatar Madhura Bhave

Polish "Provide links / when using a separate management port"

See gh-17418
parent c1086293
...@@ -60,7 +60,7 @@ class CloudFoundryWebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointH ...@@ -60,7 +60,7 @@ class CloudFoundryWebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointH
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes, Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor, CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor,
EndpointLinksResolver linksResolver) { EndpointLinksResolver linksResolver) {
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration); super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, true);
this.linksResolver = linksResolver; this.linksResolver = linksResolver;
this.securityInterceptor = securityInterceptor; this.securityInterceptor = securityInterceptor;
} }
......
...@@ -59,7 +59,7 @@ class CloudFoundryWebEndpointServletHandlerMapping extends AbstractWebMvcEndpoin ...@@ -59,7 +59,7 @@ class CloudFoundryWebEndpointServletHandlerMapping extends AbstractWebMvcEndpoin
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes, Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor, CorsConfiguration corsConfiguration, CloudFoundrySecurityInterceptor securityInterceptor,
EndpointLinksResolver linksResolver) { EndpointLinksResolver linksResolver) {
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration); super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, true);
this.securityInterceptor = securityInterceptor; this.securityInterceptor = securityInterceptor;
this.linksResolver = linksResolver; this.linksResolver = linksResolver;
} }
......
...@@ -44,6 +44,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat ...@@ -44,6 +44,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer; import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/** /**
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jersey * {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jersey
...@@ -71,14 +72,18 @@ class JerseyWebEndpointManagementContextConfiguration { ...@@ -71,14 +72,18 @@ class JerseyWebEndpointManagementContextConfiguration {
return (resourceConfig) -> { return (resourceConfig) -> {
JerseyEndpointResourceFactory resourceFactory = new JerseyEndpointResourceFactory(); JerseyEndpointResourceFactory resourceFactory = new JerseyEndpointResourceFactory();
String basePath = webEndpointProperties.getBasePath(); String basePath = webEndpointProperties.getBasePath();
ManagementPortType type = ManagementPortType.get(environment); EndpointMapping endpointMapping = new EndpointMapping(basePath);
Boolean samePort = type == ManagementPortType.SAME;
EndpointMapping endpointMapping = new EndpointMapping(basePath, samePort);
Collection<ExposableWebEndpoint> webEndpoints = Collections Collection<ExposableWebEndpoint> webEndpoints = Collections
.unmodifiableCollection(webEndpointsSupplier.getEndpoints()); .unmodifiableCollection(webEndpointsSupplier.getEndpoints());
resourceConfig.registerResources(new HashSet<>(resourceFactory.createEndpointResources(endpointMapping, resourceConfig.registerResources(new HashSet<>(resourceFactory.createEndpointResources(endpointMapping,
webEndpoints, endpointMediaTypes, new EndpointLinksResolver(allEndpoints, basePath)))); webEndpoints, endpointMediaTypes, new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping(environment, basePath))));
}; };
} }
private boolean shouldRegisterLinksMapping(Environment environment, String basePath) {
return StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
}
} }
...@@ -43,6 +43,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties ...@@ -43,6 +43,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.DispatcherHandler;
/** /**
...@@ -66,16 +67,20 @@ public class WebFluxEndpointManagementContextConfiguration { ...@@ -66,16 +67,20 @@ public class WebFluxEndpointManagementContextConfiguration {
ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties,
Environment environment) { Environment environment) {
ManagementPortType type = ManagementPortType.get(environment); String basePath = webEndpointProperties.getBasePath();
Boolean samePort = type == ManagementPortType.SAME; EndpointMapping endpointMapping = new EndpointMapping(basePath);
EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath(), samePort);
Collection<ExposableWebEndpoint> endpoints = webEndpointsSupplier.getEndpoints(); Collection<ExposableWebEndpoint> endpoints = webEndpointsSupplier.getEndpoints();
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>(); List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
allEndpoints.addAll(endpoints); allEndpoints.addAll(endpoints);
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
return new WebFluxEndpointHandlerMapping(endpointMapping, endpoints, endpointMediaTypes, return new WebFluxEndpointHandlerMapping(endpointMapping, endpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(), corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath())); shouldRegisterLinksMapping(environment, basePath));
}
private boolean shouldRegisterLinksMapping(Environment environment, String basePath) {
return StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
} }
@Bean @Bean
......
...@@ -43,6 +43,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat ...@@ -43,6 +43,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
/** /**
...@@ -71,12 +72,13 @@ public class WebMvcEndpointManagementContextConfiguration { ...@@ -71,12 +72,13 @@ public class WebMvcEndpointManagementContextConfiguration {
allEndpoints.addAll(webEndpoints); allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
ManagementPortType type = ManagementPortType.get(environment); String basePath = webEndpointProperties.getBasePath();
Boolean samePort = type == ManagementPortType.SAME; EndpointMapping endpointMapping = new EndpointMapping(basePath);
EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath(), samePort); boolean shouldRegisterLinksMapping = StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(), corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath())); shouldRegisterLinksMapping);
} }
@Bean @Bean
......
...@@ -133,7 +133,7 @@ class JerseyEndpointRequestIntegrationTests extends AbstractEndpointRequestInteg ...@@ -133,7 +133,7 @@ class JerseyEndpointRequestIntegrationTests extends AbstractEndpointRequestInteg
Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList()); Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList());
Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources( Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources(
new EndpointMapping("/actuator"), discoverer.getEndpoints(), endpointMediaTypes, new EndpointMapping("/actuator"), discoverer.getEndpoints(), endpointMediaTypes,
new EndpointLinksResolver(discoverer.getEndpoints())); new EndpointLinksResolver(discoverer.getEndpoints()), true);
config.registerResources(new HashSet<>(resources)); config.registerResources(new HashSet<>(resources));
} }
......
...@@ -119,7 +119,8 @@ class MvcEndpointRequestIntegrationTests extends AbstractEndpointRequestIntegrat ...@@ -119,7 +119,8 @@ class MvcEndpointRequestIntegrationTests extends AbstractEndpointRequestIntegrat
new ConversionServiceParameterValueMapper(), endpointMediaTypes, new ConversionServiceParameterValueMapper(), endpointMediaTypes,
Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList()); Arrays.asList(EndpointId::toString), Collections.emptyList(), Collections.emptyList());
return new WebMvcEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(), return new WebMvcEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(),
endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints())); endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints()),
true);
} }
} }
......
...@@ -28,20 +28,12 @@ public class EndpointMapping { ...@@ -28,20 +28,12 @@ public class EndpointMapping {
private final String path; private final String path;
private final Boolean samePort;
/** /**
* Creates a new {@code EndpointMapping} using the given {@code path}. * Creates a new {@code EndpointMapping} using the given {@code path}.
* @param path the path * @param path the path
* @param samePort states true or false for same port as server
*/ */
public EndpointMapping(String path, Boolean samePort) {
this.path = normalizePath(path);
this.samePort = samePort;
}
public EndpointMapping(String path) { public EndpointMapping(String path) {
this(path, true); this.path = normalizePath(path);
} }
/** /**
...@@ -52,10 +44,6 @@ public class EndpointMapping { ...@@ -52,10 +44,6 @@ public class EndpointMapping {
return this.path; return this.path;
} }
public boolean isSamePort() {
return this.samePort;
}
public String createSubPath(String path) { public String createSubPath(String path) {
return this.path + normalizePath(path); return this.path + normalizePath(path);
} }
......
...@@ -71,15 +71,16 @@ public class JerseyEndpointResourceFactory { ...@@ -71,15 +71,16 @@ public class JerseyEndpointResourceFactory {
* @param endpoints the web endpoints * @param endpoints the web endpoints
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param linksResolver resolver for determining links to available endpoints * @param linksResolver resolver for determining links to available endpoints
* @param shouldRegisterLinks should register links
* @return the resources for the operations * @return the resources for the operations
*/ */
public Collection<Resource> createEndpointResources(EndpointMapping endpointMapping, public Collection<Resource> createEndpointResources(EndpointMapping endpointMapping,
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes, Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
EndpointLinksResolver linksResolver) { EndpointLinksResolver linksResolver, boolean shouldRegisterLinks) {
List<Resource> resources = new ArrayList<>(); List<Resource> resources = new ArrayList<>();
endpoints.stream().flatMap((endpoint) -> endpoint.getOperations().stream()) endpoints.stream().flatMap((endpoint) -> endpoint.getOperations().stream())
.map((operation) -> createResource(endpointMapping, operation)).forEach(resources::add); .map((operation) -> createResource(endpointMapping, operation)).forEach(resources::add);
if (StringUtils.hasText(endpointMapping.getPath()) || !endpointMapping.isSamePort()) { if (shouldRegisterLinks) {
Resource resource = createEndpointLinksResource(endpointMapping.getPath(), endpointMediaTypes, Resource resource = createEndpointLinksResource(endpointMapping.getPath(), endpointMediaTypes,
linksResolver); linksResolver);
resources.add(resource); resources.add(resource);
......
...@@ -95,6 +95,8 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi ...@@ -95,6 +95,8 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
private final Method handleReadMethod = ReflectionUtils.findMethod(ReadOperationHandler.class, "handle", private final Method handleReadMethod = ReflectionUtils.findMethod(ReadOperationHandler.class, "handle",
ServerWebExchange.class); ServerWebExchange.class);
private final boolean shouldRegisterLinksMapping;
/** /**
* Creates a new {@code AbstractWebFluxEndpointHandlerMapping} that provides mappings * Creates a new {@code AbstractWebFluxEndpointHandlerMapping} that provides mappings
* for the operations of the given {@code webEndpoints}. * for the operations of the given {@code webEndpoints}.
...@@ -102,14 +104,16 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi ...@@ -102,14 +104,16 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
* @param endpoints the web endpoints * @param endpoints the web endpoints
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param corsConfiguration the CORS configuration for the endpoints * @param corsConfiguration the CORS configuration for the endpoints
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
*/ */
public AbstractWebFluxEndpointHandlerMapping(EndpointMapping endpointMapping, public AbstractWebFluxEndpointHandlerMapping(EndpointMapping endpointMapping,
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes, Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
CorsConfiguration corsConfiguration) { CorsConfiguration corsConfiguration, boolean shouldRegisterLinksMapping) {
this.endpointMapping = endpointMapping; this.endpointMapping = endpointMapping;
this.endpoints = endpoints; this.endpoints = endpoints;
this.endpointMediaTypes = endpointMediaTypes; this.endpointMediaTypes = endpointMediaTypes;
this.corsConfiguration = corsConfiguration; this.corsConfiguration = corsConfiguration;
this.shouldRegisterLinksMapping = shouldRegisterLinksMapping;
setOrder(-100); setOrder(-100);
} }
...@@ -120,7 +124,7 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi ...@@ -120,7 +124,7 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
registerMappingForOperation(endpoint, operation); registerMappingForOperation(endpoint, operation);
} }
} }
if (StringUtils.hasText(this.endpointMapping.getPath()) || !this.endpointMapping.isSamePort()) { if (this.shouldRegisterLinksMapping) {
registerLinksMapping(); registerLinksMapping();
} }
} }
......
...@@ -53,11 +53,12 @@ public class WebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointHandle ...@@ -53,11 +53,12 @@ public class WebFluxEndpointHandlerMapping extends AbstractWebFluxEndpointHandle
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param corsConfiguration the CORS configuration for the endpoints or {@code null} * @param corsConfiguration the CORS configuration for the endpoints or {@code null}
* @param linksResolver resolver for determining links to available endpoints * @param linksResolver resolver for determining links to available endpoints
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
*/ */
public WebFluxEndpointHandlerMapping(EndpointMapping endpointMapping, Collection<ExposableWebEndpoint> endpoints, public WebFluxEndpointHandlerMapping(EndpointMapping endpointMapping, Collection<ExposableWebEndpoint> endpoints,
EndpointMediaTypes endpointMediaTypes, CorsConfiguration corsConfiguration, EndpointMediaTypes endpointMediaTypes, CorsConfiguration corsConfiguration,
EndpointLinksResolver linksResolver) { EndpointLinksResolver linksResolver, boolean shouldRegisterLinksMapping) {
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration); super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, shouldRegisterLinksMapping);
this.linksResolver = linksResolver; this.linksResolver = linksResolver;
setOrder(-100); setOrder(-100);
} }
......
...@@ -81,6 +81,8 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin ...@@ -81,6 +81,8 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
private final CorsConfiguration corsConfiguration; private final CorsConfiguration corsConfiguration;
private final boolean shouldRegisterLinksMapping;
private final Method handleMethod = ReflectionUtils.findMethod(OperationHandler.class, "handle", private final Method handleMethod = ReflectionUtils.findMethod(OperationHandler.class, "handle",
HttpServletRequest.class, Map.class); HttpServletRequest.class, Map.class);
...@@ -92,10 +94,12 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin ...@@ -92,10 +94,12 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
* @param endpointMapping the base mapping for all endpoints * @param endpointMapping the base mapping for all endpoints
* @param endpoints the web endpoints * @param endpoints the web endpoints
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
*/ */
public AbstractWebMvcEndpointHandlerMapping(EndpointMapping endpointMapping, public AbstractWebMvcEndpointHandlerMapping(EndpointMapping endpointMapping,
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes) { Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
this(endpointMapping, endpoints, endpointMediaTypes, null); boolean shouldRegisterLinksMapping) {
this(endpointMapping, endpoints, endpointMediaTypes, null, shouldRegisterLinksMapping);
} }
/** /**
...@@ -105,14 +109,16 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin ...@@ -105,14 +109,16 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
* @param endpoints the web endpoints * @param endpoints the web endpoints
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param corsConfiguration the CORS configuration for the endpoints or {@code null} * @param corsConfiguration the CORS configuration for the endpoints or {@code null}
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
*/ */
public AbstractWebMvcEndpointHandlerMapping(EndpointMapping endpointMapping, public AbstractWebMvcEndpointHandlerMapping(EndpointMapping endpointMapping,
Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes, Collection<ExposableWebEndpoint> endpoints, EndpointMediaTypes endpointMediaTypes,
CorsConfiguration corsConfiguration) { CorsConfiguration corsConfiguration, boolean shouldRegisterLinksMapping) {
this.endpointMapping = endpointMapping; this.endpointMapping = endpointMapping;
this.endpoints = endpoints; this.endpoints = endpoints;
this.endpointMediaTypes = endpointMediaTypes; this.endpointMediaTypes = endpointMediaTypes;
this.corsConfiguration = corsConfiguration; this.corsConfiguration = corsConfiguration;
this.shouldRegisterLinksMapping = shouldRegisterLinksMapping;
setOrder(-100); setOrder(-100);
} }
...@@ -123,7 +129,7 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin ...@@ -123,7 +129,7 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
registerMappingForOperation(endpoint, operation); registerMappingForOperation(endpoint, operation);
} }
} }
if (StringUtils.hasText(this.endpointMapping.getPath()) || !this.endpointMapping.isSamePort()) { if (this.shouldRegisterLinksMapping) {
registerLinksMapping(); registerLinksMapping();
} }
} }
......
...@@ -52,11 +52,12 @@ public class WebMvcEndpointHandlerMapping extends AbstractWebMvcEndpointHandlerM ...@@ -52,11 +52,12 @@ public class WebMvcEndpointHandlerMapping extends AbstractWebMvcEndpointHandlerM
* @param endpointMediaTypes media types consumed and produced by the endpoints * @param endpointMediaTypes media types consumed and produced by the endpoints
* @param corsConfiguration the CORS configuration for the endpoints or {@code null} * @param corsConfiguration the CORS configuration for the endpoints or {@code null}
* @param linksResolver resolver for determining links to available endpoints * @param linksResolver resolver for determining links to available endpoints
* @param shouldRegisterLinksMapping whether the links endpoint should be registered
*/ */
public WebMvcEndpointHandlerMapping(EndpointMapping endpointMapping, Collection<ExposableWebEndpoint> endpoints, public WebMvcEndpointHandlerMapping(EndpointMapping endpointMapping, Collection<ExposableWebEndpoint> endpoints,
EndpointMediaTypes endpointMediaTypes, CorsConfiguration corsConfiguration, EndpointMediaTypes endpointMediaTypes, CorsConfiguration corsConfiguration,
EndpointLinksResolver linksResolver) { EndpointLinksResolver linksResolver, boolean shouldRegisterLinksMapping) {
super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration); super(endpointMapping, endpoints, endpointMediaTypes, corsConfiguration, shouldRegisterLinksMapping);
this.linksResolver = linksResolver; this.linksResolver = linksResolver;
setOrder(-100); setOrder(-100);
} }
......
...@@ -77,9 +77,4 @@ class EndpointMappingTests { ...@@ -77,9 +77,4 @@ class EndpointMappingTests {
assertThat(new EndpointMapping("/test").createSubPath("one/")).isEqualTo("/test/one"); assertThat(new EndpointMapping("/test").createSubPath("one/")).isEqualTo("/test/one");
} }
@Test
void setDifferentPort() {
assertThat(new EndpointMapping("/test", false).isSamePort()).isFalse();
}
} }
...@@ -52,6 +52,7 @@ import org.springframework.security.core.context.SecurityContext; ...@@ -52,6 +52,7 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper; import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
/** /**
...@@ -106,9 +107,10 @@ public class JerseyWebEndpointIntegrationTests ...@@ -106,9 +107,10 @@ public class JerseyWebEndpointIntegrationTests
ResourceConfig resourceConfig(Environment environment, WebEndpointDiscoverer endpointDiscoverer, ResourceConfig resourceConfig(Environment environment, WebEndpointDiscoverer endpointDiscoverer,
EndpointMediaTypes endpointMediaTypes) { EndpointMediaTypes endpointMediaTypes) {
ResourceConfig resourceConfig = new ResourceConfig(); ResourceConfig resourceConfig = new ResourceConfig();
String endpointPath = environment.getProperty("endpointPath");
Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources( Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources(
new EndpointMapping(environment.getProperty("endpointPath")), endpointDiscoverer.getEndpoints(), new EndpointMapping(endpointPath), endpointDiscoverer.getEndpoints(), endpointMediaTypes,
endpointMediaTypes, new EndpointLinksResolver(endpointDiscoverer.getEndpoints())); new EndpointLinksResolver(endpointDiscoverer.getEndpoints()), StringUtils.hasText(endpointPath));
resourceConfig.registerResources(new HashSet<>(resources)); resourceConfig.registerResources(new HashSet<>(resources));
resourceConfig.register(JacksonFeature.class); resourceConfig.register(JacksonFeature.class);
resourceConfig.register(new ObjectMapperContextResolver(new ObjectMapper()), ContextResolver.class); resourceConfig.register(new ObjectMapperContextResolver(new ObjectMapper()), ContextResolver.class);
......
...@@ -41,6 +41,7 @@ import org.springframework.http.server.reactive.HttpHandler; ...@@ -41,6 +41,7 @@ import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.ReactiveSecurityContextHolder; import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilter;
...@@ -122,9 +123,10 @@ class WebFluxEndpointIntegrationTests ...@@ -122,9 +123,10 @@ class WebFluxEndpointIntegrationTests
CorsConfiguration corsConfiguration = new CorsConfiguration(); CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedOrigins(Arrays.asList("https://example.com")); corsConfiguration.setAllowedOrigins(Arrays.asList("https://example.com"));
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST")); corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST"));
return new WebFluxEndpointHandlerMapping(new EndpointMapping(environment.getProperty("endpointPath")), String endpointPath = environment.getProperty("endpointPath");
return new WebFluxEndpointHandlerMapping(new EndpointMapping(endpointPath),
endpointDiscoverer.getEndpoints(), endpointMediaTypes, corsConfiguration, endpointDiscoverer.getEndpoints(), endpointMediaTypes, corsConfiguration,
new EndpointLinksResolver(endpointDiscoverer.getEndpoints())); new EndpointLinksResolver(endpointDiscoverer.getEndpoints()), StringUtils.hasText(endpointPath));
} }
@Bean @Bean
......
...@@ -52,6 +52,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; ...@@ -52,6 +52,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper; import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.handler.RequestMatchResult; import org.springframework.web.servlet.handler.RequestMatchResult;
...@@ -145,9 +146,10 @@ class MvcWebEndpointIntegrationTests ...@@ -145,9 +146,10 @@ class MvcWebEndpointIntegrationTests
CorsConfiguration corsConfiguration = new CorsConfiguration(); CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedOrigins(Arrays.asList("https://example.com")); corsConfiguration.setAllowedOrigins(Arrays.asList("https://example.com"));
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST")); corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST"));
return new WebMvcEndpointHandlerMapping(new EndpointMapping(environment.getProperty("endpointPath")), String endpointPath = environment.getProperty("endpointPath");
return new WebMvcEndpointHandlerMapping(new EndpointMapping(endpointPath),
endpointDiscoverer.getEndpoints(), endpointMediaTypes, corsConfiguration, endpointDiscoverer.getEndpoints(), endpointMediaTypes, corsConfiguration,
new EndpointLinksResolver(endpointDiscoverer.getEndpoints())); new EndpointLinksResolver(endpointDiscoverer.getEndpoints()), StringUtils.hasText(endpointPath));
} }
} }
......
...@@ -242,7 +242,7 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation ...@@ -242,7 +242,7 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation
Collections.emptyList()); Collections.emptyList());
Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources( Collection<Resource> resources = new JerseyEndpointResourceFactory().createEndpointResources(
new EndpointMapping("/actuator"), discoverer.getEndpoints(), endpointMediaTypes, new EndpointMapping("/actuator"), discoverer.getEndpoints(), endpointMediaTypes,
new EndpointLinksResolver(discoverer.getEndpoints())); new EndpointLinksResolver(discoverer.getEndpoints()), true);
config.registerResources(new HashSet<>(resources)); config.registerResources(new HashSet<>(resources));
} }
...@@ -288,7 +288,8 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation ...@@ -288,7 +288,8 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation
new ConversionServiceParameterValueMapper(), endpointMediaTypes, null, Collections.emptyList(), new ConversionServiceParameterValueMapper(), endpointMediaTypes, null, Collections.emptyList(),
Collections.emptyList()); Collections.emptyList());
return new WebFluxEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(), return new WebFluxEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(),
endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints())); endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints()),
true);
} }
} }
...@@ -317,7 +318,8 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation ...@@ -317,7 +318,8 @@ class WebEndpointTestInvocationContextProvider implements TestTemplateInvocation
new ConversionServiceParameterValueMapper(), endpointMediaTypes, null, Collections.emptyList(), new ConversionServiceParameterValueMapper(), endpointMediaTypes, null, Collections.emptyList(),
Collections.emptyList()); Collections.emptyList());
return new WebMvcEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(), return new WebMvcEndpointHandlerMapping(new EndpointMapping("/actuator"), discoverer.getEndpoints(),
endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints())); endpointMediaTypes, new CorsConfiguration(), new EndpointLinksResolver(discoverer.getEndpoints()),
true);
} }
} }
......
/*
* Copyright 2012-2019 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 smoketest.actuator; package smoketest.actuator;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort; import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for separate management and main service ports with empty endpoint
* base path.
*
* @author HaiTao Zhang
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "management.endpoints.web.base-path=/","management.server.port=0"}) properties = { "management.endpoints.web.base-path=/", "management.server.port=0" })
public class ManagementDifferentPortSampleActuatorApplicationTests { class ManagementDifferentPortSampleActuatorApplicationTests {
@LocalServerPort
private int port;
@LocalManagementPort @LocalManagementPort
private int managementPort; private int managementPort;
@Test @Test
void testDifferentServerPort(){ void linksEndpointShouldBeAvailable() {
if(this.managementPort != this.port) {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.managementPort + "/", String.class); .getForEntity("http://localhost:" + this.managementPort + "/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"_links\""); assertThat(entity.getBody()).contains("\"_links\"");
} }
}
private String getPassword(){ private String getPassword() {
return "password"; return "password";
} }
......
/*
* Copyright 2012-2019 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 smoketest.jersey; package smoketest.jersey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort; import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"management.server.port=0","management.endpoints.web.base-path=/"}) /**
public class JerseyDifferentPortSampleActuatorApplicationTests { * Integration tests for separate management and main service ports with empty base path
* for endpoints.
*
* @author HaiTao Zhang
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "management.server.port=0", "management.endpoints.web.base-path=/" })
class JerseyDifferentPortSampleActuatorApplicationTests {
@LocalManagementPort @LocalManagementPort
private int managementPort; private int managementPort;
@LocalServerPort
private int port;
@Test @Test
void testDifferentServerPort(){ void linksEndpointShouldBeAvailable() {
if(this.managementPort != this.port) {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.managementPort + "/", String.class); .getForEntity("http://localhost:" + this.managementPort + "/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"_links\""); assertThat(entity.getBody()).contains("\"_links\"");
} }
}
private String getPassword(){ private String getPassword() {
return "password"; return "password";
} }
} }
/*
* Copyright 2012-2019 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 smoketest.webflux; package smoketest.webflux;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort; import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"management.server.port=0","management.endpoints.web.base-path=/"}) /**
public class WebFluxDifferentPortSampleActuatorApplicationTests { * Integration tests for separate management and main service ports with empty endpoint
* base path.
*
* @author HaiTao Zhang
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "management.server.port=0", "management.endpoints.web.base-path=/" })
class WebFluxDifferentPortSampleActuatorApplicationTests {
@LocalManagementPort @LocalManagementPort
private int managementPort; private int managementPort;
@LocalServerPort
private int port;
@Test @Test
void testDifferentServerPort(){ void linksEndpointShouldBeAvailable() {
if(this.managementPort != this.port) {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.managementPort + "/", String.class); .getForEntity("http://localhost:" + this.managementPort + "/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"_links\""); assertThat(entity.getBody()).contains("\"_links\"");
} }
}
private String getPassword(){ private String getPassword() {
return "password"; return "password";
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment