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