private String readFromUrl(URL url) { try { String content = ""; try (BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) { content = reader.lines().collect(Collectors.joining("\n")); } return content; } catch (IOException e) { throw new LoadException(String.format("Could not read from URL '%s': %s", url, e.getMessage())); } }
public Content load(URL url) { Reference ref = new Reference(url, modelVersion); Content doc = docCache.get(ref); if (doc == null) { try { String text = readFromUrl(url); JsonNode root = toJson(text, url, modelVersion); doc = Content.getContentItem(ref, root); } catch (LoadException e) { doc = Content.getUnloadableContentItem(ref, e.getMessage()); } } docCache.put(ref, doc); return doc; }
private static JsonNode toJson(String text, URL url, Integer modelVersion) { try { if (modelVersion == ObjectType.SWAGGER_MODEL_VERSION) { return OpenApiIO.loadSwaggerTree(text, false); } else if (modelVersion == ObjectType.OPENAPI3_MODEL_VERSION) { return OpenApiIO.loadOpenApi3Tree(text, false); } else { throw new LoadException("Invalid model version number: " + modelVersion); } } catch (GenerationException e) { String urlMsg = url != null ? " from URL '" + url.toString() + "'" : ""; throw new LoadException(String.format("Invalid YAML or JSON%s: %s", urlMsg, e.getMessage())); } }