| <!-- |
| /*************************************************************************************************************************** |
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations under the License. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Lifecycle Hooks |
| |
| <p> |
| Lifecycle hooks allow you to hook into lifecycle events of the servlet/resource creation and REST calls. |
| </p> |
| <p> |
| For example, if you want to add an initialization method to your resource: |
| </p> |
| <p class='bpcode w800'> |
| <ja>@Rest</ja>(...) |
| <jk>public class</jk> MyResource { |
| |
| <jc>// Our database.</jc> |
| <jk>private</jk> Map<Integer,Object> <jf>myDatabase</jf>; |
| |
| <ja>@RestHook</ja>(<jsf>INIT</jsf>) |
| <jk>public void</jk> initMyDatabase(RestContextBuilder builder) <jk>throws</jk> Exception { |
| <jf>myDatabase</jf> = <jk>new</jk> LinkedHashMap<>(); |
| } |
| } |
| </p> |
| <p> |
| Or if you want to intercept REST calls: |
| </p> |
| <p class='bpcode w800'> |
| <ja>@Rest</ja>(...) |
| <jk>public class</jk> MyResource { |
| |
| <jc>// Add a request attribute to all incoming requests.</jc> |
| <ja>@RestHook</ja>(<jsf>PRE_CALL</jsf>) |
| <jk>public void</jk> onPreCall(RestRequest req) { |
| req.setAttribute(<js>"foo"</js>, <js>"bar"</js>); |
| } |
| } |
| </p> |
| <p> |
| The hook events can be broken down into two categories: |
| </p> |
| <ul class='spaced-list'> |
| <li>Resource lifecycle events: |
| <ul> |
| <li class='jf'>{@link oajr.annotation.HookEvent#INIT INIT} - Right before initialization. |
| <li class='jf'>{@link oajr.annotation.HookEvent#POST_INIT POST_INIT} - Right after initialization. |
| <li class='jf'>{@link oajr.annotation.HookEvent#POST_INIT_CHILD_FIRST POST_INIT_CHILD_FIRST} - Right after initialization, but run child methods first. |
| <li class='jf'>{@link oajr.annotation.HookEvent#DESTROY DESTROY} - Right before servlet destroy. |
| </ul> |
| <li>REST call lifecycle events: |
| <ul> |
| <li class='jf'>{@link oajr.annotation.HookEvent#START_CALL START_CALL} - At the beginning of a REST call. |
| <li class='jf'>{@link oajr.annotation.HookEvent#PRE_CALL PRE_CALL} - Right before the <ja>@RestMethod</ja> method is invoked. |
| <li class='jf'>{@link oajr.annotation.HookEvent#POST_CALL POST_CALL} - Right after the <ja>@RestMethod</ja> method is invoked. |
| <li class='jf'>{@link oajr.annotation.HookEvent#END_CALL END_CALL} - At the end of the REST call after the response has been flushed. |
| </ul> |
| </ul> |
| |
| <ul class='seealso'> |
| <li class='ja'>{@link oajr.annotation.RestHook} |
| </ul> |