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