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

org.bdware.doip.audit.client.AuditIrpClientHandler Maven / Gradle / Ivy

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

import io.netty.channel.ChannelHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.writer.AuditConfig;
import org.bdware.doip.audit.writer.AuditLogPool;
import org.bdware.doip.audit.writer.ClientContext;
import org.bdware.doip.audit.writer.NoneWriter;
import org.bdware.doip.encrypt.SM2Signer;
import org.bdware.irp.irpclient.IrpMessageCallback;
import org.bdware.irp.irpclient.NettyIrpClientHandler;
import org.bdware.irp.irplib.core.IrpMessage;
import org.bdware.irp.irplib.core.IrpResponse;

import static org.bdware.irp.irplib.core.IrpMessageCode.*;

@ChannelHandler.Sharable
public class AuditIrpClientHandler extends NettyIrpClientHandler {

    static Logger LOGGER = LogManager.getLogger(AuditIrpClientHandler.class);
    private final SM2Signer signer;
    AuditLogPool pool;
    DoIdWrapper serverDoId;
    DoIdWrapper clientDoId;

    public AuditIrpClientHandler(DoIdWrapper clientDoIdWrapper, DoIdWrapper serverDoIdWrapper, SM2Signer signer, AuditConfig auditConfig) {
        if (auditConfig != null) {
            pool = new AuditLogPool(auditConfig.createLogWriterInstance());
            this.clientDoId = clientDoIdWrapper;
            this.serverDoId = serverDoIdWrapper;
        } else {
            pool = new AuditLogPool(new NoneWriter());
        }

        this.signer = signer;
    }

    public void sendMessage(IrpMessage request, IrpMessageCallback callback) {
        switch (request.header.opCode) {
            case OC_RESOLUTION:
            case OC_RESOLUTION_DOID:
            case OC_RESOLVE_GRS:
                break;
            default:
                signer.signMessage(request);
        }

        super.sendMessage(request, new IrpMessageCallback() {
            @Override
            public void onResult(IrpMessage msg) {
                boolean verify = true;
                switch (request.header.opCode) {
                    case OC_RESOLUTION:
                    case OC_RESOLUTION_DOID:
                    case OC_RESOLVE_GRS:
                        break;
                    default:
                        verify = signer.verifyMessage(request);
                }
                if (!verify) {
                    msg = IrpResponse.newErrorResponse(msg.header.opCode, msg.header.responseCode, "signature verify failed");
                }
                ClientContext ctx = new ClientContext(clientDoId, serverDoId, channel);
                pool.extract(ctx, request, msg);
                callback.onResult(msg);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy