mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-02 04:20:38 +00:00
Clean up and simplification
This commit is contained in:
parent
7111cad402
commit
60c40e7490
4 changed files with 8 additions and 27 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -54,11 +54,12 @@ public class HttpServerHandler {
|
|||
|
||||
server.createContext("/api/v1/rpc", httpExchange -> {
|
||||
|
||||
try {
|
||||
if (!"POST".equals(httpExchange.getRequestMethod())) {
|
||||
throw new HttpServerException(405, "Method not supported.");
|
||||
sendResponse(405, null, httpExchange);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
final SignalJsonRpcCommandHandler commandHandler;
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
package org.asamk.signal.http;
|
||||
|
||||
public record HttpSimpleResponse(String status) {}
|
Loading…
Add table
Add a link
Reference in a new issue