1
+ /*
2
+ *
3
+ * * Copyright 2019-2020 the original author or authors.
4
+ * *
5
+ * * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * * you may not use this file except in compliance with the License.
7
+ * * You may obtain a copy of the License at
8
+ * *
9
+ * * https://www.apache.org/licenses/LICENSE-2.0
10
+ * *
11
+ * * Unless required by applicable law or agreed to in writing, software
12
+ * * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * * See the License for the specific language governing permissions and
15
+ * * limitations under the License.
16
+ *
17
+ */
18
+
19
+ package org .springdoc .ui ;
20
+
21
+ import org .apache .commons .lang3 .ArrayUtils ;
22
+ import org .apache .commons .lang3 .StringUtils ;
23
+ import org .springdoc .core .SpringDocConfigProperties ;
24
+ import org .springdoc .core .SwaggerUiConfigProperties ;
25
+
26
+ import org .springframework .beans .factory .InitializingBean ;
27
+ import org .springframework .beans .factory .annotation .Value ;
28
+ import org .springframework .web .util .UriComponentsBuilder ;
29
+
30
+ import static org .springdoc .core .Constants .SPRINGDOC_OAUTH2_REDIRECT_URL_VALUE ;
31
+ import static org .springdoc .core .Constants .SPRINGDOC_SWAGGER_UI_CONFIG_URL_VALUE ;
32
+ import static org .springdoc .core .Constants .SPRINGDOC_SWAGGER_UI_URL_VALUE ;
33
+ import static org .springdoc .core .Constants .SWAGGER_UI_OAUTH_REDIRECT_URL ;
34
+ import static org .springdoc .core .Constants .SWAGGGER_CONFIG_FILE ;
35
+ import static org .springframework .util .AntPathMatcher .DEFAULT_PATH_SEPARATOR ;
36
+
37
+
38
+ public abstract class AbstractSwaggerWelcome implements InitializingBean {
39
+
40
+ protected final SwaggerUiConfigProperties swaggerUiConfig ;
41
+
42
+ protected final SpringDocConfigProperties springDocConfigProperties ;
43
+
44
+ protected String uiRootPath ;
45
+
46
+ @ Value (SPRINGDOC_SWAGGER_UI_CONFIG_URL_VALUE )
47
+ private String originConfigUrl ;
48
+
49
+ @ Value (SPRINGDOC_OAUTH2_REDIRECT_URL_VALUE )
50
+ private String oauth2RedirectUrl ;
51
+
52
+ @ Value (SPRINGDOC_SWAGGER_UI_URL_VALUE )
53
+ private String swaggerUiUrl ;
54
+
55
+ public AbstractSwaggerWelcome (SwaggerUiConfigProperties swaggerUiConfig , SpringDocConfigProperties springDocConfigProperties ) {
56
+ this .swaggerUiConfig = swaggerUiConfig ;
57
+ this .springDocConfigProperties = springDocConfigProperties ;
58
+ }
59
+
60
+ @ Override
61
+ public void afterPropertiesSet () {
62
+ calculateUiRootPath ();
63
+ }
64
+
65
+
66
+ protected void calculateUiRootPath (StringBuilder ... sbUrls ) {
67
+ StringBuilder sbUrl = new StringBuilder ();
68
+ if (ArrayUtils .isNotEmpty (sbUrls ))
69
+ sbUrl = sbUrls [0 ];
70
+ String swaggerPath = swaggerUiConfig .getPath ();
71
+ if (swaggerPath .contains (DEFAULT_PATH_SEPARATOR ))
72
+ sbUrl .append (swaggerPath , 0 , swaggerPath .lastIndexOf (DEFAULT_PATH_SEPARATOR ));
73
+ this .uiRootPath = sbUrl .toString ();
74
+ }
75
+
76
+ protected String buildUrl (String contextPath , final String docsUrl ) {
77
+ if (contextPath .endsWith (DEFAULT_PATH_SEPARATOR )) {
78
+ return contextPath .substring (0 , contextPath .length () - 1 ) + docsUrl ;
79
+ }
80
+ return contextPath + docsUrl ;
81
+ }
82
+
83
+ void buildConfigUrl (String contextPath , UriComponentsBuilder uriComponentsBuilder ) {
84
+ String apiDocsUrl = springDocConfigProperties .getApiDocs ().getPath ();
85
+ if (StringUtils .isEmpty (originConfigUrl )) {
86
+ String url = buildUrl (contextPath , apiDocsUrl );
87
+ String swaggerConfigUrl = url + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE ;
88
+ swaggerUiConfig .setConfigUrl (swaggerConfigUrl );
89
+ if (SwaggerUiConfigProperties .getSwaggerUrls ().isEmpty ()) {
90
+ if (StringUtils .isEmpty (swaggerUiUrl ))
91
+ swaggerUiConfig .setUrl (url );
92
+ else
93
+ swaggerUiConfig .setUrl (swaggerUiUrl );
94
+ }
95
+ else
96
+ SwaggerUiConfigProperties .addUrl (url );
97
+ }
98
+ if (StringUtils .isEmpty (oauth2RedirectUrl )) {
99
+ swaggerUiConfig .setOauth2RedirectUrl (uriComponentsBuilder .path (this .uiRootPath ).path (SWAGGER_UI_OAUTH_REDIRECT_URL ).build ().toString ());
100
+ }
101
+ else if (!swaggerUiConfig .isValidUrl (swaggerUiConfig .getOauth2RedirectUrl ())) {
102
+ swaggerUiConfig .setOauth2RedirectUrl (uriComponentsBuilder .path (this .uiRootPath ).path (swaggerUiConfig .getOauth2RedirectUrl ()).build ().toString ());
103
+ }
104
+ }
105
+
106
+ }
0 commit comments