Search in sources :

Example 1 with FilteredIndexView

use of io.smallrye.openapi.runtime.scanner.FilteredIndexView in project wildfly by wildfly.

the class OpenAPIModelServiceConfigurator method get.

@Override
public OpenAPI get() {
    OpenApiConfig config = new OpenApiConfigImpl(this.config);
    IndexView indexView = new FilteredIndexView(this.index, config);
    OpenAPIDocumentBuilder builder = new OpenAPIDocumentBuilder();
    builder.config(config);
    Map.Entry<VirtualFile, Format> entry = findStaticFile(this.root);
    if (entry != null) {
        VirtualFile file = entry.getKey();
        Format format = entry.getValue();
        try (OpenApiStaticFile staticFile = new OpenApiStaticFile(file.openStream(), format)) {
            builder.staticFileModel(OpenApiProcessor.modelFromStaticFile(staticFile));
        } catch (IOException e) {
            throw MicroProfileOpenAPILogger.LOGGER.failedToLoadStaticFile(e, file.getPathNameRelativeTo(this.root), this.deploymentName);
        }
    }
    builder.annotationsModel(OpenApiProcessor.modelFromAnnotations(config, AnnotationScanner.class.getClassLoader(), indexView));
    builder.readerModel(OpenApiProcessor.modelFromReader(config, this.module.getClassLoader()));
    builder.filter(OpenApiProcessor.getFilter(config, this.module.getClassLoader()));
    OpenAPI model = builder.build();
    // Generate default title and description based on web metadata
    DescriptionGroupMetaData descriptionMetaData = this.metaData.getDescriptionGroup();
    String displayName = (descriptionMetaData != null) ? descriptionMetaData.getDisplayName() : null;
    String title = (displayName != null) ? displayName : this.deploymentName;
    String description = (descriptionMetaData != null) ? descriptionMetaData.getDescription() : null;
    Info info = model.getInfo();
    // Override SmallRye's default title
    if (info.getTitle().equals(DEFAULT_TITLE)) {
        info.setTitle(title);
    }
    if (info.getDescription() == null) {
        info.setDescription(description);
    }
    Host host = this.host.get();
    List<UndertowListener> listeners = host.getServer().getListeners();
    if (model.getServers() == null) {
        // Generate Server entries if none exist
        String contextPath = this.info.get().getContextPath();
        if (this.config.getOptionalValue(RELATIVE_SERVER_URLS, Boolean.class).orElse(Boolean.TRUE).booleanValue()) {
            model.setServers(Collections.singletonList(OASFactory.createServer().url(contextPath)));
        } else {
            int aliases = host.getAllAliases().size();
            int size = 0;
            for (UndertowListener listener : listeners) {
                size += aliases + listener.getSocketBinding().getClientMappings().size();
            }
            List<Server> servers = new ArrayList<>(size);
            for (UndertowListener listener : listeners) {
                SocketBinding binding = listener.getSocketBinding();
                Set<String> virtualHosts = new TreeSet<>(host.getAllAliases());
                // The name of the host is not a real virtual host (e.g. default-host)
                virtualHosts.remove(host.getName());
                InetAddress address = binding.getAddress();
                // Omit wildcard addresses
                if (!address.isAnyLocalAddress()) {
                    virtualHosts.add(address.getCanonicalHostName());
                }
                for (String virtualHost : virtualHosts) {
                    Server server = createServer(listener.getProtocol(), virtualHost, binding.getPort(), contextPath);
                    if (server != null) {
                        servers.add(server);
                    }
                }
                for (ClientMapping mapping : binding.getClientMappings()) {
                    Server server = createServer(listener.getProtocol(), mapping.getDestinationAddress(), mapping.getDestinationPort(), contextPath);
                    if (server != null) {
                        servers.add(server);
                    }
                }
            }
            model.setServers(servers);
        }
    }
    if (listeners.stream().map(UndertowListener::getProtocol).noneMatch(REQUISITE_LISTENERS::contains)) {
        LOGGER.requiredListenersNotFound(host.getServer().getName(), REQUISITE_LISTENERS);
    }
    return model;
}
Also used : FilteredIndexView(io.smallrye.openapi.runtime.scanner.FilteredIndexView) VirtualFile(org.jboss.vfs.VirtualFile) SocketBinding(org.jboss.as.network.SocketBinding) Server(org.eclipse.microprofile.openapi.models.servers.Server) ArrayList(java.util.ArrayList) Format(io.smallrye.openapi.runtime.io.Format) OpenApiConfig(io.smallrye.openapi.api.OpenApiConfig) TreeSet(java.util.TreeSet) FilteredIndexView(io.smallrye.openapi.runtime.scanner.FilteredIndexView) IndexView(org.jboss.jandex.IndexView) OpenApiStaticFile(io.smallrye.openapi.runtime.OpenApiStaticFile) Host(org.wildfly.extension.undertow.Host) IOException(java.io.IOException) Info(org.eclipse.microprofile.openapi.models.info.Info) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) OpenApiConfigImpl(io.smallrye.openapi.api.OpenApiConfigImpl) ClientMapping(org.jboss.as.network.ClientMapping) DescriptionGroupMetaData(org.jboss.metadata.javaee.spec.DescriptionGroupMetaData) Map(java.util.Map) EnumMap(java.util.EnumMap) AbstractMap(java.util.AbstractMap) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) InetAddress(java.net.InetAddress) UndertowListener(org.wildfly.extension.undertow.UndertowListener)

Aggregations

OpenApiConfig (io.smallrye.openapi.api.OpenApiConfig)1 OpenApiConfigImpl (io.smallrye.openapi.api.OpenApiConfigImpl)1 OpenApiStaticFile (io.smallrye.openapi.runtime.OpenApiStaticFile)1 Format (io.smallrye.openapi.runtime.io.Format)1 FilteredIndexView (io.smallrye.openapi.runtime.scanner.FilteredIndexView)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 OpenAPI (org.eclipse.microprofile.openapi.models.OpenAPI)1 Info (org.eclipse.microprofile.openapi.models.info.Info)1 Server (org.eclipse.microprofile.openapi.models.servers.Server)1 ClientMapping (org.jboss.as.network.ClientMapping)1 SocketBinding (org.jboss.as.network.SocketBinding)1 IndexView (org.jboss.jandex.IndexView)1 DescriptionGroupMetaData (org.jboss.metadata.javaee.spec.DescriptionGroupMetaData)1