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