Optimize ApiConfig.java

This commit is contained in:
lancer.hong
2021-10-30 18:15:28 +08:00
parent fba5927966
commit 76caa7c71f
2 changed files with 36 additions and 41 deletions

View File

@@ -24,7 +24,26 @@ public class UrlTransformUtils {
private UrlTransformUtils() {}
public static final FizzGatewayUrlAntPathMatcher ANT_PATH_MATCHER = new FizzGatewayUrlAntPathMatcher();
public static final FizzGatewayUrlAntPathMatcher ANT_PATH_MATCHER = new FizzGatewayUrlAntPathMatcher();
public static boolean isAntPathPattern(String path) {
boolean uriVar = false;
int pl = path.length();
for (int i = 0; i < pl; i++) {
char c = path.charAt(i);
if (c == '*' || c == '?') {
return true;
}
if (c == '{') {
uriVar = true;
continue;
}
if (c == '}' && uriVar) {
return true;
}
}
return false;
}
/**
* transform the backend path to the real backend request path
@@ -36,9 +55,9 @@ public class UrlTransformUtils {
* @throws IllegalArgumentException The number of capturing groups in the pattern segment does not match the number of URI template variables it defines
*/
public static String transform(String frontendPath, String backendPath, String reqPath) {
Assert.hasText(frontendPath, "frontend path cannot be null");
Assert.hasText(backendPath, "backend path cannot be null");
Assert.hasText(reqPath, "req path cannot be null");
// Assert.hasText(frontendPath, "frontend path cannot be null");
// Assert.hasText(backendPath, "backend path cannot be null");
// Assert.hasText(reqPath, "req path cannot be null");
String bp = backendPath;
Map<String, String> variables = ANT_PATH_MATCHER.extractUriTemplateVariables(frontendPath, reqPath);
for (Map.Entry<String, String> entry : variables.entrySet()) {
@@ -49,9 +68,9 @@ public class UrlTransformUtils {
backendPath = backendPath.replaceAll("\\{[^/]*}", "");
}
if (log.isDebugEnabled()) {
log.debug("req: " + reqPath + ", frontend: " + frontendPath + ", backend: " + bp + ", target: " + backendPath);
}
// if (log.isDebugEnabled()) {
// log.debug("req path: " + reqPath + ", frontend path: " + frontendPath + ", backend path: " + bp + ", target path: " + backendPath);
// }
return backendPath;
}

View File

@@ -53,25 +53,21 @@ public class ApiConfig {
public static final char ALLOW = 'a';
// public static final char FORBID = 'f';
public static final String ALL_METHOD = "AM";
private static final String match_all = "/**";
private static final int ENABLE = 1;
// private static final int UNABLE = 0;
@JsonProperty(
access = JsonProperty.Access.WRITE_ONLY
)
public int id;
@JsonProperty(
access = JsonProperty.Access.WRITE_ONLY
)
public int id; // tb_api_auth.id
@JsonProperty(
access = JsonProperty.Access.WRITE_ONLY
)
public int isDeleted = 0; // tb_api_auth.is_deleted
public int isDeleted = 0;
public Set<String> gatewayGroups = Stream.of(GatewayGroup.DEFAULT).collect(Collectors.toCollection(LinkedHashSet::new));
@@ -129,24 +125,6 @@ public class ApiConfig {
public long retryInterval = 0;
public static boolean isAntPathPattern(String path) {
boolean uriVar = false;
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
if (c == '*' || c == '?') {
return true;
}
if (c == '{') {
uriVar = true;
continue;
}
if (c == '}' && uriVar) {
return true;
}
}
return false;
}
public void setGatewayGroup(String ggs) {
gatewayGroups.remove(GatewayGroup.DEFAULT);
if (StringUtils.isBlank(ggs)) {
@@ -167,7 +145,7 @@ public class ApiConfig {
path = match_all;
} else {
path = p.trim();
if (!isAntPathPattern(path)) {
if (!UrlTransformUtils.isAntPathPattern(path)) {
exactMatch = true;
}
}
@@ -213,13 +191,13 @@ public class ApiConfig {
@Override
public int hashCode() {
return id;
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ApiConfig) {
ApiConfig that = (ApiConfig) obj;
public boolean equals(Object o) {
if (o instanceof ApiConfig) {
ApiConfig that = (ApiConfig) o;
return this.id == that.id;
}
return false;
@@ -231,8 +209,6 @@ public class ApiConfig {
.method( request.getMethod())
.backendService(this.backendService)
.backendPath( this.backendPath)
// .query( WebUtils.getClientReqQuery(exchange))
// .pluginConfigs( this.pluginConfigs)
.rpcMethod( this.rpcMethod)
.rpcParamTypes( this.rpcParamTypes)
.rpcGroup( this.rpcGroup)