From 1a4bf502bba7416e9af850b72f336208fa1a9c04 Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Sun, 11 Dec 2022 12:02:27 +0800 Subject: [PATCH] fix the wrapperException bug in DesktopEngine's executeContract and finish the reponse check --- .../org/bdware/sc/engine/DesktopEngine.java | 4 +- .../bdware/sc/engine/hook/DOOPHandler.java | 81 ++++++++++++++----- .../bdware/sc/handler/DOOPRequestHandler.java | 2 +- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 12e82d9..416add6 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -581,9 +581,7 @@ public class DesktopEngine extends JSEngine { } catch (ScriptException e) { Throwable cause = e.getCause(); e.printStackTrace(); - return new ContractResult( - Status.Exception, - new JsonPrimitive(extractException(e.getMessage(), extract(cn, cause)))); + return wrapperException(e, fun); } catch (Throwable e) { ByteArrayOutputStream bo1 = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bo1); diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java index 4064e95..b7f1090 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java @@ -60,7 +60,7 @@ public class DOOPHandler implements AnnotationHook { // set doipMsgPackerArg struct's params doipMsgPackerArg.setSource("http"); - doipMsgPackerArg.rawDoipMsg = httpInputConvertContractRequestToDoipMessage(httpReq); + doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq); } argPacks.arg = doipMsgPackerArg; @@ -75,13 +75,7 @@ public class DOOPHandler implements AnnotationHook { // get args rules and validate http args JsonElement httpArgsRules = getRulesForHTTPRequest(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs); - visitor.visit(httpArgsRules); - if (!visitor.getStatus()) { - JsonObject jo = new JsonObject(); - jo.addProperty("msg", visitor.getException()); - jo.addProperty("code", visitor.errorCode); - throw new ScriptReturnException(jo); - } + validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor); } public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) { @@ -89,13 +83,13 @@ public class DOOPHandler implements AnnotationHook { case Hello: case Delete: case ListOps: - return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}}"); + return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}"); case Retrieve: return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}"); case Create: return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}"); case Update: - return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}, \"!body\":\"string\"}"); + return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}"); case Search: return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"query\":\"string\", \"pageNum\":\"int\", \"pageSize\":\"int\", \"type\":\"string\"}}}"); case Extension: @@ -105,7 +99,25 @@ public class DOOPHandler implements AnnotationHook { } } - public static DoipMessage httpInputConvertContractRequestToDoipMessage(ContractRequest httpReq) { + public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) { + switch (basicOperations) { + case Hello: + case Retrieve: + case Create: + case Update: + case Search: + case ListOps: + return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"!body\":\"string\"}"); + case Delete: + return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"body\":\"string\"}"); + case Extension: + case Unknown: + default: + return null; + } + } + + public static DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) { BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction()); JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); // taking Extension into consideration @@ -179,17 +191,46 @@ public class DOOPHandler implements AnnotationHook { return doipMessage; } - public static DoipMessage doipOuputConvertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) { - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + public static DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) { + BasicOperations curOp = funcNameToDoipOperations.get(fn.getFunctionName()); + JsonObject jsonParams = jsonResponse.getAsJsonObject(); - String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString(); - DoipResponseCode responseCode = DoipResponseCode.UnKnownError; - for (DoipResponseCode resp : DoipResponseCode.values()) { - if (resp.toString().equals(responseCodeStr)) responseCode = resp; + // validate json response + JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); + ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse); + validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); + + JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; + JsonObject body = jsonParams.get("body") != null ? jsonParams.get("body").getAsJsonObject() : null; + + if(header != null) { + JsonObject respCodeJson = header.get("response") != null ? header.get("response").getAsJsonObject() : null; + DoipResponseCode respCode = null; + if(respCodeJson != null) { + for (DoipResponseCode responseCode : DoipResponseCode.values()) { + if(responseCode.toString().equals(respCodeJson.toString())) { + respCode = responseCode; + } + } + + if(respCode != null) msg.header.parameters.response = respCode; + } } - DoipMessage doipRet = builder.createResponse(responseCode, msg).create();; - doipRet.body.encodedData = jsonElementRet.getAsJsonObject().get("body").getAsJsonObject().get("encodedData").getAsString().getBytes(StandardCharsets.UTF_8); - return doipRet; + if(body != null) { + msg.body.encodedData = body.getAsString().getBytes(StandardCharsets.UTF_8); + } + + return msg; + } + + public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, ArgSchemaVisitor visitor) { + visitor.visit(jsonElement); + if (!visitor.getStatus()) { + JsonObject jo = new JsonObject(); + jo.addProperty("msg", visitor.getException()); + jo.addProperty("code", visitor.errorCode); + throw new ScriptReturnException(jo); + } } } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 59096de..421f4f2 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -124,7 +124,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler try { // 改变调用的函数 + 构造DoipMessagePacker Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); - return DOOPHandler.doipOuputConvertJsonElementToDoipMessage((JsonElement) ret, msg); + return DOOPHandler.convertJsonResponseToDoipMessage(fn, (JsonElement) ret, msg); } catch (Exception e) { e.printStackTrace(); }