fix the wrapperException bug in DesktopEngine's executeContract and finish the reponse check

This commit is contained in:
haoeliu@foxmail.com 2022-12-11 12:02:27 +08:00
parent 51ee18928b
commit 1a4bf502bb
3 changed files with 63 additions and 24 deletions

View File

@ -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);

View File

@ -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;
}
}
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(respCode != null) msg.header.parameters.response = respCode;
}
}
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);
}
}
}

View File

@ -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();
}