提交 bfab5dbc 编写于 作者: sinat_25235033's avatar sinat_25235033

upgrade TirePathTree 1.1, strongly tree path match

上级 3016b9a4
......@@ -135,42 +135,7 @@ public class TirePathTree {
// 模式匹配 * **
Node current = root;
String matchRole;
if (current.getChildren().containsKey(urlPac[0])) {
matchRole = searchPathRole(current.getChildren().get(urlPac[0]), urlPac, 0, method);
if (matchRole != null) {
return matchRole;
}
}
if (current.getChildren().containsKey(MATCH_ONE)) {
matchRole = searchPathRole(current.getChildren().get(MATCH_ONE), urlPac, 0, method);
if (matchRole != null) {
return matchRole;
}
}
if (current.getChildren().containsKey(MATCH_ALL)) {
Node matchAllNode = current.getChildren().get(MATCH_ALL);
if (NODE_TYPE_MAY_PATH_END.equals(matchAllNode.getNodeType())) {
Node methodNode = matchAllNode.getChildren().get(method);
if (methodNode != null && NODE_TYPE_METHOD.equals(methodNode.getNodeType())) {
return methodNode.getChildren().keySet().iterator().next();
}
} else {
for (String key : matchAllNode.getChildren().keySet()) {
int flow = 1;
while (flow < urlPac.length) {
if (key.equals(urlPac[flow])) {
matchRole = searchPathRole(matchAllNode.getChildren().get(key), urlPac, flow, method);
if (matchRole != null) {
return matchRole;
}
}
flow ++;
}
}
}
}
return null;
return searchPathRoleInChildren(current, urlPac, -1, method);
}
......@@ -192,20 +157,62 @@ public class TirePathTree {
if (isNoMatchString(current.getData(), urlPac[currentFlow])) {
return null;
}
if (currentFlow == urlPac.length - 1) {
if (NODE_TYPE_MAY_PATH_END.equals(current.getNodeType())) {
Node methodNode = current.getChildren().get(method);
if (methodNode != null && NODE_TYPE_METHOD.equals(methodNode.getNodeType())) {
return methodNode.getChildren().keySet().iterator().next();
} else {
return null;
}
if (currentFlow == urlPac.length - 1 && NODE_TYPE_MAY_PATH_END.equals(current.getNodeType())) {
Node methodNode = current.getChildren().get(method);
if (methodNode != null && NODE_TYPE_METHOD.equals(methodNode.getNodeType())) {
return methodNode.getChildren().keySet().iterator().next();
} else {
return null;
}
}
String matchRole;
String matchRole = null;
if (current.getData().equals(urlPac[currentFlow])) {
matchRole = searchPathRoleInChildren(current, urlPac, currentFlow, method);
if (matchRole != null) {
return matchRole;
}
}
if (current.getData().equals(MATCH_ONE)) {
matchRole = searchPathRoleInChildren(current, urlPac, currentFlow - 1, method);
if (matchRole != null) {
return matchRole;
}
matchRole = searchPathRoleInChildren(current, urlPac, currentFlow, method);
if (matchRole != null) {
return matchRole;
}
}
if (current.getData().equals(MATCH_ALL)) {
matchRole = searchPathRoleInChildren(current, urlPac, currentFlow - 1, method);
if (matchRole != null) {
return matchRole;
}
matchRole = searchPathRoleInChildren(current, urlPac, currentFlow, method);
if (matchRole != null) {
return matchRole;
}
matchRole = searchPathRole(current, urlPac, currentFlow + 1, method);
}
return matchRole;
}
/**
* 从当前node匹配下一节点
* @param current 当前node
* @param urlPac urlPath字符串组
* @param currentFlow 当前第一个字符串
* @param method http请求方法
* @return 匹配到返回[role,role2] 匹配不到返回null
*/
private String searchPathRoleInChildren(Node current, String[] urlPac, int currentFlow, String method) {
if (current == null || urlPac == null || currentFlow >= urlPac.length - 1
|| currentFlow < -1 || method == null || "".equals(method)) {
return null;
}
String matchRole = null;
String nextUrlPac = urlPac[currentFlow + 1];
if (current.getChildren().containsKey(nextUrlPac)) {
matchRole = searchPathRole(current.getChildren().get(nextUrlPac), urlPac, currentFlow + 1, method);
......@@ -214,34 +221,17 @@ public class TirePathTree {
}
}
if (current.getChildren().containsKey(MATCH_ONE)) {
matchRole = searchPathRole(current.getChildren().get(MATCH_ONE), urlPac, currentFlow + 1, method);
Node matchOneNode = current.getChildren().get(MATCH_ONE);
matchRole = searchPathRole(matchOneNode, urlPac, currentFlow + 1, method);
if (matchRole != null) {
return matchRole;
}
}
if (current.getChildren().containsKey(MATCH_ALL)) {
Node matchAllNode = current.getChildren().get(MATCH_ALL);
if (NODE_TYPE_MAY_PATH_END.equals(matchAllNode.getNodeType())) {
Node methodNode = matchAllNode.getChildren().get(method);
if (methodNode != null && NODE_TYPE_METHOD.equals(methodNode.getNodeType())) {
return methodNode.getChildren().keySet().iterator().next();
}
} else {
for (String key : matchAllNode.getChildren().keySet()) {
int flow = currentFlow;
while (flow < urlPac.length) {
if (key.equals(urlPac[flow])) {
matchRole = searchPathRole(matchAllNode.getChildren().get(key), urlPac, flow, method);
if (matchRole != null) {
return matchRole;
}
}
flow ++;
}
}
}
matchRole = searchPathRole(matchAllNode, urlPac, currentFlow + 1, method);
}
return null;
return matchRole;
}
/**
......
......@@ -59,8 +59,9 @@ public class TirePathTreeTest {
paths.add("/api/demo/book/*/egg===get===[role1]");
paths.add("/api/demo/book/**/egg===get===[role2]");
paths.add("/**===get===[role9]");
paths.add("/er/**/swagger===get===[role10]");
root.buildTree(paths);
Assert.assertEquals(20, root.getResourceNum());
Assert.assertEquals(21, root.getResourceNum());
}
@Test
......@@ -92,6 +93,7 @@ public class TirePathTreeTest {
Assert.assertEquals("[role6]", root.searchPathFilterRoles("/api/v5/mom/ha===put"));
Assert.assertEquals("[role9]", root.searchPathFilterRoles("/api/v5/mom/ha/good===get"));
Assert.assertEquals("[role9]", root.searchPathFilterRoles("/api/v5/mom/ha===get"));
Assert.assertEquals("[role10]", root.searchPathFilterRoles("/er/swagger===get"));
Assert.assertNull(root.searchPathFilterRoles("/api/v6/book/ha/good===put"));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册