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.rest.swagger;
014
015import java.util.*;
016
017import org.apache.juneau.cp.*;
018import org.apache.juneau.dto.swagger.Swagger;
019import org.apache.juneau.jsonschema.*;
020import org.apache.juneau.rest.*;
021import org.apache.juneau.rest.annotation.*;
022import org.apache.juneau.svl.*;
023
024/**
025 * Basic implementation of a {@link SwaggerProvider}.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Swagger</a>
029 * </ul>
030 */
031public class BasicSwaggerProvider implements SwaggerProvider {
032
033   private final VarResolver vr;
034   private final JsonSchemaGenerator js;
035   private final Messages messages;
036   private final FileFinder fileFinder;
037   private final BeanStore beanStore;
038
039   /**
040    * Constructor.
041    *
042    * @param beanStore The bean store containing injectable beans for this logger.
043    */
044   public BasicSwaggerProvider(BeanStore beanStore) {
045      this(
046         SwaggerProvider
047            .create(beanStore)
048            .varResolver(()->beanStore.getBean(VarResolver.class).get())
049            .fileFinder(()->beanStore.getBean(FileFinder.class).get())
050            .messages(()->beanStore.getBean(Messages.class).get())
051            .jsonSchemaGenerator(()->beanStore.getBean(JsonSchemaGenerator.class).get())
052      );
053   }
054
055   /**
056    * Constructor.
057    *
058    * @param builder The builder containing the settings for this Swagger provider.
059    */
060   public BasicSwaggerProvider(SwaggerProvider.Builder builder) {
061      this.beanStore = builder.beanStore;
062      this.vr = builder.varResolver().orElse(beanStore.getBean(VarResolver.class).orElse(VarResolver.DEFAULT));
063      this.js = builder.jsonSchemaGenerator().orElse(beanStore.getBean(JsonSchemaGenerator.class).orElse(JsonSchemaGenerator.DEFAULT));
064      this.messages = builder.messages().orElse(null);
065      this.fileFinder = builder.fileFinder().orElse(null);
066   }
067
068   /**
069    * Returns the Swagger associated with the specified context of a {@link Rest}-annotated class.
070    *
071    * <p>
072    * Subclasses can override this to provide their own method for generating Swagger.
073    *
074    * @param context The context of the {@link Rest}-annotated class.
075    * @param locale The request locale.
076    * @return A new {@link Swagger} object.
077    * @throws Exception If an error occurred producing the Swagger.
078    */
079   @Override /* SwaggerProvider */
080   public Swagger getSwagger(RestContext context, Locale locale) throws Exception {
081
082      Class<?> c = context.getResourceClass();
083      FileFinder ff = fileFinder != null ? fileFinder : FileFinder.create(beanStore).cp(c,null,false).build();
084      Messages mb = messages != null ? messages.forLocale(locale) : Messages.create(c).build().forLocale(locale);
085      VarResolverSession vrs = vr.createSession().bean(Messages.class, mb);
086      BasicSwaggerProviderSession session = new BasicSwaggerProviderSession(context, locale, ff, messages, vrs, js.getSession());
087
088      return session.getSwagger();
089   }
090}