use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.
the class EnvironmentController method convertToProperties.
private Map<String, Object> convertToProperties(Environment profiles) {
// Map of unique keys containing full map of properties for each unique
// key
Map<String, Map<String, Object>> map = new LinkedHashMap<>();
List<PropertySource> sources = new ArrayList<>(profiles.getPropertySources());
Collections.reverse(sources);
Map<String, Object> combinedMap = new TreeMap<>();
for (PropertySource source : sources) {
@SuppressWarnings("unchecked") Map<String, Object> value = (Map<String, Object>) source.getSource();
for (String key : value.keySet()) {
if (!key.contains("[")) {
// Not an array, add unique key to the map
combinedMap.put(key, value.get(key));
} else {
// An existing array might have already been added to the property map
// of an unequal size to the current array. Replace the array key in
// the current map.
key = key.substring(0, key.indexOf("["));
Map<String, Object> filtered = new TreeMap<>();
for (String index : value.keySet()) {
if (index.startsWith(key + "[")) {
filtered.put(index, value.get(index));
}
}
map.put(key, filtered);
}
}
}
// Combine all unique keys for array values into the combined map
for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
combinedMap.putAll(entry.getValue());
}
postProcessProperties(combinedMap);
return combinedMap;
}
use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.
the class EnvironmentRepositoryPropertySourceLocator method locate.
@Override
public org.springframework.core.env.PropertySource<?> locate(Environment environment) {
CompositePropertySource composite = new CompositePropertySource("configService");
for (PropertySource source : repository.findOne(name, profiles, label).getPropertySources()) {
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource();
composite.addPropertySource(new MapPropertySource(source.getName(), map));
}
return composite;
}
use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.
the class CipherEnvironmentEncryptor method decrypt.
private Environment decrypt(Environment environment, TextEncryptorLocator encryptor) {
Environment result = new Environment(environment);
for (PropertySource source : environment.getPropertySources()) {
Map<Object, Object> map = new LinkedHashMap<Object, Object>(source.getSource());
for (Map.Entry<Object, Object> entry : new LinkedHashSet<>(map.entrySet())) {
Object key = entry.getKey();
String name = key.toString();
String value = entry.getValue().toString();
if (value.startsWith("{cipher}")) {
map.remove(key);
try {
value = value.substring("{cipher}".length());
value = encryptor.locate(this.helper.getEncryptorKeys(name, StringUtils.arrayToCommaDelimitedString(environment.getProfiles()), value)).decrypt(this.helper.stripPrefix(value));
} catch (Exception e) {
value = "<n/a>";
name = "invalid." + name;
String message = "Cannot decrypt key: " + key + " (" + e.getClass() + ": " + e.getMessage() + ")";
if (logger.isDebugEnabled()) {
logger.debug(message, e);
} else if (logger.isWarnEnabled()) {
logger.warn(message);
}
}
map.put(name, value);
}
}
result.add(new PropertySource(source.getName(), map));
}
return result;
}
use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.
the class NativeEnvironmentRepository method clean.
protected Environment clean(Environment value) {
Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel(), this.version, value.getState());
for (PropertySource source : value.getPropertySources()) {
String name = source.getName();
if (this.environment.getPropertySources().contains(name)) {
continue;
}
name = name.replace("applicationConfig: [", "");
name = name.replace("]", "");
if (this.searchLocations != null) {
boolean matches = false;
String normal = name;
if (normal.startsWith("file:")) {
normal = StringUtils.cleanPath(new File(normal.substring("file:".length())).getAbsolutePath());
}
String profile = result.getProfiles() == null ? null : StringUtils.arrayToCommaDelimitedString(result.getProfiles());
for (String pattern : getLocations(result.getName(), profile, result.getLabel()).getLocations()) {
if (!pattern.contains(":")) {
pattern = "file:" + pattern;
}
if (pattern.startsWith("file:")) {
pattern = StringUtils.cleanPath(new File(pattern.substring("file:".length())).getAbsolutePath()) + "/";
}
if (logger.isTraceEnabled()) {
logger.trace("Testing pattern: " + pattern + " with property source: " + name);
}
if (normal.startsWith(pattern) && !normal.substring(pattern.length()).contains("/")) {
matches = true;
break;
}
}
if (!matches) {
// Don't include this one: it wasn't matched by our search locations
if (logger.isDebugEnabled()) {
logger.debug("Not adding property source: " + name);
}
continue;
}
}
logger.info("Adding property source: " + name);
result.add(new PropertySource(name, source.getSource()));
}
return result;
}
use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.
the class EnvironmentControllerTests method whenPlaceholders.
private void whenPlaceholders() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("foo", "bar");
this.environment.add(new PropertySource("one", map));
this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
}
Aggregations