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

View file

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