All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.jaxrs.config.ReflectiveJaxrsScanner Maven / Gradle / Ivy

There is a newer version: 1.6.14
Show newest version
package io.swagger.jaxrs.config;

import io.swagger.annotations.Api;
import io.swagger.config.FilterFactory;
import io.swagger.config.Scanner;
import io.swagger.config.SwaggerConfig;
import io.swagger.core.filter.SwaggerSpecFilter;
import io.swagger.models.Swagger;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class ReflectiveJaxrsScanner implements SwaggerConfig, Scanner {
    protected boolean prettyPrint = false;
    protected String resourcePackage = "";
    protected Reflections reflections;
    protected Set acceptablePackages;
    protected String filterClass;
    Logger LOGGER = LoggerFactory.getLogger(ReflectiveJaxrsScanner.class);

    protected Reflections getReflections() {
        if (reflections == null) {
            ConfigurationBuilder config = new ConfigurationBuilder();
            acceptablePackages = new HashSet();

            if (resourcePackage != "") {
                String[] parts = resourcePackage.split(",");
                for (String pkg : parts) {
                    if (!"".equals(pkg)) {
                        acceptablePackages.add(pkg);
                        config.addUrls(ClasspathHelper.forPackage(pkg));
                    }
                }
            }

            config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
            this.reflections = new Reflections(config);
        }
        return this.reflections;
    }

    public void setReflections(Reflections reflections) {
        this.reflections = reflections;
    }

    public String getResourcePackage() {
        return this.resourcePackage;
    }

    public void setResourcePackage(String resourcePackage) {
        this.resourcePackage = resourcePackage;
    }

    public Swagger configure(Swagger swagger) {
        if (filterClass != null) {
            try {
                SwaggerSpecFilter filter = (SwaggerSpecFilter) Class.forName(filterClass).newInstance();
                if (filter != null) {
                    FilterFactory.setFilter(filter);
                }
            } catch (Exception e) {
                LOGGER.error("failed to load filter", e);
            }
        }

        return swagger;
    }

    public Set> classes() {
        Set> classes = getReflections().getTypesAnnotatedWith(Api.class);
        Set> output = new HashSet>();
        for (Class cls : classes) {
            if (acceptablePackages.contains(cls.getPackage().getName())) {
                output.add(cls);
            }
        }
        return output;
    }

    public String getFilterClass() {
        return filterClass;
    }

    public void setFilterClass(String filterClass) {
        this.filterClass = filterClass;
    }

    public boolean getPrettyPrint() {
        return true;
    }

    public void setPrettyPrint(boolean shouldPrettyPrint) {
        prettyPrint = shouldPrettyPrint;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy