Contexts and Groovy DSL

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Contexts and Groovy DSL

uris77
I'm trying to follow this demo http://www.infoq.com/presentations/ratpack and ran into some issues using the dsl for context. This isn't working for me:

handlers{
  context(new Logger()) {
       handler { }
       get { ... }
   }
}

I get this error:
groovy.lang.MissingMethodException: No signature of method: Ratpack.context() is applicable for argument types: (Ratpack$_run_closure1_closure3_closure6) values: [[email protected]]
Possible solutions: collect(), collect(groovy.lang.Closure), notify(), inject(groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure)

I might be missing some import?
Reply | Threaded
Open this post in threaded view
|

Re: Contexts and Groovy DSL

Luke Daley
Administrator
Hi,

The API has changed since this presentation.

You’ll want:

handlers {
  register(Registries.just(new Logger())) {
       handler { }
       get { ... }
   }
}
Reply | Threaded
Open this post in threaded view
|

Re: Contexts and Groovy DSL

uris77
Is there any way to get a hold of the request object within register() ? So, I'm trying to have an 'easy' way to grab the current user. E.g.:

 prefix("admin") {
            register(Registries.just(new CurrentUser(request.get(SessionStorage)))) {
                get("secured"){
                    CurrentUser currentUser = context.get(CurrentUser.class)
                    render groovyTemplate([userName: currentUser.username], "secured.html")
                }
                get("logout"){
                    SessionStorage sessionStorage = request.get(SessionStorage)
                    sessionStorage.remove(sessionStorage.get(USER_PROFILE))
                    redirect("/")
                }
           }
}

I don't have access to the request object within Registries.just(), so I have to do this:

SessionStorage sessionStorage = request.get(SessionStorage)
CurrentUser currentUser = context.get(CurrentUser.class).setSessionStorage(sessionStorage)
render groovyTemplate([userName: currentUser.getUsername()], "secured.html")

Is there a preferred way of  doing this?
Reply | Threaded
Open this post in threaded view
|

Re: Contexts and Groovy DSL

uris77
Ah, nevermind. Got it now.
 prefix("admin") {
            register(Registries.just(new CurrentUser())) {
                handler {
                    SessionStorage sessionStorage = request.get(SessionStorage)
                    CurrentUser currentUser = context.get(CurrentUser.class).setSessionStorage(sessionStorage)
                    context.next(Registries.just(currentUser))
                }
                get("secured"){
                    CurrentUser currentUser = context.get(CurrentUser)
                    render groovyTemplate([userName: currentUser.getUsername()], "secured.html")
                }
                get("logout"){
                    SessionStorage sessionStorage = request.get(SessionStorage)
                    sessionStorage.remove(sessionStorage.get(USER_PROFILE))
                    redirect("/")
                }

            }
}
Reply | Threaded
Open this post in threaded view
|

Re: Contexts and Groovy DSL

Luke Daley
Administrator
Hi,

I’d do this differently…

prefix(“admin”) {
  handler { SessionStorage sessionStorage ->
    CurrentUser currentUser = new CurrentUser(sessionStorage)
    request.register(currentUser)
    next()  
  }
  get("secured”) { CurrentUser currentUser -> 
    render groovyTemplate([userName: currentUser.getUsername()], "secured.html”)
  }
  get("logout”) { SessionStorage sessionStorage ->
    sessionStorage.remove(sessionStorage.get(USER_PROFILE))
    redirect("/“)
  }
}

So two things in there:

1. It’s not a good idea to put request scoped objects into the context registry (it works, but it defeats some optimisations)
2. You can retrieve objects from the context or request registries by declaring them as params to the handler closures
Reply | Threaded
Open this post in threaded view
|

Re: Contexts and Groovy DSL

uris77
Nice. That is way more elegant than what I was trying. Thank you.