forked from springdoc/springdoc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSwaggerWelcome.java
245 lines (219 loc) · 7.57 KB
/
AbstractSwaggerWelcome.java
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
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2022 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 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 java.util.Objects;
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;
/**
* The Swagger ui calculated config.
*/
protected final SwaggerUiConfigParameters swaggerUiConfigParameters;
/**
* The Swagger config url.
*/
protected String swaggerConfigUrl;
/**
* The Api docs url.
*/
protected String apiDocsUrl;
/**
* The Context path.
*/
protected String contextPath;
/**
* Instantiates a new Abstract swagger welcome.
*
* @param swaggerUiConfig the swagger ui config
* @param springDocConfigProperties the spring doc config properties
* @param swaggerUiConfigParameters the swagger ui config parameters
*/
public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters) {
this.swaggerUiConfig = swaggerUiConfig;
this.springDocConfigProperties = springDocConfigProperties;
this.swaggerUiConfigParameters = swaggerUiConfigParameters;
}
/**
* Init.
*/
protected void init() {
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup(), groupConfig.getDisplayName()));
calculateUiRootPath();
}
/**
* 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 uriComponentsBuilder the uri components builder
*/
protected void buildConfigUrl(UriComponentsBuilder uriComponentsBuilder) {
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
apiDocsUrl = buildApiDocUrl();
swaggerConfigUrl = buildSwaggerConfigUrl();
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
String swaggerUiUrl = swaggerUiConfig.getUrl();
if (StringUtils.isEmpty(swaggerUiUrl))
swaggerUiConfigParameters.setUrl(apiDocsUrl);
else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
else
swaggerUiConfigParameters.setUrl(buildUrlWithContextPath(swaggerUiUrl));
}
else
swaggerUiConfigParameters.addUrl(apiDocsUrl);
if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) {
swaggerUiConfig.cloneUrls()
.stream()
.filter(swaggerUrl -> !swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl()))
.forEach(swaggerUrl -> {
final var url = buildUrlWithContextPath(swaggerUrl.getUrl());
if (!Objects.equals(url, swaggerUrl.getUrl())) {
swaggerUiConfigParameters.getUrls()
.stream()
.filter(swaggerUrl::equals)
.forEach(subUrl -> subUrl.setUrl(url));
}
});
}
}
calculateOauth2RedirectUrl(uriComponentsBuilder);
}
/**
* Build swagger ui url string.
*
* @param swaggerUiUrl the swagger ui url
* @return the string
*/
protected abstract String buildUrlWithContextPath(String swaggerUiUrl);
/**
* Gets uri components builder.
*
* @param sbUrl the sb url
* @return the uri components builder
*/
protected UriComponentsBuilder getUriComponentsBuilder(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 uriComponentsBuilder the uri components builder
*/
protected abstract void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder);
/**
* Calculate ui root path.
*
* @param sbUrls the sb urls
*/
protected abstract void calculateUiRootPath(StringBuilder... sbUrls);
/**
* Calculate ui root common.
*
* @param sbUrl the sb url
* @param sbUrls the sb urls
*/
protected void calculateUiRootCommon(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.
*
* @return the string
*/
protected abstract String buildApiDocUrl();
/**
* Build swagger config url string.
*
* @return the string
*/
protected abstract String buildSwaggerConfigUrl();
/**
* 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;
}
}