Skip to content

Commit

Permalink
fixes #2130 update ExternalServiceConfig to support JSON string for u…
Browse files Browse the repository at this point in the history
…rl rules and host mapping
  • Loading branch information
stevehu committed Feb 14, 2024
1 parent fa5934a commit df9d95d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import com.networknt.config.JsonMapper;
import com.networknt.handler.config.UrlRewriteRule;

import java.util.ArrayList;
Expand Down Expand Up @@ -129,15 +130,29 @@ public List<UrlRewriteRule> getUrlRewriteRules() {

public void setUrlRewriteRules() {
this.urlRewriteRules = new ArrayList<>();
if (mappedConfig.get("urlRewriteRules") !=null && mappedConfig.get("urlRewriteRules") instanceof String) {
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule((String)mappedConfig.get("urlRewriteRules")));
} else {
List<String> rules = (List)mappedConfig.get("urlRewriteRules");
if(rules != null) {
if(mappedConfig.get("urlRewriteRules") != null) {
if (mappedConfig.get("urlRewriteRules") instanceof String) {
String s = (String)mappedConfig.get("urlRewriteRules");
s = s.trim();
// There are two formats for the urlRewriteRules. One is a string separated by a space
// and the other is a list of strings separated by a space in JSON list format.
if(s.startsWith("[")) {
// multiple rules
List<String> rules = (List<String>) JsonMapper.fromJson(s, List.class);
for (String rule : rules) {
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(rule));
}
} else {
// single rule
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s));
}
} else if (mappedConfig.get("urlRewriteRules") instanceof List) {
List<String> rules = (List)mappedConfig.get("urlRewriteRules");
for (String s : rules) {
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s));
}
}

}
}

Expand All @@ -150,12 +165,26 @@ private void setConfigList() {
Object object = mappedConfig.get(PATH_HOST_MAPPINGS);
pathHostMappings = new ArrayList<>();
if(object instanceof String) {
// there is only one path to host available, split the string for path and host.
String[] parts = ((String)object).split(" ");
if(parts.length != 2) {
throw new ConfigException("path host entry must have two elements separated by a space.");
String s = (String)object;
s = s.trim();
if(s.startsWith("[")) {
// multiple path to host mappings
List<String> mappings = (List<String>) JsonMapper.fromJson(s, List.class);
for (String mapping : mappings) {
String[] parts = mapping.split(" ");
if(parts.length != 2) {
throw new ConfigException("path host entry must have two elements separated by a space.");
}
pathHostMappings.add(parts);
}
} else {
// there is only one path to host available, split the string for path and host.
String[] parts = s.split(" ");
if(parts.length != 2) {
throw new ConfigException("path host entry must have two elements separated by a space.");
}
pathHostMappings.add(parts);
}
pathHostMappings.add(parts);
} else if (object instanceof List) {
List<String> maps = (List<String>)object;
for(String s: maps) {
Expand Down
11 changes: 6 additions & 5 deletions ingress-proxy/src/test/resources/config/values.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ conquest.pathPrefixAuths: [{"pathPrefix":"/conquest","authIssuer":"example","aut

# external-service.yml
externalService.enabled: true
externalService.pathHostMappings:
- /sharepoint https://sharepoint.microsoft.com
- /get https://postman-echo.com
- /post https://postman-echo.com
- /abc https://abc.com
externalService.pathHostMappings: "[\"/sharepoint https://sharepoint.microsoft.com\",\"/get https://postman-echo.com\",\"/post https://postman-echo.com\",\"/abc https://abc.com\"]"
#externalService.pathHostMappings:
# - /sharepoint https://sharepoint.microsoft.com
# - /get https://postman-echo.com
# - /post https://postman-echo.com
# - /abc https://abc.com

0 comments on commit df9d95d

Please sign in to comment.