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"

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 static DOOPAfterExecHandler createDOOPHandler() {
if(instance == null) {
instance = new DOOPAfterExecHandler();
public DOOPAfterExecHandler(BasicOperations operations) {
jsonResponseRules = getRulesForJsonResponse(operations);
}
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,12 +39,9 @@ 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;
@ -76,7 +51,8 @@ public class DOOPAfterExecHandler implements AnnotationHook {
// 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 (originDoipMsg.header.parameters == null)
originDoipMsg.header.parameters = new HeaderParameter(null, null);
originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode);
}
}
@ -94,7 +70,6 @@ public class DOOPAfterExecHandler implements AnnotationHook {
}
public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) {
switch (basicOperations) {
case Hello:
@ -114,12 +89,9 @@ 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);
@ -137,7 +109,6 @@ public class DOOPAfterExecHandler implements AnnotationHook {
}
}
}
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
@ -66,13 +48,10 @@ 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);
}
@ -97,8 +76,7 @@ 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;
@ -136,7 +114,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
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 (includeElementData != null && includeElementData.getAsBoolean())
doipMessage.header.parameters.addAttribute("includeElementData", "true");
break;
case Extension:
DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder();

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 {

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