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.rest.server.openapi;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.bean.openapi3.*;
024import org.apache.juneau.commons.inject.*;
025import org.apache.juneau.commons.svl.*;
026import org.apache.juneau.marshall.cp.*;
027import org.apache.juneau.marshall.jsonschema.*;
028import org.apache.juneau.rest.server.*;
029
030/**
031 * Basic implementation of an {@link OpenApiProvider}.
032 *
033 * <p>
034 * Default provider used by {@link RestContext} when no override is configured. Creates a fresh
035 * {@link BasicOpenApiProviderSession} per call, which walks the {@code @Rest} / {@code @RestOp}
036 * annotation graph and emits an {@link OpenApi} 3.1 document.
037 *
038 * <h5 class='section'>See Also:</h5><ul>
039 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ApiDocsMixins">API-Docs Mixin Pack</a>
040 * </ul>
041 */
042public class BasicOpenApiProvider implements OpenApiProvider {
043
044   private final VarResolver vr;
045   private final JsonSchemaGenerator js;
046   private final Messages messages;
047   private final FileFinder fileFinder;
048   private final BeanStore beanStore;
049
050   /**
051    * Constructor.
052    *
053    * @param beanStore The bean store containing injectable beans for this provider.
054    *    <br>Must not be <jk>null</jk>.
055    */
056   public BasicOpenApiProvider(BeanStore beanStore) {
057      // @formatter:off
058      this(
059         OpenApiProvider
060            .create(beanStore)
061            .varResolver(()->beanStore.getBean(VarResolver.class).get())
062            .fileFinder(()->beanStore.getBean(FileFinder.class).get())
063            .messages(()->beanStore.getBean(Messages.class).get())
064            .jsonSchemaGenerator(()->beanStore.getBean(JsonSchemaGenerator.class).get())
065      );
066      // @formatter:on
067   }
068
069   /**
070    * Constructor.
071    *
072    * @param builder The builder containing the settings for this OpenAPI provider.
073    *    <br>Must not be <jk>null</jk>.
074    */
075   public BasicOpenApiProvider(OpenApiProvider.Builder builder) {
076      this.beanStore = builder.beanStore;
077      this.vr = builder.varResolver().orElse(beanStore.getBean(VarResolver.class).orElse(VarResolver.DEFAULT));
078      this.js = builder.jsonSchemaGenerator().orElse(beanStore.getBean(JsonSchemaGenerator.class).orElse(JsonSchemaGenerator.DEFAULT));
079      this.messages = builder.messages().orElse(null);
080      this.fileFinder = builder.fileFinder().orElse(null);
081   }
082
083   /**
084    * Returns the OpenAPI 3.1 document associated with the specified context of a {@link Rest}-annotated class.
085    *
086    * <p>
087    * Subclasses can override this to provide their own method for generating the document.
088    *
089    * @param context The context of the {@link Rest}-annotated class.
090    * @param locale The request locale.
091    * @return A new {@link OpenApi} object.
092    * @throws Exception If an error occurred producing the document.
093    */
094   @Override /* Overridden from OpenApiProvider */
095   public OpenApi getOpenApi(RestContext context, Locale locale) throws Exception {
096
097      var c = context.getResourceClass();
098      var ff = nn(fileFinder) ? fileFinder : FileFinder.create(beanStore).cp(c, null, false).build();
099      var mb = nn(messages) ? messages.forLocale(locale) : Messages.create(c).build().forLocale(locale);
100      var vrs = vr.createSession().bean(Messages.class, mb);
101      var session = new BasicOpenApiProviderSession(context, locale, ff, mb, vrs, js.getSession());
102
103      return session.getOpenApi();
104   }
105}