forked from springdoc/springdoc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSwaggerWelcome.java
More file actions
250 lines (227 loc) · 9.24 KB
/
AbstractSwaggerWelcome.java
File metadata and controls
250 lines (227 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2026 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 org.springdoc.ui;
import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.properties.SwaggerUiConfigParameters;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.UriComponentsBuilder;
import static org.springdoc.core.utils.Constants.SWAGGER_CONFIG_FILE;
import static org.springdoc.core.utils.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
import static org.springdoc.core.utils.Constants.SWAGGER_UI_URL;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
/**
* The type Abstract swagger welcome.
*
* @author bnasslashen
*/
public abstract class AbstractSwaggerWelcome {
/**
* The Swagger ui configuration.
*/
protected final SwaggerUiConfigProperties swaggerUiConfig;
/**
* The Spring doc config properties.
*/
protected final SpringDocConfigProperties springDocConfigProperties;
/**
* Instantiates a new Abstract swagger welcome.
*
* @param swaggerUiConfig the swagger ui config
* @param springDocConfigProperties the spring doc config properties
*/
protected AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) {
this.swaggerUiConfig = swaggerUiConfig;
this.springDocConfigProperties = springDocConfigProperties;
}
/**
* Init.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
*/
protected void init(SwaggerUiConfigParameters swaggerUiConfigParameters) {
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup(), groupConfig.getDisplayName()));
calculateUiRootPath(swaggerUiConfigParameters);
}
/**
* Build url string.
*
* @param contextPath the context path
* @param docsUrl the docs url
* @return the string
*/
protected String buildUrl(String contextPath, String docsUrl) {
if (contextPath.endsWith(DEFAULT_PATH_SEPARATOR)) {
return contextPath.substring(0, contextPath.length() - 1) + docsUrl;
}
if (!docsUrl.startsWith(DEFAULT_PATH_SEPARATOR))
docsUrl = DEFAULT_PATH_SEPARATOR + docsUrl;
return contextPath + docsUrl;
}
/**
* Build config url.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param uriComponentsBuilder the uri components builder
*/
protected void buildConfigUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) {
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
buildApiDocUrl(swaggerUiConfigParameters);
buildSwaggerConfigUrl(swaggerUiConfigParameters);
String swaggerUiUrl = swaggerUiConfig.getUrl();
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
if (StringUtils.isEmpty(swaggerUiUrl))
swaggerUiConfigParameters.setUrl(swaggerUiConfigParameters.getApiDocsUrl());
else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
else
swaggerUiConfigParameters.setUrl(buildUrlWithContextPath(swaggerUiConfigParameters, swaggerUiUrl));
}
else
swaggerUiConfigParameters.addUrl(resolveGroupedApiDocsUrl(swaggerUiConfigParameters, swaggerUiUrl));
if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) {
swaggerUiConfig.cloneUrls()
.stream()
.filter(swaggerUrl -> !swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl()))
.forEach(swaggerUrl -> {
final var url = buildUrlWithContextPath(swaggerUiConfigParameters, swaggerUrl.getUrl());
if (!Objects.equals(url, swaggerUrl.getUrl())) {
swaggerUiConfigParameters.getUrls()
.stream()
.filter(swaggerUrl::equals)
.forEach(subUrl -> subUrl.setUrl(url));
}
});
}
}
calculateOauth2RedirectUrl(swaggerUiConfigParameters, uriComponentsBuilder);
}
private String resolveGroupedApiDocsUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, String swaggerUiUrl) {
if (StringUtils.isEmpty(swaggerUiUrl)) {
return swaggerUiConfigParameters.getApiDocsUrl();
}
if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl)) {
return swaggerUiUrl;
}
return buildUrlWithContextPath(swaggerUiConfigParameters, swaggerUiUrl);
}
/**
* Build swagger ui url string.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param swaggerUiUrl the swagger ui url
* @return the string
*/
protected abstract String buildUrlWithContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, String swaggerUiUrl);
/**
* Gets uri components builder.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param sbUrl the sb url
* @return the uri components builder
*/
protected UriComponentsBuilder getUriComponentsBuilder(SwaggerUiConfigParameters swaggerUiConfigParameters, String sbUrl) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
if ((swaggerUiConfig.getQueryConfigEnabled() != null && swaggerUiConfig.getQueryConfigEnabled())) {
swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
.filter(entry -> !SwaggerUiConfigParameters.CONFIG_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigParameters.URLS_PROPERTY))
.filter(entry -> (entry.getValue() instanceof String) ? StringUtils.isNotEmpty((String) entry.getValue()) : entry.getValue() != null)
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
uriBuilder.queryParam(SwaggerUiConfigParameters.CONFIG_URL_PROPERTY, swaggerUiConfigParameters.getConfigUrl());
}
return uriBuilder;
}
/**
* Calculate oauth 2 redirect url.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param uriComponentsBuilder the uri components builder
*/
protected abstract void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder);
/**
* Calculate ui root path.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param sbUrls the sb urls
*/
protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder... sbUrls) {
StringBuilder sbUrl = new StringBuilder();
calculateUiRootCommon(swaggerUiConfigParameters, sbUrl, sbUrls);
}
/**
* Calculate ui root common.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param sbUrl the sb url
* @param sbUrls the sb urls
*/
protected void calculateUiRootCommon(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder sbUrl, StringBuilder[] sbUrls) {
if (ArrayUtils.isNotEmpty(sbUrls))
sbUrl = sbUrls[0];
String swaggerPath = swaggerUiConfigParameters.getPath();
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR))
sbUrl.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
swaggerUiConfigParameters.setUiRootPath(sbUrl.toString());
}
/**
* Build api doc url string.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
*/
protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameters) {
swaggerUiConfigParameters.setApiDocsUrl(buildUrlWithContextPath(swaggerUiConfigParameters, springDocConfigProperties.getApiDocs().getPath()));
}
/**
* Build swagger config url string.
*
* @param swaggerUiConfigParameters the swagger ui config parameters
*/
protected void buildSwaggerConfigUrl(SwaggerUiConfigParameters swaggerUiConfigParameters) {
swaggerUiConfigParameters.setConfigUrl(swaggerUiConfigParameters.getApiDocsUrl() + DEFAULT_PATH_SEPARATOR + SWAGGER_CONFIG_FILE);
}
/**
* Gets oauth2 redirect url.
*
* @return the oauth2 redirect url
*/
protected String getOauth2RedirectUrl() {
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
}
/**
* Gets swagger ui url.
*
* @return the swagger ui url
*/
protected String getSwaggerUiUrl() {
return SWAGGER_UI_URL;
}
}