Simplified context path implementation

For boot 1.x and 2.x, now iterate through all property sources in the
order read from env. This avoids the need to have special handling for
prioritising the different types of property sources (e.g. command line
args, env vars, app config).
This commit is contained in:
nsingh
2018-11-09 16:15:41 -08:00
parent a8757a4491
commit 41d64d5577

View File

@@ -11,7 +11,6 @@
package org.springframework.ide.vscode.commons.boot.app.cli;
import java.util.Collection;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -26,56 +25,37 @@ public class ContextPath {
protected static Logger logger = LoggerFactory.getLogger(ContextPath.class);
public static final Collection<String> BOOT_1X_CONTEXTPATH = ImmutableList.of("server.context-path",
"server.contextPath");
"server.contextPath", "SERVER_CONTEXT_PATH");
public static final Collection<String> BOOT_2X_CONTEXTPATH = ImmutableList.of("server.servlet.context-path",
"server.servlet.contextPath");
"server.servlet.contextPath", "SERVER_SERVLET_CONTEXT_PATH");
public static String getContextPath(String bootVersion, String environment) {
String contextPath = null;
if (environment != null) {
JSONObject env = new JSONObject(environment);
Collection<String> contextPathProperties = null;
if ("1.x".equals(bootVersion)) {
contextPathProperties = BOOT_1X_CONTEXTPATH;
contextPath = findContextPathInBoot1x(env);
} else if ("2.x".equals(bootVersion)) {
contextPathProperties = BOOT_2X_CONTEXTPATH;
}
if (contextPathProperties != null) {
for (String prop : contextPathProperties) {
String contextPath = findContextPath(env, prop);
if (StringUtil.hasText(contextPath)) {
return contextPath;
}
}
contextPath = findContextPathInBoot2x(env);
}
}
return null;
}
private static String findContextPath(JSONObject env, String contextPathProp) {
String contextPath = null;
if (env != null) {
// Properties defined in command line args have higher priority over
// those defined in application configuration files (properties/yaml files)
contextPath = findInCommandLineArgs(env, contextPathProp);
if (contextPath == null) {
contextPath = findInApplicationConfig(env, contextPathProp);
}
}
return contextPath;
}
private static String findInApplicationConfig(JSONObject env, String contextPathProp) {
// boot 1.x
JSONObject applicationConfig = null;
private static String findContextPathInBoot1x(JSONObject env) {
// IMPORTANT: The order in which the env objects appear are assumed to be the
// priority order defined
// by boot rules in terms of which property source has higher precedence. Iterate
// through ALL
// sources in the order obtained from the env JSON
for (String key : env.keySet()) {
if (key.startsWith("applicationConfig")) {
applicationConfig = env.getJSONObject(key);
if (applicationConfig != null) {
String contextPathValue = applicationConfig.optString(contextPathProp);
JSONObject jsonObj = env.optJSONObject(key);
if (jsonObj != null) {
for (String prop : BOOT_1X_CONTEXTPATH) {
String contextPathValue = jsonObj.optString(prop);
// Warning: fetching value above may return empty string, so null check on the
// value is not enough
if (StringUtil.hasText(contextPathValue)) {
@@ -84,70 +64,27 @@ public class ContextPath {
}
}
}
// boot 2.x
if (applicationConfig == null) {
// Not found as direct property value... in Boot 2.0 we must look inside the
// 'propertySources'.
// Similar... but structure is more complex.
JSONArray propertySources = env.optJSONArray("propertySources");
if (propertySources != null) {
for (Object _source : propertySources) {
if (_source instanceof JSONObject) {
JSONObject source = (JSONObject) _source;
String sourceName = source.optString("name");
if (sourceName != null && sourceName.startsWith("applicationConfig")) {
JSONObject props = source.optJSONObject("properties");
Set<String> keySet = props.keySet();
// Check that the context is a key before retrieving the JSON object value.
// Note: attempting to fetch the JSON object value on a key that may not exist
// throws exception
// thus the reason why we are checking that the key exists first
if (keySet.contains(contextPathProp)) {
JSONObject jsonObject = props.getJSONObject(contextPathProp);
if (jsonObject != null) {
String contextPathValue = jsonObject.optString("value");
if (StringUtil.hasText(contextPathValue)) {
return contextPathValue;
}
}
}
}
}
}
}
}
return null;
}
protected static String findInCommandLineArgs(JSONObject env, String contextPathProp) {
// boot 1.x
JSONObject commandLineArgs = env.optJSONObject("commandLineArgs");
if (commandLineArgs != null) {
String contextPathValue = commandLineArgs.optString(contextPathProp);
// Warning: fetching value above may return empty string, so null check on the
// value is not enough
if (StringUtil.hasText(contextPathValue)) {
return contextPathValue;
}
}
// boot 2.x
if (commandLineArgs == null) {
// Not found as direct property value... in Boot 2.0 we must look inside the
// 'propertySources'.
// Similar... but structure is more complex.
JSONArray propertySources = env.optJSONArray("propertySources");
if (propertySources != null) {
for (Object _source : propertySources) {
if (_source instanceof JSONObject) {
JSONObject source = (JSONObject) _source;
String sourceName = source.optString("name");
if ("commandLineArgs".equals(sourceName)) {
JSONObject props = source.optJSONObject("properties");
// Find the contextPathProp in the command line args
JSONObject valueObject = props.optJSONObject(contextPathProp);
if (valueObject != null) {
String contextPathValue = valueObject.optString("value");
private static String findContextPathInBoot2x(JSONObject env) {
JSONArray propertySources = env.optJSONArray("propertySources");
if (propertySources != null) {
// IMPORTANT: The order in which the env objects appear are assumed to be the
// priority order defined
// by boot rules in terms of which property source has higher precedence. Iterate
// through ALL
// sources in the order obtained from the env JSON
for (Object _source : propertySources) {
if (_source instanceof JSONObject) {
JSONObject source = (JSONObject) _source;
JSONObject props = source.optJSONObject("properties");
if (props != null) {
for (String property : BOOT_2X_CONTEXTPATH) {
JSONObject propertyObj = props.optJSONObject(property);
if (propertyObj != null) {
String contextPathValue = propertyObj.optString("value");
if (StringUtil.hasText(contextPathValue)) {
return contextPathValue;
}