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 RestResponseAnnotation}
047 *    <li class='link'>{@doc RestcResponse}
048 *    <li class='link'>{@doc RestSwagger}
049 *    <li class='extlink'>{@doc ExtSwaggerResponseObject}
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> parser() 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> serializer() 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 ExtSwaggerResponseObject}.
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 RestSvlVariables}
115    *       (e.g. <js>"$L{my.localized.variable}"</js>).
116    * </ul>
117    */
118   String[] description() default {};
119
120   /**
121    * Synonym for {@link #description()}.
122    */
123   String[] d() default {};
124
125   /**
126    * <mk>schema</mk> field of the {@doc ExtSwaggerResponseObject}.
127    *
128    * <h5 class='section'>Used for:</h5>
129    * <ul class='spaced-list'>
130    *    <li>
131    *       Server-side schema-based serializing and serializing validation.
132    *    <li>
133    *       Server-side generated Swagger documentation.
134    * </ul>
135    */
136   Schema schema() default @Schema;
137
138   /**
139    * <mk>headers</mk> field of the {@doc ExtSwaggerResponseObject}.
140    *
141    * <h5 class='section'>Used for:</h5>
142    * <ul class='spaced-list'>
143    *    <li>
144    *       Server-side generated Swagger documentation.
145    * </ul>
146    */
147   ResponseHeader[] headers() default {};
148
149   /**
150    * A serialized example of the body of a response.
151    *
152    * <p>
153    * This is the {@doc SimplifiedJson} of an example of the body.
154    *
155    * <p>
156    * This value is converted to a POJO and then serialized to all the registered serializers on the REST method to produce examples for all
157    * supported language types.
158    * <br>These values are then used to automatically populate the {@link #examples} field.
159    *
160    * <h5 class='section'>Example:</h5>
161    * <p class='bcode w800'>
162    *    <jc>// A JSON representation of a PetCreate object.</jc>
163    *    <ja>@Response</ja>(
164    *       example=<js>"{name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}"</js>
165    *    )
166    * </p>
167    *
168    * <p>
169    * There are several other options for defining this example:
170    * <ul class='spaced-list'>
171    *    <li>
172    *       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>).
173    *    <li>
174    *       Defining an <js>"x-example"</js> field in the Swagger Schema Object for the response object (including referenced <js>"$ref"</js> schemas).
175    *    <li>
176    *       Allowing Juneau to auto-generate a code example.
177    * </ul>
178    *
179    * <p>
180    * 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
181    * options for auto-detecting and calculation POJO examples.
182    *
183    * <p>
184    * In particular, examples can be defined via static methods, fields, and annotations on the classes themselves.
185    *
186    * <p class='bcode w800'>
187    *    <jc>// Annotation on class.</jc>
188    *    <ja>@Example</ja>(<js>"{name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}"</js>)
189    *    <jk>public class</jk> PetCreate {
190    *       ...
191    *    }
192    * </p>
193    * <p class='bcode w800'>
194    *    <jc>// Annotation on static method.</jc>
195    *    <jk>public class</jk> PetCreate {
196    *
197    *       <ja>@Example</ja>
198    *       <jk>public static</jk> PetCreate <jsm>sample</jsm>() {
199    *          <jk>return new</jk> PetCreate(<js>"Doggie"</js>, 9.99f, <js>"Dog"</js>, <jk>new</jk> String[] {<js>"friendly"</js>,<js>"cute"</js>});
200    *       }
201    *    }
202    * </p>
203    * <p class='bcode w800'>
204    *    <jc>// Static method with specific name 'example'.</jc>
205    *    <jk>public class</jk> PetCreate {
206    *
207    *       <jk>public static</jk> PetCreate <jsm>example</jsm>() {
208    *          <jk>return new</jk> PetCreate(<js>"Doggie"</js>, 9.99f, <js>"Dog"</js>, <jk>new</jk> String[] {<js>"friendly"</js>,<js>"cute"</js>});
209    *       }
210    *    }
211    * </p>
212    * <p class='bcode w800'>
213    *    <jc>// Static field.</jc>
214    *    <jk>public class</jk> PetCreate {
215    *
216    *       <ja>@Example</ja>
217    *       <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>});
218    *    }
219    * </p>
220    *
221    * <p>
222    * Examples can also be specified via generic properties as well using the {@link BeanContext#BEAN_examples} property at either the class or method level.
223    * <p class='bcode w800'>
224    *    <jc>// Examples defined at class level.</jc>
225    *    <ja>@Rest</ja>(
226    *       properties={
227    *          <ja>@Property</ja>(
228    *             name=<jsf>BEAN_examples</jsf>,
229    *             value=<js>"{'org.apache.juneau.examples.rest.petstore.PetCreate': {name:'Doggie',price:9.99,species:'Dog',tags:['friendly','cute']}}"</js>
230    *          )
231    *       }
232    *    )
233    * </p>
234    *
235    * <h5 class='section'>Used for:</h5>
236    * <ul class='spaced-list'>
237    *    <li>
238    *       Server-side generated Swagger documentation.
239    * </ul>
240    *
241    * <ul class='seealso'>
242    *    <li class='ja'>{@link Example}
243    *    <li class='jc'>{@link BeanContext}
244    *    <ul>
245    *       <li class='jf'>{@link BeanContext#BEAN_examples BEAN_examples}
246    *    </ul>
247    *    <li class='jc'>{@link JsonSchemaSerializer}
248    *    <ul>
249    *       <li class='jf'>{@link JsonSchemaGenerator#JSONSCHEMA_addExamplesTo JSONSCHEMA_addExamplesTo}
250    *       <li class='jf'>{@link JsonSchemaGenerator#JSONSCHEMA_allowNestedExamples JSONSCHEMA_allowNestedExamples}
251    *    </ul>
252    * </ul>
253    *
254    * <ul class='notes'>
255    *    <li>
256    *       The format is any {@doc SimplifiedJson} if the object can be converted to a POJO using {@link JsonParser#DEFAULT} or a simple String if the object
257    *       has a schema associated with it meancan be converted from a String.
258    *       <br>Multiple lines are concatenated with newlines.
259    *    <li>
260    *       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.
261    *    <li>
262    *       Supports {@doc RestSvlVariables}
263    *       (e.g. <js>"$L{my.localized.variable}"</js>).
264    * </ul>
265    */
266   String[] example() default {};
267
268   /**
269    * Synonym for {@link #example()}.
270    */
271   String[] ex() default {};
272
273   /**
274    * Serialized examples of the body of a response.
275    *
276    * <p>
277    * This is a {@doc SimplifiedJson} object whose keys are media types and values are string representations of that value.
278    *
279    * <p>
280    * 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.
281    * <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.
282    *
283    * <p class='bcode w800'>
284    *    <jc>// A JSON representation of a PetCreate object.</jc>
285    *    <ja>@Response</ja>(
286    *       examples={
287    *          <js>"'application/json':'{name:\\'Doggie\\',species:\\'Dog\\'}',"</js>,
288    *          <js>"'text/uon':'(name:Doggie,species=Dog)'"</js>
289    *       }
290    *    )
291    * </p>
292    *
293    * <h5 class='section'>Used for:</h5>
294    * <ul class='spaced-list'>
295    *    <li>
296    *       Server-side generated Swagger documentation.
297    * </ul>
298    *
299    * <ul class='notes'>
300    *    <li>
301    *       The format is a {@doc SimplifiedJson} object with string keys (media type) and string values (example for that media type) .
302    *    <li>
303    *       The leading/trailing <c>{ }</c> characters are optional.
304    *    <li>
305    *       Multiple lines are concatenated with newlines so that you can format the value to be readable:
306    *    <li>
307    *       Supports {@doc RestSvlVariables}
308    *       (e.g. <js>"$L{my.localized.variable}"</js>).
309    *    <li>
310    *       Resolution of variables is delayed until request time and occurs before parsing.
311    *       <br>This allows you to, for example, pull in a JSON construct from a properties file based on the locale of the HTTP request.
312    * </ul>
313    */
314   String[] examples() default {};
315
316   /**
317    * Synonym for {@link #examples()}.
318    */
319   String[] exs() default {};
320
321   /**
322    * Free-form value for the {@doc ExtSwaggerResponseObject}.
323    *
324    * <p>
325    * This is a {@doc SimplifiedJson} object that makes up the swagger information for this field.
326    *
327    * <p>
328    * The following are completely equivalent ways of defining the swagger description of the Response object:
329    * <p class='bcode w800'>
330    *    <jc>// Normal</jc>
331    *    <ja>@Response</ja>(
332    *       code=302,
333    *       description=<js>"Redirect"</js>,
334    *       headers={
335    *          <ja>@ResponseHeader</ja>(
336    *             name=<js>"Location"</js>,
337    *             type=<js>"string"</js>,
338    *             format=<js>"uri"</js>
339    *          )
340    *       }
341    *    )
342    * </p>
343    * <p class='bcode w800'>
344    *    <jc>// Free-form</jc>
345    *    <ja>@Response</ja>(
346    *       code=302,
347    *       api={
348    *          <js>"description: 'Redirect',"</js>,
349    *          <js>"headers: {"</js>,
350    *             <js>"Location: {"</js>,
351    *                <js>"type: 'string',"</js>,
352    *                <js>"format: 'uri'"</js>,
353    *             <js>"}"</js>,
354    *          <js>"}"</js>
355    *       }
356    *    )
357    * </p>
358    * <p class='bcode w800'>
359    *    <jc>// Free-form using variables</jc>
360    *    <ja>@Response</ja>(
361    *       code=302,
362    *       api=<js>"$L{redirectSwagger}"</js>
363    *    )
364    * </p>
365    * <p class='bcode w800'>
366    *    <mc>// Contents of MyResource.properties</mc>
367    *    <mk>redirectSwagger</mk> = <mv>{ description: "Redirect", headers: { Location: { type: "string", format: "uri" } } }</mv>
368    * </p>
369    *
370    * <p>
371    *    The reasons why you may want to use this field include:
372    * <ul>
373    *    <li>You want to pull in the entire Swagger JSON definition for this field from an external source such as a properties file.
374    *    <li>You want to add extra fields to the Swagger documentation that are not officially part of the Swagger specification.
375    * </ul>
376    *
377    * <h5 class='section'>Used for:</h5>
378    * <ul class='spaced-list'>
379    *    <li>
380    *       Server-side generated Swagger documentation.
381    * </ul>
382    *
383    * <ul class='notes'>
384    *    <li>
385    *       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.
386    *    <li>
387    *       The format is a {@doc SimplifiedJson} object.
388    *    <li>
389    *       The leading/trailing <c>{ }</c> characters are optional.
390    *       <br>The following two example are considered equivalent:
391    *       <p class='bcode w800'>
392    *    <ja>@Response</ja>(api=<js>"{description: 'Redirect'}"</js>)
393    *       </p>
394    *       <p class='bcode w800'>
395    *    <ja>@Response</ja>(api=<js>"description: 'Redirect''"</js>)
396    *       </p>
397    *    <li>
398    *       Multiple lines are concatenated with newlines so that you can format the value to be readable.
399    *    <li>
400    *       Supports {@doc RestSvlVariables}
401    *       (e.g. <js>"$L{my.localized.variable}"</js>).
402    *    <li>
403    *       Values defined in this field supersede values pulled from the Swagger JSON file and are superseded by individual values defined on this annotation.
404    * </ul>
405    */
406   String[] api() default {};
407}