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.http.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020import org.apache.juneau.annotation.*;
021import org.apache.juneau.httppart.*;
022import org.apache.juneau.oapi.*;
023
024/**
025 * REST request header annotation.
026 *
027 * <p>
028 * Identifies a POJO to be used as the header of an HTTP request.
029 *
030 * <p>
031 * Can be used in the following locations:
032 * <ul>
033 *    <li>Arguments and argument-types of server-side <ja>@RestOp</ja>-annotated methods.
034 *    <li>Arguments and argument-types of client-side <ja>@RemoteResource</ja>-annotated interfaces.
035 *    <li>Methods and return types of server-side and client-side <ja>@Request</ja>-annotated interfaces.
036 * </ul>
037 *
038 * <h5 class='topic'>Arguments and argument-types of server-side @RestOp-annotated methods</h5>
039 * <p>
040 * Annotation that can be applied to a parameter of a <ja>@RestOp</ja>-annotated method to identify it as a HTTP
041 * request header.
042 *
043 * <h5 class='section'>Example:</h5>
044 * <p class='bjava'>
045 *    <ja>@RestGet</ja>
046 *    <jk>public void</jk> doGet(<ja>@Header</ja>(<js>"ETag"</js>) UUID <jv>etag</jv>) {...}
047 * </p>
048 *
049 * <p>
050 * This is functionally equivalent to the following code...
051 * <p class='bjava'>
052 *    <ja>@RestGet</ja>
053 *    <jk>public void</jk> doGet(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) {
054 *       UUID <jv>etag</jv> = <jv>req</jv>.getHeader(<js>"ETag"</js>).as(UUID.<jk>class</jk>).orElse(<jk>null</jk>);
055 *       ...
056 *    }
057 * </p>
058 *
059 * <h5 class='section'>See Also:</h5><ul>
060 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Swagger</a>
061 *    <li class='extlink'><a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>
062 * </ul>
063 *
064 * <h5 class='topic'>Arguments and argument-types of client-side @RemoteResource-annotated interfaces</h5>
065 * <p>
066 * Annotation applied to Java method arguments of interface proxies to denote that they are serialized as an HTTP
067 * header value.
068 *
069 * <h5 class='section'>See Also:</h5><ul>
070 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrc.Header">@Header</a>
071 * </ul>
072 *
073 * <h5 class='topic'>Methods and return types of server-side and client-side @Request-annotated interfaces</h5>
074 * <p>
075 * <h5 class='section'>See Also:</h5><ul>
076 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrc.Request">@Request</a>
077 * </ul>
078 * <p>
079 * <h5 class='section'>See Also:</h5><ul>
080
081 * </ul>
082 */
083@Documented
084@Target({PARAMETER,METHOD,TYPE,FIELD})
085@Retention(RUNTIME)
086@Inherited
087@Repeatable(HeaderAnnotation.Array.class)
088@ContextApply(HeaderAnnotation.Applier.class)
089public @interface Header {
090
091   /**
092    * Default value for this parameter.
093    *
094    * @return The annotation value.
095    */
096   String def() default "";
097
098   /**
099    * HTTP header name.
100    * <p>
101    * A blank value (the default) indicates to reuse the bean property name when used on a request bean property.
102    *
103    * <p>
104    * The value should be either a valid HTTP header name, or <js>"*"</js> to represent multiple name/value pairs
105    *
106    * <p>
107    * A blank value (the default) has the following behavior:
108    * <ul class='spaced-list'>
109    *    <li>
110    *       If the data type is <c>NameValuePairs</c>, <c>Map</c>, or a bean,
111    *       then it's the equivalent to <js>"*"</js> which will cause the value to be serialized as name/value pairs.
112    *
113    *       <h5 class='figure'>Examples:</h5>
114    *       <p class='bjava'>
115    *    <jc>// When used on a REST method</jc>
116    *    <ja>@RestPost</ja>(<js>"/addPet"</js>)
117    *    <jk>public void</jk> addPet(<ja>@Header</ja> JsonMap <jv>allHeaderParameters</jv>) {...}
118    *       </p>
119    *       <p class='bjava'>
120    *    <jc>// When used on a remote method parameter</jc>
121    *    <ja>@RemoteResource</ja>(path=<js>"/myproxy"</js>)
122    *    <jk>public interface</jk> MyProxy {
123    *
124    *       <jc>// Equivalent to @Header("*")</jc>
125    *       <ja>@RemoteGet</ja>(<js>"/mymethod"</js>)
126    *       String myProxyMethod1(<ja>@Header</ja> Map&lt;String,Object&gt; <jv>allHeaderParameters</jv>);
127    *    }
128    *       </p>
129    *       <p class='bjava'>
130    *    <jc>// When used on a request bean method</jc>
131    *    <jk>public interface</jk> MyRequest {
132    *
133    *       <jc>// Equivalent to @Header("*")</jc>
134    *       <ja>@Header</ja>
135    *       Map&lt;String,Object&gt; getFoo();
136    *    }
137    *       </p>
138    *    </li>
139    *    <li>
140    *       If used on a request bean method, uses the bean property name.
141    *
142    *       <h5 class='figure'>Example:</h5>
143    *       <p class='bjava'>
144    *    <jk>public interface</jk> MyRequest {
145    *
146    *       <jc>// Equivalent to @Header("Foo")</jc>
147    *       <ja>@Header</ja>
148    *       <ja>@Beanp</ja>(<js>"Foo"</js>)
149    *       String getFoo();
150    *    }
151    *       </p>
152    *    </li>
153    * </ul>
154    *
155    * @return The annotation value.
156    */
157   String name() default "";
158
159   /**
160    * Dynamically apply this annotation to the specified classes.
161    *
162    * <h5 class='section'>See Also:</h5><ul>
163    *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
164    * </ul>
165    *
166    * @return The annotation value.
167    */
168   String[] on() default {};
169
170   /**
171    * Dynamically apply this annotation to the specified classes.
172    *
173    * <p>
174    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
175    *
176    * <h5 class='section'>See Also:</h5><ul>
177    *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
178    * </ul>
179    *
180    * @return The annotation value.
181    */
182   Class<?>[] onClass() default {};
183
184   /**
185    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
186    *
187    * <p>
188    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
189    *
190    * @return The annotation value.
191    */
192   Class<? extends HttpPartParser> parser() default HttpPartParser.Void.class;
193
194   /**
195    * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>.
196    *
197    * <p>
198    * The schema defining the type used for parameter.
199    *
200    * <p>
201    * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type.
202    * Values specified on this field override values specified on the type, and values specified on child types override values
203    * specified on parent types.
204    *
205    * <h5 class='section'>Used for:</h5>
206    * <ul class='spaced-list'>
207    *    <li>
208    *       Server-side schema-based parsing and parsing validation.
209    *    <li>
210    *       Server-side generated Swagger documentation.
211    *    <li>
212    *       Client-side schema-based serializing and serializing validation.
213    * </ul>
214    *
215    * @return The annotation value.
216    */
217   Schema schema() default @Schema;
218
219   /**
220    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
221    *
222    * <p>
223    * Overrides for this part the part serializer defined on the REST client which by default is {@link OpenApiSerializer}.
224    *
225    * @return The annotation value.
226    */
227   Class<? extends HttpPartSerializer> serializer() default HttpPartSerializer.Void.class;
228
229   /**
230    * A synonym for {@link #name()}.
231    *
232    * <p>
233    * Allows you to use shortened notation if you're only specifying the name.
234    *
235    * <p>
236    * The following are completely equivalent ways of defining a header entry:
237    * <p class='bjava'>
238    *    <jk>public</jk> Order placeOrder(<ja>@Header</ja>(name=<js>"api_key"</js>) String <jv>apiKey</jv>) {...}
239    * </p>
240    * <p class='bjava'>
241    *    <jk>public</jk> Order placeOrder(<ja>@Header</ja>(<js>"api_key"</js>) String <jv>apiKey</jv>) {...}
242    * </p>
243    *
244    * @return The annotation value.
245    */
246   String value() default "";
247}