GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / groovy / org / codehaus / groovy / grails / plugins / web / filters / FiltersGrailsPlugin.groovy
blob3a32247a20ed90dbee77f804771c966241504f10
1 /*
2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.codehaus.groovy.grails.plugins.web.filters
17 import org.codehaus.groovy.grails.plugins.web.filters.FiltersConfigArtefactHandler
18 import org.codehaus.groovy.grails.plugins.web.filters.FilterToHandlerAdapter
19 import org.apache.commons.logging.LogFactory
20 import org.codehaus.groovy.grails.commons.GrailsApplication
21 import org.springframework.beans.factory.config.MethodInvokingFactoryBean
22 import org.codehaus.groovy.grails.web.plugins.support.WebMetaUtils
23 import org.codehaus.groovy.grails.web.metaclass.RedirectDynamicMethod
24 import org.codehaus.groovy.grails.web.metaclass.RenderDynamicMethod
26 /**
27 * @author Mike
28 * @author Graeme Rocher
30 * @since 1.0
32 * Created: Oct 10, 2007
34 class FiltersGrailsPlugin {
35 private static TYPE = FiltersConfigArtefactHandler.TYPE
37 def version = 0.1
38 def dependsOn = [:]
39 def artefacts = [ FiltersConfigArtefactHandler ]
40 def watchedResources = "file:./grails-app/conf/**/*Filters.groovy"
41 def log = LogFactory.getLog(FiltersGrailsPlugin)
43 static final BEANS = { filter ->
44 "${filter.fullName}Class"(MethodInvokingFactoryBean) {
45 targetObject = ref("grailsApplication", true)
46 targetMethod = "getArtefact"
47 arguments = [TYPE, filter.fullName]
49 "${filter.fullName}"(filter.clazz) { bean ->
50 bean.singleton = true
51 bean.autowire = "byName"
54 def doWithSpring = {
55 filterInterceptor(org.codehaus.groovy.grails.plugins.web.filters.CompositeInterceptor) {
59 for(filter in application.getArtefacts(TYPE)) {
60 def callable = BEANS.curry(filter)
61 callable.delegate = delegate
62 callable.call()
66 def doWithDynamicMethods = { applicationContext ->
67 def mc = FilterConfig.metaClass
69 // Add the standard dynamic properties for web requests to all
70 // the filters, i.e. 'params', 'flash', 'request', etc.
71 WebMetaUtils.registerCommonWebProperties(mc, application)
73 // Also make the application context available.
74 mc.getApplicationContext = {-> applicationContext }
76 // Add redirect and render methods (copy and pasted from the
77 // controllers plugin).
78 def redirect = new RedirectDynamicMethod(applicationContext)
79 def render = new RenderDynamicMethod()
81 mc.redirect = {Map args ->
82 redirect.invoke(delegate, "redirect", args)
85 mc.render = {Object o ->
86 render.invoke(delegate, "render", [o?.inspect()] as Object[])
89 mc.render = {String txt ->
90 render.invoke(delegate, "render", [txt] as Object[])
93 mc.render = {Map args ->
94 render.invoke(delegate, "render", [args] as Object[])
97 mc.render = {Closure c ->
98 render.invoke(delegate, "render", [c] as Object[])
101 mc.render = {Map args, Closure c ->
102 render.invoke(delegate, "render", [args, c] as Object[])
106 def doWithApplicationContext = { applicationContext ->
107 reloadFilters(application, applicationContext)
110 def onChange = { event ->
111 if (log.debugEnabled) log.debug("onChange: ${event}")
113 // Get the new or modified filter and (re-)register the associated
114 // beans.
115 def newFilter = event.application.addArtefact(TYPE, event.source)
116 beans(BEANS.curry(newFilter)).registerBeans(event.ctx)
117 reloadFilters(event.application, event.ctx)
120 private reloadFilters(GrailsApplication application, applicationContext) {
121 log.info "reloadFilters"
122 def filterConfigs = application.getArtefacts(TYPE)
123 def handlers = []
124 for(c in filterConfigs) {
125 def filterClass = applicationContext.getBean("${c.fullName}Class")
126 def bean = applicationContext.getBean(c.fullName)
127 for(filterConfig in filterClass.getConfigs(bean)) {
128 handlers << new FilterToHandlerAdapter(filterConfig:filterConfig, configClass:bean)
132 if (log.isDebugEnabled()) log.debug("resulting handlers: ${handlers}")
133 applicationContext.getBean('filterInterceptor').handlers = handlers