Skip to content

Commit 98eb36e

Browse files
graemerochergraemerocher
authored andcommitted
Use @CompileStatic on FilterToHandlerAdaptor
1 parent 9ccaa08 commit 98eb36e

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

grails-plugin-filters/src/main/groovy/org/codehaus/groovy/grails/plugins/web/filters/FilterConfig.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import org.springframework.web.servlet.ModelAndView
3737
*/
3838
class FilterConfig extends ControllersApi {
3939
String name
40-
Map scope
40+
Map scope = [:]
4141
Closure before
4242
Closure after
4343
Closure afterView

grails-plugin-filters/src/main/groovy/org/codehaus/groovy/grails/plugins/web/filters/FilterToHandlerAdapter.groovy

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package 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+
1823
import java.util.regex.Pattern
1924

2025
import javax.servlet.http.HttpServletRequest
@@ -38,22 +43,24 @@ import org.codehaus.groovy.grails.commons.GrailsApplication
3843
* @author mike
3944
* @author Graeme Rocher
4045
*/
46+
@CompileStatic
4147
class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, GrailsApplicationAware {
42-
def filterConfig
48+
FilterConfig filterConfig
4349
def configClass
4450

45-
def controllerRegex
46-
def controllerExcludeRegex
47-
def actionRegex
48-
def actionExcludeRegex
49-
def uriPattern
50-
def uriExcludePattern
51-
def urlPathHelper = new UrlPathHelper()
52-
def pathMatcher = new AntPathMatcher()
51+
Pattern controllerRegex
52+
Pattern controllerExcludeRegex
53+
Pattern actionRegex
54+
Pattern actionExcludeRegex
55+
String uriPattern
56+
String uriExcludePattern
57+
UrlPathHelper urlPathHelper = new UrlPathHelper()
58+
AntPathMatcher pathMatcher = new AntPathMatcher()
5359
def useRegex // standard regex
5460
def invertRule // invert rule
5561
def useRegexFind // use find instead of match
5662
def dependsOn = [] // any filters that need to be processed before this one
63+
5764
GrailsApplication grailsApplication
5865

5966
void afterPropertiesSet() {
@@ -64,25 +71,25 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
6471
useRegexFind = scope.find
6572

6673
if (scope.controller) {
67-
controllerRegex = Pattern.compile((useRegex)?scope.controller:scope.controller.replaceAll("\\*", ".*"))
74+
controllerRegex = Pattern.compile( useRegex ? scope.controller.toString() : scope.controller.toString().replaceAll("\\*", ".*") )
6875
}
6976
else {
7077
controllerRegex = Pattern.compile(".*")
7178
}
7279

7380
if (scope.controllerExclude) {
74-
controllerExcludeRegex = Pattern.compile((useRegex)?scope.controllerExclude:scope.controllerExclude.replaceAll("\\*", ".*"))
81+
controllerExcludeRegex = Pattern.compile( useRegex ? scope.controllerExclude.toString() : scope.controllerExclude.toString().replaceAll("\\*", ".*") )
7582
}
7683

7784
if (scope.action) {
78-
actionRegex = Pattern.compile((useRegex)?scope.action:scope.action.replaceAll("\\*", ".*"))
85+
actionRegex = Pattern.compile( useRegex ? scope.action.toString() : scope.action.toString().replaceAll("\\*", ".*") )
7986
}
8087
else {
8188
actionRegex = Pattern.compile(".*")
8289
}
8390

8491
if (scope.actionExclude) {
85-
actionExcludeRegex = Pattern.compile((useRegex)?scope.actionExclude:scope.actionExclude.replaceAll("\\*", ".*"))
92+
actionExcludeRegex = Pattern.compile(useRegex ? scope.actionExclude.toString() : scope.actionExclude.toString().replaceAll("\\*", ".*") )
8693
}
8794

8895
if (scope.uri) {
@@ -96,19 +103,19 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
96103
/**
97104
* Returns the name of the controller targeted by the given request.
98105
*/
99-
String controllerName(request) {
106+
String controllerName(HttpServletRequest request) {
100107
return request.getAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE)?.toString()
101108
}
102109

103110
/**
104111
* Returns the name of the action targeted by the given request.
105112
*/
106-
String actionName(request) {
113+
String actionName(HttpServletRequest request) {
107114
return request.getAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE)?.toString()
108115
}
109116

110117
String uri(HttpServletRequest request) {
111-
def uri = request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)
118+
String uri = request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)?.toString()
112119
if (!uri) uri = request.getRequestURI()
113120
return uri.substring(request.getContextPath().length())
114121
}
@@ -122,7 +129,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
122129

123130
if (!accept(controllerName, actionName, uri)) return true
124131

125-
def callable = filterConfig.before.clone()
132+
def callable = (Closure)filterConfig.before.clone()
126133
def result = callable.call()
127134
if (result instanceof Boolean) {
128135
if (!result && filterConfig.modelAndView) {
@@ -146,7 +153,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
146153

147154
if (!accept(controllerName, actionName, uri)) return
148155

149-
def callable = filterConfig.after.clone()
156+
def callable = (Closure)filterConfig.after.clone()
150157
def currentModel = modelAndView?.model
151158
if (currentModel == null) {
152159
final templateModel = request.getAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL)
@@ -173,8 +180,10 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
173180
}
174181
}
175182

176-
private renderModelAndView(delegate, request, response, controllerName) {
177-
def viewResolver = WebUtils.lookupViewResolver(delegate.servletContext)
183+
@CompileDynamic
184+
private renderModelAndView(delegate, HttpServletRequest request, HttpServletResponse response, String controllerName) {
185+
def webRequest = GrailsWebRequest.lookup(request)
186+
def viewResolver = WebUtils.lookupViewResolver(webRequest.servletContext)
178187
def view
179188
ModelAndView modelAndView = delegate.modelAndView
180189
if (modelAndView.viewName) {
@@ -197,13 +206,14 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
197206

198207
if (!accept(controllerName, actionName, uri)) return
199208

200-
def callable = filterConfig.afterView.clone()
209+
def callable = (Closure)filterConfig.afterView.clone()
201210
callable.call(e)
202211
}
203212

204213
boolean accept(String controllerName, String actionName, String uri) {
205214
boolean matched=true
206215

216+
uri = uri.replace(';', '')
207217
if (uriPattern) {
208218
matched = pathMatcher.match(uriPattern, uri)
209219
if (matched && uriExcludePattern) {
@@ -225,7 +235,7 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
225235
}
226236
if (matched && filterConfig.scope.action) {
227237
if (!actionName && controllerName) {
228-
def controllerClass = grailsApplication?.getArtefactByLogicalPropertyName(
238+
def controllerClass = (GrailsControllerClass)grailsApplication?.getArtefactByLogicalPropertyName(
229239
DefaultGrailsControllerClass.CONTROLLER, controllerName)
230240
actionName = controllerClass?.getDefaultAction()
231241
}
Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package org.codehaus.groovy.grails.web.filters
22

33
import grails.web.CamelCaseUrlConverter
44
import grails.web.UrlConverter
5-
65
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
6+
import org.codehaus.groovy.grails.plugins.web.filters.FilterConfig
77
import org.codehaus.groovy.grails.plugins.web.filters.FilterToHandlerAdapter
88
import org.codehaus.groovy.grails.support.MockApplicationContext
99

10-
/**
10+
/**
1111
* @author Graeme Rocher
1212
* @since 1.0
1313
*/
@@ -23,20 +23,19 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
2323

2424
void testURIMapping() {
2525
def filterAdapter = new FilterToHandlerAdapter()
26-
filterAdapter.filterConfig = new Expando()
27-
filterAdapter.filterConfig.scope = new Expando()
26+
filterAdapter.filterConfig = new FilterConfig()
2827
filterAdapter.filterConfig.scope.uri = "/restricted/**"
2928
filterAdapter.afterPropertiesSet()
3029

3130
assert filterAdapter.accept("Ignore", "index", "/restricted/1")
31+
assert filterAdapter.accept("Ignore", "index", "/restricted;")
3232
assert filterAdapter.accept("Ignore", "index", "/restricted/1/2")
3333
assert !filterAdapter.accept("Ignore", "index", "/foo/1/2")
3434
}
3535

3636
void testURIMapping2() {
3737
def filterAdapter = new FilterToHandlerAdapter()
38-
filterAdapter.filterConfig = new Expando()
39-
filterAdapter.filterConfig.scope = new Expando()
38+
filterAdapter.filterConfig = new FilterConfig()
4039
filterAdapter.filterConfig.scope.controller = "trol"
4140
filterAdapter.filterConfig.scope.find = true
4241
filterAdapter.afterPropertiesSet()
@@ -49,8 +48,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
4948

5049
void testURIMapping3() {
5150
def filterAdapter = new FilterToHandlerAdapter()
52-
filterAdapter.filterConfig = new Expando()
53-
filterAdapter.filterConfig.scope = new Expando()
51+
filterAdapter.filterConfig = new FilterConfig()
5452
filterAdapter.filterConfig.scope.controller = ".*trol.*"
5553
filterAdapter.filterConfig.scope.action = "index"
5654
filterAdapter.filterConfig.scope.invert = true
@@ -66,8 +64,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
6664

6765
void testDefaultActionWithControllerMatchAndActionWildcard() {
6866
def filterAdapter = new FilterToHandlerAdapter()
69-
filterAdapter.filterConfig = new Expando()
70-
filterAdapter.filterConfig.scope = new Expando()
67+
filterAdapter.filterConfig = new FilterConfig()
7168
filterAdapter.filterConfig.scope.controller = "demo"
7269
filterAdapter.filterConfig.scope.action = "*"
7370
filterAdapter.afterPropertiesSet()
@@ -77,8 +74,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
7774

7875
void testDefaultActionWithControllerMismatchAndActionWildcard() {
7976
def filterAdapter = new FilterToHandlerAdapter()
80-
filterAdapter.filterConfig = new Expando()
81-
filterAdapter.filterConfig.scope = new Expando()
77+
filterAdapter.filterConfig = new FilterConfig()
8278
filterAdapter.filterConfig.scope.controller = "demo"
8379
filterAdapter.filterConfig.scope.action = "*"
8480
filterAdapter.afterPropertiesSet()
@@ -88,8 +84,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
8884

8985
void testDefaultActionWithControllerMatchAndActionMismatch() {
9086
def filterAdapter = new FilterToHandlerAdapter()
91-
filterAdapter.filterConfig = new Expando()
92-
filterAdapter.filterConfig.scope = new Expando()
87+
filterAdapter.filterConfig = new FilterConfig()
9388
filterAdapter.filterConfig.scope.controller = "demo"
9489
filterAdapter.filterConfig.scope.action = "foo"
9590
filterAdapter.afterPropertiesSet()
@@ -104,8 +99,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
10499
application.mainContext = mainContext
105100
application.initialise()
106101
def filterAdapter = new FilterToHandlerAdapter(grailsApplication: application)
107-
filterAdapter.filterConfig = new Expando()
108-
filterAdapter.filterConfig.scope = new Expando()
102+
filterAdapter.filterConfig = new FilterConfig()
109103
filterAdapter.filterConfig.scope.controller = "demo"
110104
filterAdapter.filterConfig.scope.action = "index"
111105
filterAdapter.afterPropertiesSet()
@@ -115,8 +109,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
115109

116110
void testDefaultActionWithControllerMatchAndNoActionSpecifiedInConfig() {
117111
def filterAdapter = new FilterToHandlerAdapter()
118-
filterAdapter.filterConfig = new Expando()
119-
filterAdapter.filterConfig.scope = new Expando()
112+
filterAdapter.filterConfig = new FilterConfig()
120113
filterAdapter.filterConfig.scope.controller = "demo"
121114
filterAdapter.afterPropertiesSet()
122115

@@ -125,8 +118,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
125118

126119
void testAppRootWithWildcardedControllerAndAction() {
127120
def filterAdapter = new FilterToHandlerAdapter()
128-
filterAdapter.filterConfig = new Expando()
129-
filterAdapter.filterConfig.scope = new Expando()
121+
filterAdapter.filterConfig = new FilterConfig()
130122
filterAdapter.filterConfig.scope.controller = "*"
131123
filterAdapter.filterConfig.scope.action = "*"
132124
filterAdapter.afterPropertiesSet()
@@ -136,8 +128,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
136128

137129
void testAppRootWithWildcardedControllerAndActionRegex() {
138130
def filterAdapter = new FilterToHandlerAdapter()
139-
filterAdapter.filterConfig = new Expando()
140-
filterAdapter.filterConfig.scope = new Expando()
131+
filterAdapter.filterConfig = new FilterConfig()
141132
filterAdapter.filterConfig.scope.controller = ".*"
142133
filterAdapter.filterConfig.scope.action = ".*"
143134
filterAdapter.filterConfig.scope.regex = true
@@ -148,8 +139,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
148139

149140
void testAppRootWithWildcardedControllerAndNoAction() {
150141
def filterAdapter = new FilterToHandlerAdapter()
151-
filterAdapter.filterConfig = new Expando()
152-
filterAdapter.filterConfig.scope = new Expando()
142+
filterAdapter.filterConfig = new FilterConfig()
153143
filterAdapter.filterConfig.scope.controller = "*"
154144
filterAdapter.afterPropertiesSet()
155145

@@ -158,8 +148,7 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
158148

159149
void testAppRootWithWildcardedControllerAndSpecificAction() {
160150
def filterAdapter = new FilterToHandlerAdapter()
161-
filterAdapter.filterConfig = new Expando()
162-
filterAdapter.filterConfig.scope = new Expando()
151+
filterAdapter.filterConfig = new FilterConfig()
163152
filterAdapter.filterConfig.scope.controller = "*"
164153
filterAdapter.filterConfig.scope.action = "something"
165154
filterAdapter.afterPropertiesSet()
@@ -169,15 +158,15 @@ class FilterToHandlerAdapterTests extends GroovyTestCase {
169158

170159
void testAppRootWithSpecificControllerAndWildcardedAction() {
171160
def filterAdapter = new FilterToHandlerAdapter()
172-
filterAdapter.filterConfig = new Expando()
173-
filterAdapter.filterConfig.scope = new Expando()
161+
filterAdapter.filterConfig = new FilterConfig()
174162
filterAdapter.filterConfig.scope.controller = "something"
175163
filterAdapter.filterConfig.scope.action = "*"
176164
filterAdapter.afterPropertiesSet()
177165

178166
assertFalse filterAdapter.accept(null, null, '/')
179167
}
180168
}
169+
181170
class DemoController {
182171
def index = {}
183172
}

0 commit comments

Comments
 (0)