public static synchronized OpenApiContextLocator getInstance() { if (instance == null) { instance = new OpenApiContextLocator(); } return instance; }
public final T openApiReader(OpenApiReader openApiReader) { setOpenApiReader(openApiReader); return (T) this; }
protected void register() { OpenApiContextLocator.getInstance().putOpenApiContext(id, this); }
private OpenAPIConfiguration mergeParentConfiguration(OpenAPIConfiguration config, OpenApiContext parent) { if (parent == null || parent.getOpenApiConfiguration() == null) { return config; OpenAPIConfiguration parentConfig = parent.getOpenApiConfiguration(); merged = (SwaggerConfiguration) config; } else { merged = (SwaggerConfiguration) ContextUtils.deepCopy(config); if (merged.getResourceClasses() == null) { merged.setResourceClasses(parentConfig.getResourceClasses()); if (merged.getFilterClass() == null) { merged.setFilterClass(parentConfig.getFilterClass()); if (merged.getIgnoredRoutes() == null) { merged.setIgnoredRoutes(parentConfig.getIgnoredRoutes()); if (merged.getOpenAPI() == null) { merged.setOpenAPI(parentConfig.getOpenAPI()); if (merged.getReaderClass() == null) { merged.setReaderClass(parentConfig.getReaderClass()); if (merged.getResourcePackages() == null) { merged.setResourcePackages(parentConfig.getResourcePackages()); if (merged.getScannerClass() == null) { merged.setScannerClass(parentConfig.getScannerClass());
openApiConfiguration = loadConfiguration(); openApiConfiguration = new SwaggerConfiguration().resourcePackages(resourcePackages).resourceClasses(resourceClasses); ((SwaggerConfiguration) openApiConfiguration).setId(id); openApiConfiguration = mergeParentConfiguration(openApiConfiguration, parent); openApiReader = buildReader(ContextUtils.deepCopy(openApiConfiguration)); openApiScanner = buildScanner(ContextUtils.deepCopy(openApiConfiguration)); objectMapperProcessor = buildObjectMapperProcessor(ContextUtils.deepCopy(openApiConfiguration)); modelConverters = buildModelConverters(ContextUtils.deepCopy(openApiConfiguration)); throw new OpenApiConfigurationException("error initializing context: " + e.getMessage(), e); ObjectMapper mapper = IntegrationObjectMapperFactory.createJson(); objectMapperProcessor.processJsonObjectMapper(mapper); ModelConverters.getInstance().addConverter(new ModelResolver(mapper)); throw new OpenApiConfigurationException("error configuring objectMapper: " + e.getMessage(), e); throw new OpenApiConfigurationException("error configuring model converters: " + e.getMessage(), e); if (openApiConfiguration.getCacheTTL() != null) { this.cacheTTL = openApiConfiguration.getCacheTTL(); register();
@Override public OpenApiContext buildContext(boolean init) throws OpenApiConfigurationException { if (StringUtils.isBlank(ctxId)) { ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT; } OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId); if (ctx == null) { OpenApiContext rootCtx = OpenApiContextLocator.getInstance().getOpenApiContext(OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT); ctx = new GenericOpenApiContext() .openApiConfiguration(openApiConfiguration) .id(ctxId) .parent(rootCtx); if (ctx.getConfigLocation() == null && configLocation != null) { ((GenericOpenApiContext) ctx).configLocation(configLocation); } if (((GenericOpenApiContext) ctx).getResourcePackages() == null && resourcePackages != null) { ((GenericOpenApiContext) ctx).resourcePackages(resourcePackages); } if (((GenericOpenApiContext) ctx).getResourceClasses() == null && resourceClasses != null) { ((GenericOpenApiContext) ctx).resourceClasses(resourceClasses); } if (init) { ctx.init(); // includes registering itself with OpenApiContextLocator } } return ctx; }
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException { if (classes != null && classes.size() != 0) { Set<Class<?>> resources = new LinkedHashSet(); classes.stream() .filter(c -> ignored.stream().noneMatch(i -> c.getName().startsWith(i))) .forEach(resources::add); if (!resources.isEmpty()) { // init context try { SwaggerConfiguration oasConfig = new SwaggerConfiguration() .resourceClasses(resources.stream().map(c -> c.getName()).collect(Collectors.toSet())); new JaxrsOpenApiContextBuilder() .openApiConfiguration(oasConfig) .buildContext(true); } catch (OpenApiConfigurationException e) { throw new RuntimeException(e.getMessage(), e); } } } }
@Override public OpenApiContext buildContext(boolean init) throws OpenApiConfigurationException { if (StringUtils.isBlank(ctxId)) { ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT; } OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId); if (ctx == null) { OpenApiContext rootCtx = OpenApiContextLocator.getInstance().getOpenApiContext(OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT); ctx = new XmlWebOpenApiContext() .servletConfig(servletConfig) .openApiConfiguration(openApiConfiguration) .id(ctxId) .parent(rootCtx); if (ctx.getConfigLocation() == null && configLocation != null) { ((XmlWebOpenApiContext) ctx).configLocation(configLocation); } if (((XmlWebOpenApiContext) ctx).getResourcePackages() == null && resourcePackages != null) { ((XmlWebOpenApiContext) ctx).resourcePackages(resourcePackages); } if (((XmlWebOpenApiContext) ctx).getResourceClasses() == null && resourceClasses != null) { ((XmlWebOpenApiContext) ctx).resourceClasses(resourceClasses); } if (init) { ctx.init(); // includes registering itself with OpenApiContextLocator } } return ctx; }
public Reader(OpenAPI openAPI) { this(); setConfiguration(new SwaggerConfiguration().openAPI(openAPI)); }
@Override public OpenAPI read() { if (cacheTTL == 0) { resetReader(); return getOpenApiReader().read(getOpenApiScanner().classes(), getOpenApiScanner().resources()); } Cache cached = cache.get("openapi"); if (cached == null || cached.isStale(cacheTTL)) { cached = new Cache(); cached.createdAt = System.currentTimeMillis(); resetReader(); cached.openApi = getOpenApiReader().read(getOpenApiScanner().classes(), getOpenApiScanner().resources()); cache.put("openapi", cached); } return cached.openApi; }
@Override public void setConfiguration(OpenAPIConfiguration openApiConfiguration) { if (openApiConfiguration != null) { this.config = ContextUtils.deepCopy(openApiConfiguration); if (openApiConfiguration.getOpenAPI() != null) { this.openAPI = this.config.getOpenAPI(); if (this.openAPI.getComponents() != null) { this.components = this.openAPI.getComponents(); } } } }
protected Map<String, OpenApiConfigurationLoader> getLocationLoaders() { Map<String, OpenApiConfigurationLoader> map = new HashMap<>(); map.put("classpath", new ClasspathOpenApiConfigurationLoader()); map.put("file", new FileOpenApiConfigurationLoader()); map.put("url", new URLOpenApiConfigurationLoader()); map.put("service", new ServiceOpenApiConfigurationLoader()); return map; }
protected OpenAPIConfiguration loadConfiguration() throws OpenApiConfigurationException { Map<String, OpenApiConfigurationLoader> loaders = getLocationLoaders(); try { if (StringUtils.isNotEmpty(configLocation)) { if (loaders.get("classpath").exists(configLocation)) { return loaders.get("classpath").load(configLocation); } if (loaders.get("file").exists(configLocation)) { return loaders.get("file").load(configLocation); } } // check known locations List<ImmutablePair<String, String>> knownLocations = getKnownLocations(); for (ImmutablePair<String, String> location : knownLocations) { if (loaders.get(location.left).exists(location.right)) { try { return loaders.get(location.left).load(location.right); } catch (IOException ioe) { // try next one } } } return null; } catch (Exception e) { throw new OpenApiConfigurationException(e.getMessage(), e); } }
@Override public void init(ServletConfig config) throws ServletException { super.init(config); String ctxId = getContextIdFromServletConfig(config); try { new ServletOpenApiContextBuilder() .servletConfig(config) .ctxId(ctxId) .buildContext(true); } catch (OpenApiConfigurationException e) { e.printStackTrace(); } }
protected OpenApiScanner buildScanner(final OpenAPIConfiguration openApiConfiguration) throws Exception { OpenApiScanner scanner; if (StringUtils.isNotBlank(openApiConfiguration.getScannerClass())) { Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getScannerClass()); scanner = (OpenApiScanner) cls.newInstance(); } else { scanner = new GenericOpenApiScanner(); } scanner.setConfiguration(openApiConfiguration); return scanner; }
protected void resetReader() { if (providedOpenApiReader == null) { try { openApiReader = buildReader(ContextUtils.deepCopy(openApiConfiguration)); } catch (Exception e) { LOGGER.error("error building reader: " + e.getMessage(), e); // keep previous reader } } }
@Override public OpenAPIConfiguration load(String path) throws IOException { File file = new File(path); return deserializeConfig(path, readInputStreamToString(new FileInputStream(file))); }
@Override public OpenAPIConfiguration load(String path) throws IOException { String sanitized = (path.startsWith("/") ? path : "/" + path); String configString = readInputStreamToString(this.getClass().getResource(sanitized).openStream()); return deserializeConfig(path, configString); }
@Override public OpenApiContext buildContext(boolean init) throws OpenApiConfigurationException { if (StringUtils.isBlank(ctxId)) { ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT; } OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId); if (ctx == null) { OpenApiContext rootCtx = OpenApiContextLocator.getInstance().getOpenApiContext(OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT); ctx = new XmlWebOpenApiContext() .servletConfig(servletConfig) .app(application) .openApiConfiguration(openApiConfiguration) .id(ctxId) .parent(rootCtx); if (ctx.getConfigLocation() == null && configLocation != null) { ((XmlWebOpenApiContext) ctx).configLocation(configLocation); } if (((XmlWebOpenApiContext) ctx).getResourcePackages() == null && resourcePackages != null) { ((XmlWebOpenApiContext) ctx).resourcePackages(resourcePackages); } if (((XmlWebOpenApiContext) ctx).getResourceClasses() == null && resourceClasses != null) { ((XmlWebOpenApiContext) ctx).resourceClasses(resourceClasses); } if (init) { ctx.init(); // includes registering itself with OpenApiContextLocator } } return ctx; }