1515 */
1616package org.codehaus.groovy.grails.plugins.web.filters
1717
18+ import groovy.transform.CompileDynamic
19+ import groovy.transform.CompileStatic
20+ import org.codehaus.groovy.grails.commons.GrailsControllerClass
21+ import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
22+
1823import java.util.regex.Pattern
1924
2025import javax.servlet.http.HttpServletRequest
2126import javax.servlet.http.HttpServletResponse
2227
23- import groovy.lang.GroovyObject ;
2428import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
2529import org.codehaus.groovy.grails.web.servlet.view.NullView
2630import org.codehaus.groovy.grails.web.util.WebUtils
@@ -38,24 +42,27 @@ import org.codehaus.groovy.grails.commons.GrailsApplication
3842 * @author mike
3943 * @author Graeme Rocher
4044 */
45+ @CompileStatic
4146class FilterToHandlerAdapter implements HandlerInterceptor , InitializingBean , GrailsApplicationAware {
42- def filterConfig
47+ FilterConfig filterConfig
4348 def configClass
4449
45- def controllerRegex
46- def controllerExcludeRegex
47- def controllerNamespaceRegex
48- def controllerNamespaceExcludeRegex
49- def actionRegex
50- def actionExcludeRegex
51- def uriPattern
52- def uriExcludePattern
53- def urlPathHelper = new UrlPathHelper ()
54- def pathMatcher = new AntPathMatcher ()
55- def useRegex // standard regex
56- def invertRule // invert rule
57- def useRegexFind // use find instead of match
58- def dependsOn = [] // any filters that need to be processed before this one
50+ Pattern controllerRegex
51+ Pattern controllerExcludeRegex
52+ Pattern controllerNamespaceRegex
53+ Pattern controllerNamespaceExcludeRegex
54+
55+ Pattern actionRegex
56+ Pattern actionExcludeRegex
57+ String uriPattern
58+ String uriExcludePattern
59+ UrlPathHelper urlPathHelper = new UrlPathHelper ()
60+ AntPathMatcher pathMatcher = new AntPathMatcher ()
61+ boolean useRegex // standard regex
62+ boolean invertRule // invert rule
63+ boolean useRegexFind // use find instead of match
64+ List dependsOn = [] // any filters that need to be processed before this one
65+
5966 GrailsApplication grailsApplication
6067
6168 void afterPropertiesSet () {
@@ -65,73 +72,89 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
6572 invertRule = scope. invert
6673 useRegexFind = scope. find
6774
68- if (scope. controller) {
69- controllerRegex = Pattern . compile((useRegex)? scope. controller: scope. controller. replaceAll(" \\ *" , " .*" ))
75+
76+ def controller = scope. controller
77+ if (controller) {
78+ controllerRegex = Pattern . compile( useRegex ? controller. toString() : controller. toString(). replaceAll(" \\ *" , " .*" ) )
7079 }
7180 else {
7281 controllerRegex = Pattern . compile(" .*" )
7382 }
7483
75- if (scope. controllerExclude) {
76- controllerExcludeRegex = Pattern . compile((useRegex)? scope. controllerExclude: scope. controllerExclude. replaceAll(" \\ *" , " .*" ))
84+
85+ def controllerExclude = scope. controllerExclude
86+ if (controllerExclude) {
87+ controllerExcludeRegex = Pattern . compile( useRegex ? controllerExclude. toString() : controllerExclude. toString(). replaceAll(" \\ *" , " .*" ) )
7788 }
7889
79- if (scope. namespace) {
80- controllerNamespaceRegex = Pattern . compile((useRegex)? scope. namespace: scope. namespace. replaceAll(" \\ *" , " .*" ))
90+
91+ def namespace = scope. namespace
92+ if (namespace) {
93+ controllerNamespaceRegex = Pattern . compile( useRegex ? namespace. toString() : namespace. toString(). replaceAll(" \\ *" , " .*" ))
8194 } else {
8295 controllerNamespaceRegex = Pattern . compile(" .*" )
8396 }
8497
85- if (scope. namespaceExclude) {
86- controllerNamespaceExcludeRegex = Pattern . compile((useRegex)? scope. namespaceExclude: scope. namespaceExclude. replaceAll(" \\ *" , " .*" ))
98+
99+ def namespaceExclude = scope. namespaceExclude
100+ if (namespaceExclude) {
101+ controllerNamespaceExcludeRegex = Pattern . compile( useRegex ? namespaceExclude. toString() : namespaceExclude. toString(). replaceAll(" \\ *" , " .*" ))
87102 }
88103
89- if (scope. action) {
90- actionRegex = Pattern . compile((useRegex)? scope. action: scope. action. replaceAll(" \\ *" , " .*" ))
104+
105+ def action = scope. action
106+ if (action) {
107+ actionRegex = Pattern . compile( useRegex ? action. toString() : action. toString(). replaceAll(" \\ *" , " .*" ) )
91108 }
92109 else {
93110 actionRegex = Pattern . compile(" .*" )
94111 }
95112
96- if (scope. actionExclude) {
97- actionExcludeRegex = Pattern . compile((useRegex)? scope. actionExclude: scope. actionExclude. replaceAll(" \\ *" , " .*" ))
113+
114+ def actionExclude = scope. actionExclude
115+ if (actionExclude) {
116+ actionExcludeRegex = Pattern . compile(useRegex ? actionExclude. toString() : actionExclude. toString(). replaceAll(" \\ *" , " .*" ) )
98117 }
99118
100- if (scope. uri) {
101- uriPattern = scope. uri. toString()
119+
120+ def uri = scope. uri
121+ if (uri) {
122+ uriPattern = uri. toString()
102123 }
103- if (scope. uriExclude) {
104- uriExcludePattern = scope. uriExclude. toString()
124+
125+ def uriExclude = scope. uriExclude
126+ if (uriExclude) {
127+ uriExcludePattern = uriExclude. toString()
105128 }
106129 }
107130
108131 /**
109132 * Returns the name of the controller targeted by the given request.
110133 */
111- String controllerName (request ) {
134+ String controllerName (HttpServletRequest request ) {
112135 return request. getAttribute(GrailsApplicationAttributes . CONTROLLER_NAME_ATTRIBUTE )?. toString()
113136 }
114137
115- def controllerClass (request ) {
116- return request. getAttribute(GrailsApplicationAttributes . CONTROLLER )
138+ GrailsControllerClass controllerClass (HttpServletRequest request ) {
139+ return ( GrailsControllerClass ) request. getAttribute(GrailsApplicationAttributes . GRAILS_CONTROLLER_CLASS )
117140 }
118141
119142 /**
120143 * Returns the namespace of the controller targeted by the given request.
121144 **/
122- String controllerNamespace (request ) {
145+ String controllerNamespace (HttpServletRequest request ) {
123146 return request. getAttribute(GrailsApplicationAttributes . CONTROLLER_NAMESPACE_ATTRIBUTE )?. toString()
124147 }
125148
126149 /**
127150 * Returns the name of the action targeted by the given request.
128151 */
129- String actionName (request ) {
152+ String actionName (HttpServletRequest request ) {
130153 return request. getAttribute(GrailsApplicationAttributes . ACTION_NAME_ATTRIBUTE )?. toString()
131154 }
132155
133156 String uri (HttpServletRequest request ) {
134- def uri = request. getAttribute(WebUtils . FORWARD_REQUEST_URI_ATTRIBUTE )
157+ String uri = request. getAttribute(WebUtils . FORWARD_REQUEST_URI_ATTRIBUTE ) ?. toString( )
135158 if (! uri) uri = request. getRequestURI()
136159 return uri. substring(request. getContextPath(). length())
137160 }
@@ -147,7 +170,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
147170
148171 if (! accept(controllerName, actionName, uri, controllerNamespace, controllerClass)) return true
149172
150- def callable = filterConfig. before. clone()
173+ def callable = ( Closure ) filterConfig. before. clone()
151174 def result = callable. call()
152175 if (result instanceof Boolean ) {
153176 if (! result && filterConfig. modelAndView) {
@@ -161,6 +184,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
161184 }
162185
163186 void postHandle (HttpServletRequest request , HttpServletResponse response , o , ModelAndView modelAndView ) {
187+ final filterConfig = this . filterConfig
164188 if (! filterConfig. after) {
165189 return
166190 }
@@ -173,7 +197,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
173197
174198 if (! accept(controllerName, actionName, uri, controllerNamespace, controllerClass)) return
175199
176- def callable = filterConfig. after. clone()
200+ def callable = ( Closure ) filterConfig. after. clone()
177201 def currentModel = modelAndView?. model
178202 if (currentModel == null ) {
179203 final templateModel = request. getAttribute(GrailsApplicationAttributes . TEMPLATE_MODEL )
@@ -182,26 +206,29 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
182206 }
183207 }
184208 def result = callable. call(currentModel)
209+ final filterConfigModel = filterConfig. modelAndView
185210 if (result instanceof Boolean ) {
186211 // if false is returned don't render a view
187212 if (! result) {
188213 modelAndView. viewName = null
189214 modelAndView. view = new NullView (response. contentType)
190215 }
191216 }
192- else if (filterConfig . modelAndView && modelAndView) {
193- if (filterConfig . modelAndView . viewName) {
194- modelAndView. viewName = filterConfig . modelAndView . viewName
217+ else if (filterConfigModel && modelAndView) {
218+ if (filterConfigModel . viewName) {
219+ modelAndView. viewName = filterConfigModel . viewName
195220 }
196- modelAndView. model. putAll(filterConfig . modelAndView . model)
221+ modelAndView. model. putAll(filterConfigModel . model)
197222 }
198- else if (filterConfig . modelAndView ?. viewName) {
223+ else if (filterConfigModel ?. viewName) {
199224 renderModelAndView(filterConfig, request, response, controllerName)
200225 }
201226 }
202227
203- private renderModelAndView (delegate , request , response , controllerName ) {
204- def viewResolver = WebUtils . lookupViewResolver(delegate. servletContext)
228+ @CompileDynamic
229+ private renderModelAndView (delegate , HttpServletRequest request , HttpServletResponse response , String controllerName ) {
230+ def webRequest = GrailsWebRequest . lookup(request)
231+ def viewResolver = WebUtils . lookupViewResolver(webRequest. servletContext)
205232 def view
206233 ModelAndView modelAndView = delegate. modelAndView
207234 if (modelAndView. viewName) {
@@ -220,19 +247,21 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
220247
221248 String controllerName = controllerName(request)
222249 String controllerNamespace = controllerNamespace(request)
223- def controllerClass = controllerClass(request)
250+ def controller = this . controllerClass(request)
224251 String actionName = actionName(request)
225252 String uri = uri(request)
226253
227- if (! accept(controllerName, actionName, uri, controllerNamespace, controllerClass )) return
254+ if (! accept(controllerName, actionName, uri, controllerNamespace, controller )) return
228255
229- def callable = filterConfig. afterView. clone()
256+ def callable = ( Closure ) filterConfig. afterView. clone()
230257 callable. call(e)
231258 }
232259
233- boolean accept (String controllerName , String actionName , String uri , String controllerNamespace , controllerClass ) {
260+ boolean accept (String controllerName , String actionName , String uri , String controllerNamespace , GrailsControllerClass controllerClass ) {
234261 boolean matched= true
235262
263+ uri = uri. replace(' ;' , ' ' )
264+ final pathMatcher = this . pathMatcher
236265 if (uriPattern) {
237266 matched = pathMatcher. match(uriPattern, uri)
238267 if (matched && uriExcludePattern) {
@@ -263,8 +292,8 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
263292 }
264293 if (matched && (filterConfig. scope. action)) {
265294 if (! actionName && controllerName) {
266- if (controllerClass && controllerClass . respondsTo( " getDefaultAction " ) ) {
267- actionName = controllerClass?. getDefaultAction()
295+ if (controllerClass) {
296+ actionName = controllerClass. defaultAction
268297 }
269298 }
270299 matched = doesMatch(actionRegex, actionName)
0 commit comments