Enrichment Coordination Service
[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.oransc.enrichment.controllers.consumer.ConsumerEiJobInfo;
27 import org.springframework.context.annotation.Bean;
28 import org.springframework.context.annotation.Configuration;
29 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
30 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
31
32 import springfox.documentation.builders.ApiInfoBuilder;
33 import springfox.documentation.builders.PathSelectors;
34 import springfox.documentation.builders.RequestHandlerSelectors;
35 import springfox.documentation.service.ApiInfo;
36 import springfox.documentation.spi.DocumentationType;
37 import springfox.documentation.spring.web.plugins.Docket;
38 import springfox.documentation.swagger2.annotations.EnableSwagger2;
39
40 /**
41  * Swagger configuration class that uses swagger2 documentation type and scans
42  * all the controllers. To access the swagger gui go to
43  * http://ip:port/swagger-ui.html
44  */
45 @Configuration
46 @EnableSwagger2
47 public class SwaggerConfig extends WebMvcConfigurationSupport {
48
49     static final String API_TITLE = "Enrichment Data service";
50     static final String DESCRIPTION = "This page lists all the rest apis for the service.";
51     static final String VERSION = "1.0";
52     @SuppressWarnings("squid:S1075") // Refactor your code to get this URI from a customizable parameter.
53     static final String RESOURCES_PATH = "classpath:/META-INF/resources/";
54     static final String WEBJARS_PATH = RESOURCES_PATH + "webjars/";
55     static final String SWAGGER_UI = "swagger-ui.html";
56     static final String WEBJARS = "/webjars/**";
57
58     /**
59      * Gets the API info.
60      *
61      * @return the API info.
62      */
63     @Bean
64     public Docket api(TypeResolver resolver) {
65         return new Docket(DocumentationType.SWAGGER_2) //
66             .apiInfo(apiInfo()) //
67             .additionalModels(resolver.resolve(ConsumerEiJobInfo.class)) //
68             .select() //
69             .apis(RequestHandlerSelectors.any()) //
70             .paths(PathSelectors.any()) //
71             .paths(Predicates.not(PathSelectors.regex("/error"))) //
72             // this endpoint is not implemented, but was visible for Swagger
73             .paths(Predicates.not(PathSelectors.regex("/actuator.*"))) //
74             // this endpoint is implemented by spring framework, exclude for now
75             .build();
76     }
77
78     private static ApiInfo apiInfo() {
79         return new ApiInfoBuilder() //
80             .title(API_TITLE) //
81             .description(DESCRIPTION) //
82             .version(VERSION) //
83             .build();
84     }
85
86     @Override
87     protected void addResourceHandlers(ResourceHandlerRegistry registry) {
88         registry.addResourceHandler(SWAGGER_UI) //
89             .addResourceLocations(RESOURCES_PATH);
90
91         registry.addResourceHandler(WEBJARS) //
92             .addResourceLocations(WEBJARS_PATH);
93     }
94
95 }