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.jsonschema.annotation.Schema;
021import org.apache.juneau.*;
022import org.apache.juneau.annotation.*;
023import org.apache.juneau.httppart.*;
024import org.apache.juneau.json.*;
025import org.apache.juneau.jsonschema.*;
026import org.apache.juneau.oapi.*;
027
028/**
029 * REST response annotation.
030 *
031 * <p>
032 * Identifies an interface to use to interact with HTTP parts of an HTTP response through a bean.
033 *
034 * <p>
035 * Can be used in the following locations:
036 *  <ul>
037 *    <li>Exception classes thrown from server-side <ja>@RestMethod</ja>-annotated methods.
038 *    <li>Return type classes of server-side <ja>@RestMethod</ja>-annotated methods.
039 *    <li>Arguments and argument-types of server-side <ja>@RestMethod</ja>-annotated methods.
040 *    <li>Return type classes of server-side <ja>@RemoteMethod</ja>-annotated methods.
041 *    <li>Client-side <ja>@RemoteMethod</ja>-annotated methods.
042 *    <li>Return type interfaces of client-side <ja>@RemoteMethod</ja>-annotated methods.
043 * </ul>
044 *
045 * <ul class='seealso'>
046 *    <li class='link'>{@doc juneau-rest-server.HttpPartAnnotations.Response}
047 *    <li class='link'>{@doc juneau-rest-client.RestProxies.Response}
048 *    <li class='link'>{@doc juneau-rest-server.Swagger}
049 *    <li class='extlink'>{@doc SwaggerResponseObject}
050 * </ul>
051 */
052@Documented
053@Target({PARAMETER,TYPE,METHOD})
054@Retention(RUNTIME)
055@Inherited
056public @interface Response {
057
058   /**
059    * Specifies the {@link HttpPartParser} class used for parsing strings to values.
060    *
061    * <p>
062    * Overrides for this part the part parser defined on the REST resource which by default is {@link OpenApiParser}.
063    */
064   Class<? extends HttpPartParser> partParser() default HttpPartParser.Null.class;
065
066   /**
067    * Specifies the {@link HttpPartSerializer} class used for serializing values to strings.
068    *
069    * <p>
070    * Overrides for this part the part serializer defined on the REST resource which by default is {@link OpenApiSerializer}.
071    */
072   Class<? extends HttpPartSerializer> partSerializer() default HttpPartSerializer.Null.class;
073
074   /**
075    * The HTTP response code.
076    *
077    * The default value is <c>500</c> for exceptions and <c>200</c> for return types.
078    */
079   int[] code() default {};
080
081   /**
082    * A synonym for {@link #code()}.
083    *
084    * <p>
085    * Allows you to use shortened notation if you're only specifying the code.
086    *
087    * <p>
088    * The following are completely equivalent ways of defining the response code:
089    * <p class='bcode w800'>
090    *    <ja>@Response</ja>(code=404)
091    *    <jk>public class</jk> NotFound <jk>extends</jk> RestException {...}
092    * </p>
093    * <p class='bcode w800'>
094    *    <ja>@Response</ja>(404)
095    *    <jk>public class</jk> NotFound <jk>extends</jk> RestException {...}
096    * </p>
097    */
098   int[] value() default {};
099
100   /**
101    * <mk>description</mk> field of the {@doc SwaggerResponseObject}.
102    *
103    * <h5 class='section'>Used for:</h5>
104    * <ul class='spaced-list'>
105    *    <li>
106    *       Server-side generated Swagger documentation.
107    * </ul>
108    *
109    * <ul class='notes'>
110    *    <li>
111    *       The format is plain text.
112    *       <br>Multiple lines are concatenated with newlines.
113    *    <li>
114    *       Supports {@doc DefaultRestSvlVariables}
115    *       (e.g. <js>"$L{my.localized.variable}"</js>).
116    * </ul>
117    */
118   String[] description() default {};
119
120   /**
121    * <mk>schema</mk> field of the {@doc SwaggerResponseObject}.
122    *
123    * <h5 class='section'>Used for:</h5>
124    * <ul class='spaced-list'>
125    *    <li>
126    *       Server-side schema-based serializing and serializing validation.
127    *    <li>
128    *       Server-side generated Swagger documentation.
129    * </ul>
130    */
131   Schema schema() default @Schema;
132
133   /**
134    * <mk>headers</mk> field of the {@doc SwaggerResponseObject}.
135    *
136    * <h5 class='section'>Used for:</h5>
137    * <ul class='spaced-list'>
138    *    <li>
139    *       Server-side generated Swagger documentation.
140    * </ul>
141    */
142   ResponseHeader[] headers() default {};
143
144   /**
145    * A serialized example of the body of a response.
146    *
147    * <p>
148    * This is the {@doc SimpleJson} of an example of the body.
149    *
150    * <p>
151    * This value is converted to a POJO and then serialized to all the registered serializers on the REST method to produce examples for all
152    * supported language types.
153    * <br>These values are then used to automatically populate the {@link #examples} field.
154    *
155    * <h5 class='section'>Example:</h5>
156    * <p class='bcode w800'>
157    *    <jc>// A JSON representation of a PetCreate object.</jc>
158    *    <ja>@Response</ja>(
159    *       example=<js>"{name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}"</js>
160    *    )
161    * </p>
162    *
163    * <p>
164    * There are several other options for defining this example:
165    * <ul class='spaced-list'>
166    *    <li>
167    *       Defining an <js>"x-example"</js> field in the inherited Swagger JSON response object (classpath file or <code><ja>@ResourceSwagger</ja>(value)</code>/<code><ja>@MethodSwagger</ja>(value)</code>).
168    *    <li>
169    *       Defining an <js>"x-example"</js> field in the Swagger Schema Object for the response object (including referenced <js>"$ref"</js> schemas).
170    *    <li>
171    *       Allowing Juneau to auto-generate a code example.
172    * </ul>
173    *
174    * <p>
175    * The latter is important because Juneau also supports auto-generation of JSON-Schema from POJO classes using {@link JsonSchemaSerializer} which has several of it's own
176    * options for auto-detecting and calculation POJO examples.
177    *
178    * <p>
179    * In particular, examples can be defined via static methods, fields, and annotations on the classes themselves.
180    *
181    * <p class='bcode w800'>
182    *    <jc>// Annotation on class.</jc>
183    *    <ja>@Example</ja>(<js>"{name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}"</js>)
184    *    <jk>public class</jk> PetCreate {
185    *       ...
186    *    }
187    * </p>
188    * <p class='bcode w800'>
189    *    <jc>// Annotation on static method.</jc>
190    *    <jk>public class</jk> PetCreate {
191    *
192    *       <ja>@Example</ja>
193    *       <jk>public static</jk> PetCreate <jsm>sample</jsm>() {
194    *          <jk>return new</jk> PetCreate(<js>"Doggie"</js>, 9.99f, <js>"Dog"</js>, <jk>new</jk> String[] {<js>"friendly"</js>,<js>"cute"</js>});
195    *       }
196    *    }
197    * </p>
198    * <p class='bcode w800'>
199    *    <jc>// Static method with specific name 'example'.</jc>
200    *    <jk>public class</jk> PetCreate {
201    *
202    *       <jk>public static</jk> PetCreate <jsm>example</jsm>() {
203    *          <jk>return new</jk> PetCreate(<js>"Doggie"</js>, 9.99f, <js>"Dog"</js>, <jk>new</jk> String[] {<js>"friendly"</js>,<js>"cute"</js>});
204    *       }
205    *    }
206    * </p>
207    * <p class='bcode w800'>
208    *    <jc>// Static field.</jc>
209    *    <jk>public class</jk> PetCreate {
210    *
211    *       <ja>@Example</ja>
212    *       <jk>public static</jk> PetCreate <jsf>EXAMPLE</jsf> = <jk>new</jk> PetCreate(<js>"Doggie"</js>, 9.99f, <js>"Dog"</js>, <jk>new</jk> String[] {<js>"friendly"</js>,<js>"cute"</js>});
213    *    }
214    * </p>
215    *
216    * <p>
217    * Examples can also be specified via generic properties as well using the {@link BeanContext#BEAN_examples} property at either the class or method level.
218    * <p class='bcode w800'>
219    *    <jc>// Examples defined at class level.</jc>
220    *    <ja>@Rest</ja>(
221    *       properties={
222    *          <ja>@Property</ja>(
223    *             name=<jsf>BEAN_examples</jsf>,
224    *             value=<js>"{'org.apache.juneau.examples.rest.petstore.PetCreate': {name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}}"</js>
225    *          )
226    *       }
227    *    )
228    * </p>
229    *
230    * <h5 class='section'>Used for:</h5>
231    * <ul class='spaced-list'>
232    *    <li>
233    *       Server-side generated Swagger documentation.
234    * </ul>
235    *
236    * <ul class='seealso'>
237    *    <li class='ja'>{@link Example}
238    *    <li class='jc'>{@link BeanContext}
239    *    <ul>
240    *       <li class='jf'>{@link BeanContext#BEAN_examples BEAN_examples}
241    *    </ul>
242    *    <li class='jc'>{@link JsonSchemaSerializer}
243    *    <ul>
244    *       <li class='jf'>{@link JsonSchemaGenerator#JSONSCHEMA_addExamplesTo JSONSCHEMA_addExamplesTo}
245    *       <li class='jf'>{@link JsonSchemaGenerator#JSONSCHEMA_allowNestedExamples JSONSCHEMA_allowNestedExamples}
246    *    </ul>
247    * </ul>
248    *
249    * <ul class='notes'>
250    *    <li>
251    *       The format is any {@doc SimpleJson} if the object can be converted to a POJO using {@link JsonParser#DEFAULT} or a simple String if the object
252    *       has a schema associated with it meancan be converted from a String.
253    *       <br>Multiple lines are concatenated with newlines.
254    *    <li>
255    *       The format of this object can also be a simple String if the body has a schema associated with it, meaning it's meant to be treated as an HTTP part.
256    *    <li>
257    *       Supports {@doc DefaultRestSvlVariables}
258    *       (e.g. <js>"$L{my.localized.variable}"</js>).
259    * </ul>
260    */
261   String[] example() default {};
262
263   /**
264    * Serialized examples of the body of a response.
265    *
266    * <p>
267    * This is a {@doc SimpleJson} object whose keys are media types and values are string representations of that value.
268    *
269    * <p>
270    * In general you won't need to populate this value directly since it will automatically be calculated based on the value provided in the {@link #example()} field.
271    * <br>However, this field allows you to override the behavior and show examples for only specified media types or different examples for different media types.
272    *
273    * <p class='bcode w800'>
274    *    <jc>// A JSON representation of a PetCreate object.</jc>
275    *    <ja>@Response</ja>(
276    *       examples={
277    *          <js>"'application/json':'{name:\\'Doggie\\',species:\\'Dog\\'}',"</js>,
278    *          <js>"'text/uon':'(name:Doggie,species=Dog)'"</js>
279    *       }
280    *    )
281    * </p>
282    *
283    * <h5 class='section'>Used for:</h5>
284    * <ul class='spaced-list'>
285    *    <li>
286    *       Server-side generated Swagger documentation.
287    * </ul>
288    *
289    * <ul class='notes'>
290    *    <li>
291    *       The format is a {@doc SimpleJson} object with string keys (media type) and string values (example for that media type) .
292    *    <li>
293    *       The leading/trailing <c>{ }</c> characters are optional.
294    *    <li>
295    *       Multiple lines are concatenated with newlines so that you can format the value to be readable:
296    *    <li>
297    *       Supports {@doc DefaultRestSvlVariables}
298    *       (e.g. <js>"$L{my.localized.variable}"</js>).
299    *    <li>
300    *       Resolution of variables is delayed until request time and occurs before parsing.
301    *       <br>This allows you to, for example, pull in a JSON construct from a properties file based on the locale of the HTTP request.
302    * </ul>
303    */
304   String[] examples() default {};
305
306   /**
307    * Free-form value for the {@doc SwaggerResponseObject}.
308    *
309    * <p>
310    * This is a {@doc SimpleJson} object that makes up the swagger information for this field.
311    *
312    * <p>
313    * The following are completely equivalent ways of defining the swagger description of the Response object:
314    * <p class='bcode w800'>
315    *    <jc>// Normal</jc>
316    *    <ja>@Response</ja>(
317    *       code=302,
318    *       description=<js>"Redirect"</js>,
319    *       headers={
320    *          <ja>@ResponseHeader</ja>(
321    *             name=<js>"Location"</js>,
322    *             type=<js>"string"</js>,
323    *             format=<js>"uri"</js>
324    *          )
325    *       }
326    *    )
327    * </p>
328    * <p class='bcode w800'>
329    *    <jc>// Free-form</jc>
330    *    <ja>@Response</ja>(
331    *       code=302,
332    *       api={
333    *          <js>"description: 'Redirect',"</js>,
334    *          <js>"headers: {"</js>,
335    *             <js>"Location: {"</js>,
336    *                <js>"type: 'string',"</js>,
337    *                <js>"format: 'uri'"</js>,
338    *             <js>"}"</js>,
339    *          <js>"}"</js>
340    *       }
341    *    )
342    * </p>
343    * <p class='bcode w800'>
344    *    <jc>// Free-form using variables</jc>
345    *    <ja>@Response</ja>(
346    *       code=302,
347    *       api=<js>"$L{redirectSwagger}"</js>
348    *    )
349    * </p>
350    * <p class='bcode w800'>
351    *    <mc>// Contents of MyResource.properties</mc>
352    *    <mk>redirectSwagger</mk> = <mv>{ description: "Redirect", headers: { Location: { type: "string", format: "uri" } } }</mv>
353    * </p>
354    *
355    * <p>
356    *    The reasons why you may want to use this field include:
357    * <ul>
358    *    <li>You want to pull in the entire Swagger JSON definition for this field from an external source such as a properties file.
359    *    <li>You want to add extra fields to the Swagger documentation that are not officially part of the Swagger specification.
360    * </ul>
361    *
362    * <h5 class='section'>Used for:</h5>
363    * <ul class='spaced-list'>
364    *    <li>
365    *       Server-side generated Swagger documentation.
366    * </ul>
367    *
368    * <ul class='notes'>
369    *    <li>
370    *       Note that the only swagger field you can't specify using this value is <js>"code"</js> whose value needs to be known during servlet initialization.
371    *    <li>
372    *       The format is a {@doc SimpleJson} object.
373    *    <li>
374    *       The leading/trailing <c>{ }</c> characters are optional.
375    *       <br>The following two example are considered equivalent:
376    *       <p class='bcode w800'>
377    *    <ja>@Response</ja>(api=<js>"{description: 'Redirect'}"</js>)
378    *       </p>
379    *       <p class='bcode w800'>
380    *    <ja>@Response</ja>(api=<js>"description: 'Redirect''"</js>)
381    *       </p>
382    *    <li>
383    *       Multiple lines are concatenated with newlines so that you can format the value to be readable.
384    *    <li>
385    *       Supports {@doc DefaultRestSvlVariables}
386    *       (e.g. <js>"$L{my.localized.variable}"</js>).
387    *    <li>
388    *       Values defined in this field supersede values pulled from the Swagger JSON file and are superseded by individual values defined on this annotation.
389    * </ul>
390    */
391   String[] api() default {};
392}