FIX ALL POSSIBLE SONAR ISSUES IN HOLMES
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / SwaggerResource.java
1 /*
2  * Copyright 2017 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 java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.net.URLDecoder;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.core.MediaType;
29 import lombok.extern.slf4j.Slf4j;
30 import org.jvnet.hk2.annotations.Service;
31
32 @Service
33 @Path("/swagger.json")
34 @Produces(MediaType.APPLICATION_JSON)
35 @Slf4j
36 public class SwaggerResource {
37
38     @GET
39     @Produces(MediaType.APPLICATION_JSON)
40     public String getSwaggerJson() {
41         URL url = SwaggerResource.class.getResource("/swagger.json");
42         String ret = "{}";
43         File file = null;
44
45         try {
46             System.out.println(URLDecoder.decode(url.getPath(), "UTF-8"));
47             file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
48         } catch (IOException e) {
49             log.warn("An error occurred while decoding url");
50         }
51
52         if (file == null) {
53             log.warn("Unable to get Swagger Json since API description file could not be read");
54             return ret;
55         }
56
57         try(BufferedReader br = new BufferedReader(new FileReader(file))) {
58             StringBuffer buffer = new StringBuffer();
59             String line = " ";
60             while ((line = br.readLine()) != null) {
61                 buffer.append(line);
62             }
63             ret = buffer.toString();
64         } catch (IOException e) {
65             log.warn("An error occurred while reading swagger.json.");
66         }
67         return ret;
68     }
69 }