update 1.8.5

support doip cluster
This commit is contained in:
CaiHQ 2023-04-20 10:05:11 +08:00
parent dc0671cfbd
commit 723c9c7a06
5 changed files with 49 additions and 32 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group = "org.bdware.sc"
version = "1.8.3"
version = "1.8.5"
tasks.withType(JavaCompile) {
// options.compilerArgs << '-Xlint:none'
// options.compilerArgs << '-Xlint:deprecation' << "-Werror"
@ -48,7 +48,7 @@ dependencies {
implementation 'com.sun.mail:javax.mail:1.6.2'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'org.bdware.bdcontract:sdk-java:1.0.2'
implementation 'org.bdware.doip:doip-audit-tool:1.2.2'
implementation 'org.bdware.doip:doip-audit-tool:1.2.4'
implementation 'org.bdware.doip:doip-sdk:1.4.0'
implementation fileTree(dir: 'lib', include: '*.jar')
testImplementation 'junit:junit:4.13.2'

View File

@ -3,10 +3,9 @@ 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.JsonDoipMessage;
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.boundry.ScriptReturnException;
@ -25,6 +24,7 @@ public class DOOPAfterExecHandler implements AnnotationHook {
public DOOPAfterExecHandler(BasicOperations operations) {
jsonResponseRules = getRulesForJsonResponse(operations);
}
@Override
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
Object originDoipMsgPacker = argPacks.arg;
@ -32,7 +32,6 @@ public class DOOPAfterExecHandler implements AnnotationHook {
if (originDoipMsgPacker instanceof DoipMessagePacker) {
DoipMessagePacker doipMessagePacker = (DoipMessagePacker) originDoipMsgPacker;
originDoipMsg = doipMessagePacker.rawDoipMsg;
// if http, directly return
if (doipMessagePacker.source.equals("http")) {
return argPacks;
@ -42,26 +41,8 @@ public class DOOPAfterExecHandler implements AnnotationHook {
// validate json response
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) {
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);
originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode);
}
}
if (body != null) {
originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
}
argPacks.ret = originDoipMsg;
JsonDoipMessage returnedMessage = JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class);
argPacks.ret = returnedMessage.toResponseDoipMessage(originDoipMsg);
return argPacks;
}
} else {
@ -78,9 +59,8 @@ public class DOOPAfterExecHandler implements AnnotationHook {
case Update:
case Search:
case ListOps:
return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"!body\":\"string\"}");
case Delete:
return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"body\":\"string\"}");
return JsonParser.parseString("{\"!header\":{\"!response\":\"string\",\"attributes\":{}},\"body\":\"string\"}");
case Extension:
case Unknown:
default:

View File

@ -50,7 +50,11 @@ public class DOOPBeforeExecHandler implements AnnotationHook {
public void validateHTTPRequestArgs(ContractRequest httpReq) {
JsonElement originArgs = httpReq.getArg();
JsonElement httpArgs = JsonParser.parseString(originArgs.getAsString());
JsonElement httpArgs = null;
if (originArgs.isJsonObject())
httpArgs = originArgs;
else
httpArgs = JsonParser.parseString(originArgs.getAsString());
// get args rules and validate http args
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor);

View File

@ -1,6 +1,9 @@
package org.bdware.sc.entity;
import org.bdware.doip.codec.JsonDoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.util.JsonUtil;
public class DoipMessagePacker {
// the DoipMessagePacker is raised by http/doip
@ -8,7 +11,8 @@ public class DoipMessagePacker {
// the well-composed DoipMessage
public DoipMessage rawDoipMsg;
public DoipMessagePacker() {}
public DoipMessagePacker() {
}
public DoipMessagePacker(String source, DoipMessage rawMsg) {
this.source = source;
@ -22,4 +26,8 @@ public class DoipMessagePacker {
public void setSource(String source) {
this.source = source;
}
public Object toJSObject() {
return JSONTool.convertJsonElementToMirror(JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage(rawDoipMsg)));
}
}

View File

@ -5,6 +5,8 @@ import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.codec.doipMessage.HeaderParameter;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.server.DoipRequestHandler;
@ -16,6 +18,9 @@ import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.FunctionNode;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
@ -38,6 +43,22 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
@Override
public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) {
String str = msg.header.parameters.operation;
if (msg.header != null && msg.header.parameters.attributes != null && msg.header.parameters.attributes.has("readGlobalVar")) {
try {
//TODO @wangxuxing
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bo);
String var = msg.header.parameters.attributes.get("readGlobalVar").getAsString();
Object result = null;
out.writeObject(result);
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.Success, msg);
builder.setBody(bo.toByteArray());
return builder.create();
} catch (Exception e) {
}
//return ....
}
logger.debug("[Call operation] name: " + str);
if (str != null) {
FunctionNode fn;
@ -142,11 +163,15 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
finalDoipMsg.header.parameters.attributes.addProperty("nodeID", String.valueOf(JavaScriptEntry.shardingID));
return finalDoipMsg;
} catch (Exception e) {
e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.Declined, msg);
builder.setBody(bo.toByteArray());
DoipMessage message = builder.create();
logger.error("buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err");
return message;
}
return null;
}
public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) {