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

org.bdware.doip.audit.server.AuditIrpRequestHandler Maven / Gradle / Ivy

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

import io.netty.channel.ChannelHandlerContext;
import org.bdware.doip.audit.AuditIrpMessageFactory;
import org.bdware.doip.audit.writer.AuditConfig;
import org.bdware.doip.audit.writer.AuditLogPool;
import org.bdware.doip.audit.writer.ServerContext;
import org.bdware.irp.irplib.core.IrpMessage;
import org.bdware.irp.irplib.core.IrpMessageSigner;
import org.bdware.irp.irplib.core.IrpResponse;
import org.bdware.irp.irpserver.IrpRequestHandlerImpl;
import org.bdware.irp.irpserver.IrpServerInfo;

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

public class AuditIrpRequestHandler extends IrpRequestHandlerImpl {
    public IrpMessageSigner signer;
    AuditLogPool pool;
    String serviceDoid;
    AuditConfig auditConfig;

    public AuditIrpRequestHandler(IrpServerInfo info, IrpMessageSigner signer, AuditConfig auditConfig) {
        super();
        this.signer = signer;
        pool = new AuditLogPool(auditConfig.createLogWriterInstance());
        serviceDoid = info.serverId;
        this.auditConfig = auditConfig;
    }

    //TODO which opCode needs sign and verify.
    @Override
    public IrpMessage onRequest(ChannelHandlerContext ctx, IrpMessage msg) {
        IrpMessage ret;
        try {
            switch (msg.header.opCode) {
                case OC_RESOLUTION:
                case OC_RESOLUTION_DOID:
                case OC_RESOLVE_GRS:
                    ret = hookResolveAuditIfNeeded(ctx, msg);
                    break;
                case OC_QUERY_ROUTER:
                    ret = hookAuditInfo(ctx, msg);
                    signer.signMessage(ret);
                    break;
                case OC_VERIFY_ROUTER:
                default:
                    ret = verifyAndHandle(ctx, msg);
                    signer.signMessage(ret);
            }
        } catch (Exception e) {
            ret = IrpResponse.newErrorResponse(msg.header.opCode, RC_ERROR, e.getMessage());
            e.printStackTrace();
        }
        pool.extract(new ServerContext(ctx, serviceDoid), msg, ret);
        return ret;
    }

    private IrpMessage hookResolveAuditIfNeeded(ChannelHandlerContext ctx, IrpMessage msg) {
        if (msg != null && (msg.header.opCode == OC_RESOLUTION || msg.header.opCode == OC_RESOLUTION_DOID)) {
            if (msg.getDoid() != null && msg.getDoid().equals(auditConfig.getAuditDoid())) {
                IrpMessage resp = AuditIrpMessageFactory.newAuditRepoResponse(auditConfig.getAuditDoid(), auditConfig.getAuditUrl());
                resp.header.opCode = OC_RESOLUTION_DOID;
                return resp;
            }
        }
        return super.onRequest(ctx, msg);
    }

    private IrpMessage verifyAndHandle(ChannelHandlerContext ctx, IrpMessage msg) {
        IrpMessage ret;
        if (signer.verifyMessage(msg))
            ret = super.onRequest(ctx, msg);
        else
            ret = IrpResponse.newErrorResponse(msg.header.opCode, RC_ERROR, "signature verify failed");
        return ret;
    }

    private IrpMessage hookAuditInfo(ChannelHandlerContext ctx, IrpMessage request) {
        if (AuditIrpMessageFactory.isAuditRepoRequest(request)) {
            return AuditIrpMessageFactory.newAuditRepoResponse(auditConfig.getAuditDoid(), auditConfig.getAuditUrl());
        } else return super.onRequest(ctx, request);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy