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