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