001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020/**
021 * Identifies Java methods on a resource/servlet class that get invoked during particular lifecycle events of
022 * the servlet or REST call.
023 *
024 * <p>
025 * For example, if you want to add an initialization method to your resource:
026 * <p class='bcode w800'>
027 *    <ja>@Rest</ja>(...)
028 *    <jk>public class</jk> MyResource  {
029 *
030 *       <jc>// Our database.</jc>
031 *       <jk>private</jk> Map&lt;Integer,Object&gt; <jf>myDatabase</jf>;
032 *
033 *       <ja>@RestHook</ja>(<jsf>INIT</jsf>)
034 *       <jk>public void</jk> initMyDatabase(RestContextBuilder builder) <jk>throws</jk> Exception {
035 *          <jf>myDatabase</jf> = <jk>new</jk> LinkedHashMap&lt;&gt;();
036 *       }
037 *    }
038 * </p>
039 *
040 * <p>
041 * Or if you want to intercept REST calls:
042 * <p class='bcode w800'>
043 *    <ja>@Rest</ja>(...)
044 *    <jk>public class</jk> MyResource {
045 *
046 *       <jc>// Add a request attribute to all incoming requests.</jc>
047 *       <ja>@RestHook</ja>(<jsf>PRE_CALL</jsf>)
048 *       <jk>public void</jk> onPreCall(RestRequest req) {
049 *          req.setAttribute(<js>"foo"</js>, <js>"bar"</js>);
050 *       }
051 *    }
052 * </p>
053 *
054 * <p>
055 * The hook events can be broken down into two categories:
056 * <ul class='spaced-list'>
057 *    <li>Resource lifecycle events:
058 *       <ul>
059 *          <li>{@link HookEvent#INIT INIT} - Right before initialization.
060 *          <li>{@link HookEvent#POST_INIT POST_INIT} - Right after initialization.
061 *          <li>{@link HookEvent#POST_INIT_CHILD_FIRST POST_INIT_CHILD_FIRST} - Right after initialization, but run child methods first.
062 *          <li>{@link HookEvent#DESTROY DESTROY} - Right before servlet destroy.
063 *       </ul>
064 *    <li>REST call lifecycle events:
065 *       <ul>
066 *          <li>{@link HookEvent#START_CALL START_CALL} - At the beginning of a REST call.
067 *          <li>{@link HookEvent#PRE_CALL PRE_CALL} - Right before the <ja>@RestMethod</ja> method is invoked.
068 *          <li>{@link HookEvent#POST_CALL POST_CALL} - Right after the <ja>@RestMethod</ja> method is invoked.
069 *          <li>{@link HookEvent#END_CALL END_CALL} - At the end of the REST call after the response has been flushed.
070 *       </ul>
071 * </ul>
072 *
073 * <ul class='seealso'>
074 *    <li class='link'>{@doc juneau-rest-server.Instantiation.LifecycleHooks}
075 * </ul>
076 */
077@Documented
078@Target(METHOD)
079@Retention(RUNTIME)
080@Inherited
081public @interface RestHook {
082
083   /**
084    * The lifecycle event.
085    */
086   HookEvent value();
087}