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