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

com.spring.boxes.dollar.support.valve.ValveExecutor Maven / Gradle / Ivy

package com.spring.boxes.dollar.support.valve;

import com.google.common.base.Preconditions;
import com.spring.boxes.dollar.*;
import com.spring.boxes.dollar.support.TerminalType;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

@Slf4j
public class ValveExecutor extends ValveUtility {

    public static boolean getDeviceValve(ValveConfigure configure, String region, TerminalType terminal, String deviceId) {
        long hashVale = getDeviceHash(deviceId);
        return getValve(configure, region, terminal, hashVale);
    }

    public static boolean getUserIdValve(ValveConfigure configure, String region, TerminalType terminal, long userId) {
        return getValve(configure, region, terminal, userId);
    }

    public static boolean getValve(ValveConfigure configure, String region, TerminalType terminal, long hashValue) {
        Preconditions.checkArgument(Objects.nonNull(configure), "Null Configure");
        Preconditions.checkArgument(StringUtils.isNotBlank(region), "Null Region");
        Preconditions.checkArgument(Objects.nonNull(terminal), "Null Terminal");

        boolean regionAllow = isRegionAllow(configure.getRegions(), region);
        log.info("Valve! regionAllow:{}", regionAllow);
        if (!regionAllow) {
            return false;
        }

        boolean terminalAllow = isTerminalAllow(configure.getTerminals(), terminal);
        log.info("Valve! terminalAllow:{}", terminalAllow);
        if (!terminalAllow) {
            return false;
        }

        boolean shuntAllow = ShuntUtils.isOnFor(configure.getValveShunt(), hashValue);
        log.info("Valve! shuntValue:{}", shuntAllow);
        return shuntAllow;
    }

    public static int getDeviceHash(String deviceId) {
        return HashUtils.getSha256Hash(deviceId);
    }

    public static boolean isRegionAllow(List regions, String region) {
        if (CollectionUtils.isEmpty(regions)) {
            return true;
        }
        boolean isAllow = false;
        for (String e : regions) {
            if (e.equalsIgnoreCase(region)) {
                isAllow = true;
                break;
            }
        }
        return isAllow;
    }

    public static boolean isTerminalAllow(List terminals, TerminalType terminal) {
        if (CollectionUtils.isEmpty(terminals)) {
            return true;
        }
        boolean isAllow = false;
        for (TerminalType e : terminals) {
            // 平台一直 & 当前版本大于许可版本
            if (e.getTerminal().equals(terminal.getTerminal()) && VersionUtils.isGt(terminal.getVersion(), e.getVersion())) {
                isAllow = true;
                break;
            }
        }
        return isAllow;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy