Grails Filter Fail (Update)

[sigh] Some days just seem to be wasted trying to work out why some apparently simple task cannot be accomplished. The frequency of such days has increased somewhat since I started using Grails – while I am a fan of the framework it is still occasionally buggy and badly documented and I seem to stumble into issues every week.

Grails Fails

Today I tried to add a filter to my website to intercept calls on a particular context and redirect the browser if the user’s account is not set up correctly.

Easy.

Step 1: run grails create-filters lourish.filters.MyFilters

Step 2: add some logic to my filter:

package lourish.filters

class MyFilters{
    def authenticateService
    def filters = {
        requiresLocation(uri:'/somepath/**') {
            before = {
                def user = authenticateService.userDomain()
                if (!user.email) {
                        redirect(controller: "user", action: "addEmail")
                        return false
                    }
                }
                return true
            }
            after = {

            }
            afterView = {

            }
        }
    }
}

Step 3: Run app and browse to /somepath and see the wonderful results

Step 4: Wonder why the filter is not called…

Some hours pass.

I’ll spare you the whole story but when I modified the filter it would start being called, as if changing the file triggered Grails into recognising it. However, next time I started the app the filter was forgotten again until the file was touched!

My eventual solution has been to put the filter in the default package and now I’ve done that it always works. I’ll perform some more tests then raise a bug if I can repro it.

Update: Dave Fails

I’ve tried pretty hard to reproduce this and I simply cannot, so after some discussion on the Grails mailing list I have to conclude that I was at fault and not Grails. It seems that the filters work correctly as long as the package declaration matches the directory structure and additionally if the package exists elsewhere you’ll get this error:

ERROR commons.DefaultGrailsApplication  - The class [lourish.filters.MyFilters.groovy]
 was not found when attempting to load Grails application
Advertisement

2 thoughts on “Grails Filter Fail (Update)

  1. Hey,

    have you figured it out in the meanwhile? I am using new Grails 2.0 and created a filter class (without create-filter script) and its not fireing either … do I have to register the filter somewhere? think it should get wired up automatically …

    timo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s