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 form-data annotation.
030 *
031 * <p>
032 *    Identifies a POJO to be used as a form-data entry on 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 URL query parameter.
045 *
046 * <p>
047 * Unlike {@link FormData @FormData}, using this annotation does not result in the servlet reading the contents of
048 * URL-encoded form posts.
049 * Therefore, this annotation can be used in conjunction with the {@link Content @Content} annotation or
050 * <c>RestRequest.getContent()</c> method for <c>application/x-www-form-urlencoded POST</c> calls.
051 *
052 * <h5 class='section'>Example:</h5>
053 * <p class='bjava'>
054 *    <ja>@RestGet</ja>
055 *    <jk>public void</jk> doGet(
056 *          <ja>@Query</ja>(<js>"p1"</js>) <jk>int</jk> <jv>p1</jv>,
057 *          <ja>@Query</ja>(<js>"p2"</js>) String <jv>p2</jv>,
058 *          <ja>@Query</ja>(<js>"p3"</js>) UUID <jv>p3</jv>
059 *       ) {...}
060 * </p>
061 *
062 * <p>
063 * This is functionally equivalent to the following code...
064 * <p class='bjava'>
065 *    <ja>@RestGet</ja>
066 *    <jk>public void</jk> doGet(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) {
067 *       <jk>int</jk> <jv>p1</jv> = <jv>req</jv>.getQueryParam(<js>"p1"</js>).asInteger().orElse(0);
068 *       String <jv>p2</jv> = <jv>req</jv>.getQueryParam(<js>"p2"</js>).asString().orElse(<jk>null</jk>);
069 *       UUID <jv>p3</jv> = <jv>req</jv>.getQueryParam(<js>"p3"</js>).as(UUID.<jk>class</jk>).orElse(<jk>null</jk>);
070 *       ...
071 *    }
072 * </p>
073 *
074 * <h5 class='section'>See Also:</h5><ul>
075 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
076 *    <li class='extlink'><a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>
077 * </ul>
078 *
079 * <h5 class='topic'>Arguments and argument-types of client-side @RemoteResource-annotated interfaces</h5>
080 * <p>
081 * <h5 class='section'>See Also:</h5><ul>
082 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Query">@Query</a>
083 * </ul>
084 *
085 * <h5 class='topic'>Methods and return types of server-side and client-side @Request-annotated interfaces</h5>
086 * <p>
087 * <h5 class='section'>See Also:</h5><ul>
088 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Request">@Request</a>
089 * </ul>
090 * <p>
091 */
092@Documented
093@Target({PARAMETER,METHOD,TYPE,FIELD})
094@Retention(RUNTIME)
095@Inherited
096@Repeatable(QueryAnnotation.Array.class)
097@ContextApply(QueryAnnotation.Applier.class)
098public @interface Query {
099
100   /**
101    * Default value for this parameter.
102    *
103    * @return The annotation value.
104    */
105   String def() default "";
106
107    /**
108     * Optional description for the exposed API.
109     *
110     * @return The annotation value.
111     * @since 9.2.0
112     */
113    String[] description() default {};
114
115    /**
116    * URL query parameter name.
117    *
118    * Required. The name of the parameter. Parameter names are case sensitive.
119    *
120    * <p>
121    * The value should be either a valid query parameter name, or <js>"*"</js> to represent multiple name/value pairs
122    *
123    * <p>
124    * A blank value (the default) has the following behavior:
125    * <ul class='spaced-list'>
126    *    <li>
127    *       If the data type is <c>NameValuePairs</c>, <c>Map</c>, or a bean,
128    *       then it's the equivalent to <js>"*"</js> which will cause the value to be serialized as name/value pairs.
129    *
130    *       <h5 class='figure'>Examples:</h5>
131    *       <p class='bjava'>
132    *    <jc>// When used on a REST method</jc>
133    *    <ja>@RestPost</ja>
134    *    <jk>public void</jk> addPet(<ja>@Query</ja> JsonMap <jv>allQueryParameters</jv>) {...}
135    *       </p>
136    *       <p class='bjava'>
137    *    <jc>// When used on a remote method parameter</jc>
138    *    <ja>@RemoteResource</ja>(path=<js>"/myproxy"</js>)
139    *    <jk>public interface</jk> MyProxy {
140    *
141    *       <jc>// Equivalent to @Query("*")</jc>
142    *       <ja>@RemoteGet</ja>(<js>"/mymethod"</js>)
143    *       String myProxyMethod1(<ja>@Query</ja> Map&lt;String,Object&gt; <jv>allQueryParameters</jv>);
144    *    }
145    *       </p>
146    *       <p class='bjava'>
147    *    <jc>// When used on a request bean method</jc>
148    *    <jk>public interface</jk> MyRequest {
149    *
150    *       <jc>// Equivalent to @Query("*")</jc>
151    *       <ja>@Query</ja>
152    *       Map&lt;String,Object&gt; getFoo();
153    *    }
154    *       </p>
155    *    </li>
156    *    <li>
157    *       If used on a request bean method, uses the bean property name.
158    *
159    *       <h5 class='figure'>Example:</h5>
160    *       <p class='bjava'>
161    *    <jk>public interface</jk> MyRequest {
162    *
163    *       <jc>// Equivalent to @Query("foo")</jc>
164    *       <ja>@Query</ja>
165    *       String getFoo();
166    *    }
167    *       </p>
168    *    </li>
169    * </ul>
170    * <h5 class='section'>Notes:</h5><ul>
171    *    <li class='note'>
172    *       The format is plain-text.
173    * </ul>
174    *
175    * @return The annotation value.
176    */
177   String name() default "";
178
179   /**
180    * Dynamically apply this annotation to the specified classes.
181    *
182    * <h5 class='section'>See Also:</h5><ul>
183    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
184    * </ul>
185    *
186    * @return The annotation value.
187    */
188   String[] on() default {};
189
190   /**
191    * Dynamically apply this annotation to the specified classes.
192    *
193    * <p>
194    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
195    *
196    * <h5 class='section'>See Also:</h5><ul>
197    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
198    * </ul>
199    *
200    * @return The annotation value.
201    */
202   Class<?>[] onClass() default {};
203
204   /**
205    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
206    *
207    * <p>
208    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
209    *
210    * @return The annotation value.
211    */
212   Class<? extends HttpPartParser> parser() default HttpPartParser.Void.class;
213
214   /**
215    * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>.
216    *
217    * <p>
218    * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type.
219    * Values specified on this field override values specified on the type, and values specified on child types override values
220    * specified on parent types.
221    *
222    * <p>
223    * The schema defining the type used for parameter.
224    *
225    * <h5 class='section'>Used for:</h5>
226    * <ul class='spaced-list'>
227    *    <li>
228    *       Server-side schema-based parsing and parsing validation.
229    *    <li>
230    *       Server-side generated Swagger documentation.
231    *    <li>
232    *       Client-side schema-based serializing and serializing validation.
233    * </ul>
234    *
235    * @return The annotation value.
236    */
237   Schema schema() default @Schema;
238
239   /**
240    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
241    *
242    * <p>
243    * Overrides for this part the part serializer defined on the REST client which by default is {@link OpenApiSerializer}.
244    *
245    * @return The annotation value.
246    */
247   Class<? extends HttpPartSerializer> serializer() default HttpPartSerializer.Void.class;
248
249   /**
250    * A synonym for {@link #name()}.
251    *
252    * <p>
253    * Allows you to use shortened notation if you're only specifying the name.
254    *
255    * <p>
256    * The following are completely equivalent ways of defining the existence of a query entry:
257    * <p class='bjava'>
258    *    <jk>public</jk> Order placeOrder(<ja>@Query</ja>(name=<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) {...}
259    * </p>
260    * <p class='bjava'>
261    *    <jk>public</jk> Order placeOrder(<ja>@Query</ja>(<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) {...}
262    * </p>
263    *
264    * @return The annotation value.
265    */
266   String value() default "";
267}