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.bean.swagger;
018
019import static org.apache.juneau.commons.utils.StringUtils.*;
020
021import java.net.*;
022
023import org.apache.juneau.*;
024
025/**
026 * Various useful static methods for creating Swagger elements.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
030 * </ul>
031 */
032public class SwaggerBuilder {
033
034   /**
035    * Creates an empty {@link Contact} element.
036    *
037    * @return The new element.
038    */
039   public static final Contact contact() {
040      return new Contact();
041   }
042
043   /**
044    * Creates an {@link Contact} element with the specified {@link Contact#setName(String) name} attribute.
045    *
046    * @param name The {@link Contact#setName(String) name} attribute.
047    * @return The new element.
048    */
049   public static final Contact contact(String name) {
050      return contact().setName(name);
051   }
052
053   /**
054    * Creates an {@link Contact} element with the specified {@link Contact#setName(String) name}, {@link Contact#setUrl(URI) url},
055    * and {@link Contact#setEmail(String) email} attributes.
056    *
057    * @param name The {@link Contact#setName(String) name} attribute.
058    * @param url
059    *    The {@link Contact#setUrl(URI) url} attribute.
060    *    <br>The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
061    *    <br>Strings must be valid URIs.
062    *    <br>URIs defined by {@link UriResolver} can be used for values.
063    * @param email The {@link Contact#setEmail(String) email} attribute.
064    * @return The new element.
065    */
066   public static final Contact contact(String name, Object url, String email) {
067      return contact().setName(name).setUrl(toUri(url)).setEmail(email);
068   }
069
070   /**
071    * Creates an empty {@link ExternalDocumentation} element.
072    *
073    * @return The new element.
074    */
075   public static final ExternalDocumentation externalDocumentation() {
076      return new ExternalDocumentation();
077   }
078
079   /**
080    * Creates an {@link ExternalDocumentation} element with the specified {@link ExternalDocumentation#setUrl(URI) url}
081    * attribute.
082    *
083    * @param url
084    *    The {@link ExternalDocumentation#setUrl(URI) url} attribute.
085    *    <br>The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
086    *    <br>Strings must be valid URIs.
087    *    <br>URIs defined by {@link UriResolver} can be used for values.
088    * @return The new element.
089    */
090   public static final ExternalDocumentation externalDocumentation(Object url) {
091      return externalDocumentation().setUrl(toUri(url));
092   }
093
094   /**
095    * Creates an {@link ExternalDocumentation} element with the specified {@link ExternalDocumentation#setUrl(URI) url}
096    * and {@link ExternalDocumentation#setDescription(String) description} attributes.
097    *
098    * @param url
099    *    The {@link ExternalDocumentation#setUrl(URI) url} attribute.
100    *    <br>The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
101    *    <br>Strings must be valid URIs.
102    *    <br>URIs defined by {@link UriResolver} can be used for values.
103    * @param description The {@link ExternalDocumentation#setDescription(String) description} attribute.
104    * @return The new element.
105    */
106   public static final ExternalDocumentation externalDocumentation(Object url, String description) {
107      return externalDocumentation().setUrl(toUri(url)).setDescription(description);
108   }
109
110   /**
111    * Creates an empty {@link HeaderInfo} element.
112    *
113    * @return The new element.
114    */
115   public static final HeaderInfo headerInfo() {
116      return new HeaderInfo();
117   }
118
119   /**
120    * Creates an {@link HeaderInfo} element with the specified {@link HeaderInfo#setType(String) type} attribute.
121    *
122    * @param type The {@link HeaderInfo#setType(String) type} attribute.
123    * @return The new element.
124    */
125   public static final HeaderInfo headerInfo(String type) {
126      return headerInfo().setType(type);
127   }
128
129   /**
130    * Creates an {@link HeaderInfo} element with the specified {@link HeaderInfo#setType(String) type} attribute.
131    *
132    * <p>
133    * Throws a runtime exception if the type is not valid.
134    *
135    * @param type
136    *    The {@link HeaderInfo#setType(String) type} attribute.
137    *    <br>Valid values:
138    *    <ul>
139    *       <li><js>"string"</js>
140    *       <li><js>"number"</js>
141    *       <li><js>"integer"</js>
142    *       <li><js>"boolean"</js>
143    *       <li><js>"array"</js>
144    *    </ul>
145    * @return The new element.
146    */
147   public static final HeaderInfo headerInfoStrict(String type) {
148      return headerInfo().strict().setType(type);
149   }
150
151   /**
152    * Creates an empty {@link Info} element.
153    *
154    * @return The new element.
155    */
156   public static final Info info() {
157      return new Info();
158   }
159
160   /**
161    * Creates an {@link Info} element with the specified {@link Info#setTitle(String) title} and {@link Info#setVersion(String) version}
162    * attributes.
163    *
164    * @param title The {@link Info#setTitle(String) title} attribute.
165    * @param version The {@link Info#setVersion(String) version} attribute.
166    * @return The new element.
167    */
168   public static final Info info(String title, String version) {
169      return info().setTitle(title).setVersion(version);
170   }
171
172   /**
173    * Creates an empty {@link Items} element.
174    *
175    * @return The new element.
176    */
177   public static final Items items() {
178      return new Items();
179   }
180
181   /**
182    * Creates an {@link Items} element with the specified {@link Items#setType(String) type} attribute.
183    *
184    * @param type The {@link Items#setType(String) type} attribute.
185    * @return The new element.
186    */
187   public static final Items items(String type) {
188      return items().setType(type);
189   }
190
191   /**
192    * Creates an {@link Items} element with the specified {@link Items#setType(String) type} attribute.
193    *
194    * <p>
195    * Throws a runtime exception if the type is not valid.
196    *
197    * @param type
198    *    The {@link Items#setType(String) type} attribute.
199    *    <br>Valid values:
200    *    <ul>
201    *       <li><js>"string"</js>
202    *       <li><js>"number"</js>
203    *       <li><js>"integer"</js>
204    *       <li><js>"boolean"</js>
205    *       <li><js>"array"</js>
206    *    </ul>
207    * @return The new element.
208    */
209   public static final Items itemsStrict(String type) {
210      return items().strict().setType(type);
211   }
212
213   /**
214    * Creates an empty {@link License} element.
215    *
216    * @return The new element.
217    */
218   public static final License license() {
219      return new License();
220   }
221
222   /**
223    * Creates an {@link License} element with the specified {@link License#setName(String) name} attribute.
224    *
225    * @param name The {@link License#setName(String) name} attribute.
226    * @return The new element.
227    */
228   public static final License license(String name) {
229      return license().setName(name);
230   }
231
232   /**
233    * Creates an {@link License} element with the specified {@link License#setName(String) name} and {@link License#setUrl(URI) url} attributes.
234    *
235    * @param name The {@link License#setName(String) name} attribute.
236    * @param url The {@link License#setUrl(URI) url} attribute.
237    * @return The new element.
238    */
239   public static final License license(String name, URI url) {
240      return license().setName(name).setUrl(url);
241   }
242
243   /**
244    * Creates an empty {@link Operation} element.
245    *
246    * @return The new element.
247    */
248   public static final Operation operation() {
249      return new Operation();
250   }
251
252   /**
253    * Creates an empty {@link OperationMap} element.
254    *
255    * @return The new element.
256    */
257   public static final OperationMap operationMap() {
258      return new OperationMap();
259   }
260
261   /**
262    * Creates an empty {@link ParameterInfo} element.
263    *
264    * @return The new element.
265    */
266   public static final ParameterInfo parameterInfo() {
267      return new ParameterInfo();
268   }
269
270   /**
271    * Creates an {@link ParameterInfo} element with the specified {@link ParameterInfo#setIn(String) in} and
272    * {@link ParameterInfo#setName(String) name} attributes.
273    *
274    * @param in The {@link ParameterInfo#setIn(String) in} attribute.
275    * @param name The {@link ParameterInfo#setName(String) name} attribute.
276    * @return The new element.
277    */
278   public static final ParameterInfo parameterInfo(String in, String name) {
279      return parameterInfo().setIn(in).setName(name);
280   }
281
282   /**
283    * Creates an {@link ParameterInfo} element with the specified {@link ParameterInfo#setIn(String) in} and
284    * {@link ParameterInfo#setName(String) name} attributes.
285    *
286    * <p>
287    * Throws a runtime exception if the type is not valid.
288    *
289    * @param in
290    *    The {@link ParameterInfo#setIn(String) in} attribute.
291    *    <br>Valid values:
292    *    <ul>
293    *       <li><js>"query"</js>
294    *       <li><js>"header"</js>
295    *       <li><js>"path"</js>
296    *       <li><js>"formData"</js>
297    *       <li><js>"body"</js>
298    *    </ul>
299    * @param name The {@link ParameterInfo#setName(String) name} attribute.
300    * @return The new element.
301    */
302   public static final ParameterInfo parameterInfoStrict(String in, String name) {
303      return parameterInfo().strict().setIn(in).setName(name);
304   }
305
306   /**
307    * Creates an empty {@link ResponseInfo} element.
308    *
309    * @return The new element.
310    */
311   public static final ResponseInfo responseInfo() {
312      return new ResponseInfo();
313   }
314
315   /**
316    * Creates an {@link ResponseInfo} element with the specified {@link ResponseInfo#setDescription(String) description} attribute.
317    *
318    * @param description The {@link ResponseInfo#setDescription(String) description} attribute.
319    * @return The new element.
320    */
321   public static final ResponseInfo responseInfo(String description) {
322      return responseInfo().setDescription(description);
323   }
324
325   /**
326    * Creates an empty {@link SchemaInfo} element.
327    *
328    * @return The new element.
329    */
330   public static final SchemaInfo schemaInfo() {
331      return new SchemaInfo();
332   }
333
334   /**
335    * Creates an empty {@link SecurityScheme} element.
336    *
337    * @return The new element.
338    */
339   public static final SecurityScheme securityScheme() {
340      return new SecurityScheme();
341   }
342
343   /**
344    * Creates an {@link SecurityScheme} element with the specified {@link SecurityScheme#setType(String) type} attribute.
345    *
346    * @param type The {@link SecurityScheme#setType(String) type} attribute.
347    * @return The new element.
348    */
349   public static final SecurityScheme securityScheme(String type) {
350      return securityScheme().setType(type);
351   }
352
353   /**
354    * Creates an {@link SecurityScheme} element with the specified {@link SecurityScheme#setType(String) type} attribute.
355    *
356    * <p>
357    * Throws a runtime exception if the type is not valid.
358    *
359    * @param type
360    *    The {@link SecurityScheme#setType(String) type} attribute.
361    *    <br>Valid values:
362    *    <ul>
363    *       <li><js>"basic"</js>
364    *       <li><js>"apiKey"</js>
365    *       <li><js>"oauth2"</js>
366    *    </ul>
367    * @return The new element.
368    */
369   public static final SecurityScheme securitySchemeStrict(String type) {
370      return securityScheme().strict().setType(type);
371   }
372
373   /**
374    * Creates an empty {@link Swagger} element.
375    *
376    * @return The new element.
377    */
378   public static final Swagger swagger() {
379      return new Swagger();
380   }
381
382   /**
383    * Creates an {@link Swagger} element with the specified {@link Swagger#setInfo(Info) info} attribute.
384    *
385    * @param info The {@link Swagger#setInfo(Info) info} attribute.
386    * @return The new element.
387    */
388   public static final Swagger swagger(Info info) {
389      return swagger().setInfo(info);
390   }
391
392   /**
393    * Creates an empty {@link Tag} element.
394    *
395    * @return The new element.
396    */
397   public static final Tag tag() {
398      return new Tag();
399   }
400
401   /**
402    * Creates an {@link Tag} element with the specified {@link Tag#setName(String) name} attribute.
403    *
404    * @param name The {@link Tag#setName(String) name} attribute.
405    * @return The new element.
406    */
407   public static final Tag tag(String name) {
408      return tag().setName(name);
409   }
410
411   /**
412    * Creates an empty {@link Xml} element.
413    *
414    * @return The new element.
415    */
416   public static final Xml xml() {
417      return new Xml();
418   }
419
420   private SwaggerBuilder() {}
421}