001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021
022import java.io.*;
023import java.lang.annotation.*;
024import java.util.*;
025import java.util.logging.*;
026
027import org.apache.juneau.*;
028import org.apache.juneau.config.*;
029import org.apache.juneau.cp.*;
030import org.apache.juneau.http.header.*;
031import org.apache.juneau.http.response.*;
032import org.apache.juneau.rest.*;
033import org.apache.juneau.rest.guard.*;
034import org.apache.juneau.rest.httppart.*;
035import org.apache.juneau.rest.matcher.*;
036
037import jakarta.servlet.*;
038import jakarta.servlet.http.*;
039
040/**
041 * Identifies a method that gets called immediately before the <ja>@RestOp</ja> annotated method gets called.
042 *
043 * <p>
044 * At this point, the {@link RestRequest} object has been fully initialized, and all {@link RestGuard} and
045 * {@link RestMatcher} objects have been called.
046 *
047 * <p>
048 * The list of valid parameter types are as follows:
049 * <ul>
050 *    <li>Servlet request/response objects:
051 *       <ul class='javatreec'>
052 *          <li>{@link HttpServletRequest}
053 *          <li>{@link HttpServletResponse}
054 *       </ul>
055 *    <li>Extended request/response objects:
056 *       <ul class='javatreec'>
057 *          <li>{@link RestRequest}
058 *          <li>{@link RestResponse}
059 *       </ul>
060 *    <li>Header objects:
061 *       <ul class='javatreec'>
062 *          <li>{@link Accept}
063 *          <li>{@link AcceptCharset}
064 *          <li>{@link AcceptEncoding}
065 *          <li>{@link AcceptLanguage}
066 *          <li>{@link Authorization}
067 *          <li>{@link CacheControl}
068 *          <li>{@link Connection}
069 *          <li>{@link ContentLength}
070 *          <li>{@link ContentType}
071 *          <li>{@link org.apache.juneau.http.header.Date}
072 *          <li>{@link Expect}
073 *          <li>{@link From}
074 *          <li>{@link Host}
075 *          <li>{@link IfMatch}
076 *          <li>{@link IfModifiedSince}
077 *          <li>{@link IfNoneMatch}
078 *          <li>{@link IfRange}
079 *          <li>{@link IfUnmodifiedSince}
080 *          <li>{@link MaxForwards}
081 *          <li>{@link Pragma}
082 *          <li>{@link ProxyAuthorization}
083 *          <li>{@link Range}
084 *          <li>{@link Referer}
085 *          <li>{@link TE}
086 *          <li>{@link UserAgent}
087 *          <li>{@link Upgrade}
088 *          <li>{@link Via}
089 *          <li>{@link Warning}
090 *          <li>{@link TimeZone}
091 *       </ul>
092 *    <li>Other objects:
093 *       <ul class='javatreec'>
094 *          <li>{@link ResourceBundle}
095 *          <li>{@link Messages}
096 *          <li>{@link InputStream}
097 *          <li>{@link ServletInputStream}
098 *          <li>{@link Reader}
099 *          <li>{@link OutputStream}
100 *          <li>{@link ServletOutputStream}
101 *          <li>{@link Writer}
102 *          <li>{@link RequestHeaders}
103 *          <li>{@link RequestQueryParams}
104 *          <li>{@link RequestFormParams}
105 *          <li>{@link RequestPathParams}
106 *          <li>{@link Logger}
107 *          <li>{@link RestContext}
108 *          <li>{@link org.apache.juneau.parser.Parser}
109 *          <li>{@link Locale}
110 *          <li>{@link Swagger}
111 *          <li>{@link RequestContent}
112 *          <li>{@link Config}
113 *          <li>{@link UriContext}
114 *          <li>{@link UriResolver}
115 *       </ul>
116 * </ul>
117 *
118 * <h5 class='figure'>Example:</h5>
119 * <p class='bjava'>
120 *    <ja>@Rest</ja>(...)
121 *    <jk>public class</jk> MyResource <jk>extends</jk> BasicRestServlet {
122 *
123 *       <jc>// Log the incoming request.</jc>
124 *       <ja>@RestPreCall</ja>
125 *       <jk>public void</jk> onPreCall(Accept <jv>accept</jv>, Logger <jv>logger</jv>) {
126 *          <jv>logger</jv>.fine(<js>"Accept {0} header found."</js>, <jv>accept</jv>);
127 *       }
128 *    }
129 * </p>
130 *
131 * <h5 class='section'>Notes:</h5><ul>
132 *    <li class='note'>
133 *       The method should return <jk>void</jk> although if it does return any value, the value will be ignored.
134 *    <li class='note'>
135 *       The method should be <jk>public</jk> although other visibilities are valid if the security manager allows it.
136 *    <li class='note'>
137 *       Static methods can be used.
138 *    <li class='note'>
139 *       Multiple pre-call methods can be defined on a class.
140 *       <br>Pre-call methods on parent classes are invoked before pre-call methods on child classes.
141 *       <br>The order of pre-call method invocations within a class is alphabetical, then by parameter count, then by parameter types.
142 *    <li class='note'>
143 *       The method can throw any exception.
144 *       <br>{@link BasicHttpException BasicHttpExceptions} can be thrown to cause a particular HTTP error status code.
145 *       <br>All other exceptions cause an HTTP 500 error status code.
146 *    <li class='note'>
147 *       Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
148 *       <br>The method is still considered part of the parent class for ordering purposes even though it's
149 *       overridden by the child class.
150 *    <li class='note'>
151 *       It's advisable not to mess around with the HTTP content itself since you may end up consuming the content
152 *       before the actual REST method has a chance to use it.
153 * </ul>
154 *
155 * <h5 class='section'>See Also:</h5><ul>
156 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/LifecycleHooks">Lifecycle Hooks</a>
157 * </ul>
158 */
159@Target({METHOD,TYPE})
160@Retention(RUNTIME)
161@Inherited
162@Repeatable(RestPreCallAnnotation.Array.class)
163public @interface RestPreCall {
164
165    /**
166     * Optional description for the exposed API.
167     *
168     * @return The annotation value.
169     * @since 9.2.0
170     */
171    String[] description() default {};
172
173    /**
174    * Dynamically apply this annotation to the specified methods.
175    *
176    * <h5 class='section'>See Also:</h5><ul>
177    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
178    * </ul>
179    *
180    * @return The annotation value.
181    */
182   String[] on() default {};
183}