Switched from Dropwizard to Springboot
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / SwaggerResource.java
1 /*
2  * Copyright 2017-2022 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.engine.resources;
18
19 import lombok.extern.slf4j.Slf4j;
20 import org.springframework.web.bind.annotation.GetMapping;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.bind.annotation.RestController;
23
24 import jakarta.ws.rs.core.MediaType;
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.net.URL;
30 import java.net.URLDecoder;
31
32 @Slf4j
33 @RestController
34 @RequestMapping("/swagger.json")
35 public class SwaggerResource {
36
37     @GetMapping(produces = MediaType.APPLICATION_JSON)
38     public String getSwaggerJson() {
39         URL url = SwaggerResource.class.getResource("/swagger.json");
40         String ret = "{}";
41         File file = null;
42
43         try {
44             System.out.println(URLDecoder.decode(url.getPath(), "UTF-8"));
45             file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
46         } catch (IOException e) {
47             log.warn("An error occurred while decoding url");
48         }
49
50         if (file == null) {
51             log.warn("Unable to get Swagger Json since API description file could not be read");
52             return ret;
53         }
54
55         try (BufferedReader br = new BufferedReader(new FileReader(file))) {
56             StringBuffer buffer = new StringBuffer();
57             String line = " ";
58             while ((line = br.readLine()) != null) {
59                 buffer.append(line);
60             }
61             ret = buffer.toString();
62         } catch (IOException e) {
63             log.warn("An error occurred while reading swagger.json.");
64         }
65         return ret;
66     }
67 }