From 723c9c7a06d5a491264d732c8c38ddad350c9937 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Thu, 20 Apr 2023 10:05:11 +0800 Subject: [PATCH] update 1.8.5 support doip cluster --- build.gradle | 4 +-- .../sc/engine/hook/DOOPAfterExecHandler.java | 30 +++--------------- .../sc/engine/hook/DOOPBeforeExecHandler.java | 6 +++- .../bdware/sc/entity/DoipMessagePacker.java | 10 +++++- .../bdware/sc/handler/DOOPRequestHandler.java | 31 +++++++++++++++++-- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/build.gradle b/build.gradle index 06fd4cb..5a87a0f 100644 --- a/build.gradle +++ b/build.gradle @@ -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' 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 623d848..4dea627 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java @@ -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: 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 8fcba4c..4798049 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java @@ -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); diff --git a/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java b/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java index 691f970..86d67cd 100644 --- a/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java +++ b/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java @@ -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))); + } } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 8d49723..6959131 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -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) {