|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Collection;
|
21 |
| -import java.util.Collections; |
22 | 21 | import java.util.HashSet;
|
23 | 22 | import java.util.List;
|
24 | 23 |
|
| 24 | +import javax.annotation.PostConstruct; |
| 25 | + |
25 | 26 | import org.glassfish.jersey.server.ResourceConfig;
|
| 27 | +import org.glassfish.jersey.server.model.Resource; |
26 | 28 |
|
| 29 | +import org.springframework.beans.factory.ObjectProvider; |
27 | 30 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
|
28 | 31 | import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
|
29 | 32 | import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
|
30 | 33 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
31 | 34 | import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
|
32 | 35 | import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
|
33 | 36 | import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
|
| 37 | +import org.springframework.boot.actuate.endpoint.web.ExposableServletEndpoint; |
34 | 38 | import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
|
35 | 39 | import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
|
36 | 40 | import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
|
|
59 | 63 | class JerseyWebEndpointManagementContextConfiguration {
|
60 | 64 |
|
61 | 65 | @Bean
|
62 |
| - public ResourceConfigCustomizer webEndpointRegistrar(WebEndpointsSupplier webEndpointsSupplier, |
| 66 | + JerseyWebEndpointsResourcesRegistrar jerseyWebEndpointsResourcesRegistrar( |
| 67 | + ObjectProvider<ResourceConfig> resourceConfig, WebEndpointsSupplier webEndpointsSupplier, |
63 | 68 | ServletEndpointsSupplier servletEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
|
64 | 69 | WebEndpointProperties webEndpointProperties) {
|
65 |
| - List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>(); |
66 |
| - allEndpoints.addAll(webEndpointsSupplier.getEndpoints()); |
67 |
| - allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); |
68 |
| - return (resourceConfig) -> { |
| 70 | + return new JerseyWebEndpointsResourcesRegistrar(resourceConfig.getIfAvailable(), webEndpointsSupplier, |
| 71 | + servletEndpointsSupplier, endpointMediaTypes, webEndpointProperties.getBasePath()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Register endpoints with the {@link ResourceConfig}. The |
| 76 | + * {@link ResourceConfigCustomizer} cannot be used because we don't want to apply |
| 77 | + */ |
| 78 | + static class JerseyWebEndpointsResourcesRegistrar { |
| 79 | + |
| 80 | + private final ResourceConfig resourceConfig; |
| 81 | + |
| 82 | + private final WebEndpointsSupplier webEndpointsSupplier; |
| 83 | + |
| 84 | + private final ServletEndpointsSupplier servletEndpointsSupplier; |
| 85 | + |
| 86 | + private final EndpointMediaTypes mediaTypes; |
| 87 | + |
| 88 | + private final String basePath; |
| 89 | + |
| 90 | + JerseyWebEndpointsResourcesRegistrar(ResourceConfig resourceConfig, WebEndpointsSupplier webEndpointsSupplier, |
| 91 | + ServletEndpointsSupplier servletEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, |
| 92 | + String basePath) { |
| 93 | + super(); |
| 94 | + this.resourceConfig = resourceConfig; |
| 95 | + this.webEndpointsSupplier = webEndpointsSupplier; |
| 96 | + this.servletEndpointsSupplier = servletEndpointsSupplier; |
| 97 | + this.mediaTypes = endpointMediaTypes; |
| 98 | + this.basePath = basePath; |
| 99 | + } |
| 100 | + |
| 101 | + @PostConstruct |
| 102 | + void register() { |
| 103 | + // We can't easily use @ConditionalOnBean because @AutoConfigureBefore is |
| 104 | + // not an option for management contexts. Instead we manually check if |
| 105 | + // the resource config bean exists |
| 106 | + if (this.resourceConfig == null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + Collection<ExposableWebEndpoint> webEndpoints = this.webEndpointsSupplier.getEndpoints(); |
| 110 | + Collection<ExposableServletEndpoint> servletEndpoints = this.servletEndpointsSupplier.getEndpoints(); |
| 111 | + EndpointLinksResolver linksResolver = getLinksResolver(webEndpoints, servletEndpoints); |
| 112 | + EndpointMapping mapping = new EndpointMapping(this.basePath); |
69 | 113 | JerseyEndpointResourceFactory resourceFactory = new JerseyEndpointResourceFactory();
|
70 |
| - String basePath = webEndpointProperties.getBasePath(); |
71 |
| - EndpointMapping endpointMapping = new EndpointMapping(basePath); |
72 |
| - Collection<ExposableWebEndpoint> webEndpoints = Collections |
73 |
| - .unmodifiableCollection(webEndpointsSupplier.getEndpoints()); |
74 |
| - resourceConfig.registerResources(new HashSet<>(resourceFactory.createEndpointResources(endpointMapping, |
75 |
| - webEndpoints, endpointMediaTypes, new EndpointLinksResolver(allEndpoints, basePath)))); |
76 |
| - }; |
| 114 | + register(resourceFactory.createEndpointResources(mapping, webEndpoints, this.mediaTypes, linksResolver)); |
| 115 | + } |
| 116 | + |
| 117 | + private EndpointLinksResolver getLinksResolver(Collection<ExposableWebEndpoint> webEndpoints, |
| 118 | + Collection<ExposableServletEndpoint> servletEndpoints) { |
| 119 | + List<ExposableEndpoint<?>> endpoints = new ArrayList<>(webEndpoints.size() + servletEndpoints.size()); |
| 120 | + endpoints.addAll(webEndpoints); |
| 121 | + endpoints.addAll(servletEndpoints); |
| 122 | + return new EndpointLinksResolver(endpoints, this.basePath); |
| 123 | + } |
| 124 | + |
| 125 | + private void register(Collection<Resource> resources) { |
| 126 | + this.resourceConfig.registerResources(new HashSet<>(resources)); |
| 127 | + } |
| 128 | + |
77 | 129 | }
|
78 | 130 |
|
79 | 131 | }
|
0 commit comments