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 java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import org.apache.commons.io.IOUtils;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.core.env.Environment;
33 import org.springframework.stereotype.Component;
36 * Holds Clamp properties and add some functionalities.
39 public class ClampProperties {
42 private ApplicationContext appContext;
44 private Environment env;
45 public static final String CONFIG_PREFIX = "clamp.config.";
50 * @param key The first key
51 * @return The string with the value
53 public String getStringValue(String key) {
54 return env.getProperty(CONFIG_PREFIX + key);
58 * get property value for a combo key (key1 + "." + key2). If not found just use
61 * @param key1 The first key
62 * @param key2 The second key after a dot
63 * @return The string with the value
65 public String getStringValue(String key1, String key2) {
66 String value = getStringValue(key1 + "." + key2);
67 if (value == null || value.length() == 0) {
68 value = getStringValue(key1);
74 * Return the file content. The value obtained from the clds-reference file will
75 * be used as a filename.
77 * @param key The key that will be used to access the clds-reference file
78 * @return File content in String
79 * @throws IOException In case of issues with the JSON parser
81 public String getFileContent(String key) throws IOException {
82 String fileReference = getStringValue(key);
83 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
87 * Return the file content. First try with combo key (key1 + "." + key2),
88 * otherwise default to just key1. The value obtained from the clds-reference
89 * file will be used as a filename.
91 * @param key1 The first key
92 * @param key2 The second key after a dot
93 * @return File content in String
94 * @throws IOException In case of issues with the JSON parser
96 public String getFileContent(String key1, String key2) throws IOException {
97 String fileReference = getStringValue(key1, key2);
98 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
101 private String getFileContentFromPath(String filepath) throws IOException {
102 URL url = appContext.getResource(filepath).getURL();
103 return IOUtils.toString(url, StandardCharsets.UTF_8);