add JoinInfo support, the DOOPAfterExecHandler has Todo to deal with

This commit is contained in:
haoeliu@foxmail.com 2023-03-02 18:53:40 +08:00
parent a117ef4a41
commit fb8305031e
5 changed files with 46 additions and 36 deletions

View File

@ -570,11 +570,6 @@ public class ContractProcess {
fun.appendBeforeInvokeHandler(argSchemaHandler); fun.appendBeforeInvokeHandler(argSchemaHandler);
} }
if (fun.isDoipOperation()) {
fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler());
fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler());
}
if (fun.isExport()) { if (fun.isExport()) {
//if(fun.annotations...) //if(fun.annotations...)
AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class); AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class);
@ -601,6 +596,10 @@ public class ContractProcess {
fun.appendAfterInvokeHandler(new HomomorphicDecryptHandler(fun)); fun.appendAfterInvokeHandler(new HomomorphicDecryptHandler(fun));
} }
if (fun.isDoipOperation()) {
fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler());
fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler());
}
} }
} }

View File

@ -1,16 +1,17 @@
package org.bdware.sc.engine; package org.bdware.sc.engine;
import com.google.gson.*; import com.google.gson.*;
import jdk.nashorn.internal.runtime.Context;
import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
import wrp.jdk.nashorn.internal.objects.NativeArray; import wrp.jdk.nashorn.internal.objects.NativeArray;
import wrp.jdk.nashorn.internal.runtime.PropertyMap; import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.runtime.ScriptObject;
import wrp.jdk.nashorn.internal.scripts.JO; 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 class JSONTool {
public static JsonElement convertMirrorToJson(Object ret2) { public static JsonElement convertMirrorToJson(Object ret2) {

View File

@ -10,6 +10,7 @@ import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.sc.JSEngine; import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.boundry.ScriptReturnException; import org.bdware.sc.boundry.ScriptReturnException;
import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.entity.DoipMessagePacker; import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.AnnotationHook; import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks; import org.bdware.sc.node.ArgPacks;
@ -53,38 +54,47 @@ public class DOOPAfterExecHandler implements AnnotationHook {
Object originDoipMsgPacker = argPacks.arg; Object originDoipMsgPacker = argPacks.arg;
DoipMessage originDoipMsg = null; DoipMessage originDoipMsg = null;
if (originDoipMsgPacker instanceof DoipMessagePacker) { 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); // if http, directly return
JsonObject jsonObjectRes = JsonUtil.fromJson(JsonUtil.toJson(argPacks.ret), JsonObject.class); if (doipMessagePacker.source.equals("http")) {
return argPacks;
} else {
// pack
JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject();
// validate json response // validate json response
JsonElement jsonResponseRules = getRulesForJsonResponse(curOp); JsonElement jsonResponseRules = getRulesForJsonResponse(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonElementRes); ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes);
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null; JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null;
String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null; String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null;
if(header != null) { originDoipMsg.header.setIsRequest(false);
String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; if(header != null) {
if(headerRespCode != null) { String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
for (DoipResponseCode responseCode : DoipResponseCode.values()) { if(headerRespCode != null) {
if(responseCode.toString().equals(headerRespCode)) { originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode);
originDoipMsg.header.parameters.response = responseCode; }
break;
// 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;
} }

View File

@ -102,7 +102,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
// taking Extension into consideration // taking Extension into consideration
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; 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; DoipMessage doipMessage = null;
switch(httpOperation) { switch(httpOperation) {
case Hello: case Hello:
@ -114,7 +114,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
case Update: case Update:
doipMessage = new DoipMessageFactory.DoipMessageBuilder() doipMessage = new DoipMessageFactory.DoipMessageBuilder()
.createRequest(header.get("identifier").getAsString(), httpOperation.getName()) .createRequest(header.get("identifier").getAsString(), httpOperation.getName())
.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)) .setBody(body.getBytes(StandardCharsets.UTF_8))
.create(); .create();
break; break;
case Search: case Search:
@ -153,7 +153,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
} }
if(body != null) { if(body != null) {
extensionBuilder.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)); extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8));
} }
doipMessage = extensionBuilder.create(); doipMessage = extensionBuilder.create();

View File

@ -141,7 +141,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);
DoipMessage finalDoipMsg = JsonUtil.fromJson(JsonUtil.toJson(ret), DoipMessage.class); DoipMessage finalDoipMsg = (DoipMessage) ret;
finalDoipMsg.requestID = msg.requestID; finalDoipMsg.requestID = msg.requestID;
if(finalDoipMsg.header.parameters == null) { if(finalDoipMsg.header.parameters == null) {
finalDoipMsg.header.parameters = new HeaderParameter("", ""); finalDoipMsg.header.parameters = new HeaderParameter("", "");