下面列出了怎么用org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint的API类实例代码及写法,或者点击链接到github查看源代码。
static Optional<Pattern> getEndpointsPatterns(WebEndpointProperties webEndpointProperties,
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
Collection<ExposableWebEndpoint> endpoints = endpointsSupplier.getEndpoints();
if (endpoints.isEmpty()) {
return Optional.empty();
}
String pattern = endpoints.stream().map(PathMappedEndpoint::getRootPath)
.map(path -> path + "|" + path + "/.*").collect(
Collectors.joining("|",
getPathPrefix(webEndpointProperties.getBasePath()) + "/(",
")"));
if (StringUtils.hasText(pattern)) {
return Optional.of(Pattern.compile(pattern));
}
return Optional.empty();
}
@Test
public void testShouldReturnEndpointsWithoutContextPath() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
ServerProperties properties = new ServerProperties();
properties.getServlet().setContextPath("foo");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern())
.isEqualTo("/actuator/(info|info/.*|health|health/.*)");
}
@Test
public void testShouldReturnEndpointsWithoutContextPathAndBasePathSetToRoot() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
webEndpointProperties.setBasePath("/");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern()).isEqualTo("/(info|info/.*|health|health/.*)");
}
@Test
public void testShouldReturnEndpointsWithContextPathAndBasePathSetToRoot() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
webEndpointProperties.setBasePath("/");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern()).isEqualTo("/(info|info/.*|health|health/.*)");
}
private ExposableWebEndpoint createEndpoint(final String name) {
return new ExposableWebEndpoint() {
@Override
public String getRootPath() {
return name;
}
@Override
public EndpointId getEndpointId() {
return EndpointId.of(name);
}
@Override
public boolean isEnableByDefault() {
return false;
}
@Override
public Collection<WebOperation> getOperations() {
return null;
}
};
}
@SuppressWarnings("unchecked")
void assertReadProperties(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).containsKey(endpointId);
ExposableWebEndpoint endpoint = endpoints.get(endpointId);
assertThat(endpoint.getOperations()).hasSize(1);
WebOperation operation = endpoint.getOperations().iterator().next();
Object invoker = ReflectionTestUtils.getField(operation, "invoker");
assertThat(invoker).isInstanceOf(ReflectiveOperationInvoker.class);
Map<String, Properties> properties = (Map<String, Properties>) ((ReflectiveOperationInvoker) invoker).invoke(
new InvocationContext(mock(SecurityContext.class), Collections.emptyMap()));
assertThat(properties).hasSize(1);
});
}
@Bean
@ConditionalOnMissingBean(WebEndpointsSupplier.class)
WebEndpointDiscoverer webEndpointDiscoverer(
ApplicationContext applicationContext,
ParameterValueMapper parameterValueMapper,
EndpointMediaTypes endpointMediaTypes,
ObjectProvider<PathMapper> endpointPathMappers,
ObjectProvider<OperationInvokerAdvisor> invokerAdvisors,
ObjectProvider<EndpointFilter<ExposableWebEndpoint>> filters) {
return new WebEndpointDiscoverer(applicationContext,
parameterValueMapper,
endpointMediaTypes,
endpointPathMappers.orderedStream().collect(toImmutableList()),
invokerAdvisors.orderedStream().collect(toImmutableList()),
filters.orderedStream().collect(toImmutableList()));
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.SAME)
public SkipPattern skipPatternForActuatorEndpointsSamePort(
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(webEndpointProperties, endpointsSupplier);
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@ConditionalOnProperty(name = "management.server.servlet.context-path", havingValue = "/", matchIfMissing = true)
public SkipPattern skipPatternForActuatorEndpointsDifferentPort(
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(webEndpointProperties, endpointsSupplier);
}
@Test
public void testShouldReturnEmptyWhenNoEndpoints() {
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = Collections::emptyList;
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(new WebEndpointProperties(), endpointsSupplier)
.pattern();
then(pattern).isEmpty();
}
static Optional<Pattern> getEndpointsPatterns(String contextPath,
WebEndpointProperties webEndpointProperties,
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
Collection<ExposableWebEndpoint> endpoints = endpointsSupplier.getEndpoints();
if (endpoints.isEmpty()) {
return Optional.empty();
}
String basePath = webEndpointProperties.getBasePath();
String pattern = patternFromEndpoints(contextPath, endpoints, basePath);
if (StringUtils.hasText(pattern)) {
return Optional.of(Pattern.compile(pattern));
}
return Optional.empty();
}
private static String patternFromEndpoints(String contextPath,
Collection<ExposableWebEndpoint> endpoints, String basePath) {
StringJoiner joiner = new StringJoiner("|",
getPathPrefix(contextPath, basePath),
getPathSuffix(contextPath, basePath));
for (ExposableWebEndpoint endpoint : endpoints) {
String path = endpoint.getRootPath();
String paths = path + "|" + path + "/.*";
joiner.add(paths);
}
return joiner.toString();
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.SAME)
public SingleSkipPattern skipPatternForActuatorEndpointsSamePort(
final ServerProperties serverProperties,
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(
serverProperties.getServlet().getContextPath(), webEndpointProperties,
endpointsSupplier);
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@ConditionalOnProperty(name = "management.server.servlet.context-path",
havingValue = "/", matchIfMissing = true)
public SingleSkipPattern skipPatternForActuatorEndpointsDifferentPort(
final ServerProperties serverProperties,
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(null, webEndpointProperties,
endpointsSupplier);
}
@Test
public void should_return_empty_when_no_endpoints() {
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = Collections::emptyList;
Optional<Pattern> pattern = new SkipPatternConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(new ServerProperties(),
new WebEndpointProperties(), endpointsSupplier)
.skipPattern();
then(pattern).isEmpty();
}
void assertActuatorEndpointLoaded(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).containsOnlyKeys(endpointId);
});
}
void assertActuatorEndpointNotLoaded(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).doesNotContainKey(endpointId);
});
}
private Map<EndpointId, ExposableWebEndpoint> mapEndpoints(Collection<ExposableWebEndpoint> endpoints) {
Map<EndpointId, ExposableWebEndpoint> endpointById = new HashMap<>();
endpoints.forEach((endpoint) -> endpointById.put(endpoint.getEndpointId(), endpoint));
return endpointById;
}