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;
}
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;
}
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"));
}
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;
}
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);
}
}
Aggregations