Skip to content

Commit

Permalink
fixes #1401 add url rewrite rules for the salesforce handler for exte…
Browse files Browse the repository at this point in the history
…rnal (#1403)
  • Loading branch information
stevehu committed Oct 14, 2022
1 parent 8b11804 commit 2f06f06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import com.networknt.handler.config.UrlRewriteRule;
import com.networknt.proxy.PathPrefixAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,7 +30,7 @@ public class SalesforceConfig {
public static final String PROXY_HOST = "proxyHost";
public static final String PROXY_PORT = "proxyPort";
public static final String ENABLE_HTTP2 = "enableHttps";

List<UrlRewriteRule> urlRewriteRules;
public static final String PATH_PREFIX_AUTHS = "pathPrefixAuths";
public static final String SERVICE_HOST = "serviceHost";

Expand All @@ -56,6 +57,7 @@ private SalesforceConfig(String configName) {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setUrlRewriteRules();
setConfigList();
}
public static SalesforceConfig load() {
Expand Down Expand Up @@ -127,6 +129,26 @@ public List<PathPrefixAuth> getPathPrefixAuths() {
public void setPathPrefixAuths(List<PathPrefixAuth> pathPrefixAuths) {
this.pathPrefixAuths = pathPrefixAuths;
}
public List<UrlRewriteRule> getUrlRewriteRules() {
return urlRewriteRules;
}

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) {
for (String s : rules) {
urlRewriteRules.add(UrlRewriteRule.convertToUrlRewriteRule(s));
}
}
}
}
public void setUrlRewriteRules(List<UrlRewriteRule> urlRewriteRules) {
this.urlRewriteRules = urlRewriteRules;
}

private void setConfigData() {
Object object = mappedConfig.get(ENABLED);
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.networknt.config.TlsUtil;
import com.networknt.handler.Handler;
import com.networknt.handler.MiddlewareHandler;
import com.networknt.handler.config.UrlRewriteRule;
import com.networknt.monad.Failure;
import com.networknt.monad.Result;
import com.networknt.monad.Success;
Expand Down Expand Up @@ -41,6 +42,7 @@
import java.text.MessageFormat;
import java.time.Duration;
import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -113,6 +115,26 @@ public void reload() {
public void handleRequest(HttpServerExchange exchange) throws Exception {
String requestPath = exchange.getRequestPath();
if(logger.isTraceEnabled()) logger.trace("requestPath = " + requestPath);

// handle the url rewrite here.
if(config.getUrlRewriteRules() != null && config.getUrlRewriteRules().size() > 0) {
boolean matched = false;
for(UrlRewriteRule rule : config.getUrlRewriteRules()) {
Matcher matcher = rule.getPattern().matcher(requestPath);
if(matcher.matches()) {
matched = true;
requestPath = matcher.replaceAll(rule.getReplace());
if(logger.isTraceEnabled()) logger.trace("rewritten requestPath = " + requestPath);
break;
}
}
// if no matched rule in the list, use the original requestPath.
if(!matched) requestPath = exchange.getRequestPath();
} else {
// there is no url rewrite rules, so use the original requestPath
requestPath = exchange.getRequestPath();
}

// make sure that the request path is in the key set. remember that key set only contains prefix not the full request path.
for(PathPrefixAuth pathPrefixAuth: config.getPathPrefixAuths()) {
if(requestPath.startsWith(pathPrefixAuth.getPathPrefix())) {
Expand Down

0 comments on commit 2f06f06

Please sign in to comment.