diff --git a/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java b/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java index c4b04046..9f979c8b 100644 --- a/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java +++ b/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java @@ -87,17 +87,20 @@ public LocalProbeTemplateService(FileSystem fs, Environment env) throws IOExcept } } - public void addTemplate(InputStream inputStream, String filename) + public ProbeTemplate addTemplate(InputStream inputStream, String filename) throws FileAlreadyExistsException, IOException, SAXException { + Path path = fs.pathOf(env.getEnv(TEMPLATE_PATH), filename); + if (fs.exists(path)) { + throw new FileAlreadyExistsException( + String.format("Probe template \"%s\" already exists.", filename)); + } try (inputStream) { ProbeTemplate template = new ProbeTemplate(); + template.setFileName(filename); // If validation fails this will throw a ProbeValidationException with details template.deserialize(inputStream); - Path path = fs.pathOf(env.getEnv(TEMPLATE_PATH), filename); - if (fs.exists(path)) { - throw new FileAlreadyExistsException(template.getFileName()); - } fs.writeString(path, template.serialize()); + return template; } } @@ -108,9 +111,10 @@ public void deleteTemplate(String templateName) throws IOException { } } - public String getTemplate(String templateName) throws IOException, SAXException { + public String getTemplateContent(String templateName) throws IOException, SAXException { Path probeTemplatePath = fs.pathOf(env.getEnv(TEMPLATE_PATH), templateName); ProbeTemplate template = new ProbeTemplate(); + template.setFileName(templateName); template.deserialize(fs.newInputStream(probeTemplatePath)); return template.serialize(); } @@ -136,8 +140,8 @@ public List getTemplates() throws FlightRecorderException { Path fileName = path.getFileName(); if (fileName != null) { ProbeTemplate template = new ProbeTemplate(); - template.deserialize(stream); template.setFileName(fileName.toString()); + template.deserialize(stream); templates.add(template); } } diff --git a/src/main/java/io/cryostat/core/agent/ProbeTemplate.java b/src/main/java/io/cryostat/core/agent/ProbeTemplate.java index fe5e4f36..13133f8b 100644 --- a/src/main/java/io/cryostat/core/agent/ProbeTemplate.java +++ b/src/main/java/io/cryostat/core/agent/ProbeTemplate.java @@ -107,7 +107,7 @@ public void deserialize(InputStream xmlStream) throws IOException, SAXException } Document document = builder.parse(stream); - stream.close(); + stream.trulyClose(); NodeList elements; // parse global configurations