sonar fix: Rsrc handling nokia/CommonUtil
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / common / util / CommonUtil.java
1 /*
2  *  Copyright 2016-2017, Nokia 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
18 package org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util;
19
20 import java.io.BufferedInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class CommonUtil {
32         private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
33         
34         public static String getJsonStrFromFile(String filePath) throws IOException {
35         String fileName = getAppRoot() + filePath;
36         String fileContent = getJsonStrFromFilePath(fileName);
37                 return fileContent;
38         }
39
40         public static String getJsonStrFromFilePath(String fileName) throws IOException {
41         String fileContent = "";
42
43         try (InputStream ins = new FileInputStream(fileName);
44             BufferedInputStream bins = new BufferedInputStream(ins)){
45
46             byte[] contentByte = new byte[ins.available()];
47             int num = bins.read(contentByte);
48
49             if(num > 0) {
50                 fileContent = new String(contentByte);
51             }
52         } catch(FileNotFoundException e) {
53                 logger.error(fileName + " is not found!", e);
54         } 
55                 return fileContent;
56         }
57         
58         public static String getAppRoot() {
59         String appRoot = System.getProperty("catalina.base");
60         if(appRoot != null) {
61             appRoot = getCanonicalPath(appRoot);
62         }
63         return appRoot;
64     }
65
66     private static String getCanonicalPath(final String inPath) {
67         String path = null;
68         try {
69             if(inPath != null) {
70                 final File file = new File(inPath);
71                 path = file.getCanonicalPath();
72             }
73         } catch(final IOException e) {
74             logger.error("file.getCanonicalPath() IOException:", e);
75         }
76         return path;
77     }
78     
79     public static byte[] getBytes(String filePath){  
80             byte[] buffer = null;  
81             File file = new File(filePath);  
82             try(FileInputStream fis = new FileInputStream(file);  
83                             ByteArrayOutputStream bos = new ByteArrayOutputStream(1000)){
84                     byte[] b = new byte[1000];  
85                     int n;  
86                     while ((n = fis.read(b)) != -1) {  
87                             bos.write(b, 0, n);  
88                     }  
89                     buffer = bos.toByteArray();  
90             } catch (FileNotFoundException e) {  
91                     logger.error("file " + filePath + " is not found.", e);
92             } catch (IOException e) {  
93                     logger.error("file " + filePath + " IOException.", e); 
94             }  
95             return buffer;  
96     }  
97  
98 }
99