Portal Login Filter reintegration
[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_URI)) {
59
60       if (isRelative) {
61         fileUri = getFullFileUri(fileUri);
62       } else {
63         fileUri = FILE_URI + uriFilePath;
64       }
65
66     }
67
68     return resourceLoader.getResource(fileUri);
69
70   }
71
72   public File getResourceAsFile(String uriFilePath, boolean isRelativePath) throws IOException {
73
74     Resource resource = getResource(uriFilePath, isRelativePath);
75
76     if (resource.exists()) {
77       return resource.getFile();
78     }
79
80     return null;
81
82   }
83
84   public byte[] getResourceAsBytes(String uriFilePath, boolean isRelativePath) throws IOException {
85
86     Resource resource = getResource(uriFilePath, isRelativePath);
87
88     if (resource.exists()) {
89       return getResourceAsBytes(resource);
90     }
91
92     return null;
93   }
94
95   public byte[] getResourceAsBytes(Resource resource) throws IOException {
96
97     if (resource != null && resource.exists()) {
98       return Files.readAllBytes(Paths.get(resource.getFile().getAbsolutePath()));
99     }
100
101     return null;
102   }
103
104   public String getResourceAsString(String uriFilePath, boolean isRelativePath) throws IOException {
105
106     Resource resource = getResource(uriFilePath, isRelativePath);
107
108     if (resource.exists()) {
109       return new String(getResourceAsBytes(resource));
110     }
111
112     return null;
113   }
114
115   public String getConfigHomeEnvVar() {
116     return configHomeEnvVar;
117   }
118
119   public void setConfigHomeEnvVar(String configHomeEnvVar) {
120     this.configHomeEnvVar = configHomeEnvVar;
121   }
122
123 }