0

I'm wanting to convert my filters to a handlerInterceptor in my plugin but am not quite sure how to go about it. It looks like I am supposed to use 'doWithSpring' and map a handler interceptor but I'm not seeing where I map the url's.

To save some discussion, I know filters do this in grails. I just want to convert this code to Java in my plugin and use a HandlerInterceptor to decrease overhead on these calls.

Anyone have any ideas?

Orubel
  • 316
  • 4
  • 16

2 Answers2

0

In Grails all handler interceptors are registered with the filterInterceptor bean which is a CompositeInterceptor. To add new handler interceptors to this bean use the addHandler method.

The source code for the CompositeInterceptor should give you a better idea of how this is done. Off the top of my head it may look something like this:

def doWithSpring = {
  filterInterceptor.addHandler(MyCustomHandlerClass)
}

As far as what URLs your handlers respond to, well, that's up to your implementation inside the handler.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • trying to avoid use of the filter as I can just use the filter directly in the plugin. want to use underlying spring handlerInterceptors directly to avoid a bit of runaround in the application. This may be outside the app context but I can always grab the context should I need it; more worried about grabbing request/response. – Orubel Jul 03 '14 at 20:38
  • Okay, I was under the impression this is the actual handler interceptors for the entire spring application with full access to the request/response. – Joshua Moore Jul 03 '14 at 20:43
  • we are talking the same thing... the handlerInterceptor. I don't want a filter as I can just use a filter directly. Why would I create one indirectly when I can go to the source? Thats what I am talking about. I want to create Java classes that make use of a handlerInterceptor in a grails plugin – Orubel Jul 03 '14 at 20:47
  • Then create those java classes and register them as I have outlined there. Nothing stopping you from doing that inside your plugin. – Joshua Moore Jul 03 '14 at 20:57
  • Your advice is to add as a filterInterceptor which I am trying to avoid as filters are merely handlerInterceptors from my understanding and it would be a 'shortcut' and provide faster processing for a single interpreted endpoint to go directly through a handlerInterceptor if you can. – Orubel Jul 03 '14 at 22:38
  • So, are you looking to register a ServletFilter or a HandlerInterceptor? Don't be confused by the name filterInterceptor, it's actually a collection of HandlerInterceptors, of which the Grails Filters do become. Sorry for not understanding exactly what you are looking for here, but it's likely the terminology that has me confused. – Joshua Moore Jul 03 '14 at 22:55
  • No problem. Yeah this is why I say handlerInterceptor because thats the name of the class that you extend. I think its my fault for confusing you with the code example below... let me fix real quick :) – Orubel Jul 03 '14 at 23:27
0

Actually I think I may have solved it. You have to extend the HandlerInterceptor and register the bean...

mvc.'interceptors'() {
    mvc.'mapping'('path': '/your/uri/**') {
        bean('class': 'CustomInterceptor')
    }
}

Here's a link to what I'm talking about (bind Spring HandlerInterceptor only to one controller)

Going to try this out and test it...

Community
  • 1
  • 1
Orubel
  • 316
  • 4
  • 16