Minor fixes and improvements

Based on code review.
This commit is contained in:
cedb 2022-11-02 11:20:51 -04:00
parent 0ce9ddb220
commit 7a2a32ee3e
2 changed files with 21 additions and 26 deletions

View file

@ -137,7 +137,11 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
if (httpAddress != null) { if (httpAddress != null) {
final var address = IOUtils.parseInetSocketAddress(httpAddress); final var address = IOUtils.parseInetSocketAddress(httpAddress);
final var handler = new HttpServerHandler(address, m); final var handler = new HttpServerHandler(address, m);
handler.init(); try {
handler.init();
} catch (IOException ex) {
throw new IOErrorException("Failed to initialize HTTP Server", ex);
}
} }
final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system")); final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system"));
if (isDbusSystem) { if (isDbusSystem) {
@ -214,7 +218,11 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
if (httpAddress != null) { if (httpAddress != null) {
final var address = IOUtils.parseInetSocketAddress(httpAddress); final var address = IOUtils.parseInetSocketAddress(httpAddress);
final var handler = new HttpServerHandler(address, c); final var handler = new HttpServerHandler(address, c);
handler.init(); try {
handler.init();
} catch (IOException ex) {
throw new IOErrorException("Failed to initialize HTTP Server", ex);
}
} }
final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system")); final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system"));
if (isDbusSystem) { if (isDbusSystem) {

View file

@ -27,29 +27,23 @@ public class HttpServerHandler {
private final InetSocketAddress address; private final InetSocketAddress address;
private final Manager m; private final SignalJsonRpcCommandHandler commandHandler;
private final MultiAccountManager c;
public HttpServerHandler(final InetSocketAddress address, final Manager m) { public HttpServerHandler(final InetSocketAddress address, final Manager m) {
this.address = address; this.address = address;
this.m = m; commandHandler = new SignalJsonRpcCommandHandler(m, Commands::getCommand);
this.c = null;
} }
public HttpServerHandler(final InetSocketAddress address, final MultiAccountManager c) { public HttpServerHandler(final InetSocketAddress address, final MultiAccountManager c) {
this.address = address; this.address = address;
this.m = null; commandHandler = new SignalJsonRpcCommandHandler(c, Commands::getCommand);
this.c = c;
} }
public void init() { public void init() throws IOException {
try { logger.info("Starting server on " + address.toString());
logger.info("Starting server on port " + address.toString()); final var server = HttpServer.create(address, 0);
final var server = HttpServer.create(new InetSocketAddress(address.getPort()), 0);
server.setExecutor(Executors.newFixedThreadPool(10)); server.setExecutor(Executors.newFixedThreadPool(10));
server.createContext("/api/v1/rpc", httpExchange -> { server.createContext("/api/v1/rpc", httpExchange -> {
@ -59,16 +53,13 @@ public class HttpServerHandler {
return; return;
} }
if (!"application/json".equals(httpExchange.getRequestHeaders().getFirst("Content-Type"))) {
sendResponse(415, null, httpExchange);
return;
}
try { try {
final SignalJsonRpcCommandHandler commandHandler;
if (c != null) {
commandHandler = new SignalJsonRpcCommandHandler(c, Commands::getCommand);
} else {
commandHandler = new SignalJsonRpcCommandHandler(m, Commands::getCommand);
}
final Object[] result = {null}; final Object[] result = {null};
final var jsonRpcSender = new JsonRpcSender(s -> { final var jsonRpcSender = new JsonRpcSender(s -> {
if (result[0] != null) { if (result[0] != null) {
@ -99,10 +90,6 @@ public class HttpServerHandler {
server.start(); server.start();
} catch (Throwable ex) {
ex.printStackTrace();
}
} }
private void sendResponse(int status, Object response, HttpExchange httpExchange) throws IOException { private void sendResponse(int status, Object response, HttpExchange httpExchange) throws IOException {