From fb8305031ee706e5e8dd5dfd39796ac8233658d4 Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Thu, 2 Mar 2023 18:53:40 +0800 Subject: [PATCH] add JoinInfo support, the DOOPAfterExecHandler has Todo to deal with --- .../java/org/bdware/sc/ContractProcess.java | 9 ++- .../java/org/bdware/sc/engine/JSONTool.java | 7 ++- .../sc/engine/hook/DOOPAfterExecHandler.java | 58 +++++++++++-------- .../sc/engine/hook/DOOPBeforeExecHandler.java | 6 +- .../bdware/sc/handler/DOOPRequestHandler.java | 2 +- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index 25c0941..4edbac6 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -570,11 +570,6 @@ public class ContractProcess { fun.appendBeforeInvokeHandler(argSchemaHandler); } - if (fun.isDoipOperation()) { - fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler()); - fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler()); - } - if (fun.isExport()) { //if(fun.annotations...) AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class); @@ -601,6 +596,10 @@ public class ContractProcess { fun.appendAfterInvokeHandler(new HomomorphicDecryptHandler(fun)); } + if (fun.isDoipOperation()) { + fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler()); + fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler()); + } } } diff --git a/src/main/java/org/bdware/sc/engine/JSONTool.java b/src/main/java/org/bdware/sc/engine/JSONTool.java index 5fec4f7..e172bd9 100644 --- a/src/main/java/org/bdware/sc/engine/JSONTool.java +++ b/src/main/java/org/bdware/sc/engine/JSONTool.java @@ -1,16 +1,17 @@ package org.bdware.sc.engine; import com.google.gson.*; -import jdk.nashorn.internal.runtime.Context; import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.sc.util.JsonUtil; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; import wrp.jdk.nashorn.internal.objects.NativeArray; import wrp.jdk.nashorn.internal.runtime.PropertyMap; -import wrp.jdk.nashorn.internal.runtime.ScriptObject; import wrp.jdk.nashorn.internal.scripts.JO; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; public class JSONTool { public static JsonElement convertMirrorToJson(Object ret2) { diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java index 3720657..a6d3255 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java @@ -10,6 +10,7 @@ import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.JSEngine; import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.boundry.ScriptReturnException; +import org.bdware.sc.engine.JSONTool; import org.bdware.sc.entity.DoipMessagePacker; import org.bdware.sc.node.AnnotationHook; import org.bdware.sc.node.ArgPacks; @@ -53,38 +54,47 @@ public class DOOPAfterExecHandler implements AnnotationHook { Object originDoipMsgPacker = argPacks.arg; DoipMessage originDoipMsg = null; if (originDoipMsgPacker instanceof DoipMessagePacker) { - originDoipMsg = ((DoipMessagePacker) originDoipMsgPacker).rawDoipMsg; - } + DoipMessagePacker doipMessagePacker = (DoipMessagePacker) originDoipMsgPacker; + originDoipMsg = doipMessagePacker.rawDoipMsg; - JsonElement jsonElementRes = JsonUtil.fromJson(JsonUtil.toJson(argPacks.ret), JsonElement.class); - JsonObject jsonObjectRes = JsonUtil.fromJson(JsonUtil.toJson(argPacks.ret), JsonObject.class); + // if http, directly return + if (doipMessagePacker.source.equals("http")) { + return argPacks; + } else { + // pack + JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject(); - // validate json response - JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); - ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonElementRes); - validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); + // validate json response + JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); + ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes); + validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); - JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null; - String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null; + JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null; + String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null; - if(header != null) { - String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; - if(headerRespCode != null) { - for (DoipResponseCode responseCode : DoipResponseCode.values()) { - if(responseCode.toString().equals(headerRespCode)) { - originDoipMsg.header.parameters.response = responseCode; - break; + originDoipMsg.header.setIsRequest(false); + if(header != null) { + String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; + if(headerRespCode != null) { + originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode); + } + + // todo 其他的规则的添加 + if(header.get("parameters") != null && header.get("parameters").getAsJsonObject().get("attributes") != null) { + originDoipMsg.header.parameters.attributes = header.get("parameters").getAsJsonObject().get("attributes").getAsJsonObject(); } } + + if(body != null) { + originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8); + } + + argPacks.ret = originDoipMsg; + return argPacks; } + } else { + return argPacks; } - - if(body != null) { - originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8); - } - - argPacks.ret = originDoipMsg; - return argPacks; } diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java index 456ce2f..0437d1f 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java @@ -102,7 +102,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook { JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); // taking Extension into consideration JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; - JsonObject body = jsonParams.get("body") != null ? jsonParams.get("body").getAsJsonObject() : null; + String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; DoipMessage doipMessage = null; switch(httpOperation) { case Hello: @@ -114,7 +114,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook { case Update: doipMessage = new DoipMessageFactory.DoipMessageBuilder() .createRequest(header.get("identifier").getAsString(), httpOperation.getName()) - .setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)) + .setBody(body.getBytes(StandardCharsets.UTF_8)) .create(); break; case Search: @@ -153,7 +153,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook { } if(body != null) { - extensionBuilder.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)); + extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8)); } doipMessage = extensionBuilder.create(); diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 4a31b11..7ab064e 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -141,7 +141,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler try { // 改变调用的函数 + 构造DoipMessagePacker Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); - DoipMessage finalDoipMsg = JsonUtil.fromJson(JsonUtil.toJson(ret), DoipMessage.class); + DoipMessage finalDoipMsg = (DoipMessage) ret; finalDoipMsg.requestID = msg.requestID; if(finalDoipMsg.header.parameters == null) { finalDoipMsg.header.parameters = new HeaderParameter("", "");