Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / 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.rulemgt.resources;
18
19 import jakarta.ws.rs.Produces;
20 import jakarta.ws.rs.core.MediaType;
21 import lombok.extern.slf4j.Slf4j;
22 import org.springframework.web.bind.annotation.GetMapping;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.bind.annotation.RestController;
25
26 import java.io.*;
27 import java.net.URL;
28 import java.net.URLDecoder;
29
30 @RestController
31 @RequestMapping("/swagger.json")
32 @Slf4j
33 public class SwaggerResource {
34
35     @GetMapping
36     @Produces(MediaType.APPLICATION_JSON)
37     public String getSwaggerJson() {
38         URL url = SwaggerResource.class.getResource("/swagger.json");
39         String ret = "{}";
40
41         File file;
42         try {
43             System.out.println(URLDecoder.decode(url.getPath(), "UTF-8"));
44             file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
45         } catch(IOException e) {
46             log.warn("An error occurred while reading swagger.json.", e);
47             return ret;
48         }
49         try(BufferedReader br = new BufferedReader(new FileReader(file));)  {
50             StringBuffer buffer = new StringBuffer();
51             String line = " ";
52             while ((line = br.readLine()) != null) {
53                 buffer.append(line);
54             }
55             ret = buffer.toString();
56         } catch (FileNotFoundException e) {
57             log.warn("Failed to read the API description file.", e);
58         } catch (IOException e) {
59             log.warn("An error occurred while reading swagger.json.", e);
60         }
61         return ret;
62     }
63 }