Skip to content

Commit

Permalink
fixes #1332 refactor salesforce handle to support multiple configurat…
Browse files Browse the repository at this point in the history
…ions (#1333)
  • Loading branch information
stevehu committed Aug 10, 2022
1 parent 486ee75 commit 28835ad
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,85 @@

import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import com.networknt.config.JsonMapper;
import io.undertow.server.handlers.PathTemplateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SalesforceConfig {
private static final Logger logger = LoggerFactory.getLogger(SalesforceConfig.class);

public static final String CONFIG_NAME = "salesforce";
private static final String ENABLED = "enabled";
private static final String TOKEN_URL = "tokenUrl";
private static final String AUTH_ISSUER = "authIssuer";
private static final String AUTH_SUBJECT = "authSubject";
private static final String AUTH_AUDIENCE = "authAudience";
private static final String CERT_FILENAME = "certFilename";
private static final String CERT_PASSWORD = "certPassword";
private static final String IV = "iv";
private static final String TOKEN_TTL = "tokenTtl";
private static final String WAIT_LENGTH = "waitLength";
private static final String PROXY_HOST = "proxyHost";
private static final String PROXY_PORT = "proxyPort";
private static final String ENABLE_HTTP2 = "enableHttps";
private static final String APPLIED_PATH_PREFIXES = "appliedPathPrefixes";
private static final String SERVICE_HOST = "serviceHost";
public static final String ENABLED = "enabled";
public static final String TOKEN_URL = "tokenUrl";
public static final String AUTH_ISSUER = "authIssuer";
public static final String AUTH_SUBJECT = "authSubject";
public static final String AUTH_AUDIENCE = "authAudience";
public static final String CERT_FILENAME = "certFilename";
public static final String CERT_PASSWORD = "certPassword";
public static final String IV = "iv";
public static final String TOKEN_TTL = "tokenTtl";
public static final String WAIT_LENGTH = "waitLength";
public static final String PROXY_HOST = "proxyHost";
public static final String PROXY_PORT = "proxyPort";
public static final String ENABLE_HTTP2 = "enableHttps";
public static final String PATH_PREFIX_AUTH = "pathPrefixAuth";
public static final String MORNING_STAR = "morningStar";
public static final String CONQUEST = "conquest";
public static final String ADVISOR_HUB = "advisorHub";
public static final String SERVICE_HOST = "serviceHost";

boolean enabled;
String tokenUrl;
String authIssuer;
String authSubject;
String authAudience;
String certFilename;
String certPassword;
String iv;
int tokenTtl;
int waitLength;
String proxyHost;
int proxyPort;
boolean enableHttp2;
List<String> appliedPathPrefixes;
Map<String, Object> pathPrefixAuth;
Map<String, Object> morningStar;
Map<String, Object> conquest;
Map<String, Object> advisorHub;

String serviceHost;
private Config config;
private Map<String, Object> mappedConfig;

public SalesforceConfig() {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigList();
private SalesforceConfig() {
this(CONFIG_NAME);
}

/**
* Please note that this constructor is only for testing to load different config files
* to test different configurations.
* @param configName String
*/
public SalesforceConfig(String configName) {
private SalesforceConfig(String configName) {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setConfigList();
setConfigMap();
}
public static SalesforceConfig load() {
return new SalesforceConfig();
}

public static SalesforceConfig load(String configName) {
return new SalesforceConfig(configName);
}

void reload() {
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigList();
setConfigMap();
}

public boolean isEnabled() {
Expand All @@ -84,22 +99,6 @@ public void setTokenUrl(String tokenUrl) {
this.tokenUrl = tokenUrl;
}

public String getAuthIssuer() {
return authIssuer;
}

public void setAuthIssuer(String authIssuer) {
this.authIssuer = authIssuer;
}

public String getAuthSubject() {
return authSubject;
}

public void setAuthSubject(String authSubject) {
this.authSubject = authSubject;
}

public String getAuthAudience() {
return authAudience;
}
Expand All @@ -124,14 +123,6 @@ public void setCertPassword(String certPassword) {
this.certPassword = certPassword;
}

public String getIv() {
return iv;
}

public void setIv(String iv) {
this.iv = iv;
}

public int getTokenTtl() {
return tokenTtl;
}
Expand Down Expand Up @@ -180,13 +171,16 @@ public void setServiceHost(String serviceHost) {
this.serviceHost = serviceHost;
}

public List<String> getAppliedPathPrefixes() {
return appliedPathPrefixes;
public Map<String, Object> getPathPrefixAuth() {
return pathPrefixAuth;
}

public void setAppliedPathPrefixes(List<String> appliedPathPrefixes) {
this.appliedPathPrefixes = appliedPathPrefixes;
public void setPathPrefixAuth(Map<String, Object> pathPrefixAuth) {
this.pathPrefixAuth = pathPrefixAuth;
}
public Map<String, Object> getMorningStar() { return morningStar; }
public Map<String, Object> getConquest() { return conquest; }
public Map<String, Object> getAdvisorHub() { return advisorHub; }

private void setConfigData() {
Object object = mappedConfig.get(ENABLED);
Expand All @@ -197,14 +191,6 @@ private void setConfigData() {
if(object != null) {
setTokenUrl((String) object);
}
object = mappedConfig.get(AUTH_ISSUER);
if(object != null) {
setAuthIssuer((String) object);
}
object = mappedConfig.get(AUTH_SUBJECT);
if(object != null) {
setAuthSubject((String) object);
}
object = mappedConfig.get(AUTH_AUDIENCE);
if(object != null) {
setAuthAudience((String) object);
Expand All @@ -217,10 +203,6 @@ private void setConfigData() {
if(object != null) {
setCertPassword((String) object);
}
object = mappedConfig.get(IV);
if(object != null) {
setIv((String) object);
}
object = mappedConfig.get(TOKEN_TTL);
if (object != null) {
setTokenTtl((int) object);
Expand All @@ -247,20 +229,125 @@ private void setConfigData() {
}
}

private void setConfigList() {
if (mappedConfig.get(APPLIED_PATH_PREFIXES) != null) {
Object object = mappedConfig.get(APPLIED_PATH_PREFIXES);
appliedPathPrefixes = new ArrayList<>();
private void setConfigMap() {
// path prefix auth mapping
if (mappedConfig.get(PATH_PREFIX_AUTH) != null) {
Object object = mappedConfig.get(PATH_PREFIX_AUTH);
pathPrefixAuth = new HashMap<>();
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("pathPrefixAuth s = " + s);
if(s.startsWith("{")) {
// json format
try {
pathPrefixAuth = JsonMapper.string2Map(s);
} catch (Exception e) {
throw new ConfigException("could not parse the pathPrefixAuth json with a map of string and object.");
}
} else {
// comma separated
String[] pairs = s.split(",");
for (int i = 0; i < pairs.length; i++) {
String pair = pairs[i];
String[] keyValue = pair.split(":");
pathPrefixAuth.put(keyValue[0], keyValue[1]);
}
}
} else if (object instanceof Map) {
pathPrefixAuth = (Map)object;
} else {
throw new ConfigException("pathPrefixAuth must be a string object map.");
}
}
// MorningStar map
if (mappedConfig.get(MORNING_STAR) != null) {
Object object = mappedConfig.get(MORNING_STAR);
morningStar = new HashMap<>();
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("morningStar s = " + s);
if(s.startsWith("{")) {
// json format
try {
morningStar = JsonMapper.string2Map(s);
} catch (Exception e) {
throw new ConfigException("could not parse the morningStar json with a map of string and object.");
}
} else {
// comma separated
String[] pairs = s.split(",");
for (int i = 0; i < pairs.length; i++) {
String pair = pairs[i];
String[] keyValue = pair.split(":");
morningStar.put(keyValue[0], keyValue[1]);
}
}
} else if (object instanceof Map) {
morningStar = (Map)object;
} else {
throw new ConfigException("morningStar must be a string object map.");
}
}
// Conquest map
if (mappedConfig.get(CONQUEST) != null) {
Object object = mappedConfig.get(CONQUEST);
conquest = new HashMap<>();
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("conquest s = " + s);
if(s.startsWith("{")) {
// json format
try {
conquest = JsonMapper.string2Map(s);
} catch (Exception e) {
throw new ConfigException("could not parse the conquest json with a map of string and object.");
}
} else {
// comma separated
String[] pairs = s.split(",");
for (int i = 0; i < pairs.length; i++) {
String pair = pairs[i];
String[] keyValue = pair.split(":");
conquest.put(keyValue[0], keyValue[1]);
}
}
} else if (object instanceof Map) {
conquest = (Map)object;
} else {
throw new ConfigException("conquest must be a string object map.");
}
}
// AdvisorHub map
if (mappedConfig.get(ADVISOR_HUB) != null) {
Object object = mappedConfig.get(ADVISOR_HUB);
advisorHub = new HashMap<>();
if(object instanceof String) {
// there is only one path available
appliedPathPrefixes.add((String)object);
} else if (object instanceof List) {
List prefixes = (List)object;
prefixes.forEach(item -> {
appliedPathPrefixes.add((String)item);
});
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("advisorHub s = " + s);
if(s.startsWith("{")) {
// json format
try {
advisorHub = JsonMapper.string2Map(s);
} catch (Exception e) {
throw new ConfigException("could not parse the advisorHub json with a map of string and object.");
}
} else {
// comma separated
String[] pairs = s.split(",");
for (int i = 0; i < pairs.length; i++) {
String pair = pairs[i];
String[] keyValue = pair.split(":");
advisorHub.put(keyValue[0], keyValue[1]);
}
}
} else if (object instanceof Map) {
advisorHub = (Map)object;
} else {
throw new ConfigException("appliedPathPrefixes must be a string or a list of strings.");
throw new ConfigException("advisorHub must be a string object map.");
}
}
}
Expand Down
Loading

0 comments on commit 28835ad

Please sign in to comment.