Skip to content

Commit

Permalink
APP-3072: Load parent URL configuration with Spring Boot (#243)
Browse files Browse the repository at this point in the history
When Spring Boot was used the overriding mechanism we introduced where
you can define the host, scheme, port and context at the top level and
also override it for each sub component (agent, pod, ...) was not used.

The logic has been builtin directly in the POJOs so it is always applied
either when we use Jackson to deserialize or the Spring Boot
configuration framework.
  • Loading branch information
symphony-youri authored Sep 25, 2020
1 parent 69ba5bf commit 3b0efc9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.symphony.bdk.core.config.exception.BdkConfigException;
import com.symphony.bdk.core.config.legacy.LegacyConfigMapper;
import com.symphony.bdk.core.config.legacy.model.LegacySymConfig;
import com.symphony.bdk.core.config.model.BdkClientConfig;
import com.symphony.bdk.core.config.model.BdkConfig;

import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down Expand Up @@ -53,8 +52,7 @@ public static BdkConfig loadFromInputStream(InputStream inputStream) throws BdkC
if (jsonNode != null) {
JSON_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (jsonNode.at("/botUsername").isMissingNode()) {
BdkConfig config = JSON_MAPPER.convertValue(jsonNode, BdkConfig.class);
return setUpGlobalValueConfig(config);
return JSON_MAPPER.convertValue(jsonNode, BdkConfig.class);
} else {
LegacySymConfig legacySymConfig = JSON_MAPPER.convertValue(jsonNode, LegacySymConfig.class);
return LegacyConfigMapper.map(legacySymConfig);
Expand Down Expand Up @@ -103,27 +101,4 @@ public static BdkConfig loadFromClasspath(String configPath) throws BdkConfigExc
throw new BdkConfigException("Config file is not found");
}

private static BdkConfig setUpGlobalValueConfig(BdkConfig config) {
config.setPod(setUpClientConfig(config, config.getPod()));
config.setAgent(setUpClientConfig(config, config.getAgent()));
config.setKeyManager(setUpClientConfig(config, config.getKeyManager()));
config.setSessionAuth(setUpClientConfig(config, config.getSessionAuth()));
return config;
}

private static BdkClientConfig setUpClientConfig(BdkConfig config, BdkClientConfig clientConfig) {
if (clientConfig.getHost() == null) {
clientConfig.setHost(config.getHost());
}
if (clientConfig.getPort() == null) {
clientConfig.setPort(config.getPort());
}
if (clientConfig.getScheme() == null) {
clientConfig.setScheme(config.getScheme());
}
if (clientConfig.getContext() == null) {
clientConfig.setContext(config.getContext());
}
return clientConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,75 @@
import lombok.Getter;
import lombok.Setter;

import java.util.function.Supplier;

@Getter
@Setter
public class BdkClientConfig {

private String scheme = null;
private String host = null;
private Integer port = null;
private String context = null;
private BdkConfig parentConfig;

private String proxyUrl = null;
private String proxyUsername = null;
private String proxyPassword = null;
private String scheme = null;
private String host = null;
private Integer port = null;
private String context = null;

private Integer connectTimeout = null;
private Integer readTimeout = null;
private Integer connectionRequestTimeout = null;
private String proxyUrl = null;
private String proxyUsername = null;
private String proxyPassword = null;

private Integer connectTimeout = null;
private Integer readTimeout = null;
private Integer connectionRequestTimeout = null;

public String getBasePath() {
return this.scheme + "://" + this.host + this.getPortAsString() + this.getFormattedContext();
}
public BdkClientConfig() {
// for Jackson deserialization
}

public String getFormattedContext() {
if (this.context == null) {
return "";
}
if (!this.context.equals("") && this.context.charAt(0) != '/') {
this.context = "/" + this.context;
}
if (!this.context.equals("") && this.context.endsWith("/")) {
this.context = this.context.substring(0, this.context.length() - 1);
}

return this.context;
}
public BdkClientConfig(BdkConfig parentConfig) {
this.parentConfig = parentConfig;
}

public String getScheme() {
return thisOrParent(scheme, parentConfig::getScheme);
}

public String getHost() {
return thisOrParent(host, parentConfig::getHost);
}

public Integer getPort() {
return thisOrParent(port, parentConfig::getPort);
}

public String getContext() {
return thisOrParent(context, parentConfig::getContext);
}

private String getPortAsString() {
return this.port != null ? ":" + this.port : "";
private <T> T thisOrParent(T thisValue, Supplier<T> parentValue) {
return thisValue == null ? parentValue.get() : thisValue;
}

public String getBasePath() {
return this.getScheme() + "://" + this.getHost() + this.getPortAsString() + this.getFormattedContext();
}

public String getFormattedContext() {
final String localContext = this.getContext();
if (localContext == null) {
return "";
}
if (!localContext.equals("") && localContext.charAt(0) != '/') {
return "/" + localContext;
}
if (!localContext.equals("") && localContext.endsWith("/")) {
return localContext.substring(0, localContext.length() - 1);
}

return localContext;
}

private String getPortAsString() {
return this.getPort() != null ? ":" + this.getPort() : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ public class BdkConfig {
private static final String DEFAULT_SCHEME = "https";
private static final int DEFAULT_HTTPS_PORT = 443;

// the targeted URLs can be defined globally here and overridden for each agent, pod, keyManager, sessionAuth config
private String scheme = DEFAULT_SCHEME;
private String host;
private Integer port = DEFAULT_HTTPS_PORT;
private String context = "";

private BdkClientConfig agent = new BdkClientConfig();
private BdkClientConfig pod = new BdkClientConfig();
private BdkClientConfig keyManager = new BdkClientConfig();
private BdkClientConfig sessionAuth = new BdkClientConfig();
private BdkClientConfig agent = new BdkClientConfig(this);
private BdkClientConfig pod = new BdkClientConfig(this);
private BdkClientConfig keyManager = new BdkClientConfig(this);
private BdkClientConfig sessionAuth = new BdkClientConfig(this);

private BdkBotConfig bot = new BdkBotConfig();
private BdkExtAppConfig app = new BdkExtAppConfig();
Expand All @@ -47,4 +48,25 @@ public boolean isOboConfigured() {
public BdkRetryConfig getDatafeedRetryConfig() {
return datafeed.getRetry() == null ? retry : datafeed.getRetry();
}

public void setAgent(BdkClientConfig agent) {
this.agent = attachParent(agent);
}

public void setPod(BdkClientConfig pod) {
this.pod = attachParent(pod);
}

public void setKeyManager(BdkClientConfig keyManager) {
this.keyManager = attachParent(keyManager);
}

public void setSessionAuth(BdkClientConfig sessionAuth) {
this.sessionAuth = attachParent(sessionAuth);
}

private BdkClientConfig attachParent(BdkClientConfig config) {
config.setParentConfig(this);
return config;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
domain: devx1.symphony.com

bdk:
pod:
scheme: https
host: ${domain}

agent:
scheme: https
host: ${domain}

keyManager:
scheme: https
host: ${domain}

host: devx1.symphony.com
bot:
username: tibot
privateKeyPath: ${user.home}/local/conf/agent/privateKey.pem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class SymphonyBdkAutoConfigurationTest {

@Test
public void shouldLoadContextWithSuccess() {
void shouldLoadContextWithSuccess() {

final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues(
Expand Down Expand Up @@ -44,4 +44,26 @@ public void shouldLoadContextWithSuccess() {
assertThat(context).doesNotHaveBean("sessionAuthApiClient");
});
}

@Test
void shouldLoadParentConfigurationIfSet() {

final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues(
"bdk.scheme=http",
"bdk.host=localhost",
"bdk.context=context",

"bdk.bot.username=tibot",
"bdk.bot.privateKeyPath=classpath:/privatekey.pem"
)
.withUserConfiguration(SymphonyBdkMockedConfiguration.class)
.withConfiguration(AutoConfigurations.of(SymphonyBdkAutoConfiguration.class));

contextRunner.run(context -> {
final SymphonyBdkCoreProperties config = context.getBean(SymphonyBdkCoreProperties.class);

assertThat(config.getAgent().getBasePath()).isEqualTo("http://localhost:443/context");
});
}
}

0 comments on commit 3b0efc9

Please sign in to comment.