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
|
* 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);
|
||||||
|
|
|
@ -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,10 +54,11 @@ public class HttpServerHandler {
|
||||||
|
|
||||||
server.createContext("/api/v1/rpc", httpExchange -> {
|
server.createContext("/api/v1/rpc", httpExchange -> {
|
||||||
|
|
||||||
|
if (!"POST".equals(httpExchange.getRequestMethod())) {
|
||||||
|
sendResponse(405, null, httpExchange);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!"POST".equals(httpExchange.getRequestMethod())) {
|
|
||||||
throw new HttpServerException(405, "Method not supported.");
|
|
||||||
}
|
|
||||||
|
|
||||||
final SignalJsonRpcCommandHandler commandHandler;
|
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) {
|
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);
|
||||||
|
|
|
@ -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