Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #1401 add url rewrite rules for the salesforce handler for exte… #1403

Merged
merged 1 commit into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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