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.*;
021import static org.apache.juneau.Constants.*;
022
023import java.lang.annotation.*;
024
025import org.apache.juneau.annotation.*;
026import org.apache.juneau.httppart.*;
027import org.apache.juneau.oapi.*;
028
029/**
030 * REST request path annotation.
031 *
032 * <p>
033 * Identifies a POJO to be used as a path entry on an HTTP request.
034 *
035 * <p>
036 * Can be used in the following locations:
037 * <ul>
038 *    <li>Arguments and argument-types of server-side <ja>@RestOp</ja>-annotated methods.
039 *    <li>Arguments and argument-types of client-side <ja>@RemoteResource</ja>-annotated interfaces.
040 *    <li>Methods and return types of server-side and client-side <ja>@Request</ja>-annotated interfaces.
041 * </ul>
042 *
043 * <h5 class='topic'>Arguments and argument-types of server-side @RestOp-annotated methods</h5>
044 * <p>
045 * Annotation that can be applied to a parameter of a <ja>@RestOp</ja>-annotated method to identify it as a variable
046 * in a URL path pattern.
047 *
048 * <h5 class='section'>Example:</h5>
049 * <p class='bjava'>
050 *    <ja>@RestGet</ja>(<js>"/myurl/{foo}/{bar}/{baz}/*"</js>)
051 *    <jk>public void</jk> doGet(
052 *          <ja>@Path</ja>(<js>"foo"</js>) String <jv>foo</jv>,
053 *          <ja>@Path</ja>(<js>"bar"</js>) <jk>int</jk> <jv>bar</jv>,
054 *          <ja>@Path</ja>(<js>"baz"</js>) UUID <jv>baz</jv>,
055 *          <ja>@Path</ja>(<js>"/*"</js>) String <jv>remainder</jv>,
056 *       ) {...}
057 * </p>
058 *
059 * <p>
060 * The special name <js>"/*"</js> is used to retrieve the path remainder after the path match (i.e. the part that matches <js>"/*"</js>).
061 *
062 * <h5 class='section'>See Also:</h5><ul>
063 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
064 *    <li class='extlink'><a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>
065 * </ul>
066 *
067 * <h5 class='topic'>Arguments and argument-types of client-side @RemoteResource-annotated interfaces</h5>
068 * <p>
069 * Annotation applied to Java method arguments of interface proxies to denote that they are path variables on the request.
070 *
071 * <h5 class='section'>See Also:</h5><ul>
072 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Path">@Path</a>
073 * </ul>
074 *
075 * <h5 class='topic'>Methods and return types of server-side and client-side @Request-annotated interfaces</h5>
076 * <p>
077 * <h5 class='section'>See Also:</h5><ul>
078 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Request">@Request</a>
079 * </ul>
080 *
081 */
082@Documented
083@Target({ PARAMETER, METHOD, TYPE, FIELD })
084@Retention(RUNTIME)
085@Inherited
086@Repeatable(PathAnnotation.Array.class)
087@ContextApply(PathAnnotation.Applier.class)
088public @interface Path {
089
090   /**
091    * Default value for this parameter.
092    *
093    * @return The annotation value.
094    */
095   String def() default NONE;
096
097   /**
098    * Optional description for the exposed API.
099    *
100    * @return The annotation value.
101    * @since 9.2.0
102    */
103   String[] description() default {};
104
105   /**
106    * URL path variable name.
107    *
108    * <p>
109    * The path remainder after the path match can be referenced using the name <js>"/*"</js>.
110    * <br>The non-URL-decoded path remainder after the path match can be referenced using the name <js>"/**"</js>.
111    *
112    * <p>
113    * The value should be either a valid path parameter 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 treated 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>
126    *    <jk>public void</jk> addPet(<ja>@Path</ja> JsonMap <jv>allPathParameters</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 @Path("*")</jc>
134    *       <ja>@RemoteGet</ja>(<js>"/mymethod/{foo}/{bar}"</js>)
135    *       String myProxyMethod1(<ja>@Path</ja> Map&lt;String,Object&gt; <jv>allPathParameters</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 @Path("*")</jc>
143    *       <ja>@Path</ja>
144    *       Map&lt;String,Object&gt; getPathVars();
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 @Path("foo")</jc>
156    *       <ja>@Path</ja>
157    *       String getFoo();
158    *    }
159    * </ul>
160    *
161    * <p>
162    * 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>.
163    * See <a class="doclink" href="https://swagger.io/specification/v2#pathTemplating">Path Templating</a> for further information.
164    *
165    * <h5 class='section'>Notes:</h5><ul>
166    *    <li class='note'>
167    *       The format is plain-text.
168    * </ul>
169    *
170    * @return The annotation value.
171    */
172   String name() default "";
173
174   /**
175    * Dynamically apply this annotation to the specified classes.
176    *
177    * <h5 class='section'>See Also:</h5><ul>
178    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
179    * </ul>
180    *
181    * @return The annotation value.
182    */
183   String[] on() default {};
184
185   /**
186    * Dynamically apply this annotation to the specified classes.
187    *
188    * <p>
189    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
190    *
191    * <h5 class='section'>See Also:</h5><ul>
192    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
193    * </ul>
194    *
195    * @return The annotation value.
196    */
197   Class<?>[] onClass() default {};
198
199   /**
200    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
201    *
202    * <p>
203    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
204    *
205    * @return The annotation value.
206    */
207   Class<? extends HttpPartParser> parser() default HttpPartParser.Void.class;
208
209   /**
210    * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>.
211    *
212    * <p>
213    * The schema defining the type used for parameter.
214    *
215    * <p>
216    * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type.
217    * Values specified on this field override values specified on the type, and values specified on child types override values
218    * specified on parent types.
219    *
220    * <h5 class='section'>Used for:</h5>
221    * <ul class='spaced-list'>
222    *    <li>
223    *       Server-side schema-based parsing and parsing validation.
224    *    <li>
225    *       Server-side generated Swagger documentation.
226    *    <li>
227    *       Client-side schema-based serializing and serializing validation.
228    * </ul>
229    *
230    * @return The annotation value.
231    */
232   Schema schema() default @Schema;
233
234   /**
235    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
236    *
237    * <p>
238    * Overrides for this part the part serializer defined on the REST client which by default is {@link OpenApiSerializer}.
239    *
240    * @return The annotation value.
241    */
242   Class<? extends HttpPartSerializer> serializer() default HttpPartSerializer.Void.class;
243
244   /**
245    * A synonym for {@link #name()}.
246    *
247    * <p>
248    * Allows you to use shortened notation if you're only specifying the name.
249    *
250    * <p>
251    * The following are completely equivalent ways of defining a path entry:
252    * <p class='bjava'>
253    *    <ja>@RestGet</ja>(<js>"/pet/{petId}"</js>)
254    *    <jk>public</jk> Pet getPet(<ja>@Path</ja>(name=<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) { ... }
255    * </p>
256    * <p class='bjava'>
257    *    <ja>@RestGet</ja>(<js>"/pet/{petId}"</js>)
258    *    <jk>public</jk> Pet getPet(<ja>@Path</ja>(<js>"petId"</js>) <jk>long</jk> <jv>petId</jv>) { ... }
259    * </p>
260    *
261    * @return The annotation value.
262    */
263   String value() default "";
264}