Search in sources :

Example 6 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerContextService method initScanner.

public SwaggerContextService initScanner() {
    String scannerIdKey;
    if (scannerId != null) {
        scannerIdKey = SCANNER_ID_PREFIX + scannerId;
    } else if (contextId != null) {
        scannerIdKey = SCANNER_ID_PREFIX + contextId;
    } else {
        if (isServletConfigAvailable(sc)) {
            scannerIdKey = (sc.getInitParameter(SCANNER_ID_KEY) != null) ? SCANNER_ID_PREFIX + sc.getInitParameter(SCANNER_ID_KEY) : null;
            if (scannerIdKey == null) {
                boolean usePathBasedConfig = Boolean.valueOf(sc.getInitParameter(USE_PATH_BASED_CONFIG));
                if (usePathBasedConfig && StringUtils.isNotBlank(basePath)) {
                    scannerIdKey = SCANNER_ID_PREFIX + basePath;
                } else {
                    scannerIdKey = (sc.getInitParameter(CONTEXT_ID_KEY) != null) ? SCANNER_ID_PREFIX + sc.getInitParameter(CONTEXT_ID_KEY) : SCANNER_ID_DEFAULT;
                }
            }
        } else {
            if (isUsePathBasedConfig() && StringUtils.isNotBlank(basePath)) {
                scannerIdKey = SCANNER_ID_PREFIX + basePath;
            } else {
                scannerIdKey = SCANNER_ID_DEFAULT;
            }
        }
    }
    Scanner value = (scanner != null) ? scanner : new DefaultJaxrsScanner();
    ScannerFactory.setScanner(value);
    if (isServletConfigAvailable(sc)) {
        sc.getServletContext().setAttribute(scannerIdKey, value);
        String shouldPrettyPrint = sc.getInitParameter("swagger.pretty.print");
        if (shouldPrettyPrint != null) {
            value.setPrettyPrint(Boolean.parseBoolean(shouldPrettyPrint));
        }
    }
    SwaggerScannerLocator.getInstance().putScanner(scannerIdKey, value);
    return this;
}
Also used : Scanner(io.swagger.config.Scanner)

Example 7 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class BaseApiListingResource method scan.

private static synchronized Swagger scan(Application app, ServletContext context, ServletConfig sc, UriInfo uriInfo) {
    Swagger swagger = null;
    SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo));
    Scanner scanner = ctxService.getScanner();
    if (scanner != null) {
        SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
        swagger = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).getSwagger();
        Set<Class<?>> classes;
        if (scanner instanceof JaxrsScanner) {
            JaxrsScanner jaxrsScanner = (JaxrsScanner) scanner;
            classes = jaxrsScanner.classesFromContext(app, sc);
        } else {
            classes = scanner.classes();
        }
        if (classes != null) {
            Reader reader = new Reader(swagger, ReaderConfigUtils.getReaderConfig(context));
            swagger = reader.read(classes);
            if (scanner instanceof SwaggerConfig) {
                swagger = ((SwaggerConfig) scanner).configure(swagger);
            } else {
                SwaggerConfig swaggerConfig = ctxService.getConfig();
                if (swaggerConfig != null) {
                    LOGGER.debug("configuring swagger with " + swaggerConfig);
                    swaggerConfig.configure(swagger);
                } else {
                    LOGGER.debug("no configurator");
                }
            }
            new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).updateSwagger(swagger);
        }
    }
    if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
        initializedScanner.put(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + ctxService.getBasePath(), true);
    } else {
        initialized = true;
    }
    return swagger;
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Scanner(io.swagger.config.Scanner) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) SwaggerConfig(io.swagger.config.SwaggerConfig) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader)

Example 8 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerContextServiceTest method getScannerWithContextInitParam.

@Test(description = "should call servletContext getAttribute with param SCANNER_ID_PREFIX + \"test.1/2\"")
public void getScannerWithContextInitParam() {
    stubWithContextInitParam();
    Scanner scanner1 = new SwaggerContextService().withServletConfig(servletConfig1).getScanner();
    Scanner scanner2 = new SwaggerContextService().withServletConfig(servletConfig2).getScanner();
    assertEquals(beanConfigScanner1, scanner1);
    assertEquals(beanConfigScanner2, scanner2);
    verify(servletConfig1, times(1)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletConfig1, times(2)).getInitParameter(eq(CONTEXT_ID_KEY));
    verify(servletContext1, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletContext2, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletConfig2, times(1)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletConfig2, times(2)).getInitParameter(eq(CONTEXT_ID_KEY));
    verify(servletContext2, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
    verify(servletContext1, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
}
Also used : DefaultJaxrsScanner(io.swagger.jaxrs.config.DefaultJaxrsScanner) Scanner(io.swagger.config.Scanner) SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Test(org.testng.annotations.Test)

Example 9 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class ApiListingJSON method scan.

protected synchronized void scan(Application app) {
    Scanner scanner = ScannerFactory.getScanner();
    LOGGER.debug("using scanner " + scanner);
    if (scanner != null) {
        SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
        Set<Class<?>> classes = null;
        classes = scanner.classes();
        if (classes != null) {
            Reader reader = new Reader(swagger);
            swagger = reader.read(classes);
            if (scanner instanceof SwaggerConfig) {
                swagger = ((SwaggerConfig) scanner).configure(swagger);
            }
        }
    }
    initialized = true;
}
Also used : Scanner(io.swagger.config.Scanner) SwaggerConfig(io.swagger.config.SwaggerConfig) Reader(io.swagger.jaxrs.Reader)

Example 10 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class ApiDeclarationServlet method init.

@Override
public void init(ServletConfig servletConfig) throws javax.servlet.ServletException {
    super.init(servletConfig);
    final ServletContext servletContext = servletConfig.getServletContext();
    final Scanner scanner = (Scanner) servletContext.getAttribute("scanner");
    if (scanner != null) {
        Swagger swagger = (Swagger) servletContext.getAttribute("swagger");
        if (swagger == null) {
            swagger = new Swagger();
        }
        final SwaggerConfig configurator = (SwaggerConfig) servletContext.getAttribute("reader");
        if (configurator != null) {
            configurator.configure(swagger);
        }
        final Set<Class<?>> classes = scanner.classes();
        if (classes != null) {
            Reader.read(swagger, classes);
        }
        servletContext.setAttribute("swagger", swagger);
    }
}
Also used : Scanner(io.swagger.config.Scanner) SwaggerConfig(io.swagger.config.SwaggerConfig) Swagger(io.swagger.models.Swagger) ServletContext(javax.servlet.ServletContext)

Aggregations

Scanner (io.swagger.config.Scanner)11 Test (org.testng.annotations.Test)5 SwaggerConfig (io.swagger.config.SwaggerConfig)4 SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)4 Reader (io.swagger.jaxrs.Reader)3 DefaultJaxrsScanner (io.swagger.jaxrs.config.DefaultJaxrsScanner)3 Swagger (io.swagger.models.Swagger)3 JaxrsScanner (io.swagger.jaxrs.config.JaxrsScanner)1 ServletContext (javax.servlet.ServletContext)1