org.bdware.doip.auditrepo.AutoAuditDO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.auditrepo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.audit.writer.AuditRepo;
import org.bdware.doip.audit.writer.AuditType;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.exception.DoDecodeException;
import org.bdware.doip.codec.operations.BasicOperations;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class AutoAuditDO extends DigitalObject {
static DoipMessageFactory factory = new DoipMessageFactory();
public AutoAuditDO(String id, DoType type) {
super(id, type);
}
public static final String AUDIT_PROXY = "AuditProxy";
public static String getAuditProxyDoid(String repoID) {
repoID =
repoID.replaceAll("/.*", "");
repoID += "/" + AUDIT_PROXY;
return repoID;
}
//------ interoperate with AuditProxy
public static DoipMessage updateMsg(String doid, JsonObject auditLog) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(doid, BasicOperations.Update.getName());
AutoAuditDO digitalObject = new AutoAuditDO(doid, DoType.DO);
digitalObject.addAttribute("auditLog", auditLog);
builder.setBody(digitalObject);
return builder.create();
}
//------ interoperate with DefaultAuditHandler
public static String getAuditConfig(String repoDoId, AuditDoipClient client) {
DoipMessage msg = client.helloSync("bdtest/repofile");
return msg.header.parameters.attributes.get("auditConfig").getAsString();
}
public static DoipMessage getRepoAuditType(String doid) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(doid, BasicOperations.Retrieve.getName());
builder.addAttributes("type", "getAuditRule");
return builder.create();
}
public static AuditType getAuditTypeFromRespMessage(DoipMessage msg) {
String str = "None";
try {
str = msg.header.parameters.attributes.get("auditType").getAsString();
} catch (Exception e) {
}
return AuditType.valueOf(str);
}
public static String updateAuditConfig(String repoDoid, AuditRepo repo, AuditType auditType, JsonObject extraConfig, AuditDoipClient client) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(repoDoid, BasicOperations.Update.getName());
builder.addAttributes("auditType", auditType.toString());
builder.addAttributes("auditRepo", new Gson().toJsonTree(repo));
builder.addAttributes("extraConfig", extraConfig);
DoipMessage msg = builder.create();
msg = client.sendMessageSync(msg);
return msg.body.getDataAsJsonString();
}
public static boolean matchInOriginDB(String repoDoid, String requestHash, String responseHash, AuditDoipClient client) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(repoDoid, BasicOperations.Retrieve.getName());
builder.addAttributes("type", "matchInOriginDB");
builder.addAttributes("requestHash", requestHash);
builder.addAttributes("responseHash", responseHash);
DoipMessage msg = builder.create();
msg = client.sendMessageSync(msg);
return Boolean.valueOf(msg.body.getDataAsJsonString());
}
public static DigitalObject getOriginalLogByHash(String repoDoid, String requestHash, String responseHash, AuditDoipClient client) throws IOException, DoDecodeException {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(repoDoid, BasicOperations.Retrieve.getName());
builder.addAttributes("type", "getOriginalLogByHash");
builder.addAttributes("requestHash", requestHash);
builder.addAttributes("responseHash", responseHash);
DoipMessage msg = builder.create();
msg = client.sendMessageSync(msg);
return msg.body.getDataAsDigitalObject();
}
public static DoipMessage createCustomizeEvent(String eventType, JsonObject schema) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest("AuditProxy", BasicOperations.Create.getName());
JsonObject body = new JsonObject();
body.addProperty("eventType", eventType);
body.add("schema", schema);
builder.setBody(body.toString().getBytes(StandardCharsets.UTF_8));
DoipMessage msg = builder.create();
return msg;
}
public static DoipMessage createUpdateEvent(String eventType, JsonObject schema) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest("AuditProxy", BasicOperations.Update.getName());
JsonObject body = new JsonObject();
body.addProperty("eventType", eventType);
body.add("schema", schema);
builder.setBody(body.toString().getBytes(StandardCharsets.UTF_8));
DoipMessage msg = builder.create();
return msg;
}
//updateConfigMsg("配置项","值"...)
//retrieveMsg("RequestHash/ResponseHash/是否返回原始数据..)
//...
}