965e965c62fab8b72ef96da6337176aae13e8c69
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.ccsdk.apps.controllerblueprints.core.utils
18
19 import org.apache.commons.io.FileUtils
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty
22 import com.att.eelf.configuration.EELFLogger
23 import com.att.eelf.configuration.EELFManager
24 import java.io.File
25 import java.net.URL
26 import java.nio.charset.Charset
27 /**
28  *
29  *
30  * @author Brinda Santh
31  */
32 object ResourceResolverUtils {
33     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
34
35     @JvmStatic
36     fun getFileContent(filename : String, basePath : String?): String {
37         log.trace("file ({}), basePath ({}) ", filename, basePath)
38         try{
39             var resolvedFileName : String = filename
40             if(filename.startsWith("http", true)
41                     || filename.startsWith("https", true)){
42                 val givenUrl : String = URL(filename).toString()
43                 val systemUrl : String = File(".").toURI().toURL().toString()
44                 log.trace("givenUrl ({}), systemUrl ({}) ", givenUrl, systemUrl)
45                 if(givenUrl.startsWith(systemUrl)){
46
47                 }
48             }else{
49                 if(!filename.startsWith("/")){
50                     if (checkNotEmpty(basePath)) {
51                         resolvedFileName = basePath.plus(File.separator).plus(filename)
52                     }else{
53                         resolvedFileName = javaClass.classLoader.getResource(".").path.plus(filename)
54                     }
55                 }
56             }
57             return FileUtils.readFileToString(File(resolvedFileName), Charset.defaultCharset())
58         }catch (e : Exception){
59             throw BluePrintException(e, "failed to file (%s), basePath (%s) ", filename, basePath)
60         }
61     }
62 }