io.github.lc.oss.commons.encryption.config.EncryptedConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of encryption.config Show documentation
Show all versions of encryption.config Show documentation
An implementation of key/value pair supporting encryption at rest
The newest version!
package io.github.lc.oss.commons.encryption.config;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import io.github.lc.oss.commons.util.TypedEnumCache;
public abstract class EncryptedConfig {
public static class User {
private final String username;
private final String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
}
private final TypedEnumCache, ConfigKey> keyCache;
private final Map map = new HashMap<>();
public > EncryptedConfig(Class keyType) {
this.keyCache = new TypedEnumCache<>(keyType);
}
@JsonAnySetter
public void set(String key, Object value) {
if (!this.keyCache.hasName(key)) {
this.logError(String.format("Ignoring unknown key '%s'.", key));
return;
}
this.set(this.keyCache.byName(key), value);
}
@SuppressWarnings("unchecked")
public void set(ConfigKey key, Object value) {
if (value instanceof Map) {
Map map = (Map) value;
if (map.containsKey("username") && map.containsKey("password") && map.size() == 2) {
this.map.put(key, new User(map.get("username"), map.get("password")));
} else {
this.map.put(key, value);
}
} else {
this.map.put(key, value);
}
}
public Object get(ConfigKey key) {
return this.map.get(key);
}
protected void logError(String message) {
}
}