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.webmvc.ui;
20
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import org.springdoc.core.SpringDocConfigProperties;
23 import org.springdoc.core.SpringDocConfiguration;
24 import org.springdoc.core.SwaggerUiConfigProperties;
25 import org.springdoc.core.SwaggerUiOAuthProperties;
26
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
34 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
35
36 import static org.springdoc.core.Constants.CLASSPATH_RESOURCE_LOCATION;
37 import static org.springdoc.core.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
38 import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
39 import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
40 import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
41
42
43 @Configuration
44 @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
45 @ConditionalOnBean(SpringDocConfiguration.class)
46 @SuppressWarnings("deprecation")
47 public class SwaggerConfig extends WebMvcConfigurerAdapter { // NOSONAR
48
49     @Value(SWAGGER_UI_PATH)
50     private String swaggerPath;
51
52     @Autowired
53     private SwaggerIndexTransformer swaggerIndexTransformer;
54
55     @Override
56     public void addResourceHandlers(ResourceHandlerRegistry registry) {
57         StringBuilder uiRootPath = new StringBuilder();
58         if (swaggerPath.contains("/"))
59             uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf('/'));
60         uiRootPath.append("/**");
61         registry.addResourceHandler(uiRootPath + "/swagger-ui/**")
62                 .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR)
63                 .resourceChain(false)
64                 .addTransformer(swaggerIndexTransformer);
65     }
66
67     @Bean
68     @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
69     SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) {
70         return new SwaggerWelcome(swaggerUiConfig, springDocConfigProperties);
71     }
72
73     @Bean
74     @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
75     SwaggerIndexTransformer indexPageTransformer(SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
76         return new SwaggerIndexTransformer(swaggerUiOAuthProperties, objectMapper);
77     }
78 }
79