Clean up and simplification

This commit is contained in:
cedb 2022-11-01 15:22:24 -04:00
parent 7111cad402
commit 60c40e7490
4 changed files with 8 additions and 27 deletions

View file

@ -10,7 +10,7 @@ import java.util.Map;
/**
* Namespace implementation, that has plural handling for list arguments and converts camel case keys to dashed strings
*/
final public class JsonRpcNamespace extends Namespace {
final class JsonRpcNamespace extends Namespace {
public JsonRpcNamespace(final Map<String, Object> attrs) {
super(attrs);

View file

@ -1,14 +0,0 @@
package org.asamk.signal.http;
public class HttpServerException extends RuntimeException {
private int httpStatus;
public HttpServerException(final int aHttpStatus, final String message) {
super(message);
}
public int getHttpStatus() {
return httpStatus;
}
}

View file

@ -54,10 +54,11 @@ public class HttpServerHandler {
server.createContext("/api/v1/rpc", httpExchange -> {
if (!"POST".equals(httpExchange.getRequestMethod())) {
sendResponse(405, null, httpExchange);
}
try {
if (!"POST".equals(httpExchange.getRequestMethod())) {
throw new HttpServerException(405, "Method not supported.");
}
final SignalJsonRpcCommandHandler commandHandler;
@ -87,12 +88,6 @@ public class HttpServerHandler {
}
}
catch (HttpServerException aEx) {
logger.error("Failed to process request.", aEx);
sendResponse(aEx.getHttpStatus(), JsonRpcResponse.forError(
new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
aEx.getMessage(), null), null), httpExchange);
}
catch (Throwable aEx) {
logger.error("Failed to process request.", aEx);
sendResponse(200, JsonRpcResponse.forError(
@ -112,7 +107,10 @@ public class HttpServerHandler {
private void sendResponse(int status, Object response, HttpExchange httpExchange) throws IOException {
if (response != null) {
final var byteResponse = objectMapper.writeValueAsBytes(response);
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.sendResponseHeaders(status, byteResponse.length);
httpExchange.getResponseBody().write(byteResponse);
} else {
httpExchange.sendResponseHeaders(status, 0);

View file

@ -1,3 +0,0 @@
package org.asamk.signal.http;
public record HttpSimpleResponse(String status) {}