Add Unit Tests.
[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.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public final class CommonUtil {
30         private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
31         
32         public static String getJsonStrFromFile(String filePath) throws IOException {
33                 InputStream ins = null;
34         BufferedInputStream bins = null;
35         String fileContent = "";
36         String fileName = getAppRoot() + filePath;
37
38         try {
39             ins = new FileInputStream(fileName);
40             bins = new BufferedInputStream(ins);
41
42             byte[] contentByte = new byte[ins.available()];
43             int num = bins.read(contentByte);
44
45             if(num > 0) {
46                 fileContent = new String(contentByte);
47             }
48         } catch(FileNotFoundException e) {
49                 logger.error(fileName + "is not found!", e);
50         } finally {
51             if(ins != null) {
52                 ins.close();
53             }
54             if(bins != null) {
55                 bins.close();
56             }
57         }
58                 return fileContent;
59         }
60         
61         private static String getAppRoot() {
62         String appRoot = System.getProperty("catalina.base");
63         if(appRoot != null) {
64             appRoot = getCanonicalPath(appRoot);
65         }
66         return appRoot;
67     }
68
69     private static String getCanonicalPath(final String inPath) {
70         String path = null;
71         try {
72             if(inPath != null) {
73                 final File file = new File(inPath);
74                 path = file.getCanonicalPath();
75             }
76         } catch(final IOException e) {
77             logger.error("file.getCanonicalPath() IOException:", e);
78         }
79         return path;
80     }
81 }