您的位置:首页 > 运维架构 > Tomcat

关于工作中需要查看tomcat源码引申出来的方法总结(包括相应的idea操作)

2019-08-15 20:42 1471 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/dataiyangu/article/details/99655194

文章目录

背景

工作中需要将tomcat的urlpattern和得到的requesturi通过反射进行比较,总不能手写吧,而且自己写的跟tomcat本身的肯定有很大的出入,于是决定找到tomcat原本的匹配方法。

我是实干家

首先,之前知道

在filter中有urlPatterns这个字段,一定有他的get方法
先找到ContextFilterMaps这个类,然后进入FilterMap这个类,好吧找到了这个方法

而谁会需要这个方法呢?
嗯,无疑是需要进行正则匹配的地方,这个时候通过idea的查看调用链


好吧,我感觉这个不好用,因为是正序的貌似,我要找到什么时候?

嗯,这个貌似是倒序的(这个同样适用于类)


通过查看这个方法的注释、参数的意思以及方法中的规则,是他无疑了
找到了

这个方法的完整代码

matchFiltersURL()

/**
* Return <code>true</code> if the context-relative request path
* matches the requirements of the specified filter mapping;
* otherwise, return <code>false</code>.
*
* @param testPath URL mapping being checked
* @param requestPath Context-relative request path of this request
*/
private boolean matchFiltersURL(String testPath, String requestPath) {

if (testPath == null)
return (false);

// Case 1 - Exact Match
if (testPath.equals(requestPath))
return (true);

// Case 2 - Path Match ("/.../*")
if (testPath.equals("/*"))
return (true);
if (testPath.endsWith("/*")) {
if (testPath.regionMatches(0, requestPath, 0,
testPath.length() - 2)) {
if (requestPath.length() == (testPath.length() - 2)) {
return (true);
} else if ('/' == requestPath.charAt(testPath.length() - 2)) {
return (true);
}
}
return (false);
}

// Case 3 - Extension Match
if (testPath.startsWith("*.")) {
int slash = requestPath.lastIndexOf('/');
int period = requestPath.lastIndexOf('.');
if ((slash >= 0) && (period > slash)
&& (period != requestPath.length() - 1)
&& ((requestPath.length() - period)
== (testPath.length() - 1))) {
return (testPath.regionMatches(2, requestPath, period + 1,
testPath.length() - 2));
}
}

// Case 4 - "Default" Match
return (false); // NOTE - Not relevant for selecting filters

}

嗯,是和我们学的filter的那四五种匹配规则一样的。

收获

查看调用链

查看类的调用视图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐