Merge both boot 1.x and 2.x context path properties

This commit is contained in:
nsingh
2018-11-08 17:44:56 -08:00
parent 9179d5d167
commit 29e52b38ae
2 changed files with 8 additions and 32 deletions

View File

@@ -397,19 +397,7 @@ public abstract class AbstractSpringBootApp implements SpringBootApp {
@Override
public String getContextPath() throws Exception {
String environment = getEnvironment();
String bootVersion = null;
// Boot 1.x
Object result = getActuatorDataFromAttribute(getObjectName("type=Endpoint,name=requestMappingEndpoint"), "Data");
if (result != null) {
bootVersion = "1.x";
}
// Boot 2.x
result = getActuatorDataFromOperation(getObjectName("type=Endpoint,name=Mappings"), "mappings");
if (result != null) {
bootVersion = "2.x";
}
return bootVersion != null && environment != null ? ContextPath.getContextPath(bootVersion, environment) : null;
return environment != null ? ContextPath.getContextPath(environment) : null;
}
/**

View File

@@ -22,29 +22,17 @@ public class ContextPath {
protected static Logger logger = LoggerFactory.getLogger(ContextPath.class);
public static final String[] BOOT_1X_CONTEXTPATH = { "server.context-path", "server.contextPath",
"SERVER_CONTEXT_PATH" };
public static final String[] BOOT_2X_CONTEXTPATH = { "server.servlet.context-path", "server.servlet.contextPath",
"SERVER_SERVLET_CONTEXT_PATH" };
public static final String[] CONTEXTPATH = { "server.context-path", "server.contextPath", "SERVER_CONTEXT_PATH",
"server.servlet.context-path", "server.servlet.contextPath", "SERVER_SERVLET_CONTEXT_PATH" };
public static String getContextPath(String bootVersion, String environment) {
public static String getContextPath(String environment) {
if (environment != null) {
JSONObject env = new JSONObject(environment);
String[] contextPathProperties = null;
if ("1.x".equals(bootVersion)) {
contextPathProperties = BOOT_1X_CONTEXTPATH;
} 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;
}
for (String prop : CONTEXTPATH) {
String contextPath = findContextPath(env, prop);
if (StringUtil.hasText(contextPath)) {
return contextPath;
}
}
}