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

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

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

import com.google.gson.Gson;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.audit.EndpointInfo;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.audit.writer.AuditType;
import org.bdware.doip.audit.writer.ConfigurableAuditConfig;
import org.bdware.doip.audit.writer.NoneAuditConfig;
import org.bdware.doip.encrypt.SM2Signer;
import org.bdware.doip.endpoint.server.DoipListenerConfig;
import org.bdware.doip.endpoint.server.DoipServerImpl;
import org.bdware.doip.endpoint.server.DoipServiceInfo;
import org.bdware.doip.endpoint.server.RepositoryHandler;
import org.zz.gmhelper.SM2KeyPair;

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

import static org.bdware.doip.audit.EndpointConfig.defaultDOIPServerPort;
import static org.bdware.doip.audit.EndpointConfig.defaultRepoType;

public class AuditDoipServer extends DoipServerImpl {
    private final ConfigurableAuditConfig auditConfig;
    SM2Signer signer;
    EndpointConfig config;
    static AuditIrpClient irpClient;

    public AuditDoipServer(EndpointConfig config) {
        this(config, null);
    }

    public AuditDoipServer(EndpointConfig config, DoipServiceInfo info) {
        super(resolveInfo(config, info));
        this.config = config;
        if (config.auditType == AuditType.None)
            this.auditConfig = new ConfigurableAuditConfig(new NoneAuditConfig(null));
        else {
            initIrpClientIfNeeded(config);
            this.auditConfig = irpClient.getAuditConfig();
        }

        if (config.privateKey != null && config.publicKey != null) {
            signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config)));
        } else signer = new SM2Signer(null);
    }

    private static void initIrpClientIfNeeded(EndpointConfig config) {
        if (irpClient == null)
            irpClient = new AuditIrpClient(config);
    }

    private static DoipServiceInfo resolveInfo(EndpointConfig config, DoipServiceInfo doipServiceInfo) {
        if (doipServiceInfo != null) {
            return doipServiceInfo;
        }
        initIrpClientIfNeeded(config);
        EndpointInfo info = irpClient.getEndpointInfo();
        if (info == null) {
            String content = "{\"date\":\"2022-1-13\",\"name\":\"testrepoforaibd\",\"doId\":\"bdware.test.local/Repo\",\"address\":\"tcp://127.0.0.1:" + defaultDOIPServerPort + "\",\"status\":\"已审核\",\"protocol\":\"DOIP\",\"pubKey\":\"empty\",\"version\":\"2.1\"}";
            info = EndpointInfo.fromJson(content);
        }
        List infos = new ArrayList<>();
        try {
            infos.add(new DoipListenerConfig(info.getURI(), info.getVersion()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        DoipServiceInfo ret = new DoipServiceInfo(info.getDoId(), info.getPubKey(), defaultRepoType, infos);
        return ret;
    }

    AuditRequestHandler currentRequestHandler;

    @Override
    public void setRepositoryHandler(RepositoryHandler handler) {
        currentRequestHandler = new AuditRequestHandler(serviceInfo, handler, signer, auditConfig);
        setRequestCallback(currentRequestHandler);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy