/
FilterConfig.groovy
277 lines (225 loc) · 8.36 KB
/
FilterConfig.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.plugins.web.filters
import javax.servlet.ServletContext
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsClassUtils
import org.codehaus.groovy.grails.plugins.web.api.ControllersApi
import org.codehaus.groovy.grails.web.servlet.FlashScope
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.context.ApplicationContext
import org.springframework.validation.Errors
import org.springframework.web.servlet.ModelAndView
/**
* @author mike
* @author Graeme Rocher
*/
class FilterConfig extends ControllersApi {
String name
Map scope
Closure before
Closure after
Closure afterView
// this modelAndView overrides ControllersApi's modelAndView
ModelAndView modelAndView
boolean initialised = false
FilterConfig() {
initializeMetaClass()
}
void initializeMetaClass() {
// use per-instance metaclass
ExpandoMetaClass emc = new ExpandoMetaClass(getClass(), false, true)
emc.initialize()
setMetaClass(emc)
}
/**
* Redirects attempt to access an 'errors' property, so we provide
* one here with a null value.
*/
def errors = null
/**
* This is the filters definition bean that declared the filter
* config. Since it may contain injected services, etc., we
* delegate any missing properties or methods to it.
*/
def filtersDefinition
/**
* When the filter does not have a particular property, it passes
* the request on to the filter definition class.
*/
def propertyMissing(String propertyName) {
// Delegate to the parent definition if it has this property.
if (wiredFiltersDefinition.metaClass.hasProperty(wiredFiltersDefinition, propertyName)) {
def getterName = GrailsClassUtils.getGetterName(propertyName)
metaClass."$getterName" = {-> delegate.wiredFiltersDefinition.getProperty(propertyName) }
return wiredFiltersDefinition."$propertyName"
}
throw new MissingPropertyException(propertyName, filtersDefinition.getClass())
}
def getWiredFiltersDefinition() {
final webRequest = GrailsWebRequest.lookup()
final grailsFilter = webRequest ? grailsApplication.getArtefact(FiltersConfigArtefactHandler.TYPE, filtersDefinition.class.name) : null
if (grailsFilter) {
applicationContext.getBean(grailsFilter.fullName)
}
else {
return filtersDefinition
}
}
/**
* When the filter does not have a particular method, it passes
* the call on to the filter definition class.
*/
def methodMissing(String methodName, args) {
// Delegate to the parent definition if it has this method.
List<MetaMethod> respondsTo = filtersDefinition.metaClass.respondsTo(filtersDefinition, methodName, args)
if (respondsTo) {
// Use DelegateMetaMethod to proxy calls to actual MetaMethod for subsequent calls to this method
DelegateMetaMethod dmm=new DelegateMetaMethod(respondsTo[0], FilterConfigDelegateMetaMethodTargetStrategy.instance)
// register the metamethod to EMC
metaClass.registerInstanceMethod(dmm)
// for this invocation we still have to make the call
return respondsTo[0].invoke(filtersDefinition, args)
}
// Ideally, we would throw a MissingMethodException here
// whether the filter config is intialised or not. However,
// if it's in the initialisation phase, the MME gets swallowed somewhere.
if (!initialised) {
throw new IllegalStateException(
"Invalid filter definition in ${wiredFiltersDefinition.getClass().name} - trying "
+ "to call method '${methodName}' outside of an interceptor.")
}
// The required method was not found on the parent filter definition either.
throw new MissingMethodException(methodName, wiredFiltersDefinition.getClass(), args)
}
String toString() {"FilterConfig[$name, scope=$scope]"}
String getActionUri() {
return super.getActionUri(this)
}
String getControllerUri() {
return super.getControllerUri(this)
}
String getTemplateUri(String name) {
return super.getTemplateUri(this, name)
}
String getViewUri(String name) {
return super.getViewUri(this, name)
}
void setErrors(Errors errors) {
super.setErrors(this, errors)
}
Errors getErrors() {
return super.getErrors(this)
}
Map getChainModel() {
return super.getChainModel(this)
}
boolean hasErrors() {
return super.hasErrors(this)
}
Object redirect(Map args) {
return super.redirect(this, args)
}
Object chain(Map args) {
return super.chain(this, args)
}
Object render(Object o) {
return super.render(this, o)
}
Object render(String txt) {
return super.render(this, txt)
}
Object render(Map args) {
return super.render(this, args)
}
Object render(Closure c) {
return super.render(this, c)
}
Object render(Map args, Closure c) {
return super.render(this, args, c)
}
Object bindData(Object target, Object args) {
return super.bindData(this, target, args)
}
Object bindData(Object target, Object args, List disallowed) {
return super.bindData(this, target, args, disallowed)
}
Object bindData(Object target, Object args, List disallowed, String filter) {
return super.bindData(this, target, args, disallowed, filter)
}
Object bindData(Object target, Object args, Map includeExclude) {
return super.bindData(this, target, args, includeExclude)
}
Object bindData(Object target, Object args, Map includeExclude, String filter) {
return super.bindData(this, target, args, includeExclude, filter)
}
Object bindData(Object target, Object args, String filter) {
return super.bindData(this, target, args, filter)
}
void header(String headerName, Object headerValue) {
super.header(this, headerName, headerValue)
}
Object withForm(Closure callable) {
return super.withForm(this, callable)
}
String forward(Map params) {
return super.forward(this, params)
}
GrailsParameterMap getParams() {
return super.getParams(this)
}
FlashScope getFlash() {
return super.getFlash(this)
}
HttpSession getSession() {
return super.getSession(this)
}
HttpServletRequest getRequest() {
return super.getRequest(this)
}
ServletContext getServletContext() {
return super.getServletContext(this)
}
HttpServletResponse getResponse() {
return super.getResponse(this)
}
GrailsApplicationAttributes getGrailsAttributes() {
return super.getGrailsAttributes(this)
}
GrailsApplication getGrailsApplication() {
return super.getGrailsApplication(this)
}
ApplicationContext getApplicationContext() {
return super.getApplicationContext(this)
}
String getActionName() {
return super.getActionName(this)
}
String getControllerName() {
return super.getControllerName(this)
}
GrailsWebRequest getWebRequest() {
return super.getWebRequest(this)
}
String getPluginContextPath() {
return super.getPluginContextPath(this)
}
}