|
2 | 2 | *
|
3 | 3 | * *
|
4 | 4 | * * *
|
5 |
| - * * * * Copyright 2019-2022 the original author or authors. |
| 5 | + * * * * Copyright 2019-2020 the original author or authors. |
6 | 6 | * * * *
|
7 | 7 | * * * * Licensed under the Apache License, Version 2.0 (the "License");
|
8 | 8 | * * * * you may not use this file except in compliance with the License.
|
|
23 | 23 |
|
24 | 24 | package org.springdoc.core;
|
25 | 25 |
|
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
26 | 29 | import org.springdoc.core.customizers.ActuatorOpenApiCustomizer;
|
27 | 30 | import org.springdoc.core.customizers.ActuatorOperationCustomizer;
|
28 |
| -import org.springframework.beans.BeansException; |
| 31 | + |
29 | 32 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
30 |
| -import org.springframework.beans.factory.config.RuntimeBeanReference; |
31 |
| -import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
32 |
| -import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
33 |
| -import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
34 | 33 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
|
35 | 34 | import org.springframework.boot.context.properties.bind.BindResult;
|
36 | 35 | import org.springframework.boot.context.properties.bind.Binder;
|
37 |
| -import org.springframework.context.ApplicationContext; |
38 |
| -import org.springframework.context.ApplicationContextAware; |
39 |
| -import org.springframework.util.ObjectUtils; |
| 36 | +import org.springframework.util.CollectionUtils; |
40 | 37 |
|
41 | 38 | import static org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP;
|
42 | 39 | import static org.springdoc.core.Constants.ALL_PATTERN;
|
|
47 | 44 | /**
|
48 | 45 | * The type Springdoc bean factory configurer.
|
49 | 46 | * @author bnasslahsen
|
50 |
| - * @author christophejan |
51 | 47 | */
|
52 |
| -public class SpringdocActuatorBeanFactoryConfigurer implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor { |
| 48 | +public class SpringdocActuatorBeanFactoryConfigurer extends SpringdocBeanFactoryConfigurer{ |
53 | 49 |
|
54 | 50 | /**
|
55 |
| - * The ApplicationContext. |
| 51 | + * The Grouped open apis. |
56 | 52 | */
|
57 |
| - protected ApplicationContext applicationContext; |
| 53 | + private List<GroupedOpenApi> groupedOpenApis; |
58 | 54 |
|
59 |
| - @Override |
60 |
| - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
61 |
| - this.applicationContext = applicationContext; |
| 55 | + /** |
| 56 | + * Instantiates a new Springdoc actuator bean factory configurer. |
| 57 | + * |
| 58 | + * @param groupedOpenApis the grouped open apis |
| 59 | + */ |
| 60 | + public SpringdocActuatorBeanFactoryConfigurer(List<GroupedOpenApi> groupedOpenApis) { |
| 61 | + this.groupedOpenApis = groupedOpenApis; |
62 | 62 | }
|
63 | 63 |
|
64 | 64 | @Override
|
65 |
| - public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { |
66 |
| - final BindResult<WebEndpointProperties> result = Binder.get(applicationContext.getEnvironment()) |
| 65 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
| 66 | + final BindResult<WebEndpointProperties> result = Binder.get(environment) |
67 | 67 | .bind(MANAGEMENT_ENDPOINTS_WEB, WebEndpointProperties.class);
|
68 | 68 | if (result.isBound()) {
|
69 | 69 | WebEndpointProperties webEndpointProperties = result.get();
|
70 | 70 |
|
71 |
| - boolean addDefaultGroup = ObjectUtils.isEmpty(applicationContext |
72 |
| - .getBeanNamesForType(GroupedOpenApi.class, true, false)); |
73 |
| - |
74 |
| - registry.registerBeanDefinition("actuatorOpenApiCustomiser", BeanDefinitionBuilder |
75 |
| - .genericBeanDefinition(ActuatorOpenApiCustomizer.class) |
76 |
| - .addConstructorArgValue(webEndpointProperties) |
77 |
| - .getBeanDefinition()); |
78 |
| - |
79 |
| - registry.registerBeanDefinition("actuatorCustomizer", BeanDefinitionBuilder |
80 |
| - .genericBeanDefinition(ActuatorOperationCustomizer.class) |
81 |
| - .getBeanDefinition()); |
82 |
| - |
83 |
| - // register the actuator group bean definition |
84 |
| - registry.registerBeanDefinition(ACTUATOR_DEFAULT_GROUP, BeanDefinitionBuilder |
85 |
| - .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
86 |
| - .setFactoryMethod("actuatorGroupFactoryMethod") |
87 |
| - .addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class)) |
88 |
| - .addConstructorArgValue(webEndpointProperties.getBasePath()) |
89 |
| - .addConstructorArgValue(new RuntimeBeanReference(ActuatorOpenApiCustomizer.class)) |
90 |
| - .addConstructorArgValue(new RuntimeBeanReference(ActuatorOperationCustomizer.class)) |
91 |
| - .getBeanDefinition()); |
92 |
| - |
93 |
| - if (addDefaultGroup) { |
94 |
| - // register the default group bean definition |
95 |
| - registry.registerBeanDefinition(DEFAULT_GROUP_NAME, BeanDefinitionBuilder |
96 |
| - .genericBeanDefinition(SpringdocActuatorBeanFactoryConfigurer.class) |
97 |
| - .setFactoryMethod("defaultGroupFactoryMethod") |
98 |
| - .addConstructorArgValue(new RuntimeBeanReference(GroupedOpenApi.Builder.class)) |
99 |
| - .addConstructorArgValue(webEndpointProperties.getBasePath()) |
100 |
| - .getBeanDefinition()); |
| 71 | + List<GroupedOpenApi> newGroups = new ArrayList<>(); |
| 72 | + |
| 73 | + ActuatorOpenApiCustomizer actuatorOpenApiCustomiser = new ActuatorOpenApiCustomizer(webEndpointProperties); |
| 74 | + beanFactory.registerSingleton("actuatorOpenApiCustomiser", actuatorOpenApiCustomiser); |
| 75 | + ActuatorOperationCustomizer actuatorCustomizer = new ActuatorOperationCustomizer(); |
| 76 | + beanFactory.registerSingleton("actuatorCustomizer", actuatorCustomizer); |
| 77 | + |
| 78 | + GroupedOpenApi actuatorGroup = GroupedOpenApi.builder().group(ACTUATOR_DEFAULT_GROUP) |
| 79 | + .pathsToMatch(webEndpointProperties.getBasePath() + ALL_PATTERN) |
| 80 | + .pathsToExclude(webEndpointProperties.getBasePath() + HEALTH_PATTERN) |
| 81 | + .addOperationCustomizer(actuatorCustomizer) |
| 82 | + .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
| 83 | + .build(); |
| 84 | + // Add the actuator group |
| 85 | + newGroups.add(actuatorGroup); |
| 86 | + |
| 87 | + if (CollectionUtils.isEmpty(groupedOpenApis)) { |
| 88 | + GroupedOpenApi defaultGroup = GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME) |
| 89 | + .pathsToMatch(ALL_PATTERN) |
| 90 | + .pathsToExclude(webEndpointProperties.getBasePath() + ALL_PATTERN) |
| 91 | + .build(); |
| 92 | + // Register the default group |
| 93 | + newGroups.add(defaultGroup); |
101 | 94 | }
|
102 |
| - } |
103 |
| - } |
104 | 95 |
|
105 |
| - @Override |
106 |
| - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
107 |
| - SpringdocBeanFactoryConfigurer.initBeanFactoryPostProcessor(beanFactory); |
108 |
| - } |
109 |
| - |
110 |
| - /** |
111 |
| - * Actuator {@link GroupedOpenApi} factory method. |
112 |
| - * |
113 |
| - * @param builder the {@link GroupedOpenApi.Builder} |
114 |
| - * @param actuatorBasePath the actuator base path |
115 |
| - * @param actuatorOpenApiCustomiser the {@link ActuatorOpenApiCustomizer} |
116 |
| - * @param actuatorOperationCustomizer the {@link ActuatorOperationCustomizer} |
117 |
| - * |
118 |
| - * @return the actuator {@link GroupedOpenApi} |
119 |
| - */ |
120 |
| - public static GroupedOpenApi actuatorGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath, |
121 |
| - ActuatorOpenApiCustomizer actuatorOpenApiCustomiser, ActuatorOperationCustomizer actuatorOperationCustomizer) { |
122 |
| - return builder.group(ACTUATOR_DEFAULT_GROUP) |
123 |
| - .pathsToMatch(actuatorBasePath + ALL_PATTERN) |
124 |
| - .pathsToExclude(actuatorBasePath + HEALTH_PATTERN) |
125 |
| - .addOpenApiCustomiser(actuatorOpenApiCustomiser) |
126 |
| - .addOperationCustomizer(actuatorOperationCustomizer) |
127 |
| - .build(); |
128 |
| - } |
129 |
| - |
130 |
| - /** |
131 |
| - * Default {@link GroupedOpenApi} factory method. |
132 |
| - * |
133 |
| - * @param builder the {@link GroupedOpenApi.Builder} |
134 |
| - * @param actuatorBasePath the actuator base path |
135 |
| - * |
136 |
| - * @return the default {@link GroupedOpenApi} |
137 |
| - */ |
138 |
| - public static GroupedOpenApi defaultGroupFactoryMethod(GroupedOpenApi.Builder builder, String actuatorBasePath) { |
139 |
| - return builder.group(DEFAULT_GROUP_NAME) |
140 |
| - .pathsToMatch(ALL_PATTERN) |
141 |
| - .pathsToExclude(actuatorBasePath + ALL_PATTERN) |
142 |
| - .build(); |
| 96 | + newGroups.forEach(elt -> beanFactory.registerSingleton(elt.getGroup(), elt)); |
| 97 | + } |
| 98 | + initBeanFactoryPostProcessor(beanFactory); |
143 | 99 | }
|
144 | 100 |
|
145 | 101 | }
|
0 commit comments