我们写一个Filters跟踪分析一下Filters是如何变化加入到CompositeInterceptor的handlers成员
class TestFilters {
def log ={
before = {
println "Before Filter"
}
}
def filters = {
//这儿的方法名doFilter不重要,可以随便命名,为了调用闭包而已
doFilter([controller: '*', action: '*',uri:'*'],log)
}
}
DefaultGrailsFiltersClass.groovy
class DefaultGrailsFiltersClass extends AbstractInjectableGrailsClass implements GrailsFiltersClass {
static FILTERS = "Filters";
//grails类必须的构造函数,可以让FiltersGrailsPlugin找到
DefaultGrailsFiltersClass(Class aClass) {
super(aClass, FILTERS)
}
public List getConfigs(Object filters) {
if (!filters) return [];
def loader = new Loader(filters)
//找到TestFilters的filters闭包属性
def filtersClosure = filters.filters
filtersClosure.delegate = loader
//调用闭包即doFilter([controller: '*', action: '*',uri:'*"],log),生成FilterConfig对象数组
filtersClosure.call()
return loader.filters;
}
}
class Loader {
def filtersDefinition
def filters = []
Loader(filtersDefinition) {
this.filtersDefinition = filtersDefinition
}
//调用闭包即filter([controller: '*', action: '*',uri:'*"],log)
//methodName=doFilter,args是[controller: '*', action: '*',uri:'*"]和闭包log
def methodMissing(String methodName, args) {
if(args) {
def fc = new FilterConfig(name: methodName, filtersDefinition: filtersDefinition)
filters << fc
if(args[0] instanceof Closure) {
fc.scope = [ uri: '/**' ]
def closure = args[0]
closure.delegate = fc
closure.call()
}
else if(args[0] instanceof Map) {
fc.scope = args[0]
if(args.size() > 1 && args[1] instanceof Closure) {
def closure = args[1]
closure.delegate = fc
//args[1]即是TestFilters的log闭包,调用对象delegate是FilterConfig
closure.resolveStrategy = Closure.DELEGATE_FIRST
//log闭包调用的第一句就是before= {...},而FilterConfig就有一个before的闭包属性
//这样fc.before即为TestFilters中的before闭包,fc.scope即为[controller: '*', action: '*',uri:'*"]
closure.call()
}
}
fc.initialised = true
}
}
}
FilterConfig.groovy
class FilterConfig {
String name
Map scope
Closure before
Closure after
Closure afterView
...
}
再看看FilterToHandlerAdapter进行FilterConfig到HandlerInterceptor的适配就很简单了,检查FilterConfig是否有before,after和afterView属性,再用scope属性验证当前ruquest的uri,controller和action
FilterToHandlerAdapter.groovy
//检查grails的controllerName,actionName,uri是否
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
if (filterConfig.before) {
String controllerName = controllerName(request)
String actionName = actionName(request)
String uri = uri(request)
if (!accept(controllerName, actionName, uri)) return true;
def callable = filterConfig.before.clone()
def result = callable.call();
if(result instanceof Boolean) {
if(!result && filterConfig.modelAndView) {
renderModelAndView(filterConfig, request, response, controllerName)
}
return result
}
}
return true;
}
boolean accept(String controllerName, String actionName, String uri) {
if (controllerRegex == null || actionRegex == null) {
def scope = filterConfig.scope
if (scope.controller) {
controllerRegex = Pattern.compile(scope.controller.replaceAll("\\*", ".*"))
}
else {
controllerRegex = Pattern.compile(".*")
}
if (scope.action) {
actionRegex = Pattern.compile(scope.action.replaceAll("\\*", ".*"))
}
else {
actionRegex = Pattern.compile(".*")
}
if (scope.uri) {
uriPattern = scope.uri.toString()
}
}
if(uriPattern) {
return pathMatcher.match(uriPattern, uri)
}
else if(controllerRegex && actionRegex) {
return controllerRegex.matcher(controllerName).matches() && actionRegex.matcher(actionName).matches()
}
}
分享到:
相关推荐
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2 共两个包,解压后需要将扩展名.zip.bak改名为.zip重新解压。 http://dist.springsource.com/release/STS/3.8.1.RELEASE/dist/ e4.6/spring-tool-...
Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with grails chinese Grails入门指南书籍和源码----下载不扣分,回帖加1分,欢迎下载,童叟无欺 getting started with ...
grails-1.0 中文参考文档 网上收集而来,对 e 文不太熟悉的朋友可以参考一下,有些翻译的不好理解,最好对照着官方的e 文的参考文档
漏洞修补好象是传asp等后缀名的时候,生成的是nba的后缀。上传原来很简单在后缀加个空格就可以了
Grails-2.4.4-用户手册
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。
grails 中文文档+grails-fckeditor-0.9.5.zip插件
grails-docs-2.0.0
prestige.zip,android的mvc模式,android的代码生成驱动的mvc模式。
grails-3.0.5-ga (2/2), for somebody who can not download
grails-quartz-0.4.1 插件,直接安装
grails-spring-websocket ils子 2.4.x 3.2.7+ 2.5.x 4.0.0+ 安装 要将插件安装到Grails应用程序中,请将以下行添加到build.gradle依赖项部分: implementation "org.grails.plugins:grails-spring-websocket:...
grails-docs-1.0,api文档。html格式
grails-doc-1.0-beta1-中文.rar。Grails 1.0 学习资料。
Grails开源框架 - 使用指南,版本1.0,中文文档。 Grails开源框架 - 使用指南 作者: Graeme Rocher, Marc Palmer 版本: 1.0 Copies of this document may be made for your own use and for distribution to ...
grails-reference-documentation-1.1.x.pdf
Grails Getting Started-II
grails参考文档 The Grails Framework - Reference Documentation Authors: Graeme Rocher, Marc Palmer Version: 1.0.3