From c17c5d955f78fbf043dc7ef7968ac3a59fa90b7c Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 15 Feb 2024 14:05:42 -0500 Subject: [PATCH] implement more suitable interface --- .../java/io/cryostat/events/EventTemplates.java | 11 ++++++++--- .../java/io/cryostat/events/S3TemplateService.java | 13 +++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/cryostat/events/EventTemplates.java b/src/main/java/io/cryostat/events/EventTemplates.java index b94dd2db3..951ed86a6 100644 --- a/src/main/java/io/cryostat/events/EventTemplates.java +++ b/src/main/java/io/cryostat/events/EventTemplates.java @@ -15,7 +15,9 @@ */ package io.cryostat.events; +import java.io.ByteArrayInputStream; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -90,8 +92,11 @@ public Uni postTemplates(@RestForm("template") FileUpload body) { .readFile(path.toString()) .onComplete( ar -> { - try { - customTemplateService.addTemplate(ar.result().toString()); + var str = ar.result().toString(); + try (var stream = + new ByteArrayInputStream( + str.getBytes(StandardCharsets.UTF_8))) { + customTemplateService.addTemplate(stream); cf.complete(null); } catch (Exception e) { logger.error(e); @@ -119,7 +124,7 @@ public Response deleteTemplatesV1(@RestPath String templateName) { @Path("/api/v3/event_templates/{templateName}") @RolesAllowed("write") public void deleteTemplates(@RestPath String templateName) { - customTemplateService.removeTemplate(templateName); + customTemplateService.deleteTemplate(templateName); } @GET diff --git a/src/main/java/io/cryostat/events/S3TemplateService.java b/src/main/java/io/cryostat/events/S3TemplateService.java index 5ec123b96..be6683a0c 100644 --- a/src/main/java/io/cryostat/events/S3TemplateService.java +++ b/src/main/java/io/cryostat/events/S3TemplateService.java @@ -15,7 +15,6 @@ */ package io.cryostat.events; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -40,10 +39,10 @@ import io.cryostat.ConfigProperties; import io.cryostat.Producers; import io.cryostat.core.FlightRecorderException; +import io.cryostat.core.templates.MutableTemplateService; import io.cryostat.core.templates.MutableTemplateService.InvalidEventTemplateException; import io.cryostat.core.templates.MutableTemplateService.InvalidXmlException; import io.cryostat.core.templates.Template; -import io.cryostat.core.templates.TemplateService; import io.cryostat.core.templates.TemplateType; import io.cryostat.util.HttpStatusCodeIdentifier; import io.cryostat.ws.MessagingServer; @@ -78,7 +77,7 @@ import software.amazon.awssdk.services.s3.model.Tagging; @ApplicationScoped -class S3TemplateService implements TemplateService { +class S3TemplateService implements MutableTemplateService { static final String EVENT_TEMPLATE_CREATED = "TemplateUploaded"; static final String EVENT_TEMPLATE_DELETED = "TemplateDeleted"; @@ -240,9 +239,10 @@ private XMLModel parseXml(InputStream inputStream) throws IOException, ParseExce } @Blocking - Template addTemplate(String templateText) + @Override + public Template addTemplate(InputStream stream) throws InvalidXmlException, InvalidEventTemplateException, IOException { - try (var stream = new ByteArrayInputStream(templateText.getBytes(StandardCharsets.UTF_8))) { + try (stream) { XMLModel model = parseXml(stream); XMLTagInstance configuration = model.getRoot(); @@ -292,7 +292,8 @@ Template addTemplate(String templateText) } @Blocking - void removeTemplate(String templateName) { + @Override + public void deleteTemplate(String templateName) { var req = DeleteObjectRequest.builder() .bucket(eventTemplatesBucket)