Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lzh_me
Sureness
提交
bfab5dbc
Sureness
项目概览
lzh_me
/
Sureness
与 Fork 源项目一致
Fork自
sureness / Sureness
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Sureness
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bfab5dbc
编写于
8月 17, 2020
作者:
sinat_25235033
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upgrade TirePathTree 1.1, strongly tree path match
上级
3016b9a4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
59 addition
and
67 deletion
+59
-67
core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java
...in/java/com/usthe/sureness/matcher/util/TirePathTree.java
+56
-66
core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java
...ava/com/usthe/sureness/matcher/util/TirePathTreeTest.java
+3
-1
未找到文件。
core/src/main/java/com/usthe/sureness/matcher/util/TirePathTree.java
浏览文件 @
bfab5dbc
...
...
@@ -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
;
}
/**
...
...
core/src/test/java/com/usthe/sureness/matcher/util/TirePathTreeTest.java
浏览文件 @
bfab5dbc
...
...
@@ -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
(
2
0
,
root
.
getResourceNum
());
Assert
.
assertEquals
(
2
1
,
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录