0

I'm starting to dig into Scalatra but I have some extensive experience in Jersey. One of the things I'm struggling with is how to get multiple filters for a controller in Scalatra? For example, I have three unique filters:

  1. CSRF
  2. XSS
  3. Authentication

Some of my controllers will only need 2 and 3, all will need 1, and others will only need 3. In the future I may have more filters. I understand the before() and after() methods, but those don't seem to help with the chaining I'm used to with Jersey. Can someone help shed light on what I should be looking for?

John S
  • 1,695
  • 2
  • 14
  • 25
  • Do your routes map well to the requirements? For instance, do all of the ones that only require filter 3 follow a similar routing pattern (like /foo/bar/* or similar)? – sberry Dec 30 '14 at 05:35
  • Yes and no, but your answer below actually works out better. I always hated having to look at the web.xml file to figure out what filter was applied to which controller, I like the before() filters much more for readability. – John S Dec 31 '14 at 05:04

1 Answers1

1

If you have well defined routes that share a common pattern depending on what you want to do then you could do

def before("/pattern1/*") {
    CSRF
}

def before("/pattern2/*") {
    XSS
    CSRF
}

def before("/pattern*") {
    Authenticate
}

And so on.

sberry
  • 128,281
  • 18
  • 138
  • 165
  • Ah, I didn't realize that you could have multiple before() and after() patterns, that actually helps a lot. Silly of me to miss that given that you can have other routes for the same HTTP type... Thanks, this helps quite a bit! – John S Dec 31 '14 at 05:03
  • No problem. It helps that I work with, and got to confirm with Ross Baker (https://github.com/rossabaker) :) – sberry Dec 31 '14 at 05:24