zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.changhong.epc.zuul.filter;
 
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.rely.api.tool.HttpContextTool;
import com.changhong.epc.zuul.filter.version.MasterVersion;
import com.changhong.epc.zuul.filter.version.NormVersion;
import com.changhong.epc.zuul.filter.version.Version;
import com.iemsoft.framework.cloud.redis.service.CacheUtils;
import com.iemsoft.framework.cloud.zuul.filter.url.UrlFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 版本号过滤器
 */
@Slf4j
@Service
public class VersionFilter implements UrlFilter, BaseConst {
 
    protected static final Map<String, Version> VERSION_ENTITY_MAP;
 
    static {
        Map<String, Version> version = new HashMap<>(2);
        /**
         * 主数据版本控制
         */
        version.put("master", new MasterVersion());
 
        /**
         * 补助标准版本控制
         */
        version.put("norm", new NormVersion());
 
        VERSION_ENTITY_MAP = Collections.unmodifiableMap(version);
    }
 
    @Override
    public void filter(HttpServletRequest request) {
        log.debug("版本号拦截url:{}", HttpContextTool.getRequestUIR(request));
        VERSION_ENTITY_MAP.entrySet()
                .stream()
                .filter(entityEntry -> HttpContextTool.getRequestUIR(request).endsWith(entityEntry.getValue().getUpdateUrl()))
                .forEach(entityEntry ->{
                    String key;
                    log.debug("更新版本号:{}", key = entityEntry.getValue().getUpdateKey());
                    CacheUtils.set(key, System.currentTimeMillis(), 0);
                });
    }
 
    public static Version getVersion(String key){
        return VERSION_ENTITY_MAP.get(key);
    }
 
    @Override
    public String getRegExp() {
        return ".+";
    }
 
}