47bb55f1e612229994554d72cff15cf8e20471ac
[vfc/nfvo/driver/vnfm/svnfm.git] /
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                 InputStream ins = null;
42         BufferedInputStream bins = null;
43         String fileContent = "";
44
45         try {
46             ins = new FileInputStream(fileName);
47             bins = new BufferedInputStream(ins);
48
49             byte[] contentByte = new byte[ins.available()];
50             int num = bins.read(contentByte);
51
52             if(num > 0) {
53                 fileContent = new String(contentByte);
54             }
55         } catch(FileNotFoundException e) {
56                 logger.error(fileName + " is not found!", e);
57         } finally {
58             if(ins != null) {
59                 ins.close();
60             }
61             if(bins != null) {
62                 bins.close();
63             }
64         }
65                 return fileContent;
66         }
67         
68         public static String getAppRoot() {
69         String appRoot = System.getProperty("catalina.base");
70         if(appRoot != null) {
71             appRoot = getCanonicalPath(appRoot);
72         }
73         return appRoot;
74     }
75
76     private static String getCanonicalPath(final String inPath) {
77         String path = null;
78         try {
79             if(inPath != null) {
80                 final File file = new File(inPath);
81                 path = file.getCanonicalPath();
82             }
83         } catch(final IOException e) {
84             logger.error("file.getCanonicalPath() IOException:", e);
85         }
86         return path;
87     }
88     
89     public static byte[] getBytes(String filePath){  
90         byte[] buffer = null;  
91         try {  
92             File file = new File(filePath);  
93             FileInputStream fis = new FileInputStream(file);  
94             ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
95             byte[] b = new byte[1000];  
96             int n;  
97             while ((n = fis.read(b)) != -1) {  
98                 bos.write(b, 0, n);  
99             }  
100             fis.close();  
101             bos.close();  
102             buffer = bos.toByteArray();  
103         } catch (FileNotFoundException e) {  
104                 logger.error("file " + filePath + " is not found.", e);
105         } catch (IOException e) {  
106                 logger.error("file " + filePath + " IOException.", e); 
107         }  
108         return buffer;  
109     }  
110  
111 }
112