|
27 | 27 | package org.springdoc.webflux.ui; |
28 | 28 |
|
29 | 29 | import java.util.Optional; |
30 | | - |
31 | | -import org.springdoc.core.properties.SpringDocConfigProperties; |
32 | 30 | import org.springdoc.core.properties.SwaggerUiConfigProperties; |
33 | 31 | import org.springdoc.core.providers.ActuatorProvider; |
34 | | - |
| 32 | +import org.springframework.boot.autoconfigure.web.WebProperties; |
| 33 | +import org.springframework.boot.webflux.autoconfigure.WebFluxProperties; |
35 | 34 | import org.springframework.http.CacheControl; |
36 | 35 | import org.springframework.web.reactive.config.ResourceHandlerRegistry; |
37 | 36 | import org.springframework.web.reactive.config.WebFluxConfigurer; |
38 | | - |
39 | | -import static org.springdoc.core.utils.Constants.CLASSPATH_RESOURCE_LOCATION; |
40 | | -import static org.springdoc.core.utils.Constants.DEFAULT_WEB_JARS_PREFIX_URL; |
41 | | -import static org.springdoc.core.utils.Constants.SWAGGER_INITIALIZER_JS; |
| 37 | +import org.springframework.web.util.pattern.PathPattern; |
| 38 | +import org.springframework.web.util.pattern.PathPatternParser; |
| 39 | +import static org.springdoc.core.utils.Constants.ALL_PATTERN; |
| 40 | +import static org.springdoc.core.utils.Constants.SWAGGER_INITIALIZER_PATTERN; |
42 | 41 | import static org.springdoc.core.utils.Constants.SWAGGER_UI_PREFIX; |
| 42 | +import static org.springdoc.core.utils.Constants.SWAGGER_UI_WEBJAR_NAME; |
| 43 | +import static org.springdoc.core.utils.Constants.SWAGGER_UI_WEBJAR_NAME_PATTERN; |
| 44 | +import static org.springdoc.core.utils.Constants.WEBJARS_RESOURCE_LOCATION; |
43 | 45 | import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR; |
44 | 46 |
|
45 | 47 | /** |
@@ -70,51 +72,117 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer { |
70 | 72 | private final SwaggerUiConfigProperties swaggerUiConfigProperties; |
71 | 73 |
|
72 | 74 | /** |
73 | | - * The Spring doc config properties. |
| 75 | + * The Spring Web config properties. |
74 | 76 | */ |
75 | | - private final SpringDocConfigProperties springDocConfigProperties; |
| 77 | + private final WebProperties springWebProperties; |
| 78 | + |
| 79 | + /** |
| 80 | + * The Spring WebFlux config properties. |
| 81 | + */ |
| 82 | + private final WebFluxProperties springWebFluxProperties; |
| 83 | + |
| 84 | + private final PathPatternParser parser = new PathPatternParser(); |
76 | 85 |
|
77 | 86 | /** |
78 | 87 | * Instantiates a new Swagger web flux configurer. |
79 | 88 | * |
80 | 89 | * @param swaggerUiConfigProperties the swagger ui calculated config |
81 | | - * @param springDocConfigProperties the spring doc config properties |
| 90 | + * @param springWebProperties the spring web config |
| 91 | + * @param springWebFluxProperties the spring webflux config |
82 | 92 | * @param swaggerIndexTransformer the swagger index transformer |
83 | 93 | * @param actuatorProvider the actuator provider |
84 | 94 | * @param swaggerResourceResolver the swagger resource resolver |
85 | 95 | */ |
86 | 96 | public SwaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, |
87 | | - SpringDocConfigProperties springDocConfigProperties, |
88 | | - SwaggerIndexTransformer swaggerIndexTransformer, |
89 | | - Optional<ActuatorProvider> actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { |
| 97 | + WebProperties springWebProperties, WebFluxProperties springWebFluxProperties, |
| 98 | + SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider, |
| 99 | + SwaggerResourceResolver swaggerResourceResolver) { |
90 | 100 | this.swaggerIndexTransformer = swaggerIndexTransformer; |
91 | 101 | this.actuatorProvider = actuatorProvider; |
92 | 102 | this.swaggerResourceResolver = swaggerResourceResolver; |
93 | 103 | this.swaggerUiConfigProperties = swaggerUiConfigProperties; |
94 | | - this.springDocConfigProperties = springDocConfigProperties; |
| 104 | + this.springWebProperties = springWebProperties; |
| 105 | + this.springWebFluxProperties = springWebFluxProperties; |
95 | 106 | } |
96 | 107 |
|
97 | 108 | @Override |
98 | 109 | public void addResourceHandlers(ResourceHandlerRegistry registry) { |
99 | | - StringBuilder uiRootPath = new StringBuilder(); |
100 | | - String swaggerPath = swaggerUiConfigProperties.getPath(); |
101 | | - if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR)) |
102 | | - uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)); |
103 | | - if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) |
104 | | - uiRootPath.append(actuatorProvider.get().getBasePath()); |
| 110 | + String swaggerUiPattern = getUiRootPath() + SWAGGER_UI_PREFIX + ALL_PATTERN; |
| 111 | + String swaggerUiResourceLocation = WEBJARS_RESOURCE_LOCATION + SWAGGER_UI_WEBJAR_NAME + DEFAULT_PATH_SEPARATOR + |
| 112 | + swaggerUiConfigProperties.getVersion() + DEFAULT_PATH_SEPARATOR; |
105 | 113 |
|
106 | | - registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + "*/*" + SWAGGER_INITIALIZER_JS) |
107 | | - .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR) |
108 | | - .setCacheControl(CacheControl.noStore()) |
| 114 | + addSwaggerUiResourceHandler(registry, swaggerUiPattern, swaggerUiResourceLocation); |
| 115 | + |
| 116 | + // Add custom mappings for Swagger UI WebJar resources if Spring resource mapping is enabled |
| 117 | + if (springWebProperties.getResources().isAddMappings()) { |
| 118 | + String webjarsPathPattern = springWebFluxProperties.getWebjarsPathPattern(); |
| 119 | + |
| 120 | + String swaggerUiWebjarPattern = mergePatterns(webjarsPathPattern, SWAGGER_UI_WEBJAR_NAME_PATTERN) + ALL_PATTERN; |
| 121 | + String swaggerUiWebjarResourceLocation = WEBJARS_RESOURCE_LOCATION; |
| 122 | + |
| 123 | + addSwaggerUiResourceHandler(registry, swaggerUiWebjarPattern, swaggerUiWebjarResourceLocation); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Adds the resource handlers for serving the Swagger UI resources. |
| 129 | + */ |
| 130 | + protected void addSwaggerUiResourceHandler(ResourceHandlerRegistry registry, String pattern, String... resourceLocations) { |
| 131 | + registry.addResourceHandler(pattern) |
| 132 | + .addResourceLocations(resourceLocations) |
109 | 133 | .resourceChain(false) |
110 | 134 | .addResolver(swaggerResourceResolver) |
111 | 135 | .addTransformer(swaggerIndexTransformer); |
112 | 136 |
|
113 | | - registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + "*/**") |
114 | | - .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR) |
| 137 | + // Ensure Swagger initializer has "no-store" Cache-Control header |
| 138 | + registry.addResourceHandler(mergePatterns(pattern, SWAGGER_INITIALIZER_PATTERN)) |
| 139 | + .setCacheControl(CacheControl.noStore()) |
| 140 | + .addResourceLocations(resourceLocations) |
115 | 141 | .resourceChain(false) |
116 | 142 | .addResolver(swaggerResourceResolver) |
117 | 143 | .addTransformer(swaggerIndexTransformer); |
118 | 144 | } |
119 | 145 |
|
| 146 | + /** |
| 147 | + * Computes and returns the root path for the Swagger UI. |
| 148 | + * |
| 149 | + * @return the Swagger UI root path. |
| 150 | + */ |
| 151 | + protected String getUiRootPath() { |
| 152 | + StringBuilder uiRootPath = new StringBuilder(); |
| 153 | + |
| 154 | + if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) { |
| 155 | + uiRootPath.append(actuatorProvider.get().getBasePath()); |
| 156 | + } |
| 157 | + |
| 158 | + String swaggerUiPath = swaggerUiConfigProperties.getPath(); |
| 159 | + if (swaggerUiPath.contains(DEFAULT_PATH_SEPARATOR)) { |
| 160 | + uiRootPath.append(swaggerUiPath, 0, swaggerUiPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)); |
| 161 | + } |
| 162 | + |
| 163 | + return uiRootPath.toString(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Combines two patterns into a new pattern according to the rules of {@link PathPattern#combine}. |
| 168 | + * |
| 169 | + * <p>For example: |
| 170 | + * <ul> |
| 171 | + * <li><code>/webjars/**</code> + <code>/swagger-ui/**</code> => <code>/webjars/swagger-ui/**</code></li> |
| 172 | + * <li><code>/documentation/swagger-ui*/**</code> + <code>/*.js</code> => <code>/documentation/swagger-ui*/*.js</code></li> |
| 173 | + * </ul> |
| 174 | + * |
| 175 | + * @param pattern1 the first pattern |
| 176 | + * @param pattern2 the second pattern |
| 177 | + * |
| 178 | + * @return the combination of the two patterns |
| 179 | + * |
| 180 | + * @see PathPattern#combine |
| 181 | + */ |
| 182 | + private String mergePatterns(String pattern1, String pattern2) { |
| 183 | + PathPattern pathPattern1 = parser.parse(parser.initFullPathPattern(pattern1)); |
| 184 | + PathPattern pathPattern2 = parser.parse(parser.initFullPathPattern(pattern2)); |
| 185 | + |
| 186 | + return pathPattern1.combine(pathPattern2).getPatternString(); |
| 187 | + } |
120 | 188 | } |
0 commit comments