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 */
084@Documented
085@Target({ PARAMETER, METHOD, TYPE, FIELD })
086@Retention(RUNTIME)
087@Inherited
088@Repeatable(HeaderAnnotation.Array.class)
089@ContextApply(HeaderAnnotation.Applier.class)
090public @interface Header {
091
092   /**
093    * Default value for this parameter.
094    *
095    * @return The annotation value.
096    */
097   String def() default "";
098
099   /**
100    * Optional description for the exposed API.
101    *
102    * @return The annotation value.
103    * @since 9.2.0
104    */
105   String[] description() default {};
106
107   /**
108    * HTTP header name.
109    * <p>
110    * A blank value (the default) indicates to reuse the bean property name when used on a request bean property.
111    *
112    * <p>
113    * The value should be either a valid HTTP header name, or <js>"*"</js> to represent multiple name/value pairs
114    *
115    * <p>
116    * A blank value (the default) has the following behavior:
117    * <ul class='spaced-list'>
118    *    <li>
119    *       If the data type is <c>NameValuePairs</c>, <c>Map</c>, or a bean,
120    *       then it's the equivalent to <js>"*"</js> which will cause the value to be serialized as name/value pairs.
121    *
122    *       <h5 class='figure'>Examples:</h5>
123    *       <p class='bjava'>
124    *    <jc>// When used on a REST method</jc>
125    *    <ja>@RestPost</ja>(<js>"/addPet"</js>)
126    *    <jk>public void</jk> addPet(<ja>@Header</ja> JsonMap <jv>allHeaderParameters</jv>) {...}
127    *       </p>
128    *       <p class='bjava'>
129    *    <jc>// When used on a remote method parameter</jc>
130    *    <ja>@RemoteResource</ja>(path=<js>"/myproxy"</js>)
131    *    <jk>public interface</jk> MyProxy {
132    *
133    *       <jc>// Equivalent to @Header("*")</jc>
134    *       <ja>@RemoteGet</ja>(<js>"/mymethod"</js>)
135    *       String myProxyMethod1(<ja>@Header</ja> Map&lt;String,Object&gt; <jv>allHeaderParameters</jv>);
136    *    }
137    *       </p>
138    *       <p class='bjava'>
139    *    <jc>// When used on a request bean method</jc>
140    *    <jk>public interface</jk> MyRequest {
141    *
142    *       <jc>// Equivalent to @Header("*")</jc>
143    *       <ja>@Header</ja>
144    *       Map&lt;String,Object&gt; getFoo();
145    *    }
146    *       </p>
147    *    </li>
148    *    <li>
149    *       If used on a request bean method, uses the bean property name.
150    *
151    *       <h5 class='figure'>Example:</h5>
152    *       <p class='bjava'>
153    *    <jk>public interface</jk> MyRequest {
154    *
155    *       <jc>// Equivalent to @Header("Foo")</jc>
156    *       <ja>@Header</ja>
157    *       <ja>@Beanp</ja>(<js>"Foo"</js>)
158    *       String getFoo();
159    *    }
160    *       </p>
161    *    </li>
162    * </ul>
163    *
164    * @return The annotation value.
165    */
166   String name() default "";
167
168   /**
169    * Dynamically apply this annotation to the specified classes.
170    *
171    * <h5 class='section'>See Also:</h5><ul>
172    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
173    * </ul>
174    *
175    * @return The annotation value.
176    */
177   String[] on() default {};
178
179   /**
180    * Dynamically apply this annotation to the specified classes.
181    *
182    * <p>
183    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
184    *
185    * <h5 class='section'>See Also:</h5><ul>
186    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
187    * </ul>
188    *
189    * @return The annotation value.
190    */
191   Class<?>[] onClass() default {};
192
193   /**
194    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
195    *
196    * <p>
197    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
198    *
199    * @return The annotation value.
200    */
201   Class<? extends HttpPartParser> parser() default HttpPartParser.Void.class;
202
203   /**
204    * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>.
205    *
206    * <p>
207    * The schema defining the type used for parameter.
208    *
209    * <p>
210    * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type.
211    * Values specified on this field override values specified on the type, and values specified on child types override values
212    * specified on parent types.
213    *
214    * <h5 class='section'>Used for:</h5>
215    * <ul class='spaced-list'>
216    *    <li>
217    *       Server-side schema-based parsing and parsing validation.
218    *    <li>
219    *       Server-side generated Swagger documentation.
220    *    <li>
221    *       Client-side schema-based serializing and serializing validation.
222    * </ul>
223    *
224    * @return The annotation value.
225    */
226   Schema schema() default @Schema;
227
228   /**
229    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
230    *
231    * <p>
232    * Overrides for this part the part serializer defined on the REST client which by default is {@link OpenApiSerializer}.
233    *
234    * @return The annotation value.
235    */
236   Class<? extends HttpPartSerializer> serializer() default HttpPartSerializer.Void.class;
237
238   /**
239    * A synonym for {@link #name()}.
240    *
241    * <p>
242    * Allows you to use shortened notation if you're only specifying the name.
243    *
244    * <p>
245    * The following are completely equivalent ways of defining a header entry:
246    * <p class='bjava'>
247    *    <jk>public</jk> Order placeOrder(<ja>@Header</ja>(name=<js>"api_key"</js>) String <jv>apiKey</jv>) {...}
248    * </p>
249    * <p class='bjava'>
250    *    <jk>public</jk> Order placeOrder(<ja>@Header</ja>(<js>"api_key"</js>) String <jv>apiKey</jv>) {...}
251    * </p>
252    *
253    * @return The annotation value.
254    */
255   String value() default "";
256}