forked from springdoc/springdoc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSwaggerIndexTransformer.java
322 lines (289 loc) · 11.8 KB
/
AbstractSwaggerIndexTransformer.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
*
* *
* * *
* * * *
* * * * * 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 java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.properties.SwaggerUiConfigParameters;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springdoc.core.properties.SwaggerUiOAuthProperties;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.core.utils.Constants;
import org.springframework.util.CollectionUtils;
import static org.springdoc.core.properties.SwaggerUiConfigParameters.QUERY_CONFIG_ENABLED_PROPERTY;
/**
* The type Abstract swagger index transformer.
* @author bnasslahsen
*/
public class AbstractSwaggerIndexTransformer {
/**
* The constant PRESETS.
*/
private static final String PRESETS = "presets: [";
/**
* The Swagger ui o auth properties.
*/
protected SwaggerUiOAuthProperties swaggerUiOAuthProperties;
/**
* The Swagger ui config parameters.
*/
protected SwaggerUiConfigParameters swaggerUiConfigParameters;
/**
* The Object mapper.
*/
protected ObjectMapper objectMapper;
/**
* The Swagger ui config.
*/
protected SwaggerUiConfigProperties swaggerUiConfig;
/**
* Instantiates a new Abstract swagger index transformer.
*
* @param swaggerUiConfig the swagger ui config
* @param swaggerUiOAuthProperties the swagger ui o auth properties
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param objectMapperProvider the object mapper provider
*/
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapperProvider objectMapperProvider) {
this.swaggerUiConfig = swaggerUiConfig;
this.swaggerUiOAuthProperties = swaggerUiOAuthProperties;
this.swaggerUiConfigParameters = swaggerUiConfigParameters;
this.objectMapper = objectMapperProvider.jsonMapper();
}
/**
* Add init oauth string.
*
* @param html the html
* @return the string
* @throws JsonProcessingException the json processing exception
*/
protected String addInitOauth(String html) throws JsonProcessingException {
StringBuilder stringBuilder = new StringBuilder(" });\n");
stringBuilder.append("ui.initOAuth(\n");
String json = objectMapper.writeValueAsString(swaggerUiOAuthProperties.getConfigParameters());
stringBuilder.append(json);
stringBuilder.append(")");
return html.replace(" });", stringBuilder.toString());
}
/**
* Read fully as string string.
*
* @param inputStream the input stream
* @return the string
* @throws IOException the io exception
*/
protected String readFullyAsString(InputStream inputStream)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return baos.toString(StandardCharsets.UTF_8.name());
}
/**
* Overwrite swagger default url string.
*
* @param html the html
* @return the string
*/
protected String overwriteSwaggerDefaultUrl(String html) {
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
}
/**
* Setting the url configured with swagger ui properties
*
* @param html
* @return modifed html
*/
protected String setConfiguredApiDocsUrl(String html){
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, swaggerUiConfig.getUrl());
}
/**
* Default transformations string.
*
* @param inputStream the input stream
* @return the string
* @throws IOException the io exception
*/
protected String defaultTransformations(InputStream inputStream) throws IOException {
String html = readFullyAsString(inputStream);
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()))
html = addInitOauth(html);
if (swaggerUiConfig.isCsrfEnabled()) {
if (swaggerUiConfig.getCsrf().isUseLocalStorage())
html = addCSRFLocalStorage(html);
else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
html = addCSRFSessionStorage(html);
else
html = addCSRF(html);
}
if (swaggerUiConfig.getSyntaxHighlight().isPresent())
html = addSyntaxHighlight(html);
if (swaggerUiConfig.getQueryConfigEnabled() == null || !swaggerUiConfig.getQueryConfigEnabled())
html = addParameters(html);
else
html = addParameter(html, QUERY_CONFIG_ENABLED_PROPERTY, swaggerUiConfig.getQueryConfigEnabled().toString());
if (swaggerUiConfig.isDisableSwaggerDefaultUrl())
html = overwriteSwaggerDefaultUrl(html);
if(StringUtils.isNotEmpty(swaggerUiConfig.getUrl())){
html = setConfiguredApiDocsUrl(html);
}
return html;
}
/**
* Add parameters string.
*
* @param html the html
* @return the string
* @throws JsonProcessingException the json processing exception
*/
protected String addParameters(String html) throws JsonProcessingException {
String layout = swaggerUiConfigParameters.getLayout() != null ? swaggerUiConfigParameters.getLayout() : "StandaloneLayout";
StringBuilder stringBuilder = new StringBuilder("layout: \"" + layout + "\" ,\n");
Map<String, Object> parametersObjectMap = swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.URLS_PROPERTY.equals(entry.getKey()))
.filter(entry -> SwaggerUiConfigParameters.VALIDATOR_URL_PROPERTY.equals(entry.getKey())
|| ((entry.getValue() instanceof String) ? StringUtils.isNotEmpty((String) entry.getValue()) : entry.getValue() != null))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
LinkedHashMap::new));
if (!CollectionUtils.isEmpty(parametersObjectMap)) {
String result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parametersObjectMap);
result = result.substring(1, result.length() - 1);
stringBuilder.append(result);
html = html.replace("layout: \"StandaloneLayout\"", stringBuilder.toString());
}
return html;
}
private String addParameter(String html, String key, String value) {
StringBuilder stringBuilder = new StringBuilder("window.ui = SwaggerUIBundle({\n");
stringBuilder.append(key + ": \"" + value + "\",");
return html.replace("window.ui = SwaggerUIBundle({", stringBuilder.toString());
}
/**
* Add csrf string.
*
* @param html the html
* @return the string
*/
protected String addCSRF(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("\t\t\tconst value = `; ${document.cookie}`;\n");
stringBuilder.append("\t\t\tconst parts = value.split(`; ");
stringBuilder.append(swaggerUiConfig.getCsrf().getCookieName());
stringBuilder.append("=`);\n");
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
stringBuilder.append("\t\t\tif (isSameOrigin && parts.length === 2) ");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = parts.pop().split(';').shift();\n");
stringBuilder.append("\t\t\treturn request;\n");
stringBuilder.append("\t\t},\n");
stringBuilder.append("\t\t" + PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}
/**
* Add csrf string.
*
* @param html the html
* @return the string
*/
protected String addCSRFLocalStorage(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("\t\t\tconst value = window.localStorage.getItem('");
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
stringBuilder.append("\t\t\tif (isSameOrigin) ");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = value;\n");
stringBuilder.append("\t\t\treturn request;\n");
stringBuilder.append("\t\t},\n");
stringBuilder.append("\t\t" + PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}
/**
* Add csrf string from Session storage.
*
* @param html the html
* @return the string
*/
protected String addCSRFSessionStorage(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("\t\t\tconst value = window.sessionStorage.getItem('");
stringBuilder.append(swaggerUiConfig.getCsrf().getSessionStorageKey() + "');\n");
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
stringBuilder.append("\t\t\tif (isSameOrigin) ");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = value.replace(/['\"]+/g,'');\n");
stringBuilder.append("\t\t\treturn request;\n");
stringBuilder.append("\t\t},\n");
stringBuilder.append("\t\t" + PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}
/**
* Add syntax highlight string.
*
* @param html the html
* @return the string
*/
protected String addSyntaxHighlight(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("syntaxHighlight: {");
if (swaggerUiConfig.getSyntaxHighlight().getActivated() != null) {
stringBuilder.append("activated: ");
stringBuilder.append(swaggerUiConfig.getSyntaxHighlight().getActivated());
}
if (StringUtils.isNotEmpty(swaggerUiConfig.getSyntaxHighlight().getTheme())) {
if (swaggerUiConfig.getSyntaxHighlight().getActivated() != null)
stringBuilder.append(" , ");
stringBuilder.append("theme: \"");
stringBuilder.append(swaggerUiConfig.getSyntaxHighlight().getTheme());
stringBuilder.append("\"");
}
stringBuilder.append("},\n");
stringBuilder.append(PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}
}