6fa38aa18cc7df0ba85f5a78d1f113dd694c4675
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / SwaggerConfig.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment;
22
23 import com.fasterxml.classmate.TypeResolver;
24 import com.google.common.base.Predicates;
25
26 import org.springframework.context.annotation.Bean;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
29 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
30
31 import springfox.documentation.builders.ApiInfoBuilder;
32 import springfox.documentation.builders.PathSelectors;
33 import springfox.documentation.builders.RequestHandlerSelectors;
34 import springfox.documentation.service.ApiInfo;
35 import springfox.documentation.spi.DocumentationType;
36 import springfox.documentation.spring.web.plugins.Docket;
37 import springfox.documentation.swagger2.annotations.EnableSwagger2;
38
39 /**
40  * Swagger configuration class that uses swagger2 documentation type and scans
41  * all the controllers. To access the swagger gui go to
42  * http://ip:port/swagger-ui.html
43  */
44 @Configuration
45 @EnableSwagger2
46 public class SwaggerConfig extends WebMvcConfigurationSupport {
47
48     static final String API_TITLE = "Enrichment Data service";
49     static final String DESCRIPTION = "This page lists all the rest apis for the service.";
50     static final String VERSION = "1.0";
51     @SuppressWarnings("squid:S1075") // Refactor your code to get this URI from a customizable parameter.
52     static final String RESOURCES_PATH = "classpath:/META-INF/resources/";
53     static final String WEBJARS_PATH = RESOURCES_PATH + "webjars/";
54     static final String SWAGGER_UI = "swagger-ui.html";
55     static final String WEBJARS = "/webjars/**";
56
57     /**
58      * Gets the API info.
59      *
60      * @return the API info.
61      */
62     @Bean
63     public Docket api(TypeResolver resolver) {
64         return new Docket(DocumentationType.SWAGGER_2) //
65             .apiInfo(apiInfo()) //
66             .select() //
67             .apis(RequestHandlerSelectors.any()) //
68             .paths(PathSelectors.any()) //
69             .paths(Predicates.not(PathSelectors.regex("/error"))) // this endpoint is not implemented, but was
70                                                                   // visible for Swagger
71             .paths(Predicates.not(PathSelectors.regex("/actuator.*"))) // this endpoint is implemented by spring
72                                                                        // framework, exclude for now
73             .build();
74     }
75
76     private static ApiInfo apiInfo() {
77         return new ApiInfoBuilder() //
78             .title(API_TITLE) //
79             .description(DESCRIPTION) //
80             .version(VERSION) //
81             .build();
82     }
83
84     @Override
85     protected void addResourceHandlers(ResourceHandlerRegistry registry) {
86         registry.addResourceHandler(SWAGGER_UI) //
87             .addResourceLocations(RESOURCES_PATH);
88
89         registry.addResourceHandler(WEBJARS) //
90             .addResourceLocations(WEBJARS_PATH);
91     }
92
93 }