@Override protected Map<String, OpenApiConfigurationLoader> getLocationLoaders() { Map<String, OpenApiConfigurationLoader> map = super.getLocationLoaders(); map.put("servlet", new ServletOpenApiConfigurationLoader(servletConfig)); map.put("servletpath", new ServletPathConfigurationLoader(servletConfig)); return map; }
public T servletConfig(ServletConfig servletConfig) { if (!ServletConfigContextUtils.isServletConfigAvailable(servletConfig)) { return (T) this; } this.servletConfig = servletConfig; this.servletContext = servletConfig.getServletContext(); return (T) this; }
@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(); } }
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) .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; }
@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; }
if (resolveResourcePackages(servletConfig) != null) { return true; if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_FILTER_KEY) != null) { return true; if (resolveResourceClasses(servletConfig) != null) { return true; if (getBooleanInitParam(servletConfig, OPENAPI_CONFIGURATION_READALLRESOURCES_KEY) != null) { return true; if (getBooleanInitParam(servletConfig, OPENAPI_CONFIGURATION_PRETTYPRINT_KEY) != null) { return true; if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_READER_KEY) != null) { return true; if (getLongInitParam(servletConfig, OPENAPI_CONFIGURATION_CACHE_TTL_KEY) != null) { return true; if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_SCANNER_KEY) != null) { return true; if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_OBJECT_MAPPER_PROCESSOR_KEY) != null) { return true; if (resolveModelConverterClasses(servletConfig) != null) { return true;
public static String getContextIdFromServletConfig(ServletConfig config) { String ctxId = null; if (isServletConfigAvailable(config)) { ctxId = getInitParam(config, OpenApiContext.OPENAPI_CONTEXT_ID_KEY); if (StringUtils.isBlank(ctxId)) { ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_PREFIX + "servlet." + config.getServletName(); } } if (StringUtils.isBlank(ctxId)) { ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT; } return ctxId; }
@Override protected OpenApiScanner buildScanner(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 JaxrsApplicationAndAnnotationScanner(); } scanner.setConfiguration(openApiConfiguration); if (scanner instanceof JaxrsOpenApiScanner) { ((JaxrsOpenApiScanner) scanner).setApplication(app); } return scanner; } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String ctxId = getContextIdFromServletConfig(getServletConfig()); OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId); OpenAPI oas = ctx.read();
@Override public OpenAPIConfiguration load(String path) throws IOException { if (servletConfig == null) { return null; } if (StringUtils.isBlank(path)) { return null; } String sanitized = (path.startsWith("/") ? path : "/" + path); String configString = readInputStreamToString(servletConfig.getServletContext().getResourceAsStream(sanitized)); return deserializeConfig(path, configString); }
@Override public Set<Class<?>> classes() { Set<Class<?>> classes = super.classes(); Set<Class<?>> output = new HashSet<Class<?>>(); if (application != null) { Set<Class<?>> clzs = application.getClasses(); if (clzs != null) { for (Class<?> clz : clzs) { if (!isIgnored(clz.getName())) { output.add(clz); } } } Set<Object> singletons = application.getSingletons(); if (singletons != null) { for (Object o : singletons) { if (!isIgnored(o.getClass().getName())) { output.add(o.getClass()); } } } } classes.addAll(output); return classes; } }
public static Long getLongInitParam(ServletConfig sc, String paramKey) { String param = getInitParam(sc, paramKey); if (StringUtils.isBlank(param)) { return null; } try { return Long.parseLong(param); } catch (NumberFormatException e) { return null; } }
@Override protected List<ImmutablePair<String, String>> getKnownLocations() { List<ImmutablePair<String, String>> locations = new LinkedList<>(Arrays.asList( new ImmutablePair<>("servlet", ServletConfigContextUtils.OPENAPI_CONFIGURATION_LOCATION_KEY), new ImmutablePair<>("servletpath", "openapi-configuration.yaml"), new ImmutablePair<>("servletpath", "openapi-configuration.json"), new ImmutablePair<>("servletpath", "WEB-INF/openapi-configuration.yaml"), new ImmutablePair<>("servletpath", "WEB-INF/openapi-configuration.json"), new ImmutablePair<>("servletpath", "openapi.yaml"), new ImmutablePair<>("servletpath", "openapi.json"), new ImmutablePair<>("servletpath", "WEB-INF/openapi.yaml"), new ImmutablePair<>("servletpath", "WEB-INF/openapi.json") )); locations.addAll(super.getKnownLocations()); locations.add(new ImmutablePair<>("servlet", "")); // get config from init params return locations; }
@Override public Set<Class<?>> classes() { Set<Class<?>> output = new HashSet<Class<?>>(); if (application != null) { Set<Class<?>> clzs = application.getClasses(); if (clzs != null) { for (Class<?> clz : clzs) { if (!isIgnored(clz.getName())) { output.add(clz); } } } Set<Object> singletons = application.getSingletons(); if (singletons != null) { for (Object o : singletons) { if (!isIgnored(o.getClass().getName())) { output.add(o.getClass()); } } } } return output; } }
public static Set<String> resolveResourceClasses(ServletConfig servletConfig) { if (!isServletConfigAvailable(servletConfig)) { return null; } String resourceClasses = getInitParam(servletConfig, OPENAPI_CONFIGURATION_RESOURCECLASSES_KEY); if (resourceClasses == null) { // jersey 2 resourceClasses = getInitParam(servletConfig, JERSEY2_CLASSES_KEY); if (resourceClasses != null) { resourceClasses = resourceClasses.replace(';', ','); } } if (StringUtils.isBlank(resourceClasses)) { return null; } return Arrays.stream(resourceClasses.split(",")).collect(Collectors.toSet()); }
public static String getInitParam(ServletConfig sc, String paramKey) { if (!isServletConfigAvailable(sc)) { return null; } return sc.getInitParameter(paramKey) == null ? sc.getInitParameter(paramKey) : sc.getInitParameter(paramKey); }
public static Boolean getBooleanInitParam(ServletConfig sc, String paramKey) { String param = getInitParam(sc, paramKey); if (StringUtils.isBlank(param)) { return null; } return Boolean.valueOf(Boolean.parseBoolean(param)); }
public static Set<String> resolveResourcePackages(ServletConfig servletConfig) { if (!isServletConfigAvailable(servletConfig)) { return null; } String resourcePackage = getInitParam(servletConfig, OPENAPI_CONFIGURATION_RESOURCEPACKAGE_KEY); if (resourcePackage == null) { // jersey 1 resourcePackage = getInitParam(servletConfig, JERSEY1_PACKAGE_KEY); if (resourcePackage != null) { resourcePackage = resourcePackage.replace(';', ','); } } if (resourcePackage == null) { // jersey 2 resourcePackage = getInitParam(servletConfig, JERSEY2_PACKAGE_KEY); if (resourcePackage != null) { resourcePackage = resourcePackage.replace(';', ','); } } if (StringUtils.isBlank(resourcePackage)) { return null; } return Arrays.stream(resourcePackage.split(",")).collect(Collectors.toSet()); }
/** * @since 2.0.6 */ public static Set<String> resolveModelConverterClasses(ServletConfig servletConfig) { if (!isServletConfigAvailable(servletConfig)) { return null; } String modelConverterClasses = getInitParam(servletConfig, OPENAPI_CONFIGURATION_MODEL_CONVERTERS_KEY); if (modelConverterClasses != null) { modelConverterClasses = modelConverterClasses.replace(';', ','); } if (StringUtils.isBlank(modelConverterClasses)) { return null; } return new LinkedHashSet<>(Arrays.stream(modelConverterClasses.split(",")).collect(Collectors.toSet())); }