X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/1ad0e94b640d16a8d832287362e1785c78d3ec49..c628e27d2e1372c4ed9bc4ee319b83700cd11b17:/src/main/java/org/asamk/signal/http/HttpServerHandler.java diff --git a/src/main/java/org/asamk/signal/http/HttpServerHandler.java b/src/main/java/org/asamk/signal/http/HttpServerHandler.java index b2544b25..9fb33a34 100644 --- a/src/main/java/org/asamk/signal/http/HttpServerHandler.java +++ b/src/main/java/org/asamk/signal/http/HttpServerHandler.java @@ -41,54 +41,56 @@ public class HttpServerHandler { public void init() throws IOException { - logger.info("Starting server on " + address.toString()); + logger.info("Starting server on " + address.toString()); - final var server = HttpServer.create(address, 0); - server.setExecutor(Executors.newFixedThreadPool(10)); + final var server = HttpServer.create(address, 0); + server.setExecutor(Executors.newFixedThreadPool(10)); - server.createContext("/api/v1/rpc", httpExchange -> { + server.createContext("/api/v1/rpc", httpExchange -> { - if (!"POST".equals(httpExchange.getRequestMethod())) { - sendResponse(405, null, httpExchange); - return; - } - - if (!"application/json".equals(httpExchange.getRequestHeaders().getFirst("Content-Type"))) { - sendResponse(415, null, httpExchange); - return; - } + if (!"POST".equals(httpExchange.getRequestMethod())) { + sendResponse(405, null, httpExchange); + return; + } - try { + if (!"application/json".equals(httpExchange.getRequestHeaders().getFirst("Content-Type"))) { + sendResponse(415, null, httpExchange); + return; + } - final Object[] result = {null}; - final var jsonRpcSender = new JsonRpcSender(s -> { - if (result[0] != null) { - throw new AssertionError("There should only be a single JSON-RPC response"); - } + try { - result[0] = s; - }); + final Object[] result = {null}; + final var jsonRpcSender = new JsonRpcSender(s -> { + if (result[0] != null) { + throw new AssertionError("There should only be a single JSON-RPC response"); + } - 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())); + result[0] = s; + }); - if (result[0] !=null) { - sendResponse(200, result[0], httpExchange); - } else { - sendResponse(201, null, httpExchange); - } + 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); - } - }); - server.start(); + } 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); + } + }); + + server.start(); }