PT 161583390 - Initial implementation to support context path
Added support for context path for boot 1.x and 2.x when property defined via command line args. Also added some relevant junits for request mappings with context paths.
This commit is contained in:
@@ -394,6 +394,24 @@ public abstract class AbstractSpringBootApp implements SpringBootApp {
|
||||
return getLiveConditionals(getAutoConfigReport(), getProcessID(), getProcessName());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publicly visible so that it can be tested via a mock app
|
||||
*
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.boot.app.cli;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ide.vscode.commons.util.StringUtil;
|
||||
|
||||
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 String getContextPath(String bootVersion, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String findContextPath(JSONObject env, String contextPathProp) {
|
||||
String contextPath = null;
|
||||
if (env != null) {
|
||||
contextPath = findInCommandLineArgs(env, contextPathProp);
|
||||
if (contextPath == null) {
|
||||
contextPath = findInApplicationConfig(env, contextPathProp);
|
||||
}
|
||||
}
|
||||
return contextPath;
|
||||
}
|
||||
|
||||
private static String findInApplicationConfig(JSONObject env, String contextPathProp) {
|
||||
// TODO Auto-generated method stub
|
||||
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");
|
||||
if (StringUtil.hasText(contextPathValue)) {
|
||||
return contextPathValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,6 +28,7 @@ public interface SpringBootApp extends Disposable {
|
||||
String getProcessID();
|
||||
String getHost() throws Exception;
|
||||
String getPort() throws Exception;
|
||||
String getContextPath() throws Exception;
|
||||
boolean isSpringBootApp();
|
||||
|
||||
String getEnvironment() throws Exception;
|
||||
|
||||
Reference in New Issue
Block a user