From b7646e2a242f70072d3b789fcc5cad3eb505d1f9 Mon Sep 17 00:00:00 2001 From: jmatsuok Date: Thu, 9 May 2024 19:21:03 -0400 Subject: [PATCH 1/3] Abstract away document type --- .../core/templates/RemoteTemplateService.java | 55 +++++++++++-------- .../core/templates/TemplateService.java | 7 +-- .../templates/RemoteTemplateServiceTest.java | 6 +- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/cryostat/core/templates/RemoteTemplateService.java b/src/main/java/io/cryostat/core/templates/RemoteTemplateService.java index 30371cd7..5e93e0be 100644 --- a/src/main/java/io/cryostat/core/templates/RemoteTemplateService.java +++ b/src/main/java/io/cryostat/core/templates/RemoteTemplateService.java @@ -55,35 +55,42 @@ protected TemplateType providedTemplateType() { } @Override - public Optional getXml(String templateName, TemplateType type) + public Optional getXml(String templateName, TemplateType type) throws FlightRecorderException { if (!providedTemplateType().equals(type)) { return Optional.empty(); } try { - return conn.getService().getServerTemplates().stream() - .map(xmlText -> Jsoup.parse(xmlText, "", Parser.xmlParser())) - .filter( - doc -> { - Elements els = doc.getElementsByTag("configuration"); - if (els.isEmpty()) { - throw new MalformedXMLException( - "Document did not contain \"configuration\" element"); - } - if (els.size() > 1) { - throw new MalformedXMLException( - "Document contains multiple \"configuration\"" - + " elements"); - } - Element configuration = els.first(); - if (!configuration.hasAttr("label")) { - throw new MalformedXMLException( - "Configuration element did not have \"label\"" - + " attribute"); - } - return configuration.attr("label").equals(templateName); - }) - .findFirst(); + Optional document = + conn.getService().getServerTemplates().stream() + .map(xmlText -> Jsoup.parse(xmlText, "", Parser.xmlParser())) + .filter( + doc -> { + Elements els = doc.getElementsByTag("configuration"); + if (els.isEmpty()) { + throw new MalformedXMLException( + "Document did not contain \"configuration\"" + + " element"); + } + if (els.size() > 1) { + throw new MalformedXMLException( + "Document contains multiple \"configuration\"" + + " elements"); + } + Element configuration = els.first(); + if (!configuration.hasAttr("label")) { + throw new MalformedXMLException( + "Configuration element did not have \"label\"" + + " attribute"); + } + return configuration.attr("label").equals(templateName); + }) + .findFirst(); + if (document.isPresent()) { + return Optional.of(document.get().toString()); + } else { + return Optional.empty(); + } } catch (org.openjdk.jmc.flightrecorder.configuration.FlightRecorderException | IOException | ServiceNotAvailableException e) { diff --git a/src/main/java/io/cryostat/core/templates/TemplateService.java b/src/main/java/io/cryostat/core/templates/TemplateService.java index ad039a14..b36d8285 100644 --- a/src/main/java/io/cryostat/core/templates/TemplateService.java +++ b/src/main/java/io/cryostat/core/templates/TemplateService.java @@ -23,18 +23,15 @@ import io.cryostat.core.FlightRecorderException; -import org.jsoup.nodes.Document; - public interface TemplateService { List