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 java .io .ByteArrayOutputStream ;
22
+ import java .io .IOException ;
23
+ import java .io .InputStream ;
24
+ import java .nio .charset .StandardCharsets ;
25
+
26
+ import com .fasterxml .jackson .core .JsonProcessingException ;
27
+ import com .fasterxml .jackson .databind .ObjectMapper ;
28
+ import org .slf4j .Logger ;
29
+ import org .slf4j .LoggerFactory ;
30
+ import org .springdoc .core .SwaggerUiOAuthProperties ;
31
+ import reactor .core .publisher .Mono ;
32
+
33
+ import org .springframework .core .io .Resource ;
34
+ import org .springframework .util .AntPathMatcher ;
35
+ import org .springframework .util .CollectionUtils ;
36
+ import org .springframework .web .reactive .resource .ResourceTransformer ;
37
+ import org .springframework .web .reactive .resource .ResourceTransformerChain ;
38
+ import org .springframework .web .reactive .resource .TransformedResource ;
39
+ import org .springframework .web .server .ServerWebExchange ;
40
+
41
+ public class SwaggerIndexTransformer implements ResourceTransformer {
42
+
43
+ private static final Logger LOGGER = LoggerFactory .getLogger (SwaggerIndexTransformer .class );
44
+
45
+ private SwaggerUiOAuthProperties swaggerUiOAuthProperties ;
46
+
47
+ private ObjectMapper objectMapper ;
48
+
49
+ public SwaggerIndexTransformer (SwaggerUiOAuthProperties swaggerUiOAuthProperties , ObjectMapper objectMapper ) {
50
+ this .swaggerUiOAuthProperties = swaggerUiOAuthProperties ;
51
+ this .objectMapper = objectMapper ;
52
+ }
53
+
54
+ @ Override
55
+ public Mono <Resource > transform (ServerWebExchange serverWebExchange , Resource resource , ResourceTransformerChain resourceTransformerChain ) {
56
+ final AntPathMatcher antPathMatcher = new AntPathMatcher ();
57
+ boolean isIndexFound = false ;
58
+ try {
59
+ isIndexFound = antPathMatcher .match ("**/swagger-ui/**/index.html" , resource .getURL ().toString ());
60
+ if (isIndexFound && !CollectionUtils .isEmpty (swaggerUiOAuthProperties .getConfigParameters ())) {
61
+ String html = readFullyAsString (resource .getInputStream ());
62
+ html = addInitOauth (html );
63
+ return Mono .just (new TransformedResource (resource , html .getBytes ()));
64
+ }
65
+ else {
66
+ return Mono .just (resource );
67
+ }
68
+ }
69
+ catch (Exception e ) {
70
+ throw new RuntimeException (e );
71
+ }
72
+ }
73
+
74
+ private String addInitOauth (String html ) throws JsonProcessingException {
75
+ StringBuilder stringBuilder = new StringBuilder ("window.ui = ui\n " );
76
+ stringBuilder .append ("ui.initOAuth(\n " );
77
+ String json = objectMapper .writeValueAsString (swaggerUiOAuthProperties .getConfigParameters ());
78
+ stringBuilder .append (json );
79
+ stringBuilder .append (")" );
80
+ return html .replace ("window.ui = ui" , stringBuilder .toString ());
81
+ }
82
+
83
+ private String readFullyAsString (InputStream inputStream )
84
+ throws IOException {
85
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
86
+ byte [] buffer = new byte [1024 ];
87
+ int length ;
88
+ while ((length = inputStream .read (buffer )) != -1 ) {
89
+ baos .write (buffer , 0 , length );
90
+ }
91
+ return baos .toString (StandardCharsets .UTF_8 .name ());
92
+ }
93
+
94
+ }
0 commit comments