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'> 027 * <ja>@RestResource</ja>(...) 028 * <jk>public class</jk> MyResource { 029 * 030 * <jc>// Our database.</jc> 031 * <jk>private</jk> Map<Integer,Object> <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<>(); 036 * } 037 * } 038 * </p> 039 * 040 * <p> 041 * Or if you want to intercept REST calls: 042 * <p class='bcode'> 043 * <ja>@RestResource</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 * <h5 class='section'>See Also:</h5> 074 * <ul> 075 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.LifecycleHooks">Overview > juneau-rest-server > Lifecycle Hooks</a> 076 * </ul> 077 */ 078@Documented 079@Target(METHOD) 080@Retention(RUNTIME) 081@Inherited 082public @interface RestHook { 083 084 /** 085 * The lifecycle event. 086 */ 087 HookEvent value(); 088}