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
020/**
021 * REST has-query-parameter annotation.
022 *
023 * <p>
024 * Identifies whether or not an HTTP request has the specified query parameter.
025 *
026 * <p>
027 * Can be used in the following locations:
028 * <ul>
029 *    <li>Arguments and argument-types of server-side <ja>@RestOp</ja>-annotated methods.
030 * </ul>
031 * <p>
032 *    Identical to {@link HasFormData @HasFormData}, but only checks the existing of the parameter in the URL string, not
033 *    URL-encoded form posts.
034 * </p>
035 * <p>
036 *    Unlike {@link HasFormData @HasFormData}, using this annotation does not result in the servlet reading the contents
037 *    of URL-encoded form posts.
038 *    Therefore, this annotation can be used in conjunction with the {@link Content @Cpmtemt} annotation or
039 *    {@code RestRequest.getContent()} method for <c>application/x-www-form-urlencoded POST</c> calls.
040 *  </p>
041 * <h5 class='figure'>Example:</h5>
042 * <p class='bjava'>
043 *    <ja>@RestGet</ja>
044 *    <jk>public void</jk> doGet(<ja>@HasQuery</ja>(<js>"p1"</js>) <jk>boolean</jk> <jv>p1</jv>) {...}
045 * </p>
046 * <p>
047 *    This is functionally equivalent to the following code:
048 * </p>
049 * <p class='bjava'>
050 *    <ja>@RestGet</ja>
051 *    <jk>public void</jk> doGet(RestRequest <jv>req</jv>) {
052 *       <jk>boolean</jk> <jv>p1</jv> = <jv>req</jv>.getQueryParam(<js>"p1"</js>).isPresent();
053 *       ...
054 *    }
055 * </p>
056 * <p>
057 *    The parameter type must be either <jk>boolean</jk> or {@link java.lang.Boolean}.
058 * </p>
059 * <p>
060 *    The following table shows the behavioral differences between <ja>@HasQuery</ja> and <ja>@Query</ja>:
061 * </p>
062 * <table class='styled w400'>
063 *    <tr>
064 *       <th><c>Query content</c></th>
065 *       <th><c><ja>@HasQuery</ja>(<js>"a"</js>)</c></th>
066 *       <th><c><ja>@Query</ja>(<js>"a"</js>)</c></th>
067 *    </tr>
068 *    <tr>
069 *       <td><c>?a=foo</c></td>
070 *       <td><jk>true</jk></td>
071 *       <td><js>"foo"</js></td>
072 *    </tr>
073 *    <tr>
074 *       <td><c>?a=</c></td>
075 *       <td><jk>true</jk></td>
076 *       <td><js>""</js></td>
077 *    </tr>
078 *    <tr>
079 *       <td><c>?a</c></td>
080 *       <td><jk>true</jk></td>
081 *       <td><jk>null</jk></td>
082 *    </tr>
083 *    <tr>
084 *       <td><c>?b=foo</c></td>
085 *       <td><jk>false</jk></td>
086 *       <td><jk>null</jk></td>
087 *    </tr>
088 * </table>
089 * <p>
090 * <h5 class='section'>See Also:</h5><ul>
091
092 * </ul>
093 */
094@Documented
095@Target({PARAMETER})
096@Retention(RUNTIME)
097@Inherited
098public @interface HasQuery {
099
100   /**
101    * URL query parameter name.
102    *
103    * Required. The name of the parameter. Parameter names are case sensitive.
104    *
105    * <h5 class='section'>Notes:</h5><ul>
106    *    <li class='note'>
107    *       The format is plain-text.
108    * </ul>
109    *
110    * @return The annotation value.
111    */
112   String name() default "";
113
114   /**
115    * A synonym for {@link #name()}.
116    *
117    * <p>
118    * Allows you to use shortened notation if you're only specifying the name.
119    *
120    * <p>
121    * The following are completely equivalent ways of defining the existence of a query entry:
122    * <p class='bjava'>
123    *    <jk>public</jk> Order placeOrder(<ja>@HasQuery</ja>(name=<js>"petId"</js>) <jk>boolean</jk> <jv>hasPetId</jv>) {...}
124    * </p>
125    * <p class='bjava'>
126    *    <jk>public</jk> Order placeOrder(<ja>@HasQuery</ja>(<js>"petId"</js>) <jk>boolean</jk> <jv>hasPetId</jv>) {...}
127    * </p>
128    *
129    * @return The annotation value.
130    */
131   String value() default "";
132}