Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / SparkyResourceLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.config;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27
28 import org.springframework.context.ResourceLoaderAware;
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.ResourceLoader;
31
32 public class SparkyResourceLoader implements ResourceLoaderAware {
33   
34   
35   private static final String FILE_URI = "file:";
36   private ResourceLoader resourceLoader;
37   private String configHomeEnvVar;
38
39   // private static Logger LOG = LoggerFactory.getInstance().getLogger(SparkyResourceLoader.class);
40
41   @Override
42   public void setResourceLoader(ResourceLoader resourceLoader) {
43     this.resourceLoader = resourceLoader;
44   }
45   
46   public String getFullFileUri(String uriFilePath) {
47     return FILE_URI + System.getProperty(configHomeEnvVar) + uriFilePath;
48   }
49
50   public String getAbsolutePath(String uriFilePath) {
51     return System.getProperty(configHomeEnvVar) + uriFilePath;
52   }
53   
54   protected Resource getResource(String uriFilePath, boolean isRelative) {
55
56     String fileUri = uriFilePath;
57
58     if (!uriFilePath.startsWith("file:")) {
59       fileUri = "file:" + uriFilePath;
60     }
61
62     if (isRelative) {
63       return resourceLoader.getResource(getFullFileUri(fileUri));
64     } else {
65       return resourceLoader.getResource(fileUri);
66     }
67
68   }
69
70   public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
71     
72     Resource resource = getResource(uriFilePath, isRelativePath); 
73
74     if (resource.exists()) {
75       return resource.getFile();
76     }
77
78     return null;
79     
80   }
81
82   public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
83
84     Resource resource = getResource(uriFilePath, isRelativePath); 
85
86     if (resource.exists()) {
87       return getResourceAsBytes(resource);
88     }
89
90     return null;
91   }
92   
93   public byte[] getResourceAsBytes(Resource resource) throws IOException {
94
95     if ( resource != null && resource.exists()) {
96       return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
97     }
98
99     return null;
100   }
101
102   public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
103
104     Resource resource = getResource(uriFilePath, isRelativePath);
105
106     if (resource.exists()) {
107       return new String(getResourceAsBytes(resource));
108     }
109
110     return null;
111   }
112
113   public String getConfigHomeEnvVar() {
114     return configHomeEnvVar;
115   }
116
117   public void setConfigHomeEnvVar(String configHomeEnvVar) {
118     this.configHomeEnvVar = configHomeEnvVar;
119   }
120
121 }