Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.51 KB

README.adoc

File metadata and controls

47 lines (37 loc) · 1.51 KB

Working with the context (20 minutes)

The context is a place where handlers can register objects so that other handlers can get them in a later stage in the chain.

To build a Registry, you can use the methods in the Registries class:

handler {
    //Builds a registry builder and adds 2 objects to it
    Registry registry = Registries.registry().add(MyService, new MyServiceImpl()).add('String instance').build()
    next(registry)
}

handler('foo') {
    assert context.get(MyService) instanceof MyServiceImpl
    assert context.get(String) == 'String instance'
}

Sometimes you just need to pass a single object to downstream handlers. Note also that alternatively, you can get access registry object by defining them as the closure argument of the handler:

handler {
    String token = request.headers.get('X-Auth-Token')
    next(just(token))
}

handler('foo') {
    String token = context.get(String)
    doWhatEver(token)
}

handler('bar') { String token ->
    doWhatEver(token)
}

For this exercise, write the necessary handler chain in Ratpack.groovy to make all the tests pass.