From bfab5dbc0fddd39b1fa61d30dda821dddeea310d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Mon, 17 Aug 2020 23:30:41 +0800 Subject: [PATCH] upgrade TirePathTree 1.1, strongly tree path match --- .../sureness/matcher/util/TirePathTree.java | 122 ++++++++---------- .../matcher/util/TirePathTreeTest.java | 4 +- 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java b/core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java index ef58645..21f1168 100644 --- a/core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java +++ b/core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java @@ -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; } /** diff --git a/core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java b/core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java index b1733e2..33c2320 100644 --- a/core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java +++ b/core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java @@ -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")); } -- GitLab