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.io.*;
019import java.lang.annotation.*;
020import java.util.*;
021import java.util.logging.*;
022
023import jakarta.servlet.*;
024import jakarta.servlet.http.*;
025
026import org.apache.juneau.*;
027import org.apache.juneau.config.*;
028import org.apache.juneau.cp.*;
029import org.apache.juneau.http.header.*;
030import org.apache.juneau.http.response.*;
031import org.apache.juneau.rest.*;
032import org.apache.juneau.rest.guard.*;
033import org.apache.juneau.rest.httppart.*;
034import org.apache.juneau.rest.matcher.*;
035
036/**
037 * Identifies a method that gets called immediately before the <ja>@RestOp</ja> annotated method gets called.
038 *
039 * <p>
040 * At this point, the {@link RestRequest} object has been fully initialized, and all {@link RestGuard} and
041 * {@link RestMatcher} objects have been called.
042 *
043 * <p>
044 * The list of valid parameter types are as follows:
045 * <ul>
046 *    <li>Servlet request/response objects:
047 *       <ul class='javatreec'>
048 *          <li>{@link HttpServletRequest}
049 *          <li>{@link HttpServletResponse}
050 *       </ul>
051 *    <li>Extended request/response objects:
052 *       <ul class='javatreec'>
053 *          <li>{@link RestRequest}
054 *          <li>{@link RestResponse}
055 *       </ul>
056 *    <li>Header objects:
057 *       <ul class='javatreec'>
058 *          <li>{@link Accept}
059 *          <li>{@link AcceptCharset}
060 *          <li>{@link AcceptEncoding}
061 *          <li>{@link AcceptLanguage}
062 *          <li>{@link Authorization}
063 *          <li>{@link CacheControl}
064 *          <li>{@link Connection}
065 *          <li>{@link ContentLength}
066 *          <li>{@link ContentType}
067 *          <li>{@link org.apache.juneau.http.header.Date}
068 *          <li>{@link Expect}
069 *          <li>{@link From}
070 *          <li>{@link Host}
071 *          <li>{@link IfMatch}
072 *          <li>{@link IfModifiedSince}
073 *          <li>{@link IfNoneMatch}
074 *          <li>{@link IfRange}
075 *          <li>{@link IfUnmodifiedSince}
076 *          <li>{@link MaxForwards}
077 *          <li>{@link Pragma}
078 *          <li>{@link ProxyAuthorization}
079 *          <li>{@link Range}
080 *          <li>{@link Referer}
081 *          <li>{@link TE}
082 *          <li>{@link UserAgent}
083 *          <li>{@link Upgrade}
084 *          <li>{@link Via}
085 *          <li>{@link Warning}
086 *          <li>{@link TimeZone}
087 *       </ul>
088 *    <li>Other objects:
089 *       <ul class='javatreec'>
090 *          <li>{@link ResourceBundle}
091 *          <li>{@link Messages}
092 *          <li>{@link InputStream}
093 *          <li>{@link ServletInputStream}
094 *          <li>{@link Reader}
095 *          <li>{@link OutputStream}
096 *          <li>{@link ServletOutputStream}
097 *          <li>{@link Writer}
098 *          <li>{@link RequestHeaders}
099 *          <li>{@link RequestQueryParams}
100 *          <li>{@link RequestFormParams}
101 *          <li>{@link RequestPathParams}
102 *          <li>{@link Logger}
103 *          <li>{@link RestContext}
104 *          <li>{@link org.apache.juneau.parser.Parser}
105 *          <li>{@link Locale}
106 *          <li>{@link Swagger}
107 *          <li>{@link RequestContent}
108 *          <li>{@link Config}
109 *          <li>{@link UriContext}
110 *          <li>{@link UriResolver}
111 *       </ul>
112 * </ul>
113 *
114 * <h5 class='figure'>Example:</h5>
115 * <p class='bjava'>
116 *    <ja>@Rest</ja>(...)
117 *    <jk>public class</jk> MyResource <jk>extends</jk> BasicRestServlet {
118 *
119 *       <jc>// Log the incoming request.</jc>
120 *       <ja>@RestPreCall</ja>
121 *       <jk>public void</jk> onPreCall(Accept <jv>accept</jv>, Logger <jv>logger</jv>) {
122 *          <jv>logger</jv>.fine(<js>"Accept {0} header found."</js>, <jv>accept</jv>);
123 *       }
124 *    }
125 * </p>
126 *
127 * <h5 class='section'>Notes:</h5><ul>
128 *    <li class='note'>
129 *       The method should return <jk>void</jk> although if it does return any value, the value will be ignored.
130 *    <li class='note'>
131 *       The method should be <jk>public</jk> although other visibilities are valid if the security manager allows it.
132 *    <li class='note'>
133 *       Static methods can be used.
134 *    <li class='note'>
135 *       Multiple pre-call methods can be defined on a class.
136 *       <br>Pre-call methods on parent classes are invoked before pre-call methods on child classes.
137 *       <br>The order of pre-call method invocations within a class is alphabetical, then by parameter count, then by parameter types.
138 *    <li class='note'>
139 *       The method can throw any exception.
140 *       <br>{@link BasicHttpException BasicHttpExceptions} can be thrown to cause a particular HTTP error status code.
141 *       <br>All other exceptions cause an HTTP 500 error status code.
142 *    <li class='note'>
143 *       Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>.
144 *       <br>The method is still considered part of the parent class for ordering purposes even though it's
145 *       overridden by the child class.
146 *    <li class='note'>
147 *       It's advisable not to mess around with the HTTP content itself since you may end up consuming the content
148 *       before the actual REST method has a chance to use it.
149 * </ul>
150 *
151 * <h5 class='section'>See Also:</h5><ul>
152 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.LifecycleHooks">Lifecycle Hooks</a>
153 * </ul>
154 */
155@Target({METHOD,TYPE})
156@Retention(RUNTIME)
157@Inherited
158@Repeatable(RestPreCallAnnotation.Array.class)
159public @interface RestPreCall {
160
161   /**
162    * Dynamically apply this annotation to the specified methods.
163    *
164    * <h5 class='section'>See Also:</h5><ul>
165    *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
166    * </ul>
167    *
168    * @return The annotation value.
169    */
170   String[] on() default {};
171}