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