All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.audit.writer.LogWriter Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.audit.writer;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bdware.doip.audit.AuditRule;
import org.bdware.doip.audit.AuditRuleDB;
import org.bdware.doip.auditrepo.AutoAuditDO;
import org.bdware.doip.codec.NaiveEnvelopeToDoMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.exception.MessageCodecException;
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;
import org.bdware.irp.irplib.crypto.IrpMessagePacket;
import org.bdware.irp.irplib.exception.IrpMessageEncodeException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class LogWriter {

    public abstract void write(AuditLog log);

    public abstract AuditLog extract(EndpointContext context, IrpMessage req, IrpMessage resp);

    public abstract AuditLog extract(EndpointContext context, DoipMessage req, DoipMessage resp);

    private static NaiveEnvelopeToDoMessage converter = new NaiveEnvelopeToDoMessage();
    static DoipMessageCallback emptyCallback = new DoipMessageCallback() {
        @Override
        public void onResult(DoipMessage msg) {
            //TODO maybe we can retry here?
        }
    };

    // doid format:
    // 1) abc.def
    // 2) abc.def/repo1, abc.def/adadsoa
    // 3) abc.def/repo1/dafd
    // 4) abc.def/repo2/xxxx/xxxx
    protected static void extractDOID(AuditProtocalLog.DoidInfo log, String doid) {
        List pos = new ArrayList<>();
        for (int i = 0; i < doid.length() && pos.size() < 2; i++)
            if (doid.charAt(i) == '/')
                pos.add(i);

        switch (pos.size()) {
            case 2:
                log.do_data_id = doid.substring(pos.get(1) + 1, doid.length());
                log.do_repo = doid.substring(pos.get(0) + 1, pos.get(1));
                log.do_prefix = doid.substring(0, pos.get(0));
                break;
            case 1:
                log.do_repo = log.do_data_id = doid.substring(pos.get(0) + 1, doid.length());
                log.do_prefix = doid.substring(0, pos.get(0));
                break;
            case 0:
            default:
                log.do_repo = log.do_data_id = null;
                log.do_prefix = doid;
                break;
        }
    }

    protected static String getSigner(DoipMessage request) {
        try {
            if (request != null && request.credential != null && request.credential.attributes != null) {
                return request.credential.getSigner();
            }
        } catch (Exception e) {

        }
        return null;
    }

    protected static String getSigner(IrpMessage request) {
        try {
            return new String(request.credential.signerDoid);
        } catch (Exception e) {
        }
        return null;
    }

    //SHA-265XX  ?X1?
    public static String calculateHash(IrpMessage msg) {
        return "" + Arrays.hashCode(toByteArray(msg));
    }

    protected static byte[] toByteArray(IrpMessage msg) {
        IrpMessagePacket messsagePacket = null;
        try {
            messsagePacket = IrpMessagePacket.irpMessageToTCPPackets(msg);
            return IrpMessagePacket.toBytes(messsagePacket);
        } catch (IrpMessageEncodeException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String calculateHash(DoipMessage msg) {
        return "" + Arrays.hashCode(toByteArray(msg));
    }

    public static String calculateHash(byte[] msg) {
        return "" + Arrays.hashCode(msg);
    }

    protected static byte[] toByteArray(DoipMessage msg) {
        ByteBuf buf = Unpooled.directBuffer();
        try {
            converter.messageToByteBuf(msg, buf);
            int size = buf.readableBytes();
            byte[] all = new byte[size];
            buf.readBytes(all);
            return all;
        } catch (MessageCodecException e) {
            e.printStackTrace();
        } finally {
            buf.release();
        }
        return new byte[0];
    }

    private static String getMergedType(DoipMessage request, DoipMessage response) {
        String type = getDoType(response);
        if (type != null) return type;
        return getDoType(request);
    }

    private static String getDoType(DoipMessage request) {
        try {
            if (request != null && request.header.parameters.attributes != null) {
                JsonObject attribute = request.header.parameters.attributes;
                if (attribute.has("type"))
                    return attribute.get("type").getAsString();
            }
        } catch (Exception e) {
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy