Lifecycle Hooks

Lifecycle hooks allow you to hook into lifecycle events of the servlet/resource creation and REST calls.

For example, if you want to add an initialization method to your resource:

@RestResource(...) public class MyResource { // Our database. private Map<Integer,Object> myDatabase; @RestHook(INIT) public void initMyDatabase(RestContextBuilder builder) throws Exception { myDatabase = new LinkedHashMap<>(); } }

Or if you want to intercept REST calls:

@RestResource(...) public class MyResource { // Add a request attribute to all incoming requests. @RestHook(PRE_CALL) public void onPreCall(RestRequest req) { req.setAttribute("foo", "bar"); } }

The hook events can be broken down into two categories:

- Resource lifecycle events:
  - {@link oajr.annotation.HookEvent#INIT INIT} - Right before initialization.
  - {@link oajr.annotation.HookEvent#POST_INIT POST_INIT} - Right after initialization.
  - {@link oajr.annotation.HookEvent#POST_INIT_CHILD_FIRST POST_INIT_CHILD_FIRST} - Right after initialization, but run child methods first.
  - {@link oajr.annotation.HookEvent#DESTROY DESTROY} - Right before servlet destroy.
- REST call lifecycle events:
  - {@link oajr.annotation.HookEvent#START_CALL START_CALL} - At the beginning of a REST call.
  - {@link oajr.annotation.HookEvent#PRE_CALL PRE_CALL} - Right before the @RestMethod method is invoked.
  - {@link oajr.annotation.HookEvent#POST_CALL POST_CALL} - Right after the @RestMethod method is invoked.
  - {@link oajr.annotation.HookEvent#END_CALL END_CALL} - At the end of the REST call after the response has been flushed.

##### See Also:

- {@link oajr.annotation.RestHook}