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

com.fizzgate.plugin.auth.ServiceConfig Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 *  Copyright (C) 2020 the original author or authors.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 */

package com.fizzgate.plugin.auth;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fizzgate.util.Consts;
import com.fizzgate.util.ThreadContext;
import com.fizzgate.util.UrlTransformUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;

import java.util.*;

/**
 * @author hongqiaowei
 */

public class ServiceConfig {

    private static final Logger log   = LoggerFactory.getLogger(ServiceConfig.class);

    private String id;

    public Map>
                                        >
           >
           apiConfigMap = new HashMap<>();

    public ServiceConfig(String id) {
        this.id = id;
    }

    public void add(ApiConfig ac) {
        for (String gatewayGroup : ac.gatewayGroups) {
            Map>> method2pathPattenMap = apiConfigMap.computeIfAbsent(gatewayGroup, k -> new HashMap<>());
            Map> pathPattern2apiConfigsMap = method2pathPattenMap.computeIfAbsent(ac.fizzMethod, k -> new HashMap<>());
            Set apiConfigs = pathPattern2apiConfigsMap.computeIfAbsent(ac.path, k -> new HashSet<>());
            apiConfigs.add(ac);
        }
        log.info("{} service add api config: {}", id, ac);
    }

    public void remove(ApiConfig ac) {
        for (String gatewayGroup : ac.gatewayGroups) {
            Map>> method2pathPattenMap = apiConfigMap.get(gatewayGroup);
            if (method2pathPattenMap != null) {
                Map> pathPattern2apiConfigsMap = method2pathPattenMap.get(ac.fizzMethod);
                if (pathPattern2apiConfigsMap != null) {
                    Set apiConfigs = pathPattern2apiConfigsMap.get(ac.path);
                    if (apiConfigs != null) {
                        apiConfigs.remove(ac);
                                                if (apiConfigs.isEmpty()) {
                                                    pathPattern2apiConfigsMap.remove(ac.path);
                                                    if (pathPattern2apiConfigsMap.isEmpty()) {
                                                        method2pathPattenMap.remove(ac.fizzMethod);
                                                        if (method2pathPattenMap.isEmpty()) {
                                                            apiConfigMap.remove(gatewayGroup);
                                                        }
                                                    }
                                                }
                    }
                }
            }
        }
        log.info("{} service remove api config: {}", id, ac);
    }

    public void update(ApiConfig ac) {
        for (String gatewayGroup : ac.gatewayGroups) {
            Map>> method2pathPattenMap = apiConfigMap.computeIfAbsent(gatewayGroup, k -> new HashMap<>());
            Map> pathPattern2apiConfigsMap = method2pathPattenMap.computeIfAbsent(ac.fizzMethod, k -> new HashMap<>());
            Set apiConfigs = pathPattern2apiConfigsMap.computeIfAbsent(ac.path, k -> new HashSet<>());
            apiConfigs.remove(ac);
            apiConfigs.add(ac);
        }
        log.info("{} service update api config: {}", id, ac);
    }

    @JsonIgnore
    public List getApiConfigs(boolean dedicatedLineRequest, Set gatewayGroups, HttpMethod method, String path) {
        ArrayList result = ThreadContext.getArrayList(ThreadContext.arrayList0);
        for (String gatewayGroup : gatewayGroups) {
            List apiConfigs = getApiConfigs(dedicatedLineRequest, gatewayGroup, method, path);
            result.addAll(apiConfigs);
        }
        return result;
    }

    @JsonIgnore
    public List getApiConfigs(boolean dedicatedLineRequest, String gatewayGroup, HttpMethod method, String path) {
        Map>> method2pathPattenMap = apiConfigMap.get(gatewayGroup);
        if (method2pathPattenMap == null) {
            return Collections.emptyList();
        } else {
            ArrayList result = ThreadContext.getArrayList();
            Map> pathPattern2apiConfigsMap = method2pathPattenMap.get(method);
            if (pathPattern2apiConfigsMap != null) {
                checkPathPattern(pathPattern2apiConfigsMap, dedicatedLineRequest, path, result);
            }
            pathPattern2apiConfigsMap = method2pathPattenMap.get(ApiConfig.ALL_METHOD);
            if (pathPattern2apiConfigsMap != null) {
                checkPathPattern(pathPattern2apiConfigsMap, dedicatedLineRequest, path, result);
            }
            return result;
        }
    }

    private void checkPathPattern(Map> pathPattern2apiConfigMap, boolean dedicatedLineRequest, String path, ArrayList result) {
        String path0 = path;
        if (!path.equals(Consts.S.FORWARD_SLASH_STR)) {
            int lastCharPos = path.length() - 1;
            char c = path.charAt(lastCharPos);
            if (c == Consts.S.FORWARD_SLASH) {
                path0 = path.substring(0, lastCharPos);
            } else {
                path0 = path + Consts.S.FORWARD_SLASH;
            }
        }
        Set>> entries = pathPattern2apiConfigMap.entrySet();
        for (Map.Entry> entry : entries) {
            String pathPattern = entry.getKey();
            Set apiConfigs = entry.getValue();
            if (pathPattern.equals(path) || pathPattern.equals(path0)) {
                for (ApiConfig ac : apiConfigs) {
                        if (dedicatedLineRequest) {
                            if (ac.dedicatedLine) {
                                result.add(ac);
                            }
                        } else {
                            if (!ac.dedicatedLine) {
                                result.add(ac);
                            }
                        }
                }
            } else if (UrlTransformUtils.ANT_PATH_MATCHER.match(pathPattern, path) || UrlTransformUtils.ANT_PATH_MATCHER.match(pathPattern, path0)) {
                for (ApiConfig ac : apiConfigs) {
                        if (dedicatedLineRequest) {
                            if (ac.dedicatedLine) {
                                result.add(ac);
                            }
                        } else {
                            if (!ac.dedicatedLine) {
                                result.add(ac);
                            }
                        }
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy