From c9d74709931828acd3c6a930a16289d7042e0c1d Mon Sep 17 00:00:00 2001 From: Carl Bingel Date: Tue, 18 Feb 2020 21:27:49 +0100 Subject: [PATCH] changed from string type status code to integer and errorMessage string --- .../JsonEventLoopReceiveMessageHandler.java | 4 +- .../signal/commands/JsonEventLoopCommand.java | 39 +++++++++-------- .../commands/JsonEventLoopStatusReport.java | 42 ++++++++++++++----- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/asamk/signal/JsonEventLoopReceiveMessageHandler.java b/src/main/java/org/asamk/signal/JsonEventLoopReceiveMessageHandler.java index ea56177d..1f6de07e 100644 --- a/src/main/java/org/asamk/signal/JsonEventLoopReceiveMessageHandler.java +++ b/src/main/java/org/asamk/signal/JsonEventLoopReceiveMessageHandler.java @@ -41,7 +41,7 @@ public class JsonEventLoopReceiveMessageHandler implements Manager.ReceiveMessag //ObjectNode result = jsonProcessor.createObjectNode(); JsonEventLoopStatusReport resp = null; if (exception != null) { - resp = new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopReceiveMessageHandler::handleMessage: Exception: " + exception.toString()); + resp = new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopReceiveMessageHandler::handleMessage: Exception: " + exception.toString()); //result.putPOJO("error", new JsonError(exception)); } if (envelope != null) { @@ -49,7 +49,7 @@ public class JsonEventLoopReceiveMessageHandler implements Manager.ReceiveMessag resp = new JsonEventLoopStatusReport( new JsonMessageEnvelope(envelope, content)); } if(resp == null) { - new JsonEventLoopStatusReport( "error", null, "JsonEventLoopReceiveMessageHandler::handleMessage: both exceptino and envelope is null!").emit(); + new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopReceiveMessageHandler::handleMessage: both exception and envelope is null!").emit(); } else { if( this.responseEmitter != null) { this.responseEmitter.emit( resp); diff --git a/src/main/java/org/asamk/signal/commands/JsonEventLoopCommand.java b/src/main/java/org/asamk/signal/commands/JsonEventLoopCommand.java index 234fe01e..aa1d1bec 100644 --- a/src/main/java/org/asamk/signal/commands/JsonEventLoopCommand.java +++ b/src/main/java/org/asamk/signal/commands/JsonEventLoopCommand.java @@ -98,9 +98,9 @@ public class JsonEventLoopCommand implements LocalCommand { try { m.sendMessage( body_text, attachments, recipientNumber); } catch( IOException e) { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "IOException: " + e.toString() ); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "IOException: " + e.toString() ); } catch( EncapsulatedExceptions e) { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "EncapsulatedException: " + e.toString() ); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "EncapsulatedException: " + e.toString() ); } } else if( reqObj.get("recipientGroupID") != null) { // send message to group @@ -109,17 +109,17 @@ public class JsonEventLoopCommand implements LocalCommand { byte[] recipientGroupID_bytearray = Util.decodeGroupId(recipientGroupID); m.sendGroupMessage( body_text, attachments, recipientGroupID_bytearray); } catch( GroupIdFormatException e) { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "GroupIdFormatException: " + e.toString() ); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_GROUP_ID_FORMAT_ERROR, "GroupIdFormatException: " + e.toString() ); } catch( IOException e) { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "IOException: " + e.toString() ); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "IOException: " + e.toString() ); } catch( EncapsulatedExceptions e) { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "EncapsulatedException: " + e.toString() ); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_SEND_ERROR, "EncapsulatedException: " + e.toString() ); } } else { - return new JsonEventLoopStatusReport( "send_message", reqID, "error", "Neither recipientNumber or recipientGroupID present in request, nothing to do"); + return new JsonEventLoopStatusReport( "send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_RECIPIENT_FORMAT_ERROR, "Neither recipientNumber or recipientGroupID present in request, nothing to do"); } - return new JsonEventLoopStatusReport("send_message", reqID, "ok"); + return new JsonEventLoopStatusReport("send_message", reqID, JsonEventLoopStatusReport.STATUSCODE_OK); } /** @@ -140,10 +140,10 @@ public class JsonEventLoopCommand implements LocalCommand { JsonNode reqID = reqObj.get("reqID"); switch(reqType) { case "alive": - resp = new JsonEventLoopStatusReport("alive", reqID, "ok"); + resp = new JsonEventLoopStatusReport("alive", reqID, JsonEventLoopStatusReport.STATUSCODE_OK); break; case "exit": - resp = new JsonEventLoopStatusReport("exit", reqID, "ok"); + resp = new JsonEventLoopStatusReport("exit", reqID, JsonEventLoopStatusReport.STATUSCODE_OK); //System.exit(0); exitNow = true; break; @@ -157,20 +157,20 @@ public class JsonEventLoopCommand implements LocalCommand { resp = this.list_contacts( reqObj, reqID); break; default: - resp = new JsonEventLoopStatusReport("error", reqID, "error", "Unknown reqType '" + reqType + "'"); + resp = new JsonEventLoopStatusReport("error", reqID, JsonEventLoopStatusReport.STATUSCODE_REQUEST_FORMAT_ERROR, "Unknown reqType '" + reqType + "'"); System.err.println("JsonEvtRequestHandler: ERROR: Unknown reqType '" + reqType + "'"); break; } } else { - resp = new JsonEventLoopStatusReport("error", null, "error", "reqType attribute missing"); + resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_REQUEST_FORMAT_ERROR, "reqType attribute missing"); System.err.println("JsonEvtRequestHandler: ERROR: reqType attribute is missing in request"); } } else { - resp = new JsonEventLoopStatusReport("error", null, "error", "Failed to parse JSON, reqObj is NULL"); + resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_JSON_PARSE_ERROR, "Failed to parse JSON, reqObj is NULL"); System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, reqObj is NULL"); } } catch( IOException e) { - resp = new JsonEventLoopStatusReport("error", null, "error", "Failed to parse JSON: " + e.toString()); + resp = new JsonEventLoopStatusReport("error", null, JsonEventLoopStatusReport.STATUSCODE_JSON_PARSE_ERROR, "Failed to parse JSON: " + e.toString()); System.err.println( "JsonEvtRequestHandler: Failed to parse JSON, text='" + textRow + "', IOException: " + e.toString()); } @@ -218,9 +218,6 @@ public class JsonEventLoopCommand implements LocalCommand { - - - @Override public void attachToSubparser(final Subparser subparser) { subparser.addArgument("--ignore-attachments") @@ -232,7 +229,7 @@ public class JsonEventLoopCommand implements LocalCommand { public int handleCommand(final Namespace ns, final Manager m) { if (!m.isRegistered()) { //System.err.println("User is not registered."); - new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopCommand::handleCommand: User is not registered, aborting"); + new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_USER_NOT_REGISTERED_ERROR, "JsonEventLoopCommand::handleCommand: User is not registered, aborting"); return 1; } @@ -242,6 +239,12 @@ public class JsonEventLoopCommand implements LocalCommand { Thread jsonStdinReaderThread = new Thread( reader); jsonStdinReaderThread.start(); + // Emit metadata response message at startup + JsonEventLoopStatusReport meta_resp = new JsonEventLoopStatusReport("metadata", null, JsonEventLoopStatusReport.STATUSCODE_OK); + meta_resp.apiVer = JsonEventLoopStatusReport.CURRENT_APIVER; + meta_resp.attachmentsPath = m.getAttachmentsPath(); + meta_resp.emit(); + // start JsonReceiveMessageHandler and let it run forever boolean ignoreAttachments = ns.getBoolean("ignore_attachments"); try { @@ -254,7 +257,7 @@ public class JsonEventLoopCommand implements LocalCommand { return 0; } catch (IOException e) { System.err.println("Error while receiving messages: " + e.getMessage()); - new JsonEventLoopStatusReport( "error", null, "error", "JsonEventLoopCommand::handleCommand: Error while receiving messages: " + e.getMessage()).emit(); + new JsonEventLoopStatusReport( "error", null, JsonEventLoopStatusReport.STATUSCODE_GENERIC_ERROR, "JsonEventLoopCommand::handleCommand: Error while receiving messages: " + e.getMessage()).emit(); return 3; } catch (AssertionError e) { handleAssertionError(e); diff --git a/src/main/java/org/asamk/signal/commands/JsonEventLoopStatusReport.java b/src/main/java/org/asamk/signal/commands/JsonEventLoopStatusReport.java index 8fa168dc..87efe204 100644 --- a/src/main/java/org/asamk/signal/commands/JsonEventLoopStatusReport.java +++ b/src/main/java/org/asamk/signal/commands/JsonEventLoopStatusReport.java @@ -13,15 +13,37 @@ import com.fasterxml.jackson.databind.*; @JsonInclude(Include.NON_NULL) public class JsonEventLoopStatusReport { + + public static final int CURRENT_APIVER = 2; - public int apiVer = 2; + // statusCode constants + public static final int STATUSCODE_UNKNOWN = -1; + public static final int STATUSCODE_OK = 0; + public static final int STATUSCODE_GENERIC_ERROR = 1; + public static final int STATUSCODE_SEND_ERROR = 2; + public static final int STATUSCODE_GROUP_ID_FORMAT_ERROR = 3; + public static final int STATUSCODE_RECIPIENT_FORMAT_ERROR = 4; + public static final int STATUSCODE_REQUEST_FORMAT_ERROR = 5; + public static final int STATUSCODE_JSON_PARSE_ERROR = 6; + public static final int STATUSCODE_USER_NOT_REGISTERED_ERROR = 7; + + // members + @JsonInclude(Include.ALWAYS) public String respType; @JsonInclude(Include.ALWAYS) public JsonNode reqID; - public String status; - public String message; + @JsonInclude(Include.ALWAYS) + public int statusCode = STATUSCODE_UNKNOWN; + @JsonInclude(Include.NON_NULL) + public String errorMessage; + @JsonInclude(Include.NON_NULL) public JsonMessageEnvelope envelope; + @JsonInclude(Include.NON_NULL) public JsonNode data; + @JsonInclude(Include.NON_DEFAULT) + public int apiVer = 0; + @JsonInclude(Include.NON_NULL) + public String attachmentsPath; /** @@ -32,13 +54,13 @@ public class JsonEventLoopStatusReport { public JsonEventLoopStatusReport( JsonMessageEnvelope en) { this.envelope = en; this.respType = "envelope"; - this.status = "ok"; + this.statusCode = STATUSCODE_OK; this.reqID = null; } public JsonEventLoopStatusReport( String respType, JsonNode reqID, JsonNode data) { this.respType = respType; - this.status = "ok"; + this.statusCode = 0; this.reqID = reqID; this.data = data; } @@ -50,10 +72,10 @@ public class JsonEventLoopStatusReport { * @param reqID Request ID object as present in the request * @param status Status of the operation, should be "ok" or "error" */ - public JsonEventLoopStatusReport( String respType, JsonNode reqID, String status) { + public JsonEventLoopStatusReport( String respType, JsonNode reqID, int statusCode) { this.respType = respType; this.reqID = reqID; - this.status = status; + this.statusCode = statusCode; } /** @@ -64,11 +86,11 @@ public class JsonEventLoopStatusReport { * @param status Status of the operation, should be "ok" or "error" * @param message Message explaining what went wrong in case of status="error" */ - public JsonEventLoopStatusReport( String respType, JsonNode reqID, String status, String message) { + public JsonEventLoopStatusReport( String respType, JsonNode reqID, int statusCode, String message) { this.respType = respType; this.reqID = reqID; - this.status = status; - this.message = message; + this.statusCode = statusCode; + this.errorMessage = message; } /**