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

org.bdware.doip.audit.server.AuditRequestHandler 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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.writer.AuditLogPool;
import org.bdware.doip.audit.writer.ConfigurableAuditConfig;
import org.bdware.doip.audit.writer.ServerContext;
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.codec.operations.BasicOperations;
import org.bdware.doip.encrypt.SM2Signer;
import org.bdware.doip.endpoint.server.DoipRequestHandler;
import org.bdware.doip.endpoint.server.DoipServiceInfo;
import org.bdware.doip.endpoint.server.RepositoryHandler;
import org.bdware.doip.endpoint.server.RequestHandlerImpl;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.Semaphore;

public class AuditRequestHandler extends RequestHandlerImpl {
    static Logger LOGGER = LogManager.getLogger(AuditRequestHandler.class);
    SM2Signer signer;
    DoipServiceInfo serviceInfo;
    DoipRequestHandler defaultAuditHandler;
    Semaphore semaphore = new Semaphore(10);
    AuditLogPool pool;
    public AuditRequestHandler(DoipServiceInfo info, RepositoryHandler handler, SM2Signer signer, ConfigurableAuditConfig auditConfig) {
        super(handler);
        pool = new AuditLogPool(auditConfig.createLogWriterInstance());
        DefaultAuditHandler auditHandler = new DefaultAuditHandler(this, auditConfig, pool);
        if (handler instanceof DefaultRepoHandlerInjector)
            auditHandler.setInjectHelloContext((DefaultRepoHandlerInjector) handler);
        defaultAuditHandler = new RequestHandlerImpl(auditHandler);

        this.signer = signer;
        this.serviceInfo = info;

    }

    public boolean isOwner(String key) {
        if (signer != null && signer.getKeyPair() != null)
            return signer.getKeyPair().getPublicKeyStr().equals(key);
        return false;
    }

    @Override
    public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) {
        try {
            semaphore.acquire();
            DoipMessage ret;
            if (msg.credential != null && msg.credential.getSigner() != null) {
                if (signer.verifyMessage(msg)) {
                    ret = filterOrHandle(ctx, msg);
                } else {
                    DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
                    DoipMessageFactory.DoipMessageBuilder resp = builder.createResponse(DoipResponseCode.UnAuth_Client, msg);
                    resp.setBody("verify failed".getBytes(StandardCharsets.UTF_8));
                    ret = resp.create();
                }
                signer.signMessage(ret);
            } else {
                ret = filterOrHandle(ctx, msg);
            }
            ServerContext context = new ServerContext(ctx, serviceInfo.id);
            pool.extract(context, msg, ret);
            return ret;
        } catch (InterruptedException e) {
            DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
            DoipMessageFactory.DoipMessageBuilder resp = builder.createResponse(DoipResponseCode.UnAuth_Client, msg);
            resp.setBody("can't acquire lock!".getBytes(StandardCharsets.UTF_8));
            return resp.create();
        } finally {
            semaphore.release();
        }
    }

    private DoipMessage filterOrHandle(ChannelHandlerContext ctx, DoipMessage msg) {
        String id = msg.header.parameters.id;
        try {
            if (serviceInfo.id.equals(id)) {
                return defaultAuditHandler.onRequest(ctx, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return super.onRequest(ctx, msg);
        }
        return super.onRequest(ctx, msg);
    }

    private boolean isCreate(DoipMessage msg) {
        try {
            return msg.header.parameters.operation.equals(BasicOperations.Create.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy