- catch (Throwable aEx) {
- logger.error("Failed to process request.", aEx);
- sendResponse(200, JsonRpcResponse.forError(
- new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
- "An internal server error has occurred.", null), null), httpExchange);
+
+ result[0] = s;
+ });
+
+ final var jsonRpcReader = new JsonRpcReader(jsonRpcSender, httpExchange.getRequestBody());
+ jsonRpcReader.readMessages((method, params) -> commandHandler.handleRequest(objectMapper, method, params),
+ response -> logger.debug("Received unexpected response for id {}", response.getId()));
+
+ if (result[0] != null) {
+ sendResponse(200, result[0], httpExchange);
+ } else {
+ sendResponse(201, null, httpExchange);
+ }
+
+ } catch (Throwable aEx) {
+ logger.error("Failed to process request.", aEx);
+ sendResponse(200,
+ JsonRpcResponse.forError(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
+ "An internal server error has occurred.",
+ null), null),
+ httpExchange);
+ }
+ }
+
+ private void handleEventsEndpoint(HttpExchange httpExchange) throws IOException {
+ if (!"/api/v1/events".equals(httpExchange.getRequestURI().getPath())) {
+ sendResponse(404, null, httpExchange);
+ return;
+ }
+ if (!"GET".equals(httpExchange.getRequestMethod())) {
+ sendResponse(405, null, httpExchange);
+ return;
+ }
+
+ try {
+ final var queryString = httpExchange.getRequestURI().getQuery();
+ final var query = queryString == null ? Map.<String, String>of() : Util.getQueryMap(queryString);
+
+ List<Manager> managers = getManagerFromQuery(query);
+ if (managers == null) {
+ sendResponse(400, null, httpExchange);
+ return;
+ }
+
+ httpExchange.getResponseHeaders().add("Content-Type", "text/event-stream");
+ httpExchange.sendResponseHeaders(200, 0);
+ final var sender = new ServerSentEventSender(httpExchange.getResponseBody());
+
+ final var shouldStop = new AtomicBoolean(false);
+ final var handlers = subscribeReceiveHandlers(managers, sender, () -> {
+ shouldStop.set(true);
+ synchronized (this) {
+ this.notifyAll();