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) {
final var address = IOUtils.parseInetSocketAddress(httpAddress);
final var handler = new HttpServerHandler(address, m);
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"));
if (isDbusSystem) {
@ -214,7 +218,11 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
if (httpAddress != null) {
final var address = IOUtils.parseInetSocketAddress(httpAddress);
final var handler = new HttpServerHandler(address, c);
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"));
if (isDbusSystem) {

View file

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