2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.config;
26 import com.google.gson.JsonElement;
28 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
32 import org.apache.commons.io.IOUtils;
33 import org.onap.clamp.clds.util.JsonUtils;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.core.env.Environment;
37 import org.springframework.stereotype.Component;
40 * Holds Clamp properties and add some functionalities.
43 public class ClampProperties {
46 private ApplicationContext appContext;
48 private Environment env;
49 public static final String CONFIG_PREFIX = "clamp.config.";
54 * @param key The first key
55 * @return The string with the value
57 public String getStringValue(String key) {
58 return env.getProperty(CONFIG_PREFIX + key);
62 * get property value for a combo key (key1 + "." + key2). If not found just use
65 * @param key1 The first key
66 * @param key2 The second key after a dot
67 * @return The string with the value
69 public String getStringValue(String key1, String key2) {
70 String value = getStringValue(key1 + "." + key2);
71 if (value == null || value.length() == 0) {
72 value = getStringValue(key1);
78 * Return json as objects that can be updated. The value obtained from the
79 * clds-reference file will be used as a filename.
81 * @param key The key that will be used to access the clds-reference file
83 * @throws IOException In case of issues with the JSON parser
85 public JsonElement getJsonTemplate(String key) throws IOException {
86 String fileReference = getStringValue(key);
87 return (fileReference != null)
88 ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class)
93 * Return json as objects that can be updated. First try with combo key (key1 +
94 * "." + key2), otherwise default to just key1. The value obtained from the
95 * clds-reference file will be used as a filename.
97 * @param key1 The first key
98 * @param key2 The second key after a dot
100 * @throws IOException In case of issues with the JSON parser
102 public JsonElement getJsonTemplate(String key1, String key2) throws IOException {
103 String fileReference = getStringValue(key1, key2);
104 return (fileReference != null)
105 ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class)
110 * Return the file content. The value obtained from the clds-reference file will
111 * be used as a filename.
113 * @param key The key that will be used to access the clds-reference file
114 * @return File content in String
115 * @throws IOException In case of issues with the JSON parser
117 public String getFileContent(String key) throws IOException {
118 String fileReference = getStringValue(key);
119 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
123 * Return the file content. First try with combo key (key1 + "." + key2),
124 * otherwise default to just key1. The value obtained from the clds-reference
125 * file will be used as a filename.
127 * @param key1 The first key
128 * @param key2 The second key after a dot
129 * @return File content in String
130 * @throws IOException In case of issues with the JSON parser
132 public String getFileContent(String key1, String key2) throws IOException {
133 String fileReference = getStringValue(key1, key2);
134 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
137 private String getFileContentFromPath(String filepath) throws IOException {
138 URL url = appContext.getResource(filepath).getURL();
139 return IOUtils.toString(url, StandardCharsets.UTF_8);