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) { } catch (ScriptException e) {
Throwable cause = e.getCause(); Throwable cause = e.getCause();
e.printStackTrace(); e.printStackTrace();
return new ContractResult( return wrapperException(e, fun);
Status.Exception,
new JsonPrimitive(extractException(e.getMessage(), extract(cn, cause))));
} catch (Throwable e) { } catch (Throwable e) {
ByteArrayOutputStream bo1 = new ByteArrayOutputStream(); ByteArrayOutputStream bo1 = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bo1); PrintStream ps = new PrintStream(bo1);

View File

@ -60,7 +60,7 @@ public class DOOPHandler implements AnnotationHook {
// set doipMsgPackerArg struct's params // set doipMsgPackerArg struct's params
doipMsgPackerArg.setSource("http"); doipMsgPackerArg.setSource("http");
doipMsgPackerArg.rawDoipMsg = httpInputConvertContractRequestToDoipMessage(httpReq); doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq);
} }
argPacks.arg = doipMsgPackerArg; argPacks.arg = doipMsgPackerArg;
@ -75,13 +75,7 @@ public class DOOPHandler implements AnnotationHook {
// get args rules and validate http args // get args rules and validate http args
JsonElement httpArgsRules = getRulesForHTTPRequest(curOp); JsonElement httpArgsRules = getRulesForHTTPRequest(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs); ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
visitor.visit(httpArgsRules); validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor);
if (!visitor.getStatus()) {
JsonObject jo = new JsonObject();
jo.addProperty("msg", visitor.getException());
jo.addProperty("code", visitor.errorCode);
throw new ScriptReturnException(jo);
}
} }
public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) { public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) {
@ -89,13 +83,13 @@ public class DOOPHandler implements AnnotationHook {
case Hello: case Hello:
case Delete: case Delete:
case ListOps: case ListOps:
return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}");
case Retrieve: case Retrieve:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}");
case Create: case Create:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}");
case Update: case Update:
return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}, \"!body\":\"string\"}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}");
case Search: case Search:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"query\":\"string\", \"pageNum\":\"int\", \"pageSize\":\"int\", \"type\":\"string\"}}}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"query\":\"string\", \"pageNum\":\"int\", \"pageSize\":\"int\", \"type\":\"string\"}}}");
case Extension: 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()); BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction());
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
// taking Extension into consideration // taking Extension into consideration
@ -179,17 +191,46 @@ public class DOOPHandler implements AnnotationHook {
return doipMessage; return doipMessage;
} }
public static DoipMessage doipOuputConvertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) { public static DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); BasicOperations curOp = funcNameToDoipOperations.get(fn.getFunctionName());
JsonObject jsonParams = jsonResponse.getAsJsonObject();
String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString(); // validate json response
DoipResponseCode responseCode = DoipResponseCode.UnKnownError; JsonElement jsonResponseRules = getRulesForJsonResponse(curOp);
for (DoipResponseCode resp : DoipResponseCode.values()) { ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse);
if (resp.toString().equals(responseCodeStr)) responseCode = resp; 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();; if(body != null) {
doipRet.body.encodedData = jsonElementRet.getAsJsonObject().get("body").getAsJsonObject().get("encodedData").getAsString().getBytes(StandardCharsets.UTF_8); msg.body.encodedData = body.getAsString().getBytes(StandardCharsets.UTF_8);
return doipRet; }
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 { try {
// 改变调用的函数 + 构造DoipMessagePacker // 改变调用的函数 + 构造DoipMessagePacker
Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }