update doop handler

This commit is contained in:
CaiHQ 2023-03-27 20:30:09 +08:00
parent 16f2d4fae1
commit 042cfae39b
7 changed files with 60 additions and 130 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group = "org.bdware.sc"
version = "1.8.1"
version = "1.8.2"
tasks.withType(JavaCompile) {
// options.compilerArgs << '-Xlint:none'
// options.compilerArgs << '-Xlint:deprecation' << "-Werror"
@ -67,7 +67,7 @@ jar {
// uncomment this when publish,
// while develop at local use "false"
configurations.runtimeClasspath.filter {
// it.getAbsolutePath().contains("/lib/")
// it.getAbsolutePath().contains("/lib/")
false
}.collect {
it.isDirectory() ? it : zipTree(it)

View File

@ -31,6 +31,7 @@ import org.bdware.sc.engine.DesktopEngine;
import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.engine.hook.*;
import org.bdware.sc.handler.ContractHandler;
import org.bdware.sc.handler.DOOPRequestHandler;
import org.bdware.sc.index.TimeSerialIndex;
import org.bdware.sc.node.*;
import org.bdware.sc.server.DoipClusterServer;
@ -80,6 +81,7 @@ public class ContractProcess {
private TimeSerialIndex logIndex;
private RocksDBUtil edion;
private String pid;
public DOOPRequestHandler doopRequestHandler;
public ContractProcess(int port, String cmi) {
handler = new ContractHandler(this);
@ -542,7 +544,7 @@ public class ContractProcess {
}
}
private void injectHandlers() {
private void injectHandlers() throws Exception {
// 正式启动
if (!this.contract.isDebug()) {
// this.engine.getResources().loadAsString("/maskConfig.json");
@ -555,6 +557,9 @@ public class ContractProcess {
}
}
}
DOOPBeforeExecHandler doopBeforeExecHandler = null;
DOOPAfterExecHandler doopAfterExecHandler = null;
doopRequestHandler = null;
for (FunctionNode fun : cn.getFunctions()) {
if (fun.isConfidential()) {
fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun));
@ -591,8 +596,12 @@ public class ContractProcess {
}
if (fun.isDoipOperation()) {
fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler());
fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler());
if (doopRequestHandler == null) {
doopRequestHandler = new DOOPRequestHandler();
}
fun.appendBeforeInvokeHandler(new DOOPBeforeExecHandler(fun.getDoipOperationInfo().operation));
fun.appendAfterInvokeHandler(new DOOPAfterExecHandler(fun.getDoipOperationInfo().operation));
doopRequestHandler.addDoipOperation(fun);
}
}
}

View File

@ -2,9 +2,6 @@ package org.bdware.sc.compiler.ap;
import org.bdware.sc.bean.DoipOperationInfo;
import org.bdware.sc.compiler.AnnotationProcessor;
import org.bdware.sc.engine.hook.DOOPAfterExecHandler;
import org.bdware.sc.engine.hook.DOOPBeforeExecHandler;
import org.bdware.sc.handler.DOOPRequestHandler;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
@ -15,23 +12,8 @@ public class DOOP extends AnnotationProcessor {
@Override
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception {
// 通过DOOP注解解析对应的值并放进对应的FunctionNode中
// 注解必须暴露出来昂
functionNode.setIsExport(true);
functionNode.setIsDoipOperation(true);
functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
// functionNode.setFunctionName(functionNode.getDoipOperationInfo().operationName);
// 维护DOOPRequestHandler
DOOPRequestHandler.createHandler();
DOOPRequestHandler.instance.addDoipOperation(functionNode);
// 维护DOOPHandler
DOOPBeforeExecHandler.createDOOPHandler();
DOOPBeforeExecHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode);
DOOPAfterExecHandler.createDOOPHandler();
DOOPAfterExecHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode);
// 维护ContractNodefunctionName is useless, use BasicOperation to map the corresponding functionNode
// contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName);
}
}

View File

@ -3,10 +3,12 @@ package org.bdware.sc.engine.hook;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.doip.codec.doipMessage.*;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.codec.doipMessage.HeaderParameter;
import org.bdware.doip.codec.doipMessage.MessageHeader;
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;
@ -16,39 +18,15 @@ import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.util.JsonUtil;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DOOPAfterExecHandler implements AnnotationHook {
public static Map<String, BasicOperations> funcNameToDoipOperations;
public static DOOPAfterExecHandler instance;
public DOOPAfterExecHandler() {
funcNameToDoipOperations = new HashMap<>();
private JsonElement jsonResponseRules;
public DOOPAfterExecHandler(BasicOperations operations) {
jsonResponseRules = getRulesForJsonResponse(operations);
}
public static DOOPAfterExecHandler createDOOPHandler() {
if(instance == null) {
instance = new DOOPAfterExecHandler();
}
return instance;
}
public void putFuncNameAndDoipOperationsMapping(FunctionNode fn) {
String basicOperationsString = fn.getDoipOperationInfo().operationType;
BasicOperations operation = BasicOperations.Unknown;
for(BasicOperations basicOperation : BasicOperations.values()) {
if(basicOperation.toString().equals(basicOperationsString)) {
operation = basicOperation;
}
}
funcNameToDoipOperations.put(fn.getFunctionName(), operation);
}
@Override
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
BasicOperations curOp = funcNameToDoipOperations.get(argPacks.request.getAction());
Object originDoipMsgPacker = argPacks.arg;
DoipMessage originDoipMsg = null;
if (originDoipMsgPacker instanceof DoipMessagePacker) {
@ -61,27 +39,25 @@ public class DOOPAfterExecHandler implements AnnotationHook {
} else {
// pack
JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject();
// 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;
// 和HTTP一样所有需要的字段自己封装校验规则也比较简单这里只做简单的包装返回即可
if(header != null) {
if (header != null) {
originDoipMsg.header = JsonUtil.fromJson(header, MessageHeader.class);
// response字段根据白皮书上的规定处于header下人为包装到parameters的response中
String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
if(headerRespCode != null) {
if (originDoipMsg.header.parameters == null) originDoipMsg.header.parameters = new HeaderParameter(null, null);
if (headerRespCode != null) {
if (originDoipMsg.header.parameters == null)
originDoipMsg.header.parameters = new HeaderParameter(null, null);
originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode);
}
}
if(body != null) {
if (body != null) {
originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
}
@ -94,7 +70,6 @@ public class DOOPAfterExecHandler implements AnnotationHook {
}
public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) {
switch (basicOperations) {
case Hello:
@ -114,31 +89,27 @@ public class DOOPAfterExecHandler implements AnnotationHook {
}
// old convert jsonResponse from argPack's ret to Doip response in doip chain logic
public static DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) {
BasicOperations curOp = funcNameToDoipOperations.get(fn.getFunctionName());
public DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) {
JsonObject jsonParams = jsonResponse.getAsJsonObject();
// 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;
String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null;
if(header != null) {
if (header != null) {
String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
if(headerRespCode != null) {
if (headerRespCode != null) {
for (DoipResponseCode responseCode : DoipResponseCode.values()) {
if(responseCode.toString().equals(headerRespCode)) {
if (responseCode.toString().equals(headerRespCode)) {
msg.header.parameters.response = responseCode;
break;
}
}
}
}
if(body != null) {
if (body != null) {
msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
}

View File

@ -12,37 +12,19 @@ import org.bdware.sc.boundry.ScriptReturnException;
import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DOOPBeforeExecHandler implements AnnotationHook {
public static Map<String, BasicOperations> funcNameToDoipOperations;
public static DOOPBeforeExecHandler instance;
public DOOPBeforeExecHandler() {
funcNameToDoipOperations = new HashMap<>();
}
public static DOOPBeforeExecHandler createDOOPHandler() {
if(instance == null) {
instance = new DOOPBeforeExecHandler();
}
private final BasicOperations httpOperation;
private JsonElement httpArgsRules;
return instance;
}
public void putFuncNameAndDoipOperationsMapping(FunctionNode fn) {
String basicOperationsString = fn.getDoipOperationInfo().operationType;
BasicOperations operation = BasicOperations.Unknown;
for(BasicOperations basicOperation : BasicOperations.values()) {
if(basicOperation.toString().equals(basicOperationsString)) {
operation = basicOperation;
}
}
funcNameToDoipOperations.put(fn.getFunctionName(), operation);
public DOOPBeforeExecHandler(BasicOperations operations) {
httpOperation = operations;
httpArgsRules = getRulesForHTTPRequest(operations);
}
@Override
@ -50,8 +32,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
Object arg = argPacks.arg;
DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker();
if (arg instanceof DoipMessagePacker){
doipMsgPackerArg = (DoipMessagePacker)arg;
if (arg instanceof DoipMessagePacker) {
doipMsgPackerArg = (DoipMessagePacker) arg;
} else {
// validate http request's params
ContractRequest httpReq = argPacks.request;
@ -66,19 +48,16 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
return argPacks;
}
public static void validateHTTPRequestArgs(ContractRequest httpReq) {
public void validateHTTPRequestArgs(ContractRequest httpReq) {
JsonElement originArgs = httpReq.getArg();
JsonElement httpArgs = JsonParser.parseString(originArgs.getAsString());
BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction());
// get args rules and validate http args
JsonElement httpArgsRules = getRulesForHTTPRequest(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor);
}
public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) {
switch(basicOperation) {
switch (basicOperation) {
case Hello:
case Delete:
case ListOps:
@ -97,14 +76,13 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
}
}
public static DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) {
BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction());
public DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) {
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
// taking Extension into consideration
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null;
String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null;
DoipMessage doipMessage = null;
switch(httpOperation) {
switch (httpOperation) {
case Hello:
case Delete:
case ListOps:
@ -121,13 +99,13 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder()
.createRequest(header.get("identifier").getAsString(), httpOperation.getName());
JsonElement query = header.get("query");
if(query != null) searchBuilder.addAttributes("query", query.getAsString());
if (query != null) searchBuilder.addAttributes("query", query.getAsString());
JsonElement pageNum = header.get("pageNum");
if(pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt());
if (pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt());
JsonElement pageSize = header.get("pageSize");
if(pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt());
if (pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt());
JsonElement type = header.get("type");
if(type != null) searchBuilder.addAttributes("type", type.getAsString());
if (type != null) searchBuilder.addAttributes("type", type.getAsString());
doipMessage = searchBuilder.create();
break;
@ -135,13 +113,14 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create();
JsonElement element = header.get("element");
JsonElement includeElementData = header.get("includeElementData");
if(element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString());
if(includeElementData != null && includeElementData.getAsBoolean()) doipMessage.header.parameters.addAttribute("includeElementData", "true");
if (element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString());
if (includeElementData != null && includeElementData.getAsBoolean())
doipMessage.header.parameters.addAttribute("includeElementData", "true");
break;
case Extension:
DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder();
if(header != null) {
if(header.get("identifier") != null) {
if (header != null) {
if (header.get("identifier") != null) {
extensionBuilder = extensionBuilder
.createRequest(header.get("identifier").getAsString(), httpOperation.getName());
}
@ -152,7 +131,7 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
}
}
if(body != null) {
if (body != null) {
extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8));
}

View File

@ -1,6 +1,5 @@
package org.bdware.sc.handler;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
@ -21,22 +20,11 @@ import java.util.HashMap;
import java.util.Map;
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
public Map<String, FunctionNode> doipFunctionNodeMap;
static Logger logger = LogManager.getLogger(NettyServerHandler.class);
static Gson gson;
public static DOOPRequestHandler instance;
public Map<String, FunctionNode> doipFunctionNodeMap;
public DOOPRequestHandler() {
doipFunctionNodeMap = new HashMap<>();
gson = new Gson();
}
public static DOOPRequestHandler createHandler() {
if(instance == null) {
instance = new DOOPRequestHandler();
}
return instance;
}
public void addDoipOperation(FunctionNode function) throws Exception {
@ -145,10 +133,10 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg);
DoipMessage finalDoipMsg = (DoipMessage) ret;
finalDoipMsg.requestID = msg.requestID;
if(finalDoipMsg.header.parameters == null) {
if (finalDoipMsg.header.parameters == null) {
finalDoipMsg.header.parameters = new HeaderParameter("", "");
}
if(finalDoipMsg.header.parameters.attributes == null) {
if (finalDoipMsg.header.parameters.attributes == null) {
finalDoipMsg.header.parameters.attributes = new JsonObject();
}
finalDoipMsg.header.parameters.attributes.addProperty("nodeID", String.valueOf(JavaScriptEntry.shardingID));
@ -162,9 +150,9 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
}
public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) {
ContractRequest cr = new ContractRequest();
ContractRequest cr = new ContractRequest();
cr.setContractID("");
if(request.credential == null) {
if (request.credential == null) {
cr.setRequester(null);
} else {
cr.setRequester(request.credential.getSigner());

View File

@ -6,6 +6,7 @@ import org.bdware.doip.endpoint.server.DoipListenerConfig;
import org.bdware.doip.endpoint.server.DoipServerImpl;
import org.bdware.doip.endpoint.server.DoipServiceInfo;
import org.bdware.doip.endpoint.server.StartServerCallback;
import org.bdware.sc.ContractProcess;
import org.bdware.sc.handler.DOOPRequestHandler;
import java.net.URI;
@ -54,7 +55,7 @@ public class DoipLocalSingleton {
}
DoipServiceInfo info = new DoipServiceInfo("aibd.govdata.tj/do.3f9c41e6-9f8e-48a0-9220-53f438d40e43", "ownerDEF", "gateRepo", infos);
server = new DoipServerImpl(info);
DOOPRequestHandler handler = DOOPRequestHandler.createHandler();
DOOPRequestHandler handler = ContractProcess.instance.doopRequestHandler;
server.setRepositoryHandler(handler);
ResultChecker checker = new ResultChecker();
server.start(checker);