mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-01-10 01:44:14 +00:00
feat: use JsonUtil to optimize CMActions
replace all JsonParser to JsonUtil
This commit is contained in:
parent
40a638be5b
commit
55d7453ab6
@ -1,6 +1,9 @@
|
||||
package org.bdware.server.action;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -37,16 +40,18 @@ import java.util.*;
|
||||
|
||||
public class CMActions implements OnHashCallback {
|
||||
private static final String PARAM_ACTION = "action";
|
||||
private static final JsonObject MISSING_ARGUMENT = JsonUtil.parseStringAsJsonObject("{\"action\":\"onExecuteResult\",\"executeTime\":-1,"
|
||||
private static final JsonObject MISSING_ARGUMENT =
|
||||
JsonUtil.parseStringAsJsonObject("{\"action\":\"onExecuteResult\",\"executeTime\":-1,"
|
||||
+ "\"status\":\"Error\",\"result\":\"missing arguments\"}");
|
||||
private static final JsonObject INVALID_DOI = JsonUtil.parseStringAsJsonObject(
|
||||
"{\"action\":\"onExecuteResult\",\"executeTime\":-1,"
|
||||
+ "\"status\":\"Error\",\"result\":\"invalid contract doi\"}");
|
||||
private static final Logger LOGGER = LogManager.getLogger(CMActions.class);
|
||||
public static FuncInvokeInfo FUNCINVOKEINFO = new FuncInvokeInfo(); // 合约调用时参数和结果
|
||||
public static ContractManager manager = initManager();
|
||||
public static FuncInvokeInfo FUNCINVOKEINFO = new FuncInvokeInfo(); // 合约调用时参数和结果
|
||||
private static SecureRandom RANDOM;
|
||||
public ContractManagerFrameHandler handler;
|
||||
|
||||
public CMActions() {
|
||||
handler = null;
|
||||
}
|
||||
@ -158,7 +163,7 @@ public class CMActions implements OnHashCallback {
|
||||
cReq.setAction(args.get("operation").getAsString());
|
||||
cReq.setArg(args.get("arg"));
|
||||
} else {
|
||||
JsonObject jo = JsonParser.parseString(args.get("arg").getAsString()).getAsJsonObject();
|
||||
JsonObject jo = JsonUtil.parseStringAsJsonObject(args.get("arg").getAsString());
|
||||
if (!jo.has("action") || !jo.has("arg")) {
|
||||
resultCallback.onResult(MISSING_ARGUMENT);
|
||||
return;
|
||||
@ -293,12 +298,12 @@ public class CMActions implements OnHashCallback {
|
||||
argTemplate = Records.get(0).get("args").getAsString();
|
||||
argSchema = Records.get(0).get("args").getAsString();
|
||||
resSchema = Records.get(0).get("res").toString();
|
||||
JsonElement argEle = JsonParser.parseString(argSchema);
|
||||
JsonElement argEle = JsonUtil.parseString(argSchema);
|
||||
MockSchemaParser parser = new MockSchemaParser();
|
||||
parser.visit(argEle);
|
||||
argSchema = parser.get().toString();
|
||||
|
||||
JsonElement resEle = JsonParser.parseString(resSchema);
|
||||
JsonElement resEle = JsonUtil.parseString(resSchema);
|
||||
parser = new MockSchemaParser();
|
||||
parser.visit(resEle);
|
||||
resSchema = parser.get().toString();
|
||||
@ -491,16 +496,15 @@ public class CMActions implements OnHashCallback {
|
||||
@Action(async = true)
|
||||
public void setMask(JsonObject args, final ResultCallback resultCallback) {
|
||||
String data = "failed";
|
||||
String contractID = "";
|
||||
String operation = "";
|
||||
//JsonElement mask = JsonParser.parseString("");
|
||||
String contractID;
|
||||
String operation;
|
||||
//JsonElement mask = JsonUtil.parseString("");
|
||||
if (args.has("contractID") && args.has("operation") && args.has("arg")) {
|
||||
contractID = args.get("contractID").getAsString();
|
||||
System.out.println(contractID);
|
||||
operation = args.get("operation").getAsString();
|
||||
System.out.println(operation);
|
||||
JsonElement mask = args.get("arg");
|
||||
// ContractClient client = manager.getClient(contractID);
|
||||
ContractClient client = manager.getClient(contractID);
|
||||
ProjectConfig config = manager.projectRecoder.getProjectConfig(contractID);
|
||||
config.setMask(operation, mask);
|
||||
@ -593,7 +597,7 @@ public class CMActions implements OnHashCallback {
|
||||
public void queryContractLogDetail(JsonObject json, ResultCallback resultCallback) {
|
||||
if (json.has("key")) {
|
||||
String str = ContractManager.logsDB.get(json.get("key").getAsString());
|
||||
JsonObject data2 = JsonParser.parseString(str).getAsJsonObject();
|
||||
JsonObject data2 = JsonUtil.parseStringAsJsonObject(str);
|
||||
data2.addProperty("action", "onQueryContractLogDetail");
|
||||
long dateTime = data2.get("date").getAsLong();
|
||||
Date date = new Date(dateTime);
|
||||
@ -653,7 +657,7 @@ public class CMActions implements OnHashCallback {
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "onStartContract");
|
||||
JsonArray array =
|
||||
JsonParser.parseString(args.get("fileList").getAsString()).getAsJsonArray();
|
||||
JsonUtil.parseString(args.get("fileList").getAsString()).getAsJsonArray();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
String fileName = array.get(i).getAsString();
|
||||
Contract c = new Contract();
|
||||
@ -1444,97 +1448,6 @@ public class CMActions implements OnHashCallback {
|
||||
}
|
||||
}
|
||||
|
||||
// @Action(userPermission = 1L << 26, async = true)
|
||||
// public void queryContractInstanceInfoByDOI(JsonObject args, ResultCallback resultCallback)
|
||||
// {
|
||||
// long s = System.currentTimeMillis();
|
||||
// String ret;
|
||||
// try {
|
||||
// String contractDOI = args.get("doi").getAsString();
|
||||
// DigitalObject contractDO;
|
||||
// DoipClient doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
|
||||
// DoMessage response = doipClient.retrieve(contractDOI, null, null);
|
||||
// if (response.parameters.response == DoResponse.Success) {
|
||||
// contractDO = DigitalObject.parse(response.body);
|
||||
// } else {
|
||||
// DoMessage resp = DOAClient.getGlobalInstance().retrieve(contractDOI, null,
|
||||
// null);
|
||||
// contractDO = DigitalObject.parse(resp.body);
|
||||
// }
|
||||
// ContractInstanceDO contractInstanceDO =
|
||||
// (ContractInstanceDO)
|
||||
// ContractManager.toObject(contractDO.elements.get(0).getData());
|
||||
// // Dictionary<String, String> contractInfo = JsonUtil.fromJson(new
|
||||
// // String(contractDO.getData()), new Hashtable<String, String>().getClass());
|
||||
//
|
||||
// ret =
|
||||
// String.format(
|
||||
// "Contract ID: %s\nContract PublicKey: %s",
|
||||
// contractInstanceDO.id, contractInstanceDO.publicKey);
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// ret = bo.toString();
|
||||
// }
|
||||
// Map<String, Object> r = new HashMap<>();
|
||||
// r.put("action", "onQueryContractInstanceInfoByDOI");
|
||||
// r.put("data", ret);
|
||||
// r.put("executeTime", System.currentTimeMillis() - s);
|
||||
// resultCallback.onResult(JsonUtil.toJson(r));
|
||||
// if (client != null && client.controller != null) {
|
||||
// client.controller.updateContract();
|
||||
// }
|
||||
// }
|
||||
|
||||
/*
|
||||
* @Action(userPermission = 1 << 19, async = true) public void
|
||||
* staticVerify(JsonObject args, ResultCallback resultCallback) { Map<String,
|
||||
* Object> r = new HashMap<>(); r.put("action", "onStaticVerifyResult");
|
||||
*
|
||||
* long start = System.currentTimeMillis(); Map<String, Object> ret = new
|
||||
* HashMap<>(); Contract c = new Contract(); // c.setType(Type.Algorithm);
|
||||
* c.setType(ContractType.Sole); ret.put("action", "onStartContract"); String path =
|
||||
* null; if (args.has("path")) path = args.get("path").getAsString(); if (path
|
||||
* != null && path.startsWith("/")) c.setScript(args.get("path").getAsString());
|
||||
* else c.setScript(args.get("script").getAsString()); if
|
||||
* (args.has("publicKey")) { c.setOwner(args.get("publicKey").getAsString());
|
||||
* c.setSignature(args.get("signature").getAsString()); } else if
|
||||
* (args.has("owner")) { c.setOwner(args.get("owner").getAsString());
|
||||
* c.setSignature(args.get("signature").getAsString());
|
||||
*
|
||||
* } else { c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
* c.doSignature(GlobalConf.instance.keyPair); }
|
||||
*
|
||||
* if (!c.verifySignature()) { ret.put("data", "verify failed");
|
||||
* resultCallback.onResult(gson.toJson(ret)); return; } if (path != null &&
|
||||
* path.startsWith("/")) { String parPath; if (args.has("isPrivate") &&
|
||||
* args.get("isPrivate").getAsBoolean()) { parPath =
|
||||
* GlobalConf.instance.privateDir + "/" + handler.pubKey; } else { parPath =
|
||||
* GlobalConf.instance.publicDir; } try { String[] pp = path.split("/"); String
|
||||
* parentPath = path; for (int i = 0; i < pp.length && i < 2; i++) { parentPath
|
||||
* += pp[i] + "/"; } System.out.println("[CMActions] pack Dir, from:" + path +
|
||||
* " --> " + parentPath); byte[] bb = YJSPacker.pack(new File(parPath,
|
||||
* parentPath).getAbsolutePath()); File temp = File.createTempFile(pp[pp.length
|
||||
* - 1], ".zip"); FileOutputStream fout = new FileOutputStream(temp);
|
||||
* fout.write(bb); fout.close(); System.out.println("StartContract, zipPath:" +
|
||||
* temp.getAbsolutePath()); // TODO script should encoded!!
|
||||
* c.setScript(temp.getAbsolutePath()); } catch (Exception e) {
|
||||
* e.printStackTrace(); } }
|
||||
*
|
||||
* System.out.println("[CMActions] verifyContract: " + gson.toJson(c));
|
||||
* r.put("data", manager.staticVerify(c)); r.put("cid", c.getID());
|
||||
* r.put("executeTime", System.currentTimeMillis() - start); //
|
||||
* GRPCPool.writeToChain(c.getOwner(), privKey, gson.toJson(r), //
|
||||
* json.getString("requestID"));
|
||||
*
|
||||
* //addLocalContractLog("staticVerify", c.getID(), path.split("/")[1],
|
||||
* c.getOwner(),null);
|
||||
*
|
||||
* resultCallback.onResult(gson.toJson(r)); }
|
||||
*/
|
||||
|
||||
@Action(userPermission = 1L << 26, async = true, httpAccess = false)
|
||||
public void setPermission(JsonObject json, ResultCallback resultCallback) {
|
||||
String closePermission = json.get("closePer").getAsString();
|
||||
@ -1648,10 +1561,101 @@ public class CMActions implements OnHashCallback {
|
||||
// rc.onResult(peers);
|
||||
}
|
||||
|
||||
// @Action(userPermission = 1L << 26, async = true)
|
||||
// public void queryContractInstanceInfoByDOI(JsonObject args, ResultCallback resultCallback)
|
||||
// {
|
||||
// long s = System.currentTimeMillis();
|
||||
// String ret;
|
||||
// try {
|
||||
// String contractDOI = args.get("doi").getAsString();
|
||||
// DigitalObject contractDO;
|
||||
// DoipClient doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
|
||||
// DoMessage response = doipClient.retrieve(contractDOI, null, null);
|
||||
// if (response.parameters.response == DoResponse.Success) {
|
||||
// contractDO = DigitalObject.parse(response.body);
|
||||
// } else {
|
||||
// DoMessage resp = DOAClient.getGlobalInstance().retrieve(contractDOI, null,
|
||||
// null);
|
||||
// contractDO = DigitalObject.parse(resp.body);
|
||||
// }
|
||||
// ContractInstanceDO contractInstanceDO =
|
||||
// (ContractInstanceDO)
|
||||
// ContractManager.toObject(contractDO.elements.get(0).getData());
|
||||
// // Dictionary<String, String> contractInfo = JsonUtil.fromJson(new
|
||||
// // String(contractDO.getData()), new Hashtable<String, String>().getClass());
|
||||
//
|
||||
// ret =
|
||||
// String.format(
|
||||
// "Contract ID: %s\nContract PublicKey: %s",
|
||||
// contractInstanceDO.id, contractInstanceDO.publicKey);
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// ret = bo.toString();
|
||||
// }
|
||||
// Map<String, Object> r = new HashMap<>();
|
||||
// r.put("action", "onQueryContractInstanceInfoByDOI");
|
||||
// r.put("data", ret);
|
||||
// r.put("executeTime", System.currentTimeMillis() - s);
|
||||
// resultCallback.onResult(JsonUtil.toJson(r));
|
||||
// if (client != null && client.controller != null) {
|
||||
// client.controller.updateContract();
|
||||
// }
|
||||
// }
|
||||
|
||||
/*
|
||||
* @Action(userPermission = 1 << 19, async = true) public void
|
||||
* staticVerify(JsonObject args, ResultCallback resultCallback) { Map<String,
|
||||
* Object> r = new HashMap<>(); r.put("action", "onStaticVerifyResult");
|
||||
*
|
||||
* long start = System.currentTimeMillis(); Map<String, Object> ret = new
|
||||
* HashMap<>(); Contract c = new Contract(); // c.setType(Type.Algorithm);
|
||||
* c.setType(ContractType.Sole); ret.put("action", "onStartContract"); String path =
|
||||
* null; if (args.has("path")) path = args.get("path").getAsString(); if (path
|
||||
* != null && path.startsWith("/")) c.setScript(args.get("path").getAsString());
|
||||
* else c.setScript(args.get("script").getAsString()); if
|
||||
* (args.has("publicKey")) { c.setOwner(args.get("publicKey").getAsString());
|
||||
* c.setSignature(args.get("signature").getAsString()); } else if
|
||||
* (args.has("owner")) { c.setOwner(args.get("owner").getAsString());
|
||||
* c.setSignature(args.get("signature").getAsString());
|
||||
*
|
||||
* } else { c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
* c.doSignature(GlobalConf.instance.keyPair); }
|
||||
*
|
||||
* if (!c.verifySignature()) { ret.put("data", "verify failed");
|
||||
* resultCallback.onResult(gson.toJson(ret)); return; } if (path != null &&
|
||||
* path.startsWith("/")) { String parPath; if (args.has("isPrivate") &&
|
||||
* args.get("isPrivate").getAsBoolean()) { parPath =
|
||||
* GlobalConf.instance.privateDir + "/" + handler.pubKey; } else { parPath =
|
||||
* GlobalConf.instance.publicDir; } try { String[] pp = path.split("/"); String
|
||||
* parentPath = path; for (int i = 0; i < pp.length && i < 2; i++) { parentPath
|
||||
* += pp[i] + "/"; } System.out.println("[CMActions] pack Dir, from:" + path +
|
||||
* " --> " + parentPath); byte[] bb = YJSPacker.pack(new File(parPath,
|
||||
* parentPath).getAbsolutePath()); File temp = File.createTempFile(pp[pp.length
|
||||
* - 1], ".zip"); FileOutputStream fout = new FileOutputStream(temp);
|
||||
* fout.write(bb); fout.close(); System.out.println("StartContract, zipPath:" +
|
||||
* temp.getAbsolutePath()); // TODO script should encoded!!
|
||||
* c.setScript(temp.getAbsolutePath()); } catch (Exception e) {
|
||||
* e.printStackTrace(); } }
|
||||
*
|
||||
* System.out.println("[CMActions] verifyContract: " + gson.toJson(c));
|
||||
* r.put("data", manager.staticVerify(c)); r.put("cid", c.getID());
|
||||
* r.put("executeTime", System.currentTimeMillis() - start); //
|
||||
* GRPCPool.writeToChain(c.getOwner(), privKey, gson.toJson(r), //
|
||||
* json.getString("requestID"));
|
||||
*
|
||||
* //addLocalContractLog("staticVerify", c.getID(), path.split("/")[1],
|
||||
* c.getOwner(),null);
|
||||
*
|
||||
* resultCallback.onResult(gson.toJson(r)); }
|
||||
*/
|
||||
|
||||
@Action(async = true, userPermission = 0L)
|
||||
public void updateNodeUnits(JsonObject args, ResultCallback rc) {
|
||||
LOGGER.debug("updateNodeUnits");
|
||||
JsonArray jsonArray = (JsonArray) JsonParser.parseString(args.get("data").getAsString());
|
||||
JsonArray jsonArray = JsonUtil.parseString(args.get("data").getAsString()).getAsJsonArray();
|
||||
JsonObject jsonObject;
|
||||
JsonArray unitPeers;
|
||||
JsonObject jsonPeer;
|
||||
@ -1660,7 +1664,7 @@ public class CMActions implements OnHashCallback {
|
||||
String nodeName;
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
jsonObject = jsonElement.getAsJsonObject();
|
||||
unitPeers = (JsonArray) JsonParser.parseString(jsonObject.get("value").getAsString());
|
||||
unitPeers = JsonUtil.parseString(jsonObject.get("value").getAsString()).getAsJsonArray();
|
||||
for (JsonElement jsonElemenPeer : unitPeers) {
|
||||
jsonPeer = jsonElemenPeer.getAsJsonObject();
|
||||
nodeName = jsonPeer.get("nodeName").getAsString();
|
||||
|
Loading…
Reference in New Issue
Block a user