From 1a2acaa460875270843b6daf4537ad1f1492bf8d Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 10:41:15 -0400 Subject: [PATCH 01/11] end paths with / so as to not match by prefix --- .../java/io/cryostat/agent/remote/EventTemplatesContext.java | 2 +- src/main/java/io/cryostat/agent/remote/EventTypesContext.java | 2 +- src/main/java/io/cryostat/agent/remote/MBeanContext.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java b/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java index fb5fca55..bf12b1e9 100644 --- a/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java +++ b/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java @@ -43,7 +43,7 @@ class EventTemplatesContext implements RemoteContext { @Override public String path() { - return "/event-templates"; + return "/event-templates/"; } @Override diff --git a/src/main/java/io/cryostat/agent/remote/EventTypesContext.java b/src/main/java/io/cryostat/agent/remote/EventTypesContext.java index 0fc79763..fb1f90cb 100644 --- a/src/main/java/io/cryostat/agent/remote/EventTypesContext.java +++ b/src/main/java/io/cryostat/agent/remote/EventTypesContext.java @@ -45,7 +45,7 @@ class EventTypesContext implements RemoteContext { @Override public String path() { - return "/event-types"; + return "/event-types/"; } @Override diff --git a/src/main/java/io/cryostat/agent/remote/MBeanContext.java b/src/main/java/io/cryostat/agent/remote/MBeanContext.java index c085df90..7ee21456 100644 --- a/src/main/java/io/cryostat/agent/remote/MBeanContext.java +++ b/src/main/java/io/cryostat/agent/remote/MBeanContext.java @@ -59,7 +59,7 @@ class MBeanContext implements RemoteContext { @Override public String path() { - return "/mbean-metrics"; + return "/mbean-metrics/"; } @Override From 18f3742a5c353022f952010306afa6e79ea37683 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 10:41:31 -0400 Subject: [PATCH 02/11] refactor: extract methods --- .../agent/remote/RecordingsContext.java | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index d5c9cc96..9955409d 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -88,37 +88,10 @@ public void handle(HttpExchange exchange) throws IOException { } switch (mtd) { case "GET": - try (OutputStream response = exchange.getResponseBody()) { - List recordings = getRecordings(); - exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); - mapper.writeValue(response, recordings); - } catch (Exception e) { - log.error("recordings serialization failure", e); - } + handleGetList(exchange); break; case "POST": - try (InputStream body = exchange.getRequestBody()) { - StartRecordingRequest req = - mapper.readValue(body, StartRecordingRequest.class); - if (!req.isValid()) { - exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); - return; - } - SerializableRecordingDescriptor recording = startRecording(req); - exchange.sendResponseHeaders(HttpStatus.SC_CREATED, 0); - try (OutputStream response = exchange.getResponseBody()) { - mapper.writeValue(response, recording); - } - } catch (QuantityConversionException - | ServiceNotAvailableException - | FlightRecorderException - | org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException - | InvalidEventTemplateException - | InvalidXmlException - | IOException e) { - log.error("Failed to start recording", e); - exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, -1); - } + handleStart(exchange); break; default: log.warn("Unknown request method {}", mtd); @@ -130,6 +103,40 @@ public void handle(HttpExchange exchange) throws IOException { } } + private void handleGetList(HttpExchange exchange) { + try (OutputStream response = exchange.getResponseBody()) { + List recordings = getRecordings(); + exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); + mapper.writeValue(response, recordings); + } catch (Exception e) { + log.error("recordings serialization failure", e); + } + } + + private void handleStart(HttpExchange exchange) throws IOException { + try (InputStream body = exchange.getRequestBody()) { + StartRecordingRequest req = mapper.readValue(body, StartRecordingRequest.class); + if (!req.isValid()) { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + return; + } + SerializableRecordingDescriptor recording = startRecording(req); + exchange.sendResponseHeaders(HttpStatus.SC_CREATED, 0); + try (OutputStream response = exchange.getResponseBody()) { + mapper.writeValue(response, recording); + } + } catch (QuantityConversionException + | ServiceNotAvailableException + | FlightRecorderException + | org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException + | InvalidEventTemplateException + | InvalidXmlException + | IOException e) { + log.error("Failed to start recording", e); + exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, -1); + } + } + private boolean ensureMethodAccepted(HttpExchange exchange) throws IOException { Set blocked = Set.of("POST"); String mtd = exchange.getRequestMethod(); From 65777347af76430e4dc13efe8341abac242a2fe5 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 10:54:23 -0400 Subject: [PATCH 03/11] add utility for extracting ID from path --- .../agent/remote/RecordingsContext.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index 9955409d..d0f38260 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -22,6 +22,8 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.inject.Inject; @@ -56,6 +58,10 @@ class RecordingsContext implements RemoteContext { + private static final String PATH = "/recordings"; + private static final Pattern PATH_ID_PATTERN = + Pattern.compile("^" + PATH + "/(\\d+)$", Pattern.MULTILINE); + private final Logger log = LoggerFactory.getLogger(getClass()); private final SmallRyeConfig config; private final ObjectMapper mapper; @@ -76,7 +82,7 @@ class RecordingsContext implements RemoteContext { @Override public String path() { - return "/recordings"; + return PATH; } @Override @@ -88,7 +94,12 @@ public void handle(HttpExchange exchange) throws IOException { } switch (mtd) { case "GET": - handleGetList(exchange); + int id = extractId(exchange); + if (id < 0) { + handleGetList(exchange); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_NOT_IMPLEMENTED, -1); + } break; case "POST": handleStart(exchange); @@ -103,6 +114,14 @@ public void handle(HttpExchange exchange) throws IOException { } } + private static int extractId(HttpExchange exchange) throws IOException { + Matcher m = PATH_ID_PATTERN.matcher(exchange.getRequestURI().getPath()); + if (!m.find()) { + return Integer.MIN_VALUE; + } + return Integer.parseInt(m.group(1)); + } + private void handleGetList(HttpExchange exchange) { try (OutputStream response = exchange.getResponseBody()) { List recordings = getRecordings(); From 8789208adad37f56482268cab7c6c1add23faa3c Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 15:00:49 -0400 Subject: [PATCH 04/11] fixup! add utility for extracting ID from path --- src/main/java/io/cryostat/agent/remote/RecordingsContext.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index d0f38260..d4c4e1fa 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -95,7 +95,7 @@ public void handle(HttpExchange exchange) throws IOException { switch (mtd) { case "GET": int id = extractId(exchange); - if (id < 0) { + if (id == Integer.MIN_VALUE) { handleGetList(exchange); } else { exchange.sendResponseHeaders(HttpStatus.SC_NOT_IMPLEMENTED, -1); From 7b6275398c60da4011ce336f93c56ccbe069206f Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 11:33:49 -0400 Subject: [PATCH 05/11] add handling for stopping recording --- .../agent/remote/RecordingsContext.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index d4c4e1fa..9e2f801f 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -92,9 +92,10 @@ public void handle(HttpExchange exchange) throws IOException { if (!ensureMethodAccepted(exchange)) { return; } + int id = Integer.MIN_VALUE; switch (mtd) { case "GET": - int id = extractId(exchange); + id = extractId(exchange); if (id == Integer.MIN_VALUE) { handleGetList(exchange); } else { @@ -104,6 +105,13 @@ public void handle(HttpExchange exchange) throws IOException { case "POST": handleStart(exchange); break; + case "PATCH": + id = extractId(exchange); + if (id < 0) { + handleStop(exchange, id); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + } default: log.warn("Unknown request method {}", mtd); exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); @@ -156,6 +164,36 @@ private void handleStart(HttpExchange exchange) throws IOException { } } + private void handleStop(HttpExchange exchange, int id) throws IOException { + FlightRecorder.getFlightRecorder().getRecordings().stream() + .filter(r -> r.getId() == id) + .findFirst() + .ifPresentOrElse( + r -> { + try { + try { + boolean stopped = r.stop(); + if (!stopped) { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_NO_CONTENT, -1); + } + } catch (IllegalStateException e) { + exchange.sendResponseHeaders(HttpStatus.SC_CONFLICT, -1); + } + } catch (IOException ioe) { + throw new IllegalStateException(ioe); + } + }, + () -> { + try { + exchange.sendResponseHeaders(HttpStatus.SC_NOT_FOUND, -1); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } + private boolean ensureMethodAccepted(HttpExchange exchange) throws IOException { Set blocked = Set.of("POST"); String mtd = exchange.getRequestMethod(); From b8d10b8f229c961609f12e7d4f616f1aae482b84 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 11:43:27 -0400 Subject: [PATCH 06/11] add handling for closing (deleting) recording --- .../agent/remote/RecordingsContext.java | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index 9e2f801f..45a65f65 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Set; +import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -52,6 +53,7 @@ import com.sun.net.httpserver.HttpExchange; import io.smallrye.config.SmallRyeConfig; import jdk.jfr.FlightRecorder; +import jdk.jfr.Recording; import org.apache.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -112,6 +114,13 @@ public void handle(HttpExchange exchange) throws IOException { } else { exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); } + case "DELETE": + id = extractId(exchange); + if (id < 0) { + handleDelete(exchange, id); + } else { + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + } default: log.warn("Unknown request method {}", mtd); exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); @@ -165,33 +174,47 @@ private void handleStart(HttpExchange exchange) throws IOException { } private void handleStop(HttpExchange exchange, int id) throws IOException { + invokeOnRecording( + exchange, + id, + r -> { + try { + boolean stopped = r.stop(); + if (!stopped) { + sendHeader(exchange, HttpStatus.SC_BAD_REQUEST); + } else { + sendHeader(exchange, HttpStatus.SC_NO_CONTENT); + } + } catch (IllegalStateException e) { + sendHeader(exchange, HttpStatus.SC_CONFLICT); + } + }); + } + + private void handleDelete(HttpExchange exchange, int id) throws IOException { + invokeOnRecording( + exchange, + id, + r -> { + r.close(); + sendHeader(exchange, HttpStatus.SC_NO_CONTENT); + }); + } + + private void invokeOnRecording(HttpExchange exchange, long id, Consumer consumer) { FlightRecorder.getFlightRecorder().getRecordings().stream() .filter(r -> r.getId() == id) .findFirst() .ifPresentOrElse( - r -> { - try { - try { - boolean stopped = r.stop(); - if (!stopped) { - exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); - } else { - exchange.sendResponseHeaders(HttpStatus.SC_NO_CONTENT, -1); - } - } catch (IllegalStateException e) { - exchange.sendResponseHeaders(HttpStatus.SC_CONFLICT, -1); - } - } catch (IOException ioe) { - throw new IllegalStateException(ioe); - } - }, - () -> { - try { - exchange.sendResponseHeaders(HttpStatus.SC_NOT_FOUND, -1); - } catch (IOException e) { - throw new IllegalStateException(e); - } - }); + consumer::accept, () -> sendHeader(exchange, HttpStatus.SC_NOT_FOUND)); + } + + private void sendHeader(HttpExchange exchange, int status) { + try { + exchange.sendResponseHeaders(status, -1); + } catch (IOException e) { + throw new IllegalStateException(e); + } } private boolean ensureMethodAccepted(HttpExchange exchange) throws IOException { From 15195e76e20f3ef0614268f1017c6639c5e8b6c0 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 15:01:10 -0400 Subject: [PATCH 07/11] fixup! add handling for closing (deleting) recording --- src/main/java/io/cryostat/agent/remote/RecordingsContext.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index 45a65f65..3fa14041 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -116,11 +116,12 @@ public void handle(HttpExchange exchange) throws IOException { } case "DELETE": id = extractId(exchange); - if (id < 0) { + if (id >= 0) { handleDelete(exchange, id); } else { exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); } + break; default: log.warn("Unknown request method {}", mtd); exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); From 8cc9a879196de5d63d4885ae3abb59c91ab777c8 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 15:01:19 -0400 Subject: [PATCH 08/11] fixup! add handling for stopping recording --- src/main/java/io/cryostat/agent/remote/RecordingsContext.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index 3fa14041..cb1cbe3d 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -109,11 +109,12 @@ public void handle(HttpExchange exchange) throws IOException { break; case "PATCH": id = extractId(exchange); - if (id < 0) { + if (id >= 0) { handleStop(exchange, id); } else { exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); } + break; case "DELETE": id = extractId(exchange); if (id >= 0) { From e4df5266dc95cb1d3d1ed76fbca4e27f8a9b20dc Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Mon, 31 Jul 2023 15:07:33 -0400 Subject: [PATCH 09/11] only allow GET requests if write-operations are not enabled --- src/main/java/io/cryostat/agent/remote/RecordingsContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index cb1cbe3d..b855302f 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -220,9 +220,9 @@ private void sendHeader(HttpExchange exchange, int status) { } private boolean ensureMethodAccepted(HttpExchange exchange) throws IOException { - Set blocked = Set.of("POST"); + Set alwaysAllowed = Set.of("GET"); String mtd = exchange.getRequestMethod(); - boolean restricted = blocked.contains(mtd); + boolean restricted = !alwaysAllowed.contains(mtd); if (!restricted) { return true; } From 0bcaf25ec30a18a8f16c2eafae3802912a07b0d5 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Wed, 9 Aug 2023 09:41:40 -0400 Subject: [PATCH 10/11] extract HTTP response body length constants --- .../java/io/cryostat/agent/WebServer.java | 12 ++++++---- .../agent/remote/EventTemplatesContext.java | 5 +++-- .../agent/remote/EventTypesContext.java | 8 ++++--- .../cryostat/agent/remote/MBeanContext.java | 6 +++-- .../agent/remote/RecordingsContext.java | 22 ++++++++++--------- .../cryostat/agent/remote/RemoteContext.java | 4 ++++ 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/cryostat/agent/WebServer.java b/src/main/java/io/cryostat/agent/WebServer.java index 1d2f78c6..bf48374c 100644 --- a/src/main/java/io/cryostat/agent/WebServer.java +++ b/src/main/java/io/cryostat/agent/WebServer.java @@ -163,7 +163,8 @@ private HttpHandler wrap(HttpHandler handler) { handler.handle(x); } catch (Exception e) { log.error("Unhandled exception", e); - x.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, 0); + x.sendResponseHeaders( + HttpStatus.SC_INTERNAL_SERVER_ERROR, RemoteContext.BODY_LENGTH_NONE); x.close(); } }; @@ -184,15 +185,18 @@ public void handle(HttpExchange exchange) throws IOException { case "POST": synchronized (WebServer.this.credentials) { executor.execute(registration.get()::tryRegister); - exchange.sendResponseHeaders(HttpStatus.SC_NO_CONTENT, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_NO_CONTENT, RemoteContext.BODY_LENGTH_NONE); } break; case "GET": - exchange.sendResponseHeaders(HttpStatus.SC_NO_CONTENT, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_NO_CONTENT, RemoteContext.BODY_LENGTH_NONE); break; default: log.warn("Unknown request method {}", mtd); - exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_METHOD_NOT_ALLOWED, RemoteContext.BODY_LENGTH_NONE); break; } } finally { diff --git a/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java b/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java index bf12b1e9..53aa3bed 100644 --- a/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java +++ b/src/main/java/io/cryostat/agent/remote/EventTemplatesContext.java @@ -53,7 +53,7 @@ public void handle(HttpExchange exchange) throws IOException { switch (mtd) { case "GET": try { - exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); + exchange.sendResponseHeaders(HttpStatus.SC_OK, BODY_LENGTH_UNKNOWN); try (OutputStream response = exchange.getResponseBody()) { FlightRecorderMXBean bean = ManagementFactory.getPlatformMXBean(FlightRecorderMXBean.class); @@ -69,7 +69,8 @@ public void handle(HttpExchange exchange) throws IOException { break; default: log.warn("Unknown request method {}", mtd); - exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_METHOD_NOT_ALLOWED, BODY_LENGTH_NONE); break; } } finally { diff --git a/src/main/java/io/cryostat/agent/remote/EventTypesContext.java b/src/main/java/io/cryostat/agent/remote/EventTypesContext.java index fb1f90cb..620f5315 100644 --- a/src/main/java/io/cryostat/agent/remote/EventTypesContext.java +++ b/src/main/java/io/cryostat/agent/remote/EventTypesContext.java @@ -59,17 +59,19 @@ public void handle(HttpExchange exchange) throws IOException { events.addAll(getEventTypes()); } catch (Exception e) { log.error("events serialization failure", e); - exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, 0); + exchange.sendResponseHeaders( + HttpStatus.SC_INTERNAL_SERVER_ERROR, BODY_LENGTH_NONE); break; } - exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); + exchange.sendResponseHeaders(HttpStatus.SC_OK, BODY_LENGTH_UNKNOWN); try (OutputStream response = exchange.getResponseBody()) { mapper.writeValue(response, events); } break; default: log.warn("Unknown request method {}", mtd); - exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_METHOD_NOT_ALLOWED, BODY_LENGTH_NONE); break; } } finally { diff --git a/src/main/java/io/cryostat/agent/remote/MBeanContext.java b/src/main/java/io/cryostat/agent/remote/MBeanContext.java index 7ee21456..b8af5143 100644 --- a/src/main/java/io/cryostat/agent/remote/MBeanContext.java +++ b/src/main/java/io/cryostat/agent/remote/MBeanContext.java @@ -70,7 +70,8 @@ public void handle(HttpExchange exchange) throws IOException { case "GET": try { MBeanMetrics metrics = getMBeanMetrics(); - exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); + exchange.sendResponseHeaders( + HttpStatus.SC_OK, RemoteContext.BODY_LENGTH_UNKNOWN); try (OutputStream response = exchange.getResponseBody()) { mapper.writeValue(response, metrics); } @@ -80,7 +81,8 @@ public void handle(HttpExchange exchange) throws IOException { break; default: log.warn("Unknown request method {}", mtd); - exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_METHOD_NOT_ALLOWED, RemoteContext.BODY_LENGTH_NONE); break; } } finally { diff --git a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java index b855302f..a59f2060 100644 --- a/src/main/java/io/cryostat/agent/remote/RecordingsContext.java +++ b/src/main/java/io/cryostat/agent/remote/RecordingsContext.java @@ -101,7 +101,8 @@ public void handle(HttpExchange exchange) throws IOException { if (id == Integer.MIN_VALUE) { handleGetList(exchange); } else { - exchange.sendResponseHeaders(HttpStatus.SC_NOT_IMPLEMENTED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_NOT_IMPLEMENTED, BODY_LENGTH_NONE); } break; case "POST": @@ -112,7 +113,7 @@ public void handle(HttpExchange exchange) throws IOException { if (id >= 0) { handleStop(exchange, id); } else { - exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); } break; case "DELETE": @@ -120,12 +121,13 @@ public void handle(HttpExchange exchange) throws IOException { if (id >= 0) { handleDelete(exchange, id); } else { - exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); } break; default: log.warn("Unknown request method {}", mtd); - exchange.sendResponseHeaders(HttpStatus.SC_METHOD_NOT_ALLOWED, -1); + exchange.sendResponseHeaders( + HttpStatus.SC_METHOD_NOT_ALLOWED, BODY_LENGTH_NONE); break; } } finally { @@ -144,7 +146,7 @@ private static int extractId(HttpExchange exchange) throws IOException { private void handleGetList(HttpExchange exchange) { try (OutputStream response = exchange.getResponseBody()) { List recordings = getRecordings(); - exchange.sendResponseHeaders(HttpStatus.SC_OK, 0); + exchange.sendResponseHeaders(HttpStatus.SC_OK, BODY_LENGTH_UNKNOWN); mapper.writeValue(response, recordings); } catch (Exception e) { log.error("recordings serialization failure", e); @@ -155,11 +157,11 @@ private void handleStart(HttpExchange exchange) throws IOException { try (InputStream body = exchange.getRequestBody()) { StartRecordingRequest req = mapper.readValue(body, StartRecordingRequest.class); if (!req.isValid()) { - exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1); + exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE); return; } SerializableRecordingDescriptor recording = startRecording(req); - exchange.sendResponseHeaders(HttpStatus.SC_CREATED, 0); + exchange.sendResponseHeaders(HttpStatus.SC_CREATED, BODY_LENGTH_UNKNOWN); try (OutputStream response = exchange.getResponseBody()) { mapper.writeValue(response, recording); } @@ -171,7 +173,7 @@ private void handleStart(HttpExchange exchange) throws IOException { | InvalidXmlException | IOException e) { log.error("Failed to start recording", e); - exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, -1); + exchange.sendResponseHeaders(HttpStatus.SC_INTERNAL_SERVER_ERROR, BODY_LENGTH_NONE); } } @@ -213,7 +215,7 @@ private void invokeOnRecording(HttpExchange exchange, long id, Consumer Date: Mon, 14 Aug 2023 11:27:21 -0400 Subject: [PATCH 11/11] cleanup --- src/main/java/io/cryostat/agent/remote/MBeanContext.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/cryostat/agent/remote/MBeanContext.java b/src/main/java/io/cryostat/agent/remote/MBeanContext.java index b8af5143..26d466e9 100644 --- a/src/main/java/io/cryostat/agent/remote/MBeanContext.java +++ b/src/main/java/io/cryostat/agent/remote/MBeanContext.java @@ -70,8 +70,7 @@ public void handle(HttpExchange exchange) throws IOException { case "GET": try { MBeanMetrics metrics = getMBeanMetrics(); - exchange.sendResponseHeaders( - HttpStatus.SC_OK, RemoteContext.BODY_LENGTH_UNKNOWN); + exchange.sendResponseHeaders(HttpStatus.SC_OK, BODY_LENGTH_UNKNOWN); try (OutputStream response = exchange.getResponseBody()) { mapper.writeValue(response, metrics); } @@ -82,7 +81,7 @@ public void handle(HttpExchange exchange) throws IOException { default: log.warn("Unknown request method {}", mtd); exchange.sendResponseHeaders( - HttpStatus.SC_METHOD_NOT_ALLOWED, RemoteContext.BODY_LENGTH_NONE); + HttpStatus.SC_METHOD_NOT_ALLOWED, BODY_LENGTH_NONE); break; } } finally {