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 path annotation.
026 *
027 * <p>
028 * Identifies a POJO to be used as a path entry on 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 variable
041 * in a URL path pattern.
042 *
043 * <h5 class='section'>Example:</h5>
044 * <p class='bjava'>
045 *    <ja>@RestGet</ja>(<js>"/myurl/{foo}/{bar}/{baz}/*"</js>)
046 *    <jk>public void</jk> doGet(
047 *          <ja>@Path</ja>(<js>"foo"</js>) String <jv>foo</jv>,
048 *          <ja>@Path</ja>(<js>"bar"</js>) <jk>int</jk> <jv>bar</jv>,
049 *          <ja>@Path</ja>(<js>"baz"</js>) UUID <jv>baz</jv>,
050 *          <ja>@Path</ja>(<js>"/*"</js>) String <jv>remainder</jv>,
051 *       ) {...}
052 * </p>
053 *
054 * <p>
055 * The special name <js>"/*"</js> is used to retrieve the path remainder after the path match (i.e. the part that matches <js>"/*"</js>).
056 *
057 * <h5 class='section'>See Also:</h5><ul>
058 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Swagger</a>
059 *    <li class='extlink'><a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>
060 * </ul>
061 *
062 * <h5 class='topic'>Arguments and argument-types of client-side @RemoteResource-annotated interfaces</h5>
063 * <p>
064 * Annotation applied to Java method arguments of interface proxies to denote that they are path variables on the request.
065 *
066 * <h5 class='section'>See Also:</h5><ul>
067 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrc.Path">@Path</a>
068 * </ul>
069 *
070 * <h5 class='topic'>Methods and return types of server-side and client-side @Request-annotated interfaces</h5>
071 * <p>
072 * <h5 class='section'>See Also:</h5><ul>
073 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrc.Request">@Request</a>
074 * </ul>
075 *
076 * <h5 class='section'>See Also:</h5><ul>
077
078 * </ul>
079 */
080@Documented
081@Target({PARAMETER,METHOD,TYPE,FIELD})
082@Retention(RUNTIME)
083@Inherited
084@Repeatable(PathAnnotation.Array.class)
085@ContextApply(PathAnnotation.Applier.class)
086public @interface Path {
087
088   /**
089    * Default value for this parameter.
090    *
091    * @return The annotation value.
092    */
093   String def() default "";
094
095   /**
096    * URL path variable name.
097    *
098    * <p>
099    * The path remainder after the path match can be referenced using the name <js>"/*"</js>.
100    * <br>The non-URL-decoded path remainder after the path match can be referenced using the name <js>"/**"</js>.
101    *
102    * <p>
103    * The value should be either a valid path parameter name, or <js>"*"</js> to represent multiple name/value pairs
104    *
105    * <p>
106    * A blank value (the default) has the following behavior:
107    * <ul class='spaced-list'>
108    *    <li>
109    *       If the data type is <c>NameValuePairs</c>, <c>Map</c>, or a bean,
110    *       then it's the equivalent to <js>"*"</js> which will cause the value to be treated as name/value pairs.
111    *
112    *       <h5 class='figure'>Examples:</h5>
113    *       <p class='bjava'>
114    *    <jc>// When used on a REST method</jc>
115    *    <ja>@RestPost</ja>
116    *    <jk>public void</jk> addPet(<ja>@Path</ja> JsonMap <jv>allPathParameters</jv>) {...}
117    *       </p>
118    *       <p class='bjava'>
119    *    <jc>// When used on a remote method parameter</jc>
120    *    <ja>@RemoteResource</ja>(path=<js>"/myproxy"</js>)
121    *    <jk>public interface</jk> MyProxy {
122    *
123    *       <jc>// Equivalent to @Path("*")</jc>
124    *       <ja>@RemoteGet</ja>(<js>"/mymethod/{foo}/{bar}"</js>)
125    *       String myProxyMethod1(<ja>@Path</ja> Map&lt;String,Object&gt; <jv>allPathParameters</jv>);
126    *    }
127    *       </p>
128    *       <p class='bjava'>
129    *    <jc>// When used on a request bean method</jc>
130    *    <jk>public interface</jk> MyRequest {
131    *
132    *       <jc>// Equivalent to @Path("*")</jc>
133    *       <ja>@Path</ja>
134    *       Map&lt;String,Object&gt; getPathVars();
135    *    }
136    *       </p>
137    *    </li>
138    *    <li>
139    *       If used on a request bean method, uses the bean property name.
140    *
141    *       <h5 class='figure'>Example:</h5>
142    *       <p class='bjava'>
143    *    <jk>public interface</jk> MyRequest {
144    *
145    *       <jc>// Equivalent to @Path("foo")</jc>
146    *       <ja>@Path</ja>
147    *       String getFoo();
148    *    }
149    * </ul>
150    *
151    * <p>
152    * The name field MUST correspond to the associated <a class="doclink" href="https://swagger.io/specification/v2#pathsPath">path</a> segment from the path field in the <a class="doclink" href="https://swagger.io/specification/v2#pathsObject">Paths Object</a>.
153    * See <a class="doclink" href="https://swagger.io/specification/v2#pathTemplating">Path Templating</a> for further information.
154    *
155    * <h5 class='section'>Notes:</h5><ul>
156    *    <li class='note'>
157    *       The format is plain-text.
158    * </ul>
159    *
160    * @return The annotation value.
161    */
162   String name() default "";
163
164   /**
165    * Dynamically apply this annotation to the specified classes.
166    *
167    * <h5 class='section'>See Also:</h5><ul>
168    *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
169    * </ul>
170    *
171    * @return The annotation value.
172    */
173   String[] on() default {};
174
175   /**
176    * Dynamically apply this annotation to the specified classes.
177    *
178    * <p>
179    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
180    *
181    * <h5 class='section'>See Also:</h5><ul>
182    *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
183    * </ul>
184    *
185    * @return The annotation value.
186    */
187   Class<?>[] onClass() default {};
188
189   /**
190    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
191    *
192    * <p>
193    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
194    *
195    * @return The annotation value.
196    */
197   Class<? extends HttpPartParser> parser() default HttpPartParser.Void.class;
198
199   /**
200    * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>.
201    *
202    * <p>
203    * The schema defining the type used for parameter.
204    *
205    * <p>
206    * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type.
207    * Values specified on this field override values specified on the type, and values specified on child types override values
208    * specified on parent types.
209    *
210    * <h5 class='section'>Used for:</h5>
211    * <ul class='spaced-list'>
212    *    <li>
213    *       Server-side schema-based parsing and parsing validation.
214    *    <li>
215    *       Server-side generated Swagger documentation.
216    *    <li>
217    *       Client-side schema-based serializing and serializing validation.
218    * </ul>
219    *
220    * @return The annotation value.
221    */
222   Schema schema() default @Schema;
223
224   /**
225    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
226    *
227    * <p>
228    * Overrides for this part the part serializer defined on the REST client which by default is {@link OpenApiSerializer}.
229    *
230    * @return The annotation value.
231    */
232   Class<? extends HttpPartSerializer> serializer() default HttpPartSerializer.Void.class;
233
234   /**
235    * A synonym for {@link #name()}.
236    *
237    * <p>
238    * Allows you to use shortened notation if you're only specifying the name.
239    *
240    * <p>
241    * The following are completely equivalent ways of defining a path entry:
242    * <p class='bjava'>
243    *    <ja>@RestGet</ja>(<js>"/pet/{petId}"</js>)
244    *    <jk>public</jk> Pet getPet(<ja>@Path</ja>(name=<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) { ... }
245    * </p>
246    * <p class='bjava'>
247    *    <ja>@RestGet</ja>(<js>"/pet/{petId}"</js>)
248    *    <jk>public</jk> Pet getPet(<ja>@Path</ja>(<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) { ... }
249    * </p>
250    *
251    * @return The annotation value.
252    */
253   String value() default "";
254}