diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index 49c83662..419dd32b 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -31,6 +31,7 @@ import javax.inject.Inject; import org.openjdk.jmc.common.unit.IConstrainedMap; +import org.openjdk.jmc.common.unit.ITypedQuantity; import org.openjdk.jmc.common.unit.QuantityConversionException; import org.openjdk.jmc.common.unit.UnitLookup; import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID; @@ -50,6 +51,7 @@ import io.cryostat.core.templates.Template; import io.cryostat.core.templates.TemplateType; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.net.httpserver.HttpExchange; import io.smallrye.config.SmallRyeConfig; @@ -110,8 +112,19 @@ public void handle(HttpExchange exchange) throws IOException { break; case "PATCH": id = extractId(exchange); - if (id >= 0) { - handleStop(exchange, id); + String request = requestStopOrUpdate(exchange); + if (request == "STOPPED") { + if (id >= 0) { + handleStop(exchange, id); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); + } + } else if (request == "UPDATE") { + if (id >= 0) { + handleRecordingUpdate(exchange, id); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); + } } else { exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); } @@ -135,6 +148,14 @@ public void handle(HttpExchange exchange) throws IOException { } } + private static String requestStopOrUpdate(HttpExchange exchange) throws IOException { + try (InputStream body = exchange.getRequestBody()) { + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonMap = mapper.readTree(body); + return jsonMap.get("state").toString(); + } + } + private static long extractId(HttpExchange exchange) throws IOException { Matcher m = PATH_ID_PATTERN.matcher(exchange.getRequestURI().getPath()); if (!m.find()) { @@ -231,6 +252,38 @@ private void handleStartRecordingOrSnapshot(HttpExchange exchange) throws IOExce } } + private void handleRecordingUpdate(HttpExchange exchange, long id) throws IOException { + try (InputStream body = exchange.getRequestBody()) { + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonMap = mapper.readTree(body); + invokeOnRecording( + exchange, + id, + r -> { + try { + if (jsonMap.has("name")) { + r.setName(jsonMap.get("name").toString()); + } + if (jsonMap.has("toDisk")) { + r.setToDisk(jsonMap.get("toDisk").asBoolean()); + } + // if (jsonMap.has("duration")) { + // IQuantity duration = UnitLookup.MILLISECOND.quantity(jsonMap.get("duration").asLong()); + // r.setDuration(jsonMap.get("duration").asLong()); + // } + if (jsonMap.has("maxSize")) { + r.setMaxSize(jsonMap.get("name").asLong()); + } + // if (jsonMap.has("maxAge")) { + // r.setMaxAge(jsonMap.get("name").asLong()); + // } + } catch (IllegalStateException e) { + sendHeader(exchange, HttpStatus.SC_CONFLICT); + } + }); + } + } + private void handleStop(HttpExchange exchange, long id) throws IOException { invokeOnRecording( exchange, @@ -378,7 +431,7 @@ boolean requestsBundledTemplate() { boolean requestSnapshot() { boolean snapshotName = name.equals("snapshot"); boolean snapshotTemplate = - StringUtils.isBlank(template) && StringUtils.isBlank(localTemplateName); + StringUtils.isBlank(template) && StringUtils.isBlank(localTemplateName); boolean snapshotFeatures = duration == 0 && maxSize == 0 && maxAge == 0; return snapshotName && snapshotTemplate && snapshotFeatures; }