<commit_before>/*
 * 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 grails.util.GrailsUtil
import org.apache.commons.logging.LogFactory
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsClass
import org.codehaus.groovy.grails.web.metaclass.RedirectDynamicMethod
import org.codehaus.groovy.grails.web.metaclass.RenderDynamicMethod
import org.codehaus.groovy.grails.web.plugins.support.WebMetaUtils
import org.springframework.beans.factory.config.MethodInvokingFactoryBean

 /**
 * @author Mike
 * @author <NAME>
 *
 * @since 1.0
 */
class FiltersGrailsPlugin {

    private static final TYPE = FiltersConfigArtefactHandler.TYPE

    def version = GrailsUtil.getGrailsVersion()
    def dependsOn = [:]
    def artefacts = [ FiltersConfigArtefactHandler ]
    def watchedResources = "file:./grails-app/conf/**/*Filters.groovy"
    def log = LogFactory.getLog(FiltersGrailsPlugin)

    static final BEANS = { GrailsClass filter ->
        "${filter.fullName}Class"(MethodInvokingFactoryBean) {
            targetObject = ref("grailsApplication", true)
            targetMethod = "getArtefact"
            arguments = [TYPE, filter.fullName]
        }
        "${filter.fullName}"(filter.clazz) { bean ->
            bean.singleton = true
            bean.autowire = "byName"
        }
    }

    def doWithSpring = {
        filterInterceptor(CompositeInterceptor)

        for (filter in application.getArtefacts(TYPE)) {
            def callable = BEANS.curry(filter)
            callable.delegate = delegate
            callable.call()
        }
    }

    def doWithDynamicMethods = { applicationContext ->
        def mc = FilterConfig.metaClass

        // Add the standard dynamic properties for web requests to all
        // the filters, i.e. 'params', 'flash', 'request', etc.
        WebMetaUtils.registerCommonWebProperties(mc, application)

        // Also make the application context available.
        mc.getApplicationContext = {-> applicationContext }

        // Add redirect and render methods (copy and pasted from the
        // controllers plugin).
        def redirect = new RedirectDynamicMethod()
        def render = new RenderDynamicMethod()

        mc.redirect = {Map args ->
            redirect.invoke(delegate, "redirect", args)
        }

        mc.render = {Object o ->
            render.invoke(delegate, "render", [o?.inspect()] as Object[])
        }

        mc.render = {String txt ->
            render.invoke(delegate, "render", [txt] as Object[])
        }

        mc.render = {Map args ->
            render.invoke(delegate, "render", [args] as Object[])
        }

        mc.render = {Closure c ->
            render.invoke(delegate, "render", [c] as Object[])
        }

        mc.render = {Map args, Closure c ->
            render.invoke(delegate, "render", [args, c] as Object[])
        }
    }

    def doWithApplicationContext = { applicationContext ->
        reloadFilters(application, applicationContext)
    }
<commit_msg>filters now share the same API as controllers<commit_after>/*
 * 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 grails.util.GrailsUtil
import org.apache.commons.logging.LogFactory
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsClass
import org.codehaus.groovy.grails.commons.metaclass.MetaClassEnhancer
import org.codehaus.groovy.grails.plugins.web.api.ControllersApi
import org.springframework.beans.factory.config.MethodInvokingFactoryBean

 /**
 * @author Mike
 * @author <NAME>
 *
 * @since 1.0
 */
class FiltersGrailsPlugin {

    private static final TYPE = FiltersConfigArtefactHandler.TYPE

    def version = GrailsUtil.getGrailsVersion()
    def dependsOn = [:]
    def artefacts = [ FiltersConfigArtefactHandler ]
    def watchedResources = "file:./grails-app/conf/**/*Filters.groovy"
    def log = LogFactory.getLog(FiltersGrailsPlugin)

    static final BEANS = { GrailsClass filter ->
        "${filter.fullName}Class"(MethodInvokingFactoryBean) {
            targetObject = ref("grailsApplication", true)
            targetMethod = "getArtefact"
            arguments = [TYPE, filter.fullName]
        }
        "${filter.fullName}"(filter.clazz) { bean ->
            bean.singleton = true
            bean.autowire = "byName"
        }
    }

    def doWithSpring = {
        filterInterceptor(CompositeInterceptor)

        for (filter in application.getArtefacts(TYPE)) {
            def callable = BEANS.curry(filter)
            callable.delegate = delegate
            callable.call()
        }
    }

    def doWithDynamicMethods = { applicationContext ->
        def mc = FilterConfig.metaClass
        ControllersApi controllerApi = applicationContext.getBean("instanceControllersApi",ControllersApi)
        MetaClassEnhancer enhancer = new MetaClassEnhancer()
        enhancer.addApi controllerApi
        enhancer.enhance mc
    }

    def doWithApplicationContext = { applicationContext ->
        reloadFilters(application, applicationContext)
    }

{'fraction_non_alphanumeric': 0.09450930893026191, 'fraction_numerical': 0.004417797412432944, 'mean_word_length': 2.9033251231527095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5716652', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1708, 'n_words': 462}