org.bdware.doip.audit.server.DefaultAuditHandler 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.audit.server;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bdware.doip.audit.writer.*;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.codec.digitalObject.Element;
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.endpoint.server.RepositoryHandler;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class DefaultAuditHandler implements RepositoryHandler {
private final ConfigurableAuditConfig auditConfig;
private final AuditRequestHandler auditRequestHandler;
AuditLogPool pool;
public DefaultAuditHandler(AuditRequestHandler auditRequestHandler, ConfigurableAuditConfig auditConfig, AuditLogPool pool) {
this.pool = pool;
this.auditConfig = auditConfig;
this.auditRequestHandler = auditRequestHandler;
}
private DefaultRepoHandlerInjector injector;
public void setInjectHelloContext(DefaultRepoHandlerInjector ctx) {
injector = ctx;
}
//每个操作都要实现
//把这个仓库当成一个DO。
//它的update操作就是对它的存证配置进行设置。
//它的retrieve操作就是对它的存证数据(原始数据)的查询。
@Override
public DoipMessage handleHello(DoipMessage request) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
try {
builder.createResponse(DoipResponseCode.Success, request);
builder.addAttributes("version", "auto_audit_repo_v_0.8.6");
builder.addAttributes("auditConfig", auditConfig.getAuditType().toString());
builder.setBody(("Hello!").getBytes(StandardCharsets.UTF_8));
if (injector != null) {
injector.injectHello(request, builder);
}
} catch (Exception e) {
builder.createResponse(DoipResponseCode.Invalid, request);
if (e.getMessage() != null)
builder.setBody(("exception " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
e.printStackTrace();
}
return builder.create();
}
@Override
public DoipMessage handleListOps(DoipMessage request) {
if (injector != null) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
injector.injectListOps(request, builder);
return builder.create();
}
return null;
}
@Override
public DoipMessage handleCreate(DoipMessage request) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
try {
builder.createResponse(DoipResponseCode.Success, request);
builder.addAttributes("version", "auto_audit_repo_v_0.8.6");
builder.addAttributes("auditConfig", "...");
builder.setBody(("Hello!").getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
builder.createResponse(DoipResponseCode.Invalid, request);
if (e.getMessage() != null)
builder.setBody(("exception " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
e.printStackTrace();
}
if (injector != null) {
injector.injectCreate(request, builder);
}
return builder.create();
}
@Override
public DoipMessage handleUpdate(DoipMessage request) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
try {
if (request.credential != null && auditRequestHandler.isOwner(request.credential.getSigner())) {
AuditRepo auditRepo = new Gson().fromJson(request.header.parameters.attributes.get("auditRepo"), AuditRepo.class);
AuditType auditType = AuditType.valueOf(request.header.parameters.attributes.get("auditType").getAsString());
JsonObject config = request.header.parameters.attributes.get("extraConfig").getAsJsonObject();
//TODO 校验AuditConfig的正确性
AuditConfig newAuditConfig = AuditConfig.newInstance(auditRepo, auditType, config);
auditConfig.changeAuditConfig(newAuditConfig);
builder.createResponse(DoipResponseCode.Success, request);
builder.setBody(("success").getBytes(StandardCharsets.UTF_8));
} else {
builder.createResponse(DoipResponseCode.UnAuth_Client, request);
builder.setBody(("invalid keypair").getBytes(StandardCharsets.UTF_8));
}
} catch (Exception e) {
builder.createResponse(DoipResponseCode.Declined, request);
if (e.getMessage() != null)
builder.setBody(("exception: " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
e.printStackTrace();
}
if (injector != null)
injector.injectUpdate(request, builder);
return builder.create();
}
@Override
public DoipMessage handleDelete(DoipMessage request) {
if (injector != null) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
injector.injectDelete(request, builder);
return builder.create();
}
return null;
}
@Override
public DoipMessage handleRetrieve(DoipMessage request) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
try {
String opType = request.header.parameters.attributes.get("type").getAsString();
String requestHash;
String responseHash;
switch (opType) {
case "getAuditRule":
AuditType auditType = auditConfig.getAuditType();
builder.createResponse(DoipResponseCode.Success, request);
builder.addAttributes("auditType", auditType.toString());
break;
case "getDOAuditRule":
String doId = request.header.parameters.attributes.get("doId").getAsString();
int offset = request.header.parameters.attributes.get("offset").getAsInt();
int count = request.header.parameters.attributes.get("count").getAsInt();
builder.createResponse(DoipResponseCode.Success, request);
builder.addAttributes("msg", "unsupport");
break;
case "matchInOriginDB":
requestHash = request.header.parameters.attributes.get("requestHash").getAsString();
responseHash = request.header.parameters.attributes.get("responseHash").getAsString();
boolean existFlag = auditConfig.isExist(requestHash, responseHash);
if (existFlag) {
builder.setBody("true".getBytes(StandardCharsets.UTF_8));
} else builder.setBody("false".getBytes(StandardCharsets.UTF_8));
break;
case "getOriginalLogByHash":
requestHash = request.header.parameters.attributes.get("requestHash").getAsString();
responseHash = request.header.parameters.attributes.get("responseHash").getAsString();
List val = auditConfig.getOriginalLogByHash(requestHash, responseHash);
DigitalObject digitalObject = new DigitalObject(request.header.parameters.id, DoType.DO);
Element element0 = new Element(request.header.parameters.id + "/originalMsg_" + requestHash, "bytes");
element0.setData(val.get(0).req_msg);
Element element1 = new Element(request.header.parameters.id + "/originalMsg_" + responseHash, "bytes");
element1.setData(val.get(0).resp_msg);
digitalObject.addElements(element0);
digitalObject.addElements(element1);
builder.createResponse(DoipResponseCode.Success, request);
builder.setBody(digitalObject);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
builder.createResponse(DoipResponseCode.Declined, request);
if (e.getMessage() != null)
builder.setBody(e.getMessage().getBytes(StandardCharsets.UTF_8));
}
if (injector != null)
injector.injectRetrieve(request, builder);
return builder.create();
}
}