Skip to content

Commit

Permalink
implement more suitable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 15, 2024
1 parent e655059 commit c17c5d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/main/java/io/cryostat/events/EventTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,8 +92,11 @@ public Uni<Void> 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);
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/cryostat/events/S3TemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c17c5d9

Please sign in to comment.