org.bdware.doip.audit.writer.CustomEventWriter 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
The newest version!
package org.bdware.doip.audit.writer;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.auditrepo.AutoAuditDO;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.exception.DoipConnectException;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.irp.irplib.core.IrpMessage;
import org.bdware.irp.irplib.core.IrpMessageCode;
public class CustomEventWriter extends LogWriter {
DoipClientImpl doipClient;
AuditConfig auditConfg;
static final Logger LOGGER = LogManager.getLogger(HashWriter.class);
public CustomEventWriter(AuditConfig config) {
super();
doipClient = new DoipClientImpl();
auditConfg = config;
if (config.getAuditUrl() != null)
doipClient.connect(ClientConfig.fromUrl(config.getAuditUrl()));
}
public void createCustomizedEvent(String eventType, JsonObject schema, DoipMessageCallback resultCallback) {
try {
ensureConnected();
doipClient.sendRawMessage(AutoAuditDO.createCustomizeEvent(eventType, schema), resultCallback);
} catch (Throwable e) {
e.printStackTrace();
}
}
public void updateCustomizedEvent(String eventType, JsonObject schema, DoipMessageCallback resultCallback) {
try {
ensureConnected();
doipClient.sendRawMessage(AutoAuditDO.createUpdateEvent(eventType, schema), resultCallback);
} catch (Throwable e) {
e.printStackTrace();
}
}
public void writeEvent(AuditEventLog log) {
log.eventType = "event";
write(log);
}
public void writeDoEvent(AuditDOEventLog log) {
log.eventType = "do_event";
write(log);
}
@Override
public void write(AuditLog log) {
try {
LOGGER.info(new Gson().toJson(log));
ensureConnected();
doipClient.sendRawMessage(AutoAuditDO.updateMsg(auditConfg.getAuditDoid(), log.asJson()), emptyCallback);
} catch (Throwable e) {
e.printStackTrace();
}
}
private void ensureConnected() {
if (!doipClient.isConnected()) {
try {
doipClient.reconnect(auditConfg.getAuditUrl());
} catch (DoipConnectException e) {
throw new RuntimeException(e);
}
}
}
public AuditLog extract(EndpointContext ctx, final IrpMessage request, final IrpMessage response) {
try {
AuditProtocalLog log = new AuditProtocalLog();
log.time = System.currentTimeMillis();
log.doid = new AuditProtocalLog.DoidInfo();
extractDOID(log.doid, request.getDoid());
log.eventType = "irp";
log.op = IrpMessageCode.toOpStr(request.header.opCode);
log.status_code = IrpMessageCode.getResponseCodeInfo(response.header.responseCode);
log.request_hash = calculateHash(request);
log.response_hash = calculateHash(response);
log.setClientPubkey(getSigner(request));
log.setClientIP(ctx.getClientIP());
log.setClientDoId(ctx.getClientDoId());
log.setServerPubkey(getSigner(response));
log.setServerIP(ctx.getServerIP());
log.setServerDoId(ctx.getServerDoId());
return log;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public AuditLog extract(EndpointContext ctx, DoipMessage request, DoipMessage response) {
try {
AuditProtocalLog log = new AuditProtocalLog();
log.time = System.currentTimeMillis();
log.doid = new AuditProtocalLog.DoidInfo();
extractDOID(log.doid, request.header.parameters.id);
log.eventType = "doip";
log.op = request.header.parameters.operation;
log.status_code = response.header.parameters.response.getName();
log.request_hash = calculateHash(request);
log.response_hash = calculateHash(response);
log.setClientPubkey(getSigner(request));
log.setClientIP(ctx.getClientIP());
log.setClientDoId(ctx.getClientDoId());
log.setServerPubkey(getSigner(response));
log.setServerIP(ctx.getServerIP());
log.setServerDoId(ctx.getServerDoId());
return log;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void sendRawMessage(DoipMessage msg, DoipMessageCallback doipMessageCallback) {
try {
ensureConnected();
doipClient.sendRawMessage(msg, doipMessageCallback);
} catch (Throwable e) {
e.printStackTrace();
}
}
}