diff --git a/src/main/java/com/nis/domain/basics/AsnIpCfg.java b/src/main/java/com/nis/domain/basics/AsnIpCfg.java index 87b005ce4..7d5703ac7 100644 --- a/src/main/java/com/nis/domain/basics/AsnIpCfg.java +++ b/src/main/java/com/nis/domain/basics/AsnIpCfg.java @@ -148,5 +148,21 @@ public class AsnIpCfg extends BaseCfg { public void setRegionId(Integer regionId) { this.regionId = regionId; } + @Override + public String toString() { + return "AsnIpCfg [regionId=" + regionId + ", ratelimit=" + ratelimit + ", ipType=" + ipType + ", ipPattern=" + + ipPattern + ", srcIpAddress=" + srcIpAddress + ", destIpAddress=" + destIpAddress + ", portPattern=" + + portPattern + ", srcPort=" + srcPort + ", destPort=" + destPort + ", asnIpGroupName=" + asnIpGroupName + + ", asnIpGroup=" + asnIpGroup + ", direction=" + direction + ", protocol=" + protocol + ", protocolId=" + + protocolId + ", compileId=" + compileId + ", isIssued=" + isIssued + ", userRegion1=" + userRegion1 + + ", userRegion2=" + userRegion2 + ", userRegion3=" + userRegion3 + ", userRegion4=" + userRegion4 + + ", userRegion5=" + userRegion5 + ", cfgId=" + cfgId + ", cfgDesc=" + cfgDesc + ", action=" + action + + ", isValid=" + isValid + ", isAudit=" + isAudit + ", creatorId=" + creatorId + ", createTime=" + + createTime + ", editorId=" + editorId + ", editTime=" + editTime + ", auditorId=" + auditorId + + ", auditTime=" + auditTime + ", serviceId=" + serviceId + ", requestId=" + requestId + + ", cancelRequestId=" + cancelRequestId + ", requestName=" + requestName + ", isAreaEffective=" + + isAreaEffective + ", classify=" + classify + ", attribute=" + attribute + ", lable=" + lable + + ", cfgType=" + cfgType + ", functionId=" + functionId + ", doLog=" + doLog + "]"; + } } diff --git a/src/main/java/com/nis/domain/basics/PolicyGroupInfo.java b/src/main/java/com/nis/domain/basics/PolicyGroupInfo.java index 162c76306..5d4ec3c1b 100644 --- a/src/main/java/com/nis/domain/basics/PolicyGroupInfo.java +++ b/src/main/java/com/nis/domain/basics/PolicyGroupInfo.java @@ -72,5 +72,10 @@ public class PolicyGroupInfo extends BaseCfg implements Seriali public void setAsnNo(Long asnNo) { this.asnNo = asnNo; } + @Override + public String toString() { + // TODO Auto-generated method stub + return super.toString(); + } } diff --git a/src/main/java/com/nis/domain/basics/Varibles.java b/src/main/java/com/nis/domain/basics/Varibles.java new file mode 100644 index 000000000..54f5b1eeb --- /dev/null +++ b/src/main/java/com/nis/domain/basics/Varibles.java @@ -0,0 +1,26 @@ +package com.nis.domain.basics; + +import java.io.Serializable; + +public class Varibles implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = -5747624301389103730L; + private String variableName; + private Long value; + public String getVariableName() { + return variableName; + } + public void setVariableName(String variableName) { + this.variableName = variableName; + } + public Long getValue() { + return value; + } + public void setValue(Long value) { + this.value = value; + } + +} diff --git a/src/main/java/com/nis/domain/configuration/BaseCfg.java b/src/main/java/com/nis/domain/configuration/BaseCfg.java index b4a7a5191..a664d1cef 100644 --- a/src/main/java/com/nis/domain/configuration/BaseCfg.java +++ b/src/main/java/com/nis/domain/configuration/BaseCfg.java @@ -30,6 +30,14 @@ import com.nis.web.security.UserUtils; * @version V1.0 */ public class BaseCfg extends BaseEntity implements Cloneable{ + protected Integer index;//excel导入时所在的行 + + public Integer getIndex() { + return index; + } + public void setIndex(Integer index) { + this.index = index; + } protected Integer areaType; protected List areaIsp; protected List areaCfg; diff --git a/src/main/java/com/nis/domain/configuration/BaseIpCfg.java b/src/main/java/com/nis/domain/configuration/BaseIpCfg.java index 4e98f88f2..c9ce097c0 100644 --- a/src/main/java/com/nis/domain/configuration/BaseIpCfg.java +++ b/src/main/java/com/nis/domain/configuration/BaseIpCfg.java @@ -343,5 +343,10 @@ public class BaseIpCfg extends BaseCfg { public void setIndexTable(String indexTable) { this.indexTable = indexTable; } - + public Integer getIndex() { + return index; + } + public void setIndex(Integer index) { + this.index = index; + } } diff --git a/src/main/java/com/nis/domain/configuration/BaseStringCfg.java b/src/main/java/com/nis/domain/configuration/BaseStringCfg.java index 802003922..8796a0613 100644 --- a/src/main/java/com/nis/domain/configuration/BaseStringCfg.java +++ b/src/main/java/com/nis/domain/configuration/BaseStringCfg.java @@ -245,5 +245,10 @@ public class BaseStringCfg extends BaseCfg { } return list; } - + public Integer getIndex() { + return index; + } + public void setIndex(Integer index) { + this.index = index; + } } diff --git a/src/main/java/com/nis/domain/configuration/ComplexkeywordCfg.java b/src/main/java/com/nis/domain/configuration/ComplexkeywordCfg.java index 5344e8541..df8bba28c 100644 --- a/src/main/java/com/nis/domain/configuration/ComplexkeywordCfg.java +++ b/src/main/java/com/nis/domain/configuration/ComplexkeywordCfg.java @@ -216,4 +216,10 @@ public class ComplexkeywordCfg extends BaseCfg{ } return list; } + public Integer getIndex() { + return index; + } + public void setIndex(Integer index) { + this.index = index; + } } diff --git a/src/main/java/com/nis/domain/configuration/template/AppDomainTemplate.java b/src/main/java/com/nis/domain/configuration/template/AppDomainTemplate.java index 4b379f180..6de65bea2 100644 --- a/src/main/java/com/nis/domain/configuration/template/AppDomainTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/AppDomainTemplate.java @@ -13,7 +13,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入字符串类配置 */ -public class AppDomainTemplate { +public class AppDomainTemplate extends BasicTemplate{ private String cfgDesc; private String cfgKeywords; private Integer matchMethod ; diff --git a/src/main/java/com/nis/domain/configuration/template/AsnIpTemplate.java b/src/main/java/com/nis/domain/configuration/template/AsnIpTemplate.java index 43c15ff90..396a07f93 100644 --- a/src/main/java/com/nis/domain/configuration/template/AsnIpTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/AsnIpTemplate.java @@ -5,7 +5,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入ASN IP类配置 */ -public class AsnIpTemplate { +public class AsnIpTemplate extends BasicTemplate{ /** * @Fields serialVersionUID:TODO(用一句话描述这个变量表示什么) diff --git a/src/main/java/com/nis/domain/configuration/template/BasicTemplate.java b/src/main/java/com/nis/domain/configuration/template/BasicTemplate.java new file mode 100644 index 000000000..9f874bbd0 --- /dev/null +++ b/src/main/java/com/nis/domain/configuration/template/BasicTemplate.java @@ -0,0 +1,17 @@ +package com.nis.domain.configuration.template; + +public class BasicTemplate { + /** + * 数据在excel中的第几行 + */ + protected Integer index; + + public Integer getIndex() { + return index; + } + + public void setIndex(Integer index) { + this.index = index; + } + +} diff --git a/src/main/java/com/nis/domain/configuration/template/ComplexStringAllTemplate.java b/src/main/java/com/nis/domain/configuration/template/ComplexStringAllTemplate.java index ef0ab7a96..18e23d19e 100644 --- a/src/main/java/com/nis/domain/configuration/template/ComplexStringAllTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/ComplexStringAllTemplate.java @@ -13,7 +13,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入增强字符串类配置 */ -public class ComplexStringAllTemplate { +public class ComplexStringAllTemplate extends BasicTemplate{ private String cfgDesc; private String district; diff --git a/src/main/java/com/nis/domain/configuration/template/DnsResStrategyTemplate.java b/src/main/java/com/nis/domain/configuration/template/DnsResStrategyTemplate.java index 81df09e59..98c58d362 100644 --- a/src/main/java/com/nis/domain/configuration/template/DnsResStrategyTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/DnsResStrategyTemplate.java @@ -2,7 +2,7 @@ package com.nis.domain.configuration.template; import com.nis.util.excel.ExcelField; -public class DnsResStrategyTemplate { +public class DnsResStrategyTemplate extends BasicTemplate{ @ExcelField(title="policy_name",align=2,sort=1) private String cfgDesc; @ExcelField(title="group",align=2,sort=2) diff --git a/src/main/java/com/nis/domain/configuration/template/IpAllTemplate.java b/src/main/java/com/nis/domain/configuration/template/IpAllTemplate.java index ebab64ba7..01e03464e 100644 --- a/src/main/java/com/nis/domain/configuration/template/IpAllTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/IpAllTemplate.java @@ -14,7 +14,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入IP类配置 */ -public class IpAllTemplate { +public class IpAllTemplate extends BasicTemplate{ /** * @Fields serialVersionUID:TODO(用一句话描述这个变量表示什么) diff --git a/src/main/java/com/nis/domain/configuration/template/IpWhitelistTemplate.java b/src/main/java/com/nis/domain/configuration/template/IpWhitelistTemplate.java index 20b1c9b1d..dd512c90a 100644 --- a/src/main/java/com/nis/domain/configuration/template/IpWhitelistTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/IpWhitelistTemplate.java @@ -13,7 +13,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入IP白名单配置模板 */ -public class IpWhitelistTemplate { +public class IpWhitelistTemplate extends BasicTemplate{ /** * @Fields serialVersionUID:TODO(用一句话描述这个变量表示什么) diff --git a/src/main/java/com/nis/domain/configuration/template/StringAllTemplate.java b/src/main/java/com/nis/domain/configuration/template/StringAllTemplate.java index 46f8dbd21..c6e1b097d 100644 --- a/src/main/java/com/nis/domain/configuration/template/StringAllTemplate.java +++ b/src/main/java/com/nis/domain/configuration/template/StringAllTemplate.java @@ -13,7 +13,7 @@ import com.nis.util.excel.ExcelField; /** * @Description: excel导入字符串类配置 */ -public class StringAllTemplate { +public class StringAllTemplate extends BasicTemplate{ private String cfgDesc; private String cfgKeywords; diff --git a/src/main/java/com/nis/domain/specific/ConfigGroupInfo.java b/src/main/java/com/nis/domain/specific/ConfigGroupInfo.java index b00eea4fe..7dade5be2 100644 --- a/src/main/java/com/nis/domain/specific/ConfigGroupInfo.java +++ b/src/main/java/com/nis/domain/specific/ConfigGroupInfo.java @@ -1,11 +1,17 @@ package com.nis.domain.specific; +import java.io.Serializable; import java.util.Date; -import com.google.gson.annotations.Expose; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + import com.nis.util.excel.ExcelField; -public class ConfigGroupInfo { +public class ConfigGroupInfo implements Serializable{ + /** + * + */ + private static final long serialVersionUID = -4539188977409108742L; private Integer id; private Integer groupId; @ExcelField(title="group_name",sort=302) @@ -72,5 +78,10 @@ public class ConfigGroupInfo { public void setGroupType(Integer groupType) { this.groupType = groupType; } + @Override + public String toString() { + // TODO Auto-generated method stub + return ReflectionToStringBuilder.toString(this); + } } diff --git a/src/main/java/com/nis/util/AsnCacheUtils.java b/src/main/java/com/nis/util/AsnCacheUtils.java new file mode 100644 index 000000000..d59d07007 --- /dev/null +++ b/src/main/java/com/nis/util/AsnCacheUtils.java @@ -0,0 +1,147 @@ +package com.nis.util; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.log4j.Logger; + +import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.web.dao.specific.ConfigGroupInfoDao; +import com.nis.web.service.SpringContextHolder; + +import jersey.repackaged.com.google.common.collect.Maps; +import net.sf.ehcache.Cache; +import net.sf.ehcache.Element; + +/** + * asn no缓存工具类 + * @author Administrator + * + */ +public class AsnCacheUtils{ + private static Logger logger=Logger.getLogger(AsnCacheUtils.class); + /** + * ASN号缓存 + */ + private static final String ASN_NO_CACHE = "asnNoCache"; + private static final int cache_rage = 1000; + private final static ConfigGroupInfoDao configGroupInfoDao = SpringContextHolder.getBean(ConfigGroupInfoDao.class); + /** + * 获取缓存 + * @param cacheName + * @param key + * @return + */ + public static ConfigGroupInfo get(Long key) { + Element element = getCache(ASN_NO_CACHE).get(key/cache_rage); + if(element!=null) { + Map map=(Map)element.getObjectValue(); + if(map.containsKey(key)) { + return map.get(key); + } + } + return null; + } + public static Map getMap(Object key) { + Element element = getCache(ASN_NO_CACHE).get(key); + return (Map)element.getObjectValue(); + } + /** + * 初始化缓存 + */ + //@PostConstruct + public synchronized static void init() { + long start=System.currentTimeMillis(); + logger.warn("AsnCacheUtils init start..."); + Cache cache=getCache(ASN_NO_CACHE); + //查询总量 + Long count=configGroupInfoDao.getCountByType(4); + //缓存key + boolean loadDatabase=false; + if(cache.getKeys().size()==0) { + loadDatabase=true; + }else { + long c=0l; + for(Object key:cache.getKeys()) { + c+=getMap(key).size(); + } + if(c!=count) { + loadDatabase=true; + } + } + if(loadDatabase) { + List list=configGroupInfoDao.findAllList(4); + Map> groupMap=Maps.newHashMap(); + for(ConfigGroupInfo configGroupInfo:list) { + if(groupMap.containsKey(configGroupInfo.getAsnId()/cache_rage)) { + groupMap.get(configGroupInfo.getAsnId()/cache_rage) + .put(configGroupInfo.getAsnId(), configGroupInfo); + }else { + Map m=Maps.newHashMap(); + m.put(configGroupInfo.getAsnId(), configGroupInfo); + groupMap.put(configGroupInfo.getAsnId()/cache_rage, m); + } + } + for(Entry> e:groupMap.entrySet()) { + Element element = new Element(e.getKey(), e.getValue()); + cache.put(element); + } + } + long end=System.currentTimeMillis(); + logger.warn("AsnCacheUtils init finish,cost:"+(end-start)); + } + /** + * 写入ASN_NO_CACHE缓存 + * @param key + * @return + */ + public static void put(Long key, ConfigGroupInfo value) { + Long _key=key/cache_rage; + Element element = getCache(ASN_NO_CACHE).get(_key); + if(element==null) { + Map map=Maps.newHashMap(); + map.put(key, value); + element = new Element(_key, map); + }else { + Map map=(Map)element.getObjectValue(); + map.put(key, value); + element = new Element(_key, map); + } + getCache(ASN_NO_CACHE).put(element); + } + /** + * 从缓存中移除 + * @param cacheName + * @param key + */ + public static void remove(String key) { + getCache(ASN_NO_CACHE).remove(key); + } + public static void remove(Long key) { + Long _key=key/cache_rage; + Element element = getCache(ASN_NO_CACHE).get(_key); + if(element!=null) { + Map map=(Map)element.getObjectValue(); + if(map.containsKey(key)) { + map.remove(key); + } + if(map.isEmpty()) { + getCache(ASN_NO_CACHE).remove(_key); + }else { + element=new Element(_key,map); + getCache(ASN_NO_CACHE).put(element); + } + + } + } + private static Cache getCache(String cacheName){ + Cache cache = CacheUtils.getCacheManager().getCache(cacheName); + if (cache == null){ + CacheUtils.getCacheManager().addCache(cacheName); + cache = CacheUtils.getCacheManager().getCache(cacheName); + cache.getCacheConfiguration().setEternal(true); + } + return cache; + } +} \ No newline at end of file diff --git a/src/main/java/com/nis/util/Constants.java b/src/main/java/com/nis/util/Constants.java index 80fadc82d..695ce1c82 100644 --- a/src/main/java/com/nis/util/Constants.java +++ b/src/main/java/com/nis/util/Constants.java @@ -3,6 +3,7 @@ package com.nis.util; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; import com.google.gson.GsonBuilder; @@ -707,4 +708,21 @@ public final class Constants { public static final String CERT_VALIDATE_FILE=Configurations.getStringProperty("cert_validate_file", "x509"); //证书校验成功关键字 public static final String CERT_VALIDATE_SUCCESS_INFO=Configurations.getStringProperty("cert_validate_success_info", "x509"); + /** + * 一些正则的pattern,预先编译,避免多次编译 + */ + public static final Pattern IPV4_IP_RANGE_REGEXP_NEW_PATTERN = Pattern.compile(IPV4_IP_RANGE_REGEXP_NEW); + public static final Pattern RANGE_PATTERN = Pattern.compile("\\d+\\-\\d+"); + public static final Pattern PORT_MASK_PATTERN = Pattern.compile("^[0-9]+/[0-9]+$"); + public static final Pattern PORT_PATTERN = Pattern.compile("^[0-9]+$"); + public static final Pattern IPV4_IP_SUBNET_ORIGINAL_PATTERN = Pattern.compile(IPV4_IP_SUBNET_REGEXP_ORIGINAL); + public static final Pattern IPV4_IP_SUBNET_PATTERN = Pattern.compile(IPV4_IP_SUBNET_REGEXP); + public static final Pattern IPV6_IP_SUBNET_PATTERN = Pattern.compile(IPV6_IP_SUBNET_REGEXP); + public static final Pattern IPV4_IP_RANGE_PATTERN = Pattern.compile(IPV4_IP_RANGE_REGEXP); + public static final Pattern IPV6_IP_RANGE_PATTERN = Pattern.compile(IPV6_IP_RANGE_REGEXP); + public static final Pattern IPV4_IP_PATTERN = Pattern.compile(IPV4_IP_REGEXP); + public static final Pattern IPV6_IP_PATTERN = Pattern.compile(IPV6_IP_REGEXP); + //IP复用maat json中的ip region单次send 最大个数 + public static final Integer MAAT_JSON_SEND_SIZE=Configurations.getIntProperty("maat_json_send_size", 1000); + public static final Integer MULITY_THREAD_SIZE=Configurations.getIntProperty("mulity_thread_size", 5); } diff --git a/src/main/java/com/nis/util/excel/CheckComplexStringFormatThread.java b/src/main/java/com/nis/util/excel/CheckComplexStringFormatThread.java new file mode 100644 index 000000000..cad6c61ce --- /dev/null +++ b/src/main/java/com/nis/util/excel/CheckComplexStringFormatThread.java @@ -0,0 +1,318 @@ +package com.nis.util.excel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import org.jets3t.service.ServiceException; +import org.springframework.beans.BeanUtils; + +import com.beust.jcommander.internal.Lists; +import com.beust.jcommander.internal.Sets; +import com.nis.domain.FunctionRegionDict; +import com.nis.domain.FunctionServiceDict; +import com.nis.domain.configuration.ComplexkeywordCfg; +import com.nis.domain.configuration.DnsResStrategy; +import com.nis.util.Constants; +import com.nis.web.service.configuration.DnsResStrategyService; + +public class CheckComplexStringFormatThread implements Callable{ + private Logger logger=Logger.getLogger(CheckComplexStringFormatThread.class); + private BlockingQueue srcQueue; + private BlockingQueue destQueue; + private Properties prop; + private DnsResStrategyService dnsResStrategyService; + private FunctionServiceDict serviceDict; + private FunctionRegionDict regionDict; + public CheckComplexStringFormatThread(FunctionServiceDict serviceDict,FunctionRegionDict regionDict,Properties prop,BlockingQueue srcQueue,BlockingQueue destQueue) { + this.serviceDict=serviceDict; + this.regionDict=regionDict; + this.srcQueue=srcQueue; + this.destQueue=destQueue; + this.prop=prop; + } + @Override + public String call() throws Exception { + List dataList=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + String msg=null; + while(!srcQueue.isEmpty()) { + int size=srcQueue.drainTo(dataList,Constants.MAAT_JSON_SEND_SIZE); + if(regionDict.getRegionType().intValue()==3) { + try { + List cfgs=this.checkComplexStringCfg(dataList); + destQueue.addAll(cfgs); + }catch (Exception e) { + // TODO: handle exception + msg=e.getMessage(); + break; + } + } + dataList.clear(); + } + return msg; + } + public List checkComplexStringCfg( + List list) throws ServiceException { + logger.warn("start to validate complexString data..."); + long start=System.currentTimeMillis(); + List stringList = new ArrayList(); + String exprTypeP = regionDict.getConfigExprType(); + if (StringUtils.isBlank(exprTypeP)) { + throw new RuntimeException("Found String region,but exprType is Empty"); + } + String matchMethodP = regionDict.getConfigMatchMethod(); + if (StringUtils.isBlank(matchMethodP)) { + throw new RuntimeException("Found String region,but matchMethod is Empty"); + } + String hexP = regionDict.getConfigHex(); + if (StringUtils.isBlank(hexP)) { + throw new RuntimeException("Found String region,but hex is Empty"); + } + String mulityKeywordsP = regionDict.getConfigMultiKeywords(); + if (StringUtils.isBlank(mulityKeywordsP)) { + throw new RuntimeException("Found String region,but mulityKeywords is Empty"); + } + String dirtrictP = regionDict.getConfigDistrict(); + StringBuffer errTip = new StringBuffer(); + Pattern pattern = Pattern.compile("\t|\r|\n|\b|\f"); + for (int i = 0; i < list.size(); i++) { + StringBuffer errInfo = new StringBuffer(); + ComplexkeywordCfg baseStringCfg = new ComplexkeywordCfg(); + BeanUtils.copyProperties(list.get(i), baseStringCfg); + if (regionDict.getRegionType().equals(3)) { + if (regionDict.getFunctionId().equals(7)) { + Long dnsStrategyId = baseStringCfg.getDnsStrategyId(); + if (dnsStrategyId != null&&dnsStrategyId>0) { + List dnsStrategys = dnsResStrategyService.findDnsResStrategys(dnsStrategyId, + Constants.VALID_YES, Constants.AUDIT_YES); + if (dnsStrategys == null || dnsStrategys.size() == 0) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("dns_res_strategy")) + ";"); + } + } + } + String keyword = baseStringCfg.getCfgKeywords(); + String district = baseStringCfg.getDistrict(); + if (StringUtils.isBlank(keyword)) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("key_word") + " ") + ";"); + } + if (StringUtils.isNotBlank(dirtrictP)) { + if (StringUtils.isBlank(district)) { + if (dirtrictP.indexOf(",") == -1) { + baseStringCfg.setDistrict(dirtrictP); + } else { + // baseStringCfg.setDistrict(dirtrictP.split(",")[0]); + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("district") + " ") + + ";"); + } + } else if (dirtrictP.indexOf(district) == -1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("district") + " ") + + ";"); + } else if (dirtrictP.indexOf("others")>-1&&district.equals("others")) { + //不允许自定义匹配区域导入 + errInfo.append(prop.getProperty("district")+" "+ + String.format(prop.getProperty("can_not_be"), " 'others'")+ ";"); + } + } + if (mulityKeywordsP.equals("0")) { + if (keyword.indexOf("\n") > -1) { + errInfo.append( + String.format(prop.getProperty("not_multiple"), prop.getProperty("key_word")) + ";"); + } + Matcher m = pattern.matcher(keyword); + if (m.find()) { + errInfo.append(String.format(prop.getProperty("has_invisible_char"), + prop.getProperty("key_word") + " '" + keyword + "'") + ";"); + } + } else { + boolean has = false; + Set keywordSet=Sets.newHashSet(); + + for (String key : keyword.split("\n")) { + Matcher m = pattern.matcher(key); + if (m.find()) { + has = true; + errInfo.append(String.format(prop.getProperty("has_invisible_char"), + prop.getProperty("key_word") + " '" + key + "'") + ";"); + break; + } + if(!keywordSet.contains(key)) { + keywordSet.add(key); + }else { + errInfo.append(prop.getProperty("key_word") + " '" + key + "'"+" "+prop.getProperty("repeat") + ";"); + } + } + if (!has) { + if(keyword.replaceAll("\n","").length()>1024) { + errInfo.append(String.format(prop.getProperty("most_keywords"), + prop.getProperty("key_word")) + ";"); + }else { + String reWord = keyword.replaceAll("\n", Constants.KEYWORD_EXPR); + baseStringCfg.setCfgKeywords(reWord); + } + } + } + Integer exprType = baseStringCfg.getExprType(); + boolean has = false; + if (exprType == null) { + if (exprTypeP.indexOf(",") == -1) { + if (mulityKeywordsP.equals("0") && exprTypeP.equals("1")) { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + baseStringCfg.setExprType(Integer.parseInt(exprTypeP)); + } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("0")) { + baseStringCfg.setExprType(0); + } else if (exprTypeP.indexOf("1") > -1 && mulityKeywordsP.equals("1") + && keyword.indexOf("\n") > -1) { + baseStringCfg.setExprType(1); + } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("1") + && keyword.indexOf("\n") == -1) { + baseStringCfg.setExprType(0); + } else { + baseStringCfg.setExprType(Integer.parseInt(exprTypeP.split(",")[0])); + } + // errInfo.append(String.format(prop.getProperty("can_not_null"), + // prop.getProperty("expression_type"))+";"); + } else { + for (String exp : exprTypeP.split(",")) { + if (exp.equals(exprType.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("expression_type")) + + ";"); + } + has = false; + } + exprType = baseStringCfg.getExprType(); + Integer matchMethod = baseStringCfg.getMatchMethod(); + if (matchMethod == null) { + if (matchMethodP.indexOf(",") == -1) { + if (exprTypeP.equals("1") && !matchMethodP.equals("0")) { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP)); + } else if (exprType != null && exprType.intValue() == 1) { + if (matchMethodP.indexOf("0") > -1) { + baseStringCfg.setMatchMethod(0); + } else { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + + } else { + baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP.split(",")[0])); + } + // errInfo.append(String.format(prop.getProperty("can_not_null"), + // prop.getProperty("match_method"))+";"); + } else { + for (String exp : matchMethodP.split(",")) { + if (exp.equals(matchMethod.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("match_method")) + + ";"); + } + } + Integer isHex = baseStringCfg.getIsHex(); + Integer isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); + if (isHex == null || isCaseInsenstive == null) { + if (isHex == null) { + if (hexP.equals("0") || hexP.equals("2")) { + baseStringCfg.setIsHex(0); + } else if (hexP.equals("1")) { + baseStringCfg.setIsHex(1); + } else { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("is_hex")) + ";"); + } + } + if (isCaseInsenstive == null) { + if (hexP.equals("0") || hexP.equals("1")) { + baseStringCfg.setIsCaseInsenstive(0); + } else if (hexP.equals("2")) { + baseStringCfg.setIsCaseInsenstive(1); + } else { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("is_case_insenstive")) + ";"); + } + } + } else { + if (isHex.intValue() != 0 && isHex.intValue() != 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (isCaseInsenstive.intValue() != 0 && isCaseInsenstive.intValue() != 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + if (hexP.indexOf("1") == -1 && isHex.intValue() == 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (hexP.equals("1") && isHex.intValue() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (hexP.indexOf("2") == -1 && isCaseInsenstive.intValue() == 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + if (hexP.equals("2") && isCaseInsenstive.intValue() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + } + isHex = baseStringCfg.getIsHex(); + isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); + if (isHex != null && isCaseInsenstive != null) { + if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 0) { + baseStringCfg.setIsHexbin(0); + } else if (isHex.intValue() == 1 && isCaseInsenstive.intValue() == 0) { + baseStringCfg.setIsHexbin(1); + } else if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 1) { + baseStringCfg.setIsHexbin(2); + }else { + errInfo.append(prop.getProperty("hex_case_insensitive")+ ";"); + } + } + } + if (errInfo.toString().length() > 0) {// + errTip.append(String.format(prop.getProperty("line"), baseStringCfg.getIndex()) + ","); + errTip.append(errInfo); + errTip.append("
"); + } + stringList.add(baseStringCfg); + } + if (errTip.toString().length() > 0) { + throw new ServiceException(errTip.toString()); + } + long end=System.currentTimeMillis(); + logger.warn("validate complexString data finish,cost:"+(end-start)); + return stringList; + } + public DnsResStrategyService getDnsResStrategyService() { + return dnsResStrategyService; + } + public void setDnsResStrategyService(DnsResStrategyService dnsResStrategyService) { + this.dnsResStrategyService = dnsResStrategyService; + } +} diff --git a/src/main/java/com/nis/util/excel/CheckDnsResStrategyFormatThread.java b/src/main/java/com/nis/util/excel/CheckDnsResStrategyFormatThread.java new file mode 100644 index 000000000..fed0b6439 --- /dev/null +++ b/src/main/java/com/nis/util/excel/CheckDnsResStrategyFormatThread.java @@ -0,0 +1,155 @@ +package com.nis.util.excel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import org.jets3t.service.ServiceException; +import org.springframework.beans.BeanUtils; + +import com.beust.jcommander.internal.Lists; +import com.beust.jcommander.internal.Sets; +import com.nis.domain.FunctionRegionDict; +import com.nis.domain.FunctionServiceDict; +import com.nis.domain.basics.PolicyGroupInfo; +import com.nis.domain.configuration.ComplexkeywordCfg; +import com.nis.domain.configuration.DnsResStrategy; +import com.nis.util.Constants; +import com.nis.web.service.basics.PolicyGroupInfoService; +import com.nis.web.service.configuration.DnsResStrategyService; + +public class CheckDnsResStrategyFormatThread implements Callable{ + private Logger logger=Logger.getLogger(CheckDnsResStrategyFormatThread.class); + private BlockingQueue srcQueue; + private BlockingQueue destQueue; + private Properties prop; + private DnsResStrategyService dnsResStrategyService; + private FunctionServiceDict serviceDict; + private FunctionRegionDict regionDict; + private PolicyGroupInfoService policyGroupInfoService; + public CheckDnsResStrategyFormatThread(FunctionServiceDict serviceDict,FunctionRegionDict regionDict,Properties prop,BlockingQueue srcQueue,BlockingQueue destQueue) { + this.serviceDict=serviceDict; + this.regionDict=regionDict; + this.srcQueue=srcQueue; + this.destQueue=destQueue; + this.prop=prop; + } + @Override + public String call() throws Exception { + List dataList=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + String msg=null; + while(!srcQueue.isEmpty()) { + int size=srcQueue.drainTo(dataList,Constants.MAAT_JSON_SEND_SIZE); + if(regionDict.getRegionType().intValue()==6) { + try { + List cfgs=this.checkDnsResStrategyCfg(dataList); + destQueue.addAll(cfgs); + }catch (Exception e) { + // TODO: handle exception + msg=e.getMessage(); + break; + } + } + dataList.clear(); + } + return msg; + } + public List checkDnsResStrategyCfg(List list) + throws ServiceException { + List dnsResStrategies=Lists.newArrayList(); + StringBuffer errTip = new StringBuffer(); + for (int i = 0; i < list.size(); i++) { + StringBuffer errInfo = new StringBuffer(); + DnsResStrategy dnsResStrategy=new DnsResStrategy(); + BeanUtils.copyProperties(list.get(i), dnsResStrategy); + String groupName=dnsResStrategy.getCfgDesc(); + if(StringUtils.isBlank(groupName)) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("policy_name")) + ";"); + } + Integer resGroup1Id=dnsResStrategy.getResGroup1Id(); + if(resGroup1Id==null) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("group")) + ";"); + }else { + PolicyGroupInfo info=policyGroupInfoService.getById(resGroup1Id); + if(info==null) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("group")) + ";"); + } + } + Integer resGroup1Num=dnsResStrategy.getResGroup1Num(); + if(resGroup1Num==null) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("res_group_num")) + ";"); + } + String ttl=dnsResStrategy.getTtl(); + if(StringUtils.isBlank(ttl)) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("min_ttl")) + ";"); + }else { + Pattern p=Constants.RANGE_PATTERN; + Matcher m=p.matcher(ttl); + if(!m.matches()) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("min_ttl")) + ";"); + }else { + String minTtl=ttl.split("-")[0]; + String maxTtl=ttl.split("-")[1]; + Integer min=null,max=null; + try { + min=Integer.parseInt(minTtl); + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("min_ttl")) + ";"); + } + try { + max=Integer.parseInt(maxTtl); + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("min_ttl")) + ";"); + } + if(min.intValue()>max.intValue()) + {errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("min_ttl")) + ";"); + }else { + dnsResStrategy.setMinTtl(min); + dnsResStrategy.setMaxTtl(max); + } + } + } + if (errInfo.toString().length() > 0) {// + errTip.append(String.format(prop.getProperty("line"), dnsResStrategy.getIndex()) + ","); + errTip.append(errInfo); + errTip.append("
"); + } + dnsResStrategies.add(dnsResStrategy); + } + if (errTip.toString().length() > 0) { + throw new ServiceException(errTip.toString()); + } + return dnsResStrategies; + } + public DnsResStrategyService getDnsResStrategyService() { + return dnsResStrategyService; + } + public void setDnsResStrategyService(DnsResStrategyService dnsResStrategyService) { + this.dnsResStrategyService = dnsResStrategyService; + } + public PolicyGroupInfoService getPolicyGroupInfoService() { + return policyGroupInfoService; + } + public void setPolicyGroupInfoService(PolicyGroupInfoService policyGroupInfoService) { + this.policyGroupInfoService = policyGroupInfoService; + } + +} diff --git a/src/main/java/com/nis/util/excel/CheckIpFormatThread.java b/src/main/java/com/nis/util/excel/CheckIpFormatThread.java new file mode 100644 index 000000000..7772ccaf2 --- /dev/null +++ b/src/main/java/com/nis/util/excel/CheckIpFormatThread.java @@ -0,0 +1,1046 @@ +package com.nis.util.excel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import org.jets3t.service.ServiceException; +import org.springframework.beans.BeanUtils; + +import com.beust.jcommander.internal.Lists; +import com.nis.domain.FunctionRegionDict; +import com.nis.domain.FunctionServiceDict; +import com.nis.domain.SysDataDictionaryItem; +import com.nis.domain.configuration.BaseIpCfg; +import com.nis.domain.configuration.DnsResStrategy; +import com.nis.domain.configuration.IpPortCfg; +import com.nis.domain.configuration.template.IpRateLimitTemplate; +import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.util.AsnCacheUtils; +import com.nis.util.Constants; +import com.nis.util.DictUtils; +import com.nis.web.service.configuration.DnsResStrategyService; + +public class CheckIpFormatThread implements Callable{ + private Logger logger=Logger.getLogger(CheckIpFormatThread.class); + private BlockingQueue srcQueue; + private BlockingQueue destQueue; + private Properties prop; + private DnsResStrategyService dnsResStrategyService; + private FunctionServiceDict serviceDict; + private FunctionRegionDict regionDict; + private List> asnNoMaps; + public CheckIpFormatThread(FunctionServiceDict serviceDict,FunctionRegionDict regionDict,Properties prop,BlockingQueue srcQueue,BlockingQueue destQueue) { + this.serviceDict=serviceDict; + this.regionDict=regionDict; + this.srcQueue=srcQueue; + this.destQueue=destQueue; + this.prop=prop; + } + @Override + public String call() throws Exception { + List dataList=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + String msg=null; + while(!srcQueue.isEmpty()) { + int size=srcQueue.drainTo(dataList,Constants.MAAT_JSON_SEND_SIZE); + if(regionDict.getRegionType().intValue()==1) { + try { + List ipcfgs=this.checkIpCfg(asnNoMaps, dataList); + boolean success=destQueue.addAll(ipcfgs); + }catch (Exception e) { + e.printStackTrace(); + // TODO: handle exception + msg=e.getMessage(); + break; + } + } + dataList.clear(); + } + return msg; + } + public List checkIpCfg(List> asnNos, List list) + throws ServiceException { + logger.warn("start to validate ip..."); + long start=System.currentTimeMillis(); + List ipList = new ArrayList(); + // ip必填一个,configIpPortShow不能为空,否则字典配错; + String configIpPortShow = regionDict.getConfigIpPortShow(); + if (StringUtils.isBlank(configIpPortShow)) { + throw new RuntimeException("Found IP region,but configIpPortShow is Empty"); + } + String ipType = regionDict.getConfigIpType(); + if (StringUtils.isBlank(ipType)) { + throw new RuntimeException("Found IP region,but ipType is Empty"); + } + boolean canIPv4 = ipType.indexOf("4") > -1; + boolean canIPv6 = ipType.indexOf("6") > -1; + String ipPattern = regionDict.getConfigIpPattern(); + if (StringUtils.isBlank(ipPattern)) { + throw new RuntimeException("Found IP region,but ipPattern is Empty"); + } + boolean canIPFormat = ipType.indexOf("3") > -1; + boolean canIPSubnetFormat = ipType.indexOf("1") > -1; + boolean canIPRangeFormat = ipType.indexOf("2") > -1; + String portPattern = regionDict.getConfigPortPattern(); + String protocol = regionDict.getConfigProtocol(); + String direction = regionDict.getConfigDirection(); + StringBuffer errTip = new StringBuffer(); + for (int i = 0; i < list.size(); i++) { + StringBuffer errInfo = new StringBuffer(); + IpPortCfg baseIpCfg = new IpPortCfg(); + BeanUtils.copyProperties(list.get(i), baseIpCfg); + // 特殊字段验证 + // packet ip ratelimit + if (serviceDict!=null&&serviceDict.getFunctionId().intValue() == 5 && serviceDict.getAction().intValue() == 64) { + String userRegion1 = IpRateLimitTemplate.userRegion1; + baseIpCfg.setUserRegion1(userRegion1); + if("0".equals(userRegion1)) { + String userRegion2 = baseIpCfg.getUserRegion2(); + try { + // Double ratelimt= Double.parseDouble(userRegion2); + List ratelimtList = DictUtils.getDictList("RATE_LIMIT"); + boolean has = false; + for (SysDataDictionaryItem ratelimit : ratelimtList) { + if (ratelimit.getItemCode().equals(userRegion2)) { + has = true; + break; + } + } + if (!has) { + errInfo.append(prop.getProperty("ratelimit") + "." + + String.format(prop.getProperty("must_between"), 0.001, 0.009) + ";"); + } + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("ratelimit")) + ";"); + } + }else if("1".equals(userRegion1)) { + String userRegion3 = baseIpCfg.getUserRegion3(); + if (StringUtils.isNotBlank(userRegion3)) { + try { + Integer ratelimt=Integer.parseInt(userRegion3); + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("bandwith")) + ";"); + } + }else { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("bandwith")) + ";"); + } + + } + + } + // payload ip + if (regionDict.getFunctionId().equals(212)) { + // replace_zone + String userRegion1 = baseIpCfg.getUserRegion1(); + if (StringUtils.isNotBlank(userRegion1)) { + Object val = DictUtils.getDictLabel("INTERCEPT_REPLACE_ZONE", userRegion1); + if (StringUtils.isBlank(val.toString())) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("replace_zone")) + + ";"); + } + } else { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("replace_zone")) + ";"); + } + + // replaced_content + String userRegion2 = baseIpCfg.getUserRegion2(); + if (StringUtils.isBlank(userRegion2)) { + errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("replaced_content")) + + ";"); + } + // replace_content + String userRegion3 = baseIpCfg.getUserRegion3(); + if (StringUtils.isBlank(userRegion3)) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("replace_content")) + ";"); + } + } + // dns ip + if (regionDict.getFunctionId().equals(7)) { + Integer dnsStrategyId = baseIpCfg.getDnsStrategyId(); + if (dnsStrategyId != null&&dnsStrategyId>0) { + List dnsStrategys = dnsResStrategyService + .findDnsResStrategys(Long.valueOf(dnsStrategyId), Constants.VALID_YES, Constants.AUDIT_YES); + if (dnsStrategys == null || dnsStrategys.size() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("dns_res_strategy")) + + ";"); + } + } + } + // dns fake ip + if (regionDict.getFunctionId().equals(401)) { + Integer dnsStrategyId = baseIpCfg.getDnsStrategyId(); + if (dnsStrategyId != null) { + if(dnsStrategyId>0) { + List dnsStrategys = dnsResStrategyService + .findDnsResStrategys(Long.valueOf(dnsStrategyId), Constants.VALID_YES, Constants.AUDIT_YES); + if (dnsStrategys == null || dnsStrategys.size() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("dns_res_strategy")) + + ";"); + } + } + }else { + baseIpCfg.setDnsStrategyId(0); + } + } + //p2p ip + if (regionDict.getFunctionId().equals(510) && "p2p_ip".equals(regionDict.getConfigServiceType())) { // P2p + // IP + String userRegion1 = baseIpCfg.getUserRegion1(); + List ipTypes = DictUtils.getDictList("P2P_IP_TYPE"); + boolean has = false; + for (SysDataDictionaryItem type : ipTypes) { + if (type.getItemCode().equals(userRegion1)) { + has = true; + break; + } + } + if (!has) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("p2p_ip_config_type")) + + ";"); + } + } + if (regionDict.getFunctionId().equals(600)) {//asn + //asn id + String userRegion1 = baseIpCfg.getUserRegion1(); + if(StringUtils.isBlank(userRegion1)) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("asn_no")) + + ";"); + }else { + try { + Long asnNo=Long.parseLong(userRegion1); + if(asnNo.longValue()>4294967295l||asnNo.longValue()<1) { + errInfo.append(prop.getProperty("asn_no")+" "+ + String.format(prop.getProperty("must_between"),"1","4294967295" ) + + ";"); + }else { +// ConfigGroupInfo configGroupInfo=asnIpCfgService.getConfigGroupInfoByAsnNo(asnNo); + ConfigGroupInfo configGroupInfo=AsnCacheUtils.get(asnNo); + if(asnNos!=null) { + //缓存中没有 + if(configGroupInfo==null) { + asnNos.get(0).put(asnNo,-1); + }else { + asnNos.get(1).put(asnNo,configGroupInfo.getGroupId()); + } + } + } + + }catch (Exception e) { + e.printStackTrace(); + // TODO: handle exception + errInfo.append( + String.format(prop.getProperty("not_number"), prop.getProperty("asn_no")) + + ";"); + } + } + } + if (regionDict.getFunctionId().equals(301)) { + String antiddosProtocol=baseIpCfg.getAntiddosProtocol(); + Long bpsThreadshold= baseIpCfg.getBpsThreadshold(); + Long ppsThreadshold= baseIpCfg.getPpsThreadshold(); + if(StringUtils.isNotBlank(antiddosProtocol)) { + List antiddosProtocolDicts=DictUtils.getDictList("ANTIDDOS_PROTOCOL"); + boolean has=false; + for(SysDataDictionaryItem ditc:antiddosProtocolDicts) { + if(ditc.getItemCode().equals(antiddosProtocol)) { + has=true; + break; + } + } + if(!has) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("antiddos_protocol")) + + ";"); + } + if(bpsThreadshold==null) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("bps_threadshold")) + + ";"); + } + if(ppsThreadshold==null) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("pps_threadshold")) + + ";"); + } + } + } + if (regionDict.getRegionType().equals(1)) { + boolean srcIpEmpty = false; + boolean destIpEmpty = false; + boolean srcPortEmpty = false; + boolean destPortEmpty = false; + Pattern ipv4IpRangeRegexpNew = Constants.IPV4_IP_RANGE_REGEXP_NEW_PATTERN; + if (StringUtils.isBlank(baseIpCfg.getSrcIpAddress())) { + srcIpEmpty = true; + }else { + //处理0.0.0.1-2这种格式,转换为0.0.0.1-0.0.0.2 + Matcher m=ipv4IpRangeRegexpNew.matcher(baseIpCfg.getSrcIpAddress()); + if(m.matches()) { + String prefix=baseIpCfg.getSrcIpAddress().split("-")[0]; + String subfix=baseIpCfg.getSrcIpAddress().split("-")[1]; + baseIpCfg.setSrcIpAddress(prefix+"-"+prefix.substring(0, prefix.lastIndexOf(".")+1)+subfix); + } + } + if (StringUtils.isBlank(baseIpCfg.getDestIpAddress())) { + destIpEmpty = true; + }else { + //处理0.0.0.1-2这种格式,转换为0.0.0.1-0.0.0.2 + Matcher m=ipv4IpRangeRegexpNew.matcher(baseIpCfg.getDestIpAddress()); + if(m.matches()) { + String prefix=baseIpCfg.getDestIpAddress().split("-")[0]; + String subfix=baseIpCfg.getDestIpAddress().split("-")[1]; + baseIpCfg.setDestIpAddress(prefix+"-"+prefix.substring(0, prefix.lastIndexOf(".")+1)+subfix); + } + } + if (StringUtils.isBlank(baseIpCfg.getSrcPort())) { + srcPortEmpty = true; + } + if (StringUtils.isBlank(baseIpCfg.getDestPort())) { + destPortEmpty = true; + } + if (srcIpEmpty && destIpEmpty) { + if (configIpPortShow.indexOf("1") > -1 && configIpPortShow.indexOf("3") > -1) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("client_ip", "Client IP")) + ";"); + } else if (configIpPortShow.indexOf("1") > -1) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("client_ip", "Client IP")) + ";"); + } else if (configIpPortShow.indexOf("3") > -1) { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("server_ip", "Server IP")) + ";"); + } + } else if (destIpEmpty) {// 目的IP为空 + if (configIpPortShow.indexOf("3") > -1 && configIpPortShow.indexOf("1") == -1) {// 目的IP必填 + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("server_ip", "Server IP")) + ";"); + } else if (configIpPortShow.indexOf("1") > -1) { + String matchType = this.validIPAddress("client_ip", errInfo, baseIpCfg.getSrcIpAddress(), + ipType, ipPattern); + if (matchType.startsWith("IPV6")) { + if (matchType.endsWith("RANGE")) { + // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); + baseIpCfg.setIpPattern(2); + if (baseIpCfg.getSrcIpAddress() + .startsWith(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { + errInfo.append(prop.getProperty("client_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0]) + + ";"); + } + } else if (matchType.endsWith("SUBNET")) { + // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); + baseIpCfg.setIpPattern(1); + if (baseIpCfg.getSrcIpAddress() + .startsWith(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { + errInfo.append( + prop.getProperty("client_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) + + ";"); + } + } else { + // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); + baseIpCfg.setIpPattern(3); + if (baseIpCfg.getSrcIpAddress().equals(Constants.IPV6_DEFAULT_IP_VALUE)) { + errInfo.append(prop.getProperty("client_ip")+" " + String.format( + prop.getProperty("can_not_be"), Constants.IPV6_DEFAULT_IP_VALUE) + ";"); + } + } + baseIpCfg.setIpType(6); + } else { + if (matchType.endsWith("RANGE")) { + this.validIPv4Range("client_ip", errInfo, baseIpCfg.getSrcIpAddress()); + // baseIpCfg.setSrcIpAddress("0.0.0.0-"+baseIpCfg.getSrcIpAddress().split("-")[0].substring(0,baseIpCfg.getSrcIpAddress().indexOf("-")+1)+baseIpCfg.getSrcIpAddress().split("-")[1]); + baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); + baseIpCfg.setIpPattern(2); + if (baseIpCfg.getSrcIpAddress() + .startsWith(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { + errInfo.append(prop.getProperty("client_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0]) + + ";"); + } + } else if (matchType.endsWith("SUBNET")) { + // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); + baseIpCfg.setIpPattern(1); + if (baseIpCfg.getSrcIpAddress() + .startsWith(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { + errInfo.append( + prop.getProperty("client_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) + + ";"); + } + } else { + baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); + baseIpCfg.setIpPattern(3); + if (baseIpCfg.getSrcIpAddress().equals(Constants.IPV4_DEFAULT_IP_VALUE)) { + errInfo.append(prop.getProperty("client_ip")+" " + String.format( + prop.getProperty("can_not_be"), Constants.IPV4_DEFAULT_IP_VALUE) + ";"); + } + } + baseIpCfg.setIpType(4); + } + } + + } else if (srcIpEmpty) {// 源IP为空 + if (configIpPortShow.indexOf("1") > -1 && configIpPortShow.indexOf("3") == -1) {// 源IP必填 + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("client_ip", "Client IP")) + ";"); + } else if (configIpPortShow.indexOf("3") > -1) { + String matchType = this.validIPAddress("server_ip", errInfo, baseIpCfg.getDestIpAddress(), + ipType, ipPattern); + if (matchType.startsWith("IPV6")) { + if (matchType.endsWith("RANGE")) { + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); + baseIpCfg.setIpPattern(2); + if (baseIpCfg.getDestIpAddress() + .startsWith(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { + errInfo.append(prop.getProperty("server_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0]) + + ";"); + } + } else if (matchType.endsWith("SUBNET")) { + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); + baseIpCfg.setIpPattern(1); + if (baseIpCfg.getDestIpAddress() + .startsWith(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { + errInfo.append( + prop.getProperty("server_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) + + ";"); + } + } else { + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); + baseIpCfg.setIpPattern(3); + if (baseIpCfg.getDestIpAddress().equals(Constants.IPV6_DEFAULT_IP_VALUE)) { + errInfo.append(prop.getProperty("server_ip")+" " + String.format( + prop.getProperty("can_not_be"), Constants.IPV6_DEFAULT_IP_VALUE) + ";"); + } + } + baseIpCfg.setIpType(6); + } else { + if (matchType.endsWith("RANGE")) { + this.validIPv4Range("server_ip", errInfo, baseIpCfg.getDestIpAddress()); + // baseIpCfg.setDestIpAddress("0.0.0.0-"+baseIpCfg.getDestIpAddress().split("-")[0] + // .substring(0,baseIpCfg.getDestIpAddress().indexOf("-")+1) + // +baseIpCfg.getDestIpAddress().split("-")[1]); + baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); + baseIpCfg.setIpPattern(2); + if (baseIpCfg.getDestIpAddress() + .startsWith(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { + errInfo.append(prop.getProperty("server_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0]) + + ";"); + } + } else if (matchType.endsWith("SUBNET")) { + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); + baseIpCfg.setIpPattern(1); + if (baseIpCfg.getDestIpAddress() + .startsWith(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { + errInfo.append( + prop.getProperty("server_ip")+" " + + String.format(prop.getProperty("can_not_be"), + Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) + + ";"); + } + } else { + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); + baseIpCfg.setIpPattern(3); + if (baseIpCfg.getDestIpAddress().equals(Constants.IPV4_DEFAULT_IP_VALUE)) { + errInfo.append(prop.getProperty("server_ip")+" " + String.format( + prop.getProperty("can_not_be"), Constants.IPV4_DEFAULT_IP_VALUE) + ";"); + } + } + baseIpCfg.setIpType(4); + } + } + } else {// 全不为空 + String matchType = this.validIPAddress("client_ip", errInfo, baseIpCfg.getSrcIpAddress(), ipType, + ipPattern); + String matchType1 = this.validIPAddress("server_ip", errInfo, baseIpCfg.getDestIpAddress(), ipType, + ipPattern); + if (StringUtils.isNotBlank(matchType) && StringUtils.isNotBlank(matchType1) + && !matchType.equals(matchType1)) { + errInfo.append(prop.get("the_same_ip_type_pattern") + ";"); + } + // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); + // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); + if (matchType.startsWith("IPV6")) { + baseIpCfg.setIpType(6); + } else { + baseIpCfg.setIpType(4); + } + if (matchType.endsWith("RANGE")) { + if (matchType.startsWith("IPV4")) { + this.validIPv4Range("client_ip", errInfo, baseIpCfg.getSrcIpAddress()); + this.validIPv4Range("server_ip", errInfo, baseIpCfg.getDestIpAddress()); + } + + if (baseIpCfg.getSrcIpAddress().split("-")[0] + .equals(baseIpCfg.getDestIpAddress().split("-")[0])) { + errInfo.append(String.format(prop.getProperty("are_the_same"), + baseIpCfg.getSrcIpAddress().split("-")[0], + baseIpCfg.getDestIpAddress().split("-")[0]) + ";"); + } + baseIpCfg.setIpPattern(2); + } else if (matchType.endsWith("SUBNET")) { + if (baseIpCfg.getSrcIpAddress().split("/")[0] + .equals(baseIpCfg.getDestIpAddress().split("/")[0])) { + errInfo.append(String.format(prop.getProperty("are_the_same"), + baseIpCfg.getSrcIpAddress().split("/")[0], + baseIpCfg.getDestIpAddress().split("/")[0]) + ";"); + } + baseIpCfg.setIpPattern(1); + } else { + if (baseIpCfg.getSrcIpAddress().equals(baseIpCfg.getDestIpAddress())) { + errInfo.append(String.format(prop.getProperty("are_the_same"), + prop.getProperty("client_ip"), prop.getProperty("server_ip")) + ";"); + } + baseIpCfg.setIpPattern(3); + } + } + + // TODO 判断源IP和目的IP的值 + // TODO 判断源IP和目的IP格式 + if (destPortEmpty) { + if (srcPortEmpty) { + baseIpCfg.setSrcPort("0"); + baseIpCfg.setDestPort("0"); + baseIpCfg.setPortPattern(1); + } else { + if (baseIpCfg.getSrcPort().indexOf("/") > -1) { + baseIpCfg.setDestPort("0/0"); + baseIpCfg.setPortPattern(2); + } else { + baseIpCfg.setDestPort("0"); + baseIpCfg.setPortPattern(1); + } + baseIpCfg.setSrcPort(baseIpCfg.getSrcPort().trim()); + } + } else { + if (srcPortEmpty) { + if (baseIpCfg.getDestPort().indexOf("/") > -1) { + baseIpCfg.setSrcPort("0/0"); + baseIpCfg.setPortPattern(2); + } else { + baseIpCfg.setSrcPort("0"); + baseIpCfg.setPortPattern(1); + } + baseIpCfg.setDestPort(baseIpCfg.getSrcPort().trim()); + } else { + if (baseIpCfg.getSrcPort().indexOf("/") > -1) { + baseIpCfg.setPortPattern(2); + } else { + baseIpCfg.setPortPattern(1); + } + baseIpCfg.setSrcPort(baseIpCfg.getSrcPort().trim()); + baseIpCfg.setDestPort(baseIpCfg.getDestPort().trim()); + + } + } + // TODO 判断源端口和目的端口格式 + // TODO 判断源和目的端口的值 + boolean validPort = this.validPort(errInfo, baseIpCfg.getSrcPort(), baseIpCfg.getDestPort(), + portPattern); + if (baseIpCfg.getProtocol()==null) { + //packet ip reject + if(regionDict.getFunctionId().equals(5)&&serviceDict!=null&&serviceDict.getServiceId().equals(16)) { + baseIpCfg.setProtocol(6); + }else { + if (StringUtils.isBlank(regionDict.getConfigProtocol())) { + baseIpCfg.setProtocol(0); + } else { + baseIpCfg.setProtocol(Integer.parseInt(regionDict.getConfigProtocol().split(",")[0])); + } + + } + + baseIpCfg.setProtocolId(serviceDict==null?0:serviceDict.getProtocolId()); + } else { + baseIpCfg.setProtocol(baseIpCfg.getProtocol()); + baseIpCfg.setProtocolId(serviceDict==null?0:serviceDict.getProtocolId()); + } + // TODO 判断Protocol的值 + boolean validProtocol = this.validProtocol(errInfo, baseIpCfg.getProtocol(), protocol); + // 验证protocol与port是否符合逻辑 + if (validProtocol && validPort) { + validProtocolAndPort(errInfo, baseIpCfg.getProtocol(), baseIpCfg.getSrcPort(), + baseIpCfg.getDestPort()); + } + if (baseIpCfg.getDirection()==null) { + if (StringUtils.isBlank(regionDict.getConfigDirection())) { + baseIpCfg.setDirection(0); + } else { + baseIpCfg.setDirection(Integer.parseInt(regionDict.getConfigDirection().split(",")[0])); + } + } else { + baseIpCfg.setDirection(baseIpCfg.getDirection()); + } + + // TODO 判断Direction的值 + this.validDirection(errInfo, baseIpCfg.getDirection(), direction); + } + if (errInfo.toString().length() > 0) {// + errTip.append(String.format(prop.getProperty("line"), baseIpCfg.getIndex()) + ","); + errTip.append(errInfo); + errTip.append("
"); + } + ipList.add(baseIpCfg); + } + if (errTip.toString().length() > 0) { + throw new ServiceException(errTip.toString()); + } + long end=System.currentTimeMillis(); + logger.warn("validate ip finish,cost:"+(end-start)); + return ipList; + } + /** + * 协议与端口验证规则 + * + * @param errInfo + * @param protocol + * @param srcPort + * @param destPort + */ + public void validProtocolAndPort(StringBuffer errInfo, Integer protocol, String srcPort, String destPort) { + if (srcPort.indexOf("/") > -1) { + srcPort = srcPort.split("/")[0]; + } + if (destPort.indexOf("/") > -1) { + destPort = destPort.split("/")[0]; + } + if (protocol == 0) { + return; + } else if (protocol != 6 && protocol != 17) { + if (!"0".equals(srcPort) || !"0".equals(destPort)) { + errInfo.append(prop.get("protocol_and_port") + ";"); + } + } + + } + + /** + * 验证方向 + * + * @param errInfo + * @param direction + * @param directionPattern + * @throws ServiceException + */ + public boolean validDirection(StringBuffer errInfo, Integer direction, String directionPattern) + throws ServiceException { + if (direction == null) { + errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("direction")) + ";"); + return false; + } + boolean has = false; + for (String p : directionPattern.split(",")) { + if (p.equals(direction.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("direction")) + "." + + String.format(prop.getProperty("alternative_values"), directionPattern) + ";"); + } + return has; + } + + /** + * 验证协议 + * + * @param errInfo + * @param protocol + * @param protocolPattern + * @return + * @throws ServiceException + */ + public boolean validProtocol(StringBuffer errInfo, Integer protocol, String protocolPattern) + throws ServiceException { + if (protocol == null) { + errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("protocol")) + ";"); + return false; + } + boolean has = false; + for (String p : protocolPattern.split(",")) { + if (p.equals(protocol.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("protocol")) + "." + + String.format(prop.getProperty("alternative_values"), protocolPattern) + ";"); + } + return has; + } + + /** + * 验证端口格式 + * + * @param srcPortStr + * @param destPortStr + * @param portPattern + * @throws ServiceException + */ + public boolean validPort(StringBuffer errInfo, String srcPortStr, String destPortStr, String portPattern) + throws ServiceException { + boolean valid = true; + if (portPattern.equals("1")) { + try { + Integer srcPort = Integer.parseInt(srcPortStr); + if (srcPort < 0 || srcPort > 65535) { + errInfo.append(prop.getProperty("client_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("client_port")) + ";"); + valid = false; + } + try { + Integer destPort = Integer.parseInt(destPortStr); + if (destPort < 0 || destPort > 65535) { + errInfo.append(prop.getProperty("server_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } catch (Exception e) { + // TODO: handle exception + errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("server_port")) + ";"); + valid = false; + } + } else if (portPattern.equals("2")) { + Pattern p = Constants.PORT_MASK_PATTERN; + Matcher m = p.matcher(srcPortStr); + if (!m.matches()) { + errInfo.append( + String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("client_port")) + ";"); + valid = false; + } + m = p.matcher(destPortStr); + if (!m.matches()) { + errInfo.append( + String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("server_port")) + ";"); + valid = false; + } + Integer srcPort = Integer.parseInt(srcPortStr.split("/")[0]); + Integer srcPortMask = Integer.parseInt(srcPortStr.split("/")[1]); + Integer destPort = Integer.parseInt(destPortStr.split("/")[0]); + Integer destPortMask = Integer.parseInt(destPortStr.split("/")[1]); + if (srcPort < 0 || srcPort > 65535) { + errInfo.append(prop.getProperty("client_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + if (srcPortMask < 0 || srcPortMask > 65535) { + errInfo.append(prop.getProperty("client_port_mask") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + if (destPort < 0 || destPort > 65535) { + errInfo.append(prop.getProperty("server_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + if (destPortMask < 0 || destPortMask > 65535) { + errInfo.append(prop.getProperty("server_port_mask") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } else if (portPattern.indexOf("1") > -1 && portPattern.indexOf("2") > -1) { + Pattern p = Constants.PORT_MASK_PATTERN; + Pattern p1 = Constants.PORT_PATTERN; + Matcher m = p.matcher(srcPortStr);// 源端口是端口掩码格式 + Matcher m1 = p.matcher(destPortStr);// 目的端口是端口掩码格式 + Matcher m2 = p1.matcher(srcPortStr);// 源端口是端口格式 + Matcher m3 = p1.matcher(destPortStr);// 目的端口是端口格式 + if (m.matches()) { + Integer srcPort = Integer.parseInt(srcPortStr.split("/")[0]); + Integer srcPortMask = Integer.parseInt(srcPortStr.split("/")[1]); + if (srcPort < 0 || srcPort > 65535) { + errInfo.append(prop.getProperty("client_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + if (srcPortMask < 0 || srcPortMask > 65535) { + errInfo.append(prop.getProperty("client_port_mask") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } + if (m1.matches()) { + Integer destPort = Integer.parseInt(destPortStr.split("/")[0]); + Integer destPortMask = Integer.parseInt(destPortStr.split("/")[1]); + + if (destPort < 0 || destPort > 65535) { + errInfo.append(prop.getProperty("server_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + if (destPortMask < 0 || destPortMask > 65535) { + errInfo.append(prop.getProperty("server_port_mask") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } + if (m2.matches()) { + try { + Integer srcPort = Integer.parseInt(srcPortStr); + if (srcPort < 0 || srcPort > 65535) { + errInfo.append(prop.getProperty("client_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } catch (Exception e) { + // TODO: handle exception + errInfo.append( + String.format(prop.getProperty("not_number"), prop.getProperty("client_port")) + ";"); + valid = false; + } + } + if (m3.matches()) { + try { + Integer destPort = Integer.parseInt(destPortStr); + if (destPort < 0 || destPort > 65535) { + errInfo.append(prop.getProperty("server_port") + + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); + valid = false; + } + } catch (Exception e) { + // TODO: handle exception + errInfo.append( + String.format(prop.getProperty("not_number"), prop.getProperty("server_port")) + ";"); + valid = false; + } + } + if ((m.matches() && m3.matches()) || (m2.matches() && m1.matches())) { + errInfo.append(prop.getProperty("the_same_port_pattern") + ";"); + valid = false; + } + if (!m.matches() && !m2.matches()) { + errInfo.append( + String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("client_port")) + ";"); + valid = false; + } + if (!m1.matches() && !m3.matches()) { + errInfo.append( + String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("server_port")) + ";"); + valid = false; + } + + } + return valid; + } + /** + * IPv4 范围验证 + * + * @param ipName + * @param errInfo + * @param ip + */ + public void validIPv4Range(String ipName, StringBuffer errInfo, String ip) { + String prefix = ip.split("-")[0]; + String subfix = ip.split("-")[1]; + if ((Integer.parseInt(prefix.split("\\.")[0])<128&&Integer.parseInt(subfix.split("\\.")[0])<128) + ||(Integer.parseInt(prefix.split("\\.")[0])>127&&Integer.parseInt(subfix.split("\\.")[0])>127&&Integer.parseInt(prefix.split("\\.")[0])<192&&Integer.parseInt(subfix.split("\\.")[0])<192) + ||(Integer.parseInt(prefix.split("\\.")[0])>191&&Integer.parseInt(subfix.split("\\.")[0])>191&&Integer.parseInt(prefix.split("\\.")[0])<224&&Integer.parseInt(subfix.split("\\.")[0])<224) + ||(Integer.parseInt(prefix.split("\\.")[0])>223&&Integer.parseInt(subfix.split("\\.")[0])>223&&Integer.parseInt(prefix.split("\\.")[0])<240&&Integer.parseInt(subfix.split("\\.")[0])<240) + ||(Integer.parseInt(prefix.split("\\.")[0])>239&&Integer.parseInt(subfix.split("\\.")[0])>239&&Integer.parseInt(prefix.split("\\.")[0])<256&&Integer.parseInt(subfix.split("\\.")[0])<256)) { + }else { + errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_in_the_same") + ";"); + } +// if (Integer.parseInt(prefix.split("\\.")[0]) < 192 || Integer.parseInt(prefix.split("\\.")[0]) > 223 +// || Integer.parseInt(subfix.split("\\.")[0]) < 192 || Integer.parseInt(subfix.split("\\.")[0]) > 223) { +// errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_in_c") + ";"); +// } + if (!prefix.split("\\.")[0].equals(subfix.split("\\.")[0]) + || !prefix.split("\\.")[1].equals(subfix.split("\\.")[1]) + || !prefix.split("\\.")[2].equals(subfix.split("\\.")[2])) { + errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_bit_field") + ";"); + } + if (ipToInt(prefix) >= ipToInt(subfix)) { + errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_smaller") + ";"); + } + } + + /** + * 根据位运算把 byte[] -> int + * + * @param bytes + * @return int + */ + public static int bytesToInt(byte[] bytes) { + int addr = bytes[3] & 0xFF; + addr |= ((bytes[2] << 8) & 0xFF00); + addr |= ((bytes[1] << 16) & 0xFF0000); + addr |= ((bytes[0] << 24) & 0xFF000000); + return addr; + } + + public static int ipToInt(String ipAddr) { + try { + return bytesToInt(ipToBytesByReg(ipAddr)); + } catch (Exception e) { + throw new IllegalArgumentException(ipAddr + " is invalid IP"); + } + } + + /** + * 把IP地址转化为int + * + * @param ipAddr + * @return int + */ + public static byte[] ipToBytesByReg(String ipAddr) { + byte[] ret = new byte[4]; + try { + String[] ipArr = ipAddr.split("\\."); + ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF); + ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF); + ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF); + ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF); + return ret; + } catch (Exception e) { + throw new IllegalArgumentException(ipAddr + " is invalid IP"); + } + + } + + /** + * 校验ip + * + * @param ipName + * @param errInfo + * @param ip + * @param ipType + * @param ipPattern + * @return + * @throws ServiceException + */ + public String validIPAddress(String ipName, StringBuffer errInfo, String ip, String ipType, String ipPattern) + throws ServiceException { + String matchType = ""; + Pattern ipv4IpSubnetOriginalRegexp = Constants.IPV4_IP_SUBNET_ORIGINAL_PATTERN; + Pattern ipv4IpSubnetRegexp = Constants.IPV4_IP_SUBNET_PATTERN; + Pattern ipv6IpSubnetRegexp = Constants.IPV6_IP_SUBNET_PATTERN; + Pattern ipv4IpRangeRegexp = Constants.IPV4_IP_RANGE_PATTERN; + Pattern ipv6IpRangeRegexp = Constants.IPV6_IP_RANGE_PATTERN; + Pattern ipv4IpRegexp = Constants.IPV4_IP_PATTERN; + Pattern ipv6IpRegexp = Constants.IPV6_IP_PATTERN; + //减少正则匹配次数 + if(ip.indexOf("-")==-1&&ip.indexOf("/")==-1) { + Matcher matcher = ipv4IpRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV4_IP"; + }else { + matcher = ipv6IpRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV6_IP"; + } else { + errInfo.append(String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty(ipName)) + + ";"); + } + } + }else if(ip.indexOf("/")!=-1) { + Matcher matcher = ipv4IpSubnetRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV4_IP_SUBNET"; + } else { + matcher = ipv6IpSubnetRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV6_IP_SUBNET"; + } else { + errInfo.append(String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty(ipName)) + + ";"); + } + } + }else if(ip.indexOf("-")!=-1) { + Matcher matcher = ipv4IpRangeRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV4_IP_RANGE"; + } else { + matcher = ipv6IpRangeRegexp.matcher(ip); + if (matcher.matches()) { + matchType = "IPV6_IP_RANGE"; + } else { + matcher = ipv4IpSubnetOriginalRegexp.matcher(ip); + if(matcher.matches()) { + errInfo.append(prop.getProperty(ipName)+" "+prop.getProperty("ipv4_mask_range_tip")+ ";"); + }else { + errInfo.append( + String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty(ipName)) + + ";"); + } + } + } + } + if (ipType.indexOf("4") == -1 && matchType.startsWith("IPV4")) { + errInfo.append( + prop.getProperty("ip_type") + " " + String.format(prop.getProperty("can_not_be"), "IPv4") + ",4;"); + } + if (ipType.indexOf("6") == -1 && matchType.startsWith("IPV6")) { + errInfo.append( + prop.getProperty("ip_type") + " " + String.format(prop.getProperty("can_not_be"), "IPv6") + ",6;"); + } + if (ipPattern.indexOf("3") == -1 && matchType.endsWith("IP")) { + errInfo.append( + prop.getProperty("ip_pattern") + " " + String.format(prop.getProperty("can_not_be"), "IP") + ",3;"); + } + if (ipPattern.indexOf("1") == -1 && matchType.endsWith("SUBNET")) { + errInfo.append(prop.getProperty("ip_pattern") + " " + + String.format(prop.getProperty("can_not_be"), prop.getProperty("ip_subnet")) + ",1;"); + } + if (ipPattern.indexOf("2") == -1 && matchType.endsWith("RANGE")) { + errInfo.append(prop.getProperty("ip_pattern") + " " + + String.format(prop.getProperty("can_not_be"), prop.getProperty("ip_range")) + ",2;"); + } + return matchType; + } + + public DnsResStrategyService getDnsResStrategyService() { + return dnsResStrategyService; + } + public void setDnsResStrategyService(DnsResStrategyService dnsResStrategyService) { + this.dnsResStrategyService = dnsResStrategyService; + } + public List> getAsnNoMaps() { + return asnNoMaps; + } + public void setAsnNoMaps(List> asnNoMaps) { + this.asnNoMaps = asnNoMaps; + } + +} diff --git a/src/main/java/com/nis/util/excel/CheckStringFormatThread.java b/src/main/java/com/nis/util/excel/CheckStringFormatThread.java new file mode 100644 index 000000000..eb160cd93 --- /dev/null +++ b/src/main/java/com/nis/util/excel/CheckStringFormatThread.java @@ -0,0 +1,330 @@ +package com.nis.util.excel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import org.jets3t.service.ServiceException; +import org.springframework.beans.BeanUtils; + +import com.beust.jcommander.internal.Lists; +import com.beust.jcommander.internal.Sets; +import com.nis.domain.FunctionRegionDict; +import com.nis.domain.FunctionServiceDict; +import com.nis.domain.SysDataDictionaryItem; +import com.nis.domain.configuration.BaseStringCfg; +import com.nis.util.Constants; +import com.nis.util.DictUtils; +import com.nis.web.service.configuration.DnsResStrategyService; + +public class CheckStringFormatThread implements Callable{ + private Logger logger=Logger.getLogger(CheckStringFormatThread.class); + private BlockingQueue srcQueue; + private BlockingQueue> destQueue; + private Properties prop; + private DnsResStrategyService dnsResStrategyService; + private FunctionServiceDict serviceDict; + private FunctionRegionDict regionDict; + public CheckStringFormatThread(FunctionServiceDict serviceDict,FunctionRegionDict regionDict,Properties prop,BlockingQueue srcQueue,BlockingQueue> destQueue) { + this.serviceDict=serviceDict; + this.regionDict=regionDict; + this.srcQueue=srcQueue; + this.destQueue=destQueue; + this.prop=prop; + } + @Override + public String call() throws Exception { + List dataList=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + String msg=null; + while(!srcQueue.isEmpty()) { + int size=srcQueue.drainTo(dataList,Constants.MAAT_JSON_SEND_SIZE); + if(regionDict.getRegionType().intValue()==2) { + try { + List> cfgs=this.checkStringCfg(dataList); + destQueue.addAll(cfgs); + }catch (Exception e) { + // TODO: handle exception + msg=e.getMessage(); + break; + } + } + dataList.clear(); + } + return msg; + } + + public List> checkStringCfg(List list) throws ServiceException { + logger.warn("start to validate stringCfg data..."); + long start=System.currentTimeMillis(); + List> stringList = new ArrayList>(); + String exprTypeP = regionDict.getConfigExprType(); + if (StringUtils.isBlank(exprTypeP)) { + throw new RuntimeException("Found String region,but exprType is Empty"); + } + String matchMethodP = regionDict.getConfigMatchMethod(); + if (StringUtils.isBlank(matchMethodP)) { + throw new RuntimeException("Found String region,but matchMethod is Empty"); + } + String hexP = regionDict.getConfigHex(); + if (StringUtils.isBlank(hexP)) { + throw new RuntimeException("Found String region,but hex is Empty"); + } + String mulityKeywordsP = regionDict.getConfigMultiKeywords(); + if (StringUtils.isBlank(mulityKeywordsP)) { + throw new RuntimeException("Found String region,but mulityKeywords is Empty"); + } + StringBuffer errTip = new StringBuffer(); + Pattern pattern = Pattern.compile("\t|\r|\n|\b|\f"); + Pattern domainPattern = Pattern.compile(Constants.DOMAIN_REGEXP); + for (int i = 0; i < list.size(); i++) { + StringBuffer errInfo = new StringBuffer(); + BaseStringCfg baseStringCfg = new BaseStringCfg(); + BeanUtils.copyProperties(list.get(i), baseStringCfg); + if (regionDict.getRegionType().equals(2)) { + if (regionDict.getFunctionId().equals(510) && "p2p_hash".equals(regionDict.getConfigServiceType())) { + String userRegion1 = baseStringCfg.getUserRegion1(); + if (StringUtils.isNotBlank(userRegion1)) { + List hashs = DictUtils.getDictList("P2P_HASH_TYPE"); + boolean has = false; + for (SysDataDictionaryItem hash : hashs) { + if (hash.getItemCode().equals(userRegion1)) { + has = true; + break; + } + } + if (!has) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), + prop.getProperty("p2p_hash_type") + " ") + ";"); + } + } + } + String keyword = baseStringCfg.getCfgKeywords(); + if (!regionDict.getFunctionId().equals(403)) { + if (StringUtils.isBlank(keyword)) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("key_word") + " ") + ";"); + } + if (mulityKeywordsP.equals("0")) { + if (keyword.indexOf("\n") > -1) { + errInfo.append( + String.format(prop.getProperty("not_multiple"), prop.getProperty("key_word")) + ";"); + } + Matcher m = pattern.matcher(keyword); + if (m.find()) { + errInfo.append(String.format(prop.getProperty("has_invisible_char"), + prop.getProperty("key_word") + " '" + keyword + "'") + ";"); + } + } else { + boolean has = false; + Set keywordSet=Sets.newHashSet(); + for (String key : keyword.split("\n")) { + Matcher m = pattern.matcher(key); + if (m.find()) { + has = true; + errInfo.append(String.format(prop.getProperty("has_invisible_char"), + prop.getProperty("key_word") + " '" + key + "'") + ";"); + break; + } + if(!keywordSet.contains(key)) { + keywordSet.add(key); + }else { + errInfo.append(prop.getProperty("key_word") + " '" + key + "'"+" "+prop.getProperty("repeat") + ";"); + } + } + if (!has) { + if(keyword.replaceAll("\n","").length()>1024) { + errInfo.append(String.format(prop.getProperty("most_keywords"), + prop.getProperty("key_word")) + ";"); + }else { + String reWord = keyword.replaceAll("\n", Constants.KEYWORD_EXPR); + baseStringCfg.setCfgKeywords(reWord); + } + } + } + }else { + if (StringUtils.isBlank(keyword)) { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("domain_name") + " ") + ";"); + }else { + Matcher m = pattern.matcher(keyword); + if (m.find()) { + errInfo.append(String.format(prop.getProperty("has_invisible_char"), + prop.getProperty("domain_name") + " '" + keyword + "'") + ";"); + }else { + m = domainPattern.matcher(keyword); + if(!m.matches()) { + errInfo.append(String.format(prop.getProperty("not_valid_domain"), + prop.getProperty("domain_name") + " '" + keyword + "'") + ";"); + }else { + baseStringCfg.setDomain(keyword); + } + + } + } + + } + + Integer exprType = baseStringCfg.getExprType(); + boolean has = false; + if (exprType == null) { + if (exprTypeP.indexOf(",") == -1) { + if (mulityKeywordsP.equals("0") && exprTypeP.equals("1")) { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + baseStringCfg.setExprType(Integer.parseInt(exprTypeP)); + } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("0")) { + baseStringCfg.setExprType(0); + } else if (exprTypeP.indexOf("1") > -1 && mulityKeywordsP.equals("1") + && keyword.indexOf("\n") > -1) { + baseStringCfg.setExprType(1); + } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("1") + && keyword.indexOf("\n") == -1) { + baseStringCfg.setExprType(0); + } else { + baseStringCfg.setExprType(Integer.parseInt(exprTypeP.split(",")[0])); + } + // errInfo.append(String.format(prop.getProperty("can_not_null"), + // prop.getProperty("expression_type"))+";"); + } else { + for (String exp : exprTypeP.split(",")) { + if (exp.equals(exprType.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("expression_type")) + + ";"); + } + has = false; + } + exprType = baseStringCfg.getExprType(); + Integer matchMethod = baseStringCfg.getMatchMethod(); + if (matchMethod == null) { + if (matchMethodP.indexOf(",") == -1) { + if (exprTypeP.equals("1") && !matchMethodP.equals("0")) { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP)); + } else if (exprType != null && exprType.intValue() == 1) { + if (matchMethodP.indexOf("0") > -1) { + baseStringCfg.setMatchMethod(0); + } else { + throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); + } + + } else { + baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP.split(",")[0])); + } + // errInfo.append(String.format(prop.getProperty("can_not_null"), + // prop.getProperty("match_method"))+";"); + } else { + for (String exp : matchMethodP.split(",")) { + if (exp.equals(matchMethod.toString())) { + has = true; + break; + } + } + if (!has) { + errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("match_method")) + + ";"); + } + } + + Integer isHex = baseStringCfg.getIsHex(); + Integer isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); + if (isHex == null || isCaseInsenstive == null) { + if (isHex == null) { + if (hexP.equals("0") || hexP.equals("2")) { + baseStringCfg.setIsHex(0); + } else if (hexP.equals("1")) { + baseStringCfg.setIsHex(1); + } else { + errInfo.append( + String.format(prop.getProperty("can_not_null"), prop.getProperty("is_hex")) + ";"); + } + } + if (isCaseInsenstive == null) { + if (hexP.equals("0") || hexP.equals("1")) { + baseStringCfg.setIsCaseInsenstive(0); + } else if (hexP.equals("2")) { + baseStringCfg.setIsCaseInsenstive(1); + } else { + errInfo.append(String.format(prop.getProperty("can_not_null"), + prop.getProperty("is_case_insenstive")) + ";"); + } + } + } else { + if (isHex.intValue() != 0 && isHex.intValue() != 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (isCaseInsenstive.intValue() != 0 && isCaseInsenstive.intValue() != 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + if (hexP.indexOf("1") == -1 && isHex.intValue() == 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (hexP.equals("1") && isHex.intValue() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); + } + if (hexP.indexOf("2") == -1 && isCaseInsenstive.intValue() == 1) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + if (hexP.equals("2") && isCaseInsenstive.intValue() == 0) { + errInfo.append( + String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) + + ";"); + } + } + isHex = baseStringCfg.getIsHex(); + isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); + if (isHex != null && isCaseInsenstive != null) { + if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 0) { + baseStringCfg.setIsHexbin(0); + } else if (isHex.intValue() == 1 && isCaseInsenstive.intValue() == 0) { + baseStringCfg.setIsHexbin(1); + } else if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 1) { + baseStringCfg.setIsHexbin(2); + }else { + errInfo.append(prop.getProperty("hex_case_insensitive")+ ";"); + } + } + + } + if (errInfo.toString().length() > 0) {// + errTip.append(String.format(prop.getProperty("line"), baseStringCfg.getIndex()) + ","); + errTip.append(errInfo); + errTip.append("
"); + } + stringList.add(baseStringCfg); + } + if (errTip.toString().length() > 0) { + throw new ServiceException(errTip.toString()); + } + long end=System.currentTimeMillis(); + logger.warn("validate stringCfg data finish,cost:"+(end-start)); + return stringList; + } + + public DnsResStrategyService getDnsResStrategyService() { + return dnsResStrategyService; + } + public void setDnsResStrategyService(DnsResStrategyService dnsResStrategyService) { + this.dnsResStrategyService = dnsResStrategyService; + } +} diff --git a/src/main/java/com/nis/util/excel/ImportBigExcel.java b/src/main/java/com/nis/util/excel/ImportBigExcel.java new file mode 100644 index 000000000..8bfab75a6 --- /dev/null +++ b/src/main/java/com/nis/util/excel/ImportBigExcel.java @@ -0,0 +1,525 @@ +/** + * Copyright © 2012-2014 JeeSite All rights reserved. + */ +package com.nis.util.excel; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.ss.usermodel.DateUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.multipart.MultipartFile; +import org.xml.sax.SAXException; + +import com.google.common.collect.Lists; +import com.nis.domain.FunctionRegionDict; +import com.nis.domain.FunctionServiceDict; +import com.nis.util.DictUtils; +import com.nis.util.Reflections; + +/** + * 导入Excel文件(支持“XLS”和“XLSX”格式) + * @author ThinkGem + * @version 2013-03-10 + */ +public class ImportBigExcel extends XLSXCovertCSVReader{ + + private static Logger log = LoggerFactory.getLogger(ImportBigExcel.class); + /** + * 标题行号 + */ + private Integer headerNum; + private int sheetIndex; + private Properties props; + private File uploadFile; + private FunctionRegionDict regionDict; + private FunctionServiceDict serviceDict; + //ip相关 + private boolean srcIpShow=false; + private boolean destIpShow=false; + private boolean srcPortShow=false; + private boolean destPortShow=false; + private boolean directionShow=false; + private boolean protocolShow=false; + //字符串公共属性 + private boolean matchMethodShow=false; + private boolean hexShow=false; + private boolean isCaseSensitiveShow=false; + //增强字符串相关 + private boolean districtShow=false; + private List annotationList; + private List> dataList=Lists.newArrayList(); + /** + * 构造函数 + * @param file 导入文件对象 + * @param headerNum 标题行号,数据行号=标题行号+1 + * @param sheetIndex 工作表编号 + * @throws InvalidFormatException + * @throws IOException + */ + public ImportBigExcel(MultipartFile multipartFile, Integer headerNum, int sheetIndex) + throws InvalidFormatException, IOException { + File upFile=new File(System.currentTimeMillis()+multipartFile.getOriginalFilename()); + upFile.mkdirs(); + multipartFile.transferTo(upFile); + if (!upFile.exists()){ + throw new RuntimeException("导入文档为空!"); + }else if(upFile.getName().toLowerCase().endsWith("xls")){ + throw new RuntimeException("xls not supported"); + }else if(upFile.getName().toLowerCase().endsWith("xlsx")){ + }else{ + throw new RuntimeException("文档格式不正确!"); + } + this.sheetIndex = sheetIndex; + this.headerNum = headerNum; + this.uploadFile=upFile; + log.debug("Initialize success."); + } + /** + * //递归获取cls实体对象及父级对象的属性 + * wx:修改,子类覆盖父类的同名方法 + * @param list + * @param cls + */ + public void getFields(List list,Class cls) { + Field[] fields=cls.getDeclaredFields(); + if(fields != null && fields.length > 0){ + List tempList=new ArrayList<>(); + for (Field field : fields) { + if(list.size()==0) { + tempList.add(field); + }else { + boolean has=false; + for(Field checkF:list) { + if(checkF.getName().equals(field.getName())) { + has=true; + break; + } + } + if(!has) { + tempList.add(field); + } + } + } + list.addAll(tempList); + } + if(cls.getSuperclass() != null){ + getFields(list,cls.getSuperclass()); + } + } + /** + * //递归获取cls实体对象及父级对象的method + * @param list + * @param cls + */ + public void getMethods(List list,Class cls) { + Method[] methods=cls.getDeclaredMethods(); + if(methods != null && methods.length > 0){ + List tempList=new ArrayList<>(); + for (Method method : methods) { + if(list.size()==0) { + tempList.add(method); + }else { + boolean has=false; + for(Method checkM:list) { + if(checkM.getName().equals(method.getName())) { + has=true; + break; + } + } + if(!has) { + tempList.add(method); + } + } + } + list.addAll(tempList); + } + if(cls.getSuperclass() != null){ + getMethods(list,cls.getSuperclass()); + } + } + + /** + * 加载region,service,注解 + * @param cls + * @param props + * @param regionDict + * @param serviceDict + * @param groups + */ + public void loadInitParams(Class cls,Properties props,FunctionRegionDict regionDict,FunctionServiceDict serviceDict, int... groups) { + this.props=props; + this.regionDict=regionDict; + this.serviceDict=serviceDict; + //从region中获取配置信息 + Integer regionType=regionDict.getRegionType(); + + if(regionType==1) { + String ipPortShow=regionDict.getConfigIpPortShow(); + if(StringUtils.isNotBlank(ipPortShow)) { + if(ipPortShow.indexOf("1")>-1) { + srcIpShow=true; + } + if(ipPortShow.indexOf("2")>-1) { + srcPortShow=true; + } + if(ipPortShow.indexOf("3")>-1) { + destIpShow=true; + } + if(ipPortShow.indexOf("4")>-1) { + destPortShow=true; + } + }; + //协议方向等,如果只有一个值,就 不需要输入了 + String direction=regionDict.getConfigDirection(); + if(StringUtils.isNotBlank(direction)&&direction.indexOf(",")>-1) { + directionShow=true; + } + String protocol=regionDict.getConfigProtocol(); + if(StringUtils.isNotBlank(protocol)&&protocol.indexOf(",")>-1) { + protocolShow=true; + } + //packet ip reject + if(regionDict.getFunctionId().equals(5)&&serviceDict!=null&&serviceDict.getServiceId().equals(16)) { + protocolShow=false; + } + }else if(regionType==2||regionType==3){ + String matchMethod= regionDict.getConfigMatchMethod(); + if(StringUtils.isNotBlank(matchMethod)&&matchMethod.indexOf(",")>-1) { + matchMethodShow=true; + } + String hex= regionDict.getConfigHex(); + if(StringUtils.isNotBlank(hex)&&hex.indexOf(",")>-1) { + hexShow=true; + isCaseSensitiveShow=true; + } + String district=regionDict.getConfigDistrict(); + if(StringUtils.isNotBlank(district)&&district.indexOf(",")>-1) { + districtShow=true; + } + } + List annotationList = Lists.newArrayList(); + List fs=new ArrayList(); + // Get annotation field + //递归获取cls实体对象及父级对象的属性 + getFields(fs, cls); + for (Field f : fs){ + //排除不需要导出的字段 + if(!srcIpShow) { + if(f.getName().equalsIgnoreCase("srcIpAddress")) { + continue; + } + } + if(!destIpShow) { + if(f.getName().equalsIgnoreCase("destIpAddress")) { + continue; + } + } + if(!srcPortShow) { + if(f.getName().equalsIgnoreCase("srcPort")) { + continue; + } + } + if(!destPortShow) { + if(f.getName().equalsIgnoreCase("destPort")) { + continue; + } + } + if(!directionShow) { + if(f.getName().equalsIgnoreCase("direction")) { + continue; + } + } + if(!protocolShow) { + if(f.getName().equalsIgnoreCase("protocol")) { + continue; + } + } + if(!matchMethodShow) { + if(f.getName().equalsIgnoreCase("matchMethod")) { + continue; + } + } + if(!hexShow) { + if(f.getName().equalsIgnoreCase("isHex")) { + continue; + } + } + if(!isCaseSensitiveShow) { + if(f.getName().equalsIgnoreCase("isCaseInsenstive")) { + continue; + } + } + if(!districtShow) { + if(f.getName().equalsIgnoreCase("district")) { + continue; + } + } + ExcelField ef = f.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==2)){ + if (groups!=null && groups.length>0){ + boolean inGroup = false; + for (int g : groups){ + if (inGroup){ + break; + } + for (int efg : ef.groups()){ + if (g == efg){ + inGroup = true; + annotationList.add(new Object[]{ef, f}); + break; + } + } + } + }else{ + annotationList.add(new Object[]{ef, f}); + } + } + } + // Get annotation method +// Method[] ms = cls.getDeclaredMethods(); + List ms=new ArrayList(); + // Get annotation method + //递归获取cls实体对象及父级对象的属性 + getMethods(ms, cls); + for (Method m : ms){ + //排除不需要导出的字段 + if(!srcIpShow) { + if(m.getName().equalsIgnoreCase("get"+"srcIpAddress")||m.getName().equalsIgnoreCase("set"+"srcIpAddress")) { + continue; + } + } + if(!destIpShow) { + if(m.getName().equalsIgnoreCase("get"+"destIpAddress")||m.getName().equalsIgnoreCase("set"+"destIpAddress")) { + continue; + } + } + if(!srcPortShow) { + if(m.getName().equalsIgnoreCase("get"+"srcPort")||m.getName().equalsIgnoreCase("set"+"srcPort")) { + continue; + } + } + if(!destPortShow) { + if(m.getName().equalsIgnoreCase("get"+"destPort")||m.getName().equalsIgnoreCase("set"+"destPort")) { + continue; + } + } + if(!directionShow) { + if(m.getName().equalsIgnoreCase("get"+"direction")||m.getName().equalsIgnoreCase("set"+"direction")) { + continue; + } + } + if(!protocolShow) { + if(m.getName().equalsIgnoreCase("get"+"protocol")||m.getName().equalsIgnoreCase("set"+"protocol")) { + continue; + } + } + if(!matchMethodShow) { + if(m.getName().equalsIgnoreCase("get"+"matchMethod")||m.getName().equalsIgnoreCase("set"+"matchMethod")) { + continue; + } + } + if(!hexShow) { + if(m.getName().equalsIgnoreCase("get"+"isHex")||m.getName().equalsIgnoreCase("set"+"isHex")) { + continue; + } + } + if(!isCaseSensitiveShow) { + if(m.getName().equalsIgnoreCase("get"+"isCaseInsenstive")||m.getName().equalsIgnoreCase("set"+"isCaseInsenstive")) { + continue; + } + } + if(!districtShow) { + if(m.getName().equalsIgnoreCase("get"+"district")||m.getName().equalsIgnoreCase("set"+"district")) { + continue; + } + } + ExcelField ef = m.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==2)){ + if (groups!=null && groups.length>0){ + boolean inGroup = false; + for (int g : groups){ + if (inGroup){ + break; + } + for (int efg : ef.groups()){ + if (g == efg){ + inGroup = true; + annotationList.add(new Object[]{ef, m}); + break; + } + } + } + }else{ + annotationList.add(new Object[]{ef, m}); + } + } + } + // Field sorting + Collections.sort(annotationList, new Comparator() { + public int compare(Object[] o1, Object[] o2) { + return new Integer(((ExcelField)o1[0]).sort()).compareTo( + new Integer(((ExcelField)o2[0]).sort())); + }; + }); + this.annotationList=annotationList; + } + @Override + public List optRows(int sheetIndex, int curRow, List rowlist) { + // TODO Auto-generated method stub + List rowlistCopy=Lists.newArrayList(); + rowlistCopy.addAll(rowlist); + dataList.add(rowlistCopy); + return null; + } + /** + * 获取导入数据列表 + * @param cls 导入对象类型 + * @param groups 导入分组 + * @throws SQLException + * @throws SAXException + * @throws ParserConfigurationException + * @throws OpenXML4JException + * @throws IOException + */ + public BlockingQueue getDataList(Class cls) throws InstantiationException, IllegalAccessException, IOException, OpenXML4JException, ParserConfigurationException, SAXException, SQLException{ + log.warn("start to load data..."); + this.processOneSheet(uploadFile, this.sheetIndex); + long start=System.currentTimeMillis(); + if(regionDict==null) { + throw new RuntimeException("regionDict is null!"); + } + // Get excel data + BlockingQueue _dataList =new ArrayBlockingQueue(dataList.size(),true); + for (int i = 0; i < dataList.size(); i++) { + E e = (E)cls.newInstance(); + if(i<=headerNum) { + continue; + } + int column = 0; + List row=dataList.get(i); + StringBuilder sb = new StringBuilder(); + for (Object[] os : annotationList){ + Object val=row.get(column); + column++; + if (val != null){ + ExcelField ef = (ExcelField)os[0]; + // If is dict type, get dict value + if (StringUtils.isNotBlank(ef.dictType())){ + Object val1 = DictUtils.getDictCode(ef.dictType(), val.toString(), ""); + //没有获取到字典值的话会影响验证判断 + if(val1!=null&&StringUtils.isNotBlank(val1.toString())) { + val=val1; + } + //log.debug("Dictionary type value: ["+i+","+colunm+"] " + val); + } + // Get param type and type cast + Class valType = Class.class; + if (os[1] instanceof Field){ + valType = ((Field)os[1]).getType(); + }else if (os[1] instanceof Method){ + Method method = ((Method)os[1]); + if ("get".equals(method.getName().substring(0, 3))){ + valType = method.getReturnType(); + }else if("set".equals(method.getName().substring(0, 3))){ + valType = ((Method)os[1]).getParameterTypes()[0]; + } + } + //log.debug("Import value type: ["+i+","+column+"] " + valType); + try { + if (valType == String.class){ + String s = String.valueOf(val.toString()); + //0.0.0.0表示任意IP的含义 + if(StringUtils.endsWith(s, ".0") && !s.endsWith("0.0.0.0")){ + val = StringUtils.substringBefore(s, ".0"); + }else{ + val = String.valueOf(val.toString()); + } + }else if (valType == Integer.class){ + val = Double.valueOf(val.toString()).intValue(); + }else if (valType == Long.class){ + val = Double.valueOf(val.toString()).longValue(); + }else if (valType == Double.class){ + val = Double.valueOf(val.toString()); + }else if (valType == Float.class){ + val = Float.valueOf(val.toString()); + }else if (valType == Date.class){ + val = DateUtil.getJavaDate((Double)val); + }else{ + if (ef.fieldType() != Class.class){ + val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString()); + }else{ + val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), + "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString()); + } + } + } catch (Exception ex) { + log.info("Get cell value ["+i+","+column+"] error: " + ex.toString()); + val = null; + } + // set entity value + if (os[1] instanceof Field){ + Reflections.invokeSetter(e, ((Field)os[1]).getName(), val); + }else if (os[1] instanceof Method){ + String mthodName = ((Method)os[1]).getName(); + if ("get".equals(mthodName.substring(0, 3))){ + mthodName = "set"+StringUtils.substringAfter(mthodName, "get"); + } + Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val}); + } + //设置索引值 + Reflections.invokeSetter(e, "index", (i+1)); + } + sb.append(val+", "); + } + _dataList.offer(e); + row.clear(); + log.debug("Read success: ["+i+"] "+sb.toString()); + } + long end=System.currentTimeMillis(); + log.warn(" load data finish,cost:"+(end-start)); + return _dataList; + } + public File getUploadFile() { + return uploadFile; + } + public void setUploadFile(File uploadFile) { + this.uploadFile = uploadFile; + } + +// /** +// * 导入测试 +// */ +// public static void main(String[] args) throws Throwable { +// +// ImportExcel ei = new ImportExcel("target/export.xlsx", 1); +// +// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) { +// Row row = ei.getRow(i); +// for (int j = 0; j < ei.getLastCellNum(); j++) { +// Object val = ei.getCellValue(row, j); +// System.out.print(val+", "); +// } +// System.out.print("\n"); +// } +// +// } + +} diff --git a/src/main/java/com/nis/util/excel/XLSXCovertCSVReader.java b/src/main/java/com/nis/util/excel/XLSXCovertCSVReader.java new file mode 100644 index 000000000..3ad9f4205 --- /dev/null +++ b/src/main/java/com/nis/util/excel/XLSXCovertCSVReader.java @@ -0,0 +1,593 @@ +package com.nis.util.excel; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.apache.poi.hssf.usermodel.HSSFDataFormatter; +import org.apache.poi.hssf.usermodel.HSSFDateUtil; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; +import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; +import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; + + +/** + * 使用CVS模式解决XLSX文件,可以有效解决用户模式内存溢出的问题 + * 该模式是POI官方推荐的读取大数据的模式,在用户模式下,数据量较大、Sheet较多、或者是有很多无用的空行的情况 + * ,容易出现内存溢出,用户模式读取Excel的典型代码如下: FileInputStream file=new + * FileInputStream("c:\\test.xlsx"); Workbook wb=new XSSFWorkbook(file); + * + * + * @author 山人 + */ +public abstract class XLSXCovertCSVReader { + + /** + * The type of the data value is indicated by an attribute on the cell. The + * value is usually in a "v" element within the cell. + */ + enum xssfDataType { + BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER, + } + /** + * 使用xssf_sax_API处理Excel,请参考: http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api + *

+ * Also see Standard ECMA-376, 1st edition, part 4, pages 1928ff, at + * http://www.ecma-international.org/publications/standards/Ecma-376.htm + *

+ * A web-friendly version is http://openiso.org/Ecma/376/Part4 + */ + //定义前一个元素和当前元素的位置,用来计算其中空的单元格数量,如A6和A8等 + private String preRefnum = null, refnum = null; + //定义该文档一行最大的单元格数,用来补全一行最后可能缺失的单元格 + private String maxRefnum = null; + private String lastContents; + private int curCol = 0; +// private Integer type; + private List rowData= new ArrayList(); + //excel记录行操作方法,以sheet索引,行索引和行元素列表为参数,对sheet的一行元素进行操作,元素为String类型 + public abstract List optRows(int sheetIndex,int curRow, List rowlist) ; + class MyXSSFSheetHandler extends DefaultHandler { + + /** + * Table with styles + */ + private StylesTable stylesTable; + + /** + * Table with unique strings + */ + private ReadOnlySharedStringsTable sharedStringsTable; + + + /** + * Number of columns to read starting with leftmost + */ + + // Set when V start element is seen + private boolean vIsOpen; + + // Set when cell start element is seen; + // used when cell close element is seen. + private xssfDataType nextDataType; + private int sheetIndex = -1; + // Used to format numeric cell values. + private short formatIndex; + private String formatString; + private final DataFormatter formatter; + + private int thisColumn = -1; + // The last column printed to the output stream + private int lastColumnNumber = -1; + private List rowlist = new ArrayList(); + // Gathers characters as they are seen. + private StringBuffer value; +// private String[] record; + // private List rows = new ArrayList(); + private boolean isCellNull = false; + + /** + * Accepts objects needed while parsing. + * + * @param styles + * Table of styles + * @param strings + * Table of shared strings + * @param cols + * Minimum number of columns to show + * @param target + * Sink for output + */ + public MyXSSFSheetHandler(StylesTable styles, + ReadOnlySharedStringsTable strings) { + this.stylesTable = styles; + this.sharedStringsTable = strings; + this.value = new StringBuffer(); + this.nextDataType = xssfDataType.NUMBER; + this.formatter = new DataFormatter(); + rowlist.clear();// 每次读取都清空行集合 + } + + /* + * (non-Javadoc) + * + * @see + * org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, + * java.lang.String, java.lang.String, org.xml.sax.Attributes) + */ + public void startElement(String uri, String localName, String name, + Attributes attributes) throws SAXException { + + if ("inlineStr".equals(name) || "v".equals(name)) { + vIsOpen = true; + // Clear contents cache + value.setLength(0); + } + // c => cell + else if ("c".equals(name)) { + // Get the cell reference + String r = attributes.getValue("r"); + int firstDigit = -1; + for (int c = 0; c < r.length(); ++c) { + if (Character.isDigit(r.charAt(c))) { + firstDigit = c; + break; + } + } + thisColumn = nameToColumn(r.substring(0, firstDigit)); + String cellType = attributes.getValue("t"); + + if(preRefnum == null){ + preRefnum = attributes.getValue("r"); + } +// else{ +// preRefnum = refnum; +// } + //当前单元格的位置 + refnum = attributes.getValue("r"); + + + // Figure out if the value is an index in the SST + + // Set up defaults. + this.nextDataType = xssfDataType.NUMBER; + this.formatIndex = -1; + this.formatString = null; + + String cellStyleStr = attributes.getValue("s"); + if ("b".equals(cellType)) + nextDataType = xssfDataType.BOOL; + else if ("e".equals(cellType)) + nextDataType = xssfDataType.ERROR; + else if ("inlineStr".equals(cellType)) + nextDataType = xssfDataType.INLINESTR; + else if ("s".equals(cellType)) + nextDataType = xssfDataType.SSTINDEX; + else if ("str".equals(cellType)) + nextDataType = xssfDataType.FORMULA; + else if (cellStyleStr != null) { + // It's a number, but almost certainly one + // with a special style or format + int styleIndex = Integer.parseInt(cellStyleStr); + XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); + this.formatIndex = style.getDataFormat(); + this.formatString = style.getDataFormatString(); + if (this.formatString == null) + this.formatString = BuiltinFormats + .getBuiltinFormat(this.formatIndex); + } + } + lastContents = ""; + } + + /* + * (non-Javadoc) + * + * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, + * java.lang.String, java.lang.String) + */ + public void endElement(String uri, String localName, String name) + throws SAXException { + + Object thisVal = null; + + // v => contents of a cell + if ("v".equals(name)) { + // Process the value contents as required. + // Do now, as characters() may be called more than once + switch (nextDataType) { + + case BOOL: + char first = value.charAt(0); + thisVal = first == '0' ? false : true; + break; + + case ERROR: + thisVal = "\"ERROR:" + value.toString() + '"'; + break; + + case FORMULA: + // A formula could result in a string value, + // so always add double-quote characters. + thisVal = value.toString() ; + break; + + case INLINESTR: + // TODO: have seen an example of this, so it's untested. + XSSFRichTextString rtsi = new XSSFRichTextString( + value.toString()); + thisVal = rtsi.toString() ; + break; + + case SSTINDEX: + String sstIndex = value.toString(); + try { + int idx = Integer.parseInt(sstIndex); + XSSFRichTextString rtss = new XSSFRichTextString( + sharedStringsTable.getEntryAt(idx)); + thisVal = rtss.toString(); + } catch (NumberFormatException ex) { + System.out.println("Failed to parse SST index '" + sstIndex + + "': " + ex.toString()); + } + break; + + case NUMBER: + String n = value.toString(); + // 判断是否是日期格式 + if (HSSFDateUtil.isADateFormat(this.formatIndex, n)) { + Double d = Double.parseDouble(n); + Date date=HSSFDateUtil.getJavaDate(d); + thisVal=date; + } else if (this.formatString != null) + thisVal = formatter.formatRawCellContents( + Double.parseDouble(n), this.formatIndex, + this.formatString); + else if(n.indexOf("E")!=-1) {//处理科学计数法 + BigDecimal bd = new BigDecimal(n); + thisVal = bd.toPlainString(); + }else + thisVal = n; + break; + + default: + thisVal = "(TODO: Unexpected type: " + nextDataType + ")"; + break; + } + + // Output after we've seen the string contents + // Emit commas for any fields that were missing on this row + if (lastColumnNumber == -1) { + lastColumnNumber = 0; + } + + if(!refnum.equals(preRefnum)){ + int len = countNullCell(refnum, preRefnum); + for(int i=0;i 0) { + sheetIndex++; + // Columns are 0 based + if (lastColumnNumber == -1) { + lastColumnNumber = 0; + } + //默认第一行为表头,以该行单元格数目为最大数目 + if(sheetIndex == 0){ + maxRefnum = refnum; + } + //补全一行尾部可能缺失的单元格 + if(maxRefnum != null){ + int len = countNullCell(maxRefnum, refnum); + for(int i=0;i<=len;i++){ + rowlist.add(curCol, ""); + curCol++; + } + } + if ( rowlist.get(0) != null + && rowlist.get(1) != null)// 判断是否空行 + { + optRows(sheetIndex,lastColumnNumber,rowlist); + + rowlist.clear(); +// rows.add(record.clone()); + isCellNull = false; +// for (int i = 0; i < record.length; i++) { +// record[i] = null; +// } + } + } + rowlist.clear(); + rowData.clear(); + curCol = 0; + preRefnum = null; + refnum = null; + lastColumnNumber = -1; + } + + } + +// public List getRows() { +// return rows; +// } +// +// public void setRows(List rows) { +// this.rows = rows; +// } + + /** + * Captures characters only if a suitable element is open. Originally + * was just "v"; extended for inlineStr also. + */ + public void characters(char[] ch, int start, int length) + throws SAXException { + if (vIsOpen) + value.append(ch, start, length); + } + + /** + * Converts an Excel column name like "C" to a zero-based index. + * + * @param name + * @return Index corresponding to the specified name + */ + private int nameToColumn(String name) { + int column = -1; + for (int i = 0; i < name.length(); ++i) { + int c = name.charAt(i); + column = (column + 1) * 26 + c - 'A'; + } + return column; + } + + private String formateDateToString(Date date) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期 + return sdf.format(date); + + } + + } + + // ///////////////////////////////////// + + private String path; + + /** + * Creates a new XLSX -> CSV converter + * + * @param pkg + * The XLSX package to process + * @param output + * The PrintStream to output the CSV to + * @param minColumns + * The minimum number of columns to output, or -1 for no minimum + */ + public XLSXCovertCSVReader( + String path) { + this.path = path; + } + public XLSXCovertCSVReader( + ) { + } + /** + * Parses and shows the content of one sheet using the specified styles and + * shared-strings tables. + * + * @param styles + * @param strings + * @param sheetInputStream + */ + public void processSheet(StylesTable styles, + ReadOnlySharedStringsTable strings, InputStream sheetInputStream) + throws IOException, ParserConfigurationException, SAXException { + + InputSource sheetSource = new InputSource(sheetInputStream); + SAXParserFactory saxFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxFactory.newSAXParser(); + XMLReader sheetParser = saxParser.getXMLReader(); + MyXSSFSheetHandler handler = new MyXSSFSheetHandler(styles, strings); + sheetParser.setContentHandler(handler); + sheetParser.parse(sheetSource); + } + + /** + * 初始化这个处理程序 将 + * + * @throws IOException + * @throws OpenXML4JException + * @throws ParserConfigurationException + * @throws SAXException + */ + public void processOneSheet(String path,int sheetId) throws IOException, OpenXML4JException, + ParserConfigurationException, SAXException,SQLException { + OPCPackage xlsxPackage = OPCPackage.open(path, PackageAccess.READ); + ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable( + xlsxPackage); + XSSFReader xssfReader = new XSSFReader(xlsxPackage); + List list = null; + StylesTable styles = xssfReader.getStylesTable(); + InputStream stream=xssfReader.getSheet("rId"+sheetId); + processSheet(styles, strings,stream ); + stream.close(); + xlsxPackage.close(); + } + public void processOneSheet(File file,int sheetId) throws IOException, OpenXML4JException, + ParserConfigurationException, SAXException,SQLException { + OPCPackage xlsxPackage = OPCPackage.open(file, PackageAccess.READ); + ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable( + xlsxPackage); + XSSFReader xssfReader = new XSSFReader(xlsxPackage); + List list = null; + StylesTable styles = xssfReader.getStylesTable(); + InputStream stream=xssfReader.getSheet("rId"+sheetId); + processSheet(styles, strings,stream ); + stream.close(); + xlsxPackage.close(); + } + /** + * 读取Excel + * + * @param path + * 文件路径 + * @param sheetName + * sheet名称 + * @param minColumns + * 列总数 + * @return + * @throws SAXException + * @throws ParserConfigurationException + * @throws OpenXML4JException + * @throws IOException + */ + public static void readerExcel(String path, String sheetName, + int minColumns) throws IOException, OpenXML4JException, + ParserConfigurationException, SAXException { + OPCPackage p = OPCPackage.open(path, PackageAccess.READ); +// XLSXCovertCSVReader xlsx2csv = new XLSXCovertCSVReader(p, +// sheetName); + // xlsx2csv.process(p, sheetName); + p.close(); + } + +public List getMyDataList(List dataList) { + + List myDataList = new ArrayList(); + if(dataList==null||dataList.size()<=0) return myDataList; + + for(int i=0;i> checkStringCfg(FunctionServiceDict serviceDict, FunctionRegionDict regionDict, - List list) throws ServiceException { - List> stringList = new ArrayList>(); - Properties prop = this.getMsgProp(); - String exprTypeP = regionDict.getConfigExprType(); - if (StringUtil.isEmpty(exprTypeP)) { - throw new RuntimeException("Found String region,but exprType is Empty"); + /** + * 多线程验证 + * @param serviceDict + * @param regionDict + * @param asnNos + * @param list + * @return + * @throws ServiceException + * @throws InterruptedException + * @throws ExecutionException + */ + public BlockingQueue checkIpCfgMulity(FunctionServiceDict serviceDict, FunctionRegionDict regionDict,List> asnNos, BlockingQueue list) throws ServiceException, InterruptedException, ExecutionException{ + logger.warn("start checkIpCfgMulity ,size "+list.size()); + long start=System.currentTimeMillis(); + BlockingQueue queue=new ArrayBlockingQueue<>(list.size()); + ExecutorService service=Executors.newFixedThreadPool(Constants.MULITY_THREAD_SIZE); + List> futures=new ArrayList<>(); + for(int i=0;i hashs = DictUtils.getDictList("P2P_HASH_TYPE"); - boolean has = false; - for (SysDataDictionaryItem hash : hashs) { - if (hash.getItemCode().equals(userRegion1)) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("p2p_hash_type") + " ") + ";"); - } - } - } - String keyword = baseStringCfg.getCfgKeywords(); - if (!regionDict.getFunctionId().equals(403)) { - if (StringUtils.isBlank(keyword)) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("key_word") + " ") + ";"); - } - if (mulityKeywordsP.equals("0")) { - if (keyword.indexOf("\n") > -1) { - errInfo.append( - String.format(prop.getProperty("not_multiple"), prop.getProperty("key_word")) + ";"); - } - Matcher m = pattern.matcher(keyword); - if (m.find()) { - errInfo.append(String.format(prop.getProperty("has_invisible_char"), - prop.getProperty("key_word") + " '" + keyword + "'") + ";"); - } - } else { - boolean has = false; - Set keywordSet=Sets.newHashSet(); - for (String key : keyword.split("\n")) { - Matcher m = pattern.matcher(key); - if (m.find()) { - has = true; - errInfo.append(String.format(prop.getProperty("has_invisible_char"), - prop.getProperty("key_word") + " '" + key + "'") + ";"); - break; - } - if(!keywordSet.contains(key)) { - keywordSet.add(key); - }else { - errInfo.append(prop.getProperty("key_word") + " '" + key + "'"+" "+prop.getProperty("repeat") + ";"); - } - } - if (!has) { - if(keyword.replaceAll("\n","").length()>1024) { - errInfo.append(String.format(prop.getProperty("most_keywords"), - prop.getProperty("key_word")) + ";"); - }else { - String reWord = keyword.replaceAll("\n", Constants.KEYWORD_EXPR); - baseStringCfg.setCfgKeywords(reWord); - } - } - } - }else { - if (StringUtils.isBlank(keyword)) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("domain_name") + " ") + ";"); - }else { - Matcher m = pattern.matcher(keyword); - if (m.find()) { - errInfo.append(String.format(prop.getProperty("has_invisible_char"), - prop.getProperty("domain_name") + " '" + keyword + "'") + ";"); - }else { - m = domainPattern.matcher(keyword); - if(!m.matches()) { - errInfo.append(String.format(prop.getProperty("not_valid_domain"), - prop.getProperty("domain_name") + " '" + keyword + "'") + ";"); - }else { - baseStringCfg.setDomain(keyword); - } - - } - } - - } - - Integer exprType = baseStringCfg.getExprType(); - boolean has = false; - if (exprType == null) { - if (exprTypeP.indexOf(",") == -1) { - if (mulityKeywordsP.equals("0") && exprTypeP.equals("1")) { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - baseStringCfg.setExprType(Integer.parseInt(exprTypeP)); - } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("0")) { - baseStringCfg.setExprType(0); - } else if (exprTypeP.indexOf("1") > -1 && mulityKeywordsP.equals("1") - && keyword.indexOf("\n") > -1) { - baseStringCfg.setExprType(1); - } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("1") - && keyword.indexOf("\n") == -1) { - baseStringCfg.setExprType(0); - } else { - baseStringCfg.setExprType(Integer.parseInt(exprTypeP.split(",")[0])); - } - // errInfo.append(String.format(prop.getProperty("can_not_null"), - // prop.getProperty("expression_type"))+";"); - } else { - for (String exp : exprTypeP.split(",")) { - if (exp.equals(exprType.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("expression_type")) - + ";"); - } - has = false; - } - exprType = baseStringCfg.getExprType(); - Integer matchMethod = baseStringCfg.getMatchMethod(); - if (matchMethod == null) { - if (matchMethodP.indexOf(",") == -1) { - if (exprTypeP.equals("1") && !matchMethodP.equals("0")) { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP)); - } else if (exprType != null && exprType.intValue() == 1) { - if (matchMethodP.indexOf("0") > -1) { - baseStringCfg.setMatchMethod(0); - } else { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - - } else { - baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP.split(",")[0])); - } - // errInfo.append(String.format(prop.getProperty("can_not_null"), - // prop.getProperty("match_method"))+";"); - } else { - for (String exp : matchMethodP.split(",")) { - if (exp.equals(matchMethod.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("match_method")) - + ";"); - } - } - - Integer isHex = baseStringCfg.getIsHex(); - Integer isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); - if (isHex == null || isCaseInsenstive == null) { - if (isHex == null) { - if (hexP.equals("0") || hexP.equals("2")) { - baseStringCfg.setIsHex(0); - } else if (hexP.equals("1")) { - baseStringCfg.setIsHex(1); - } else { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("is_hex")) + ";"); - } - } - if (isCaseInsenstive == null) { - if (hexP.equals("0") || hexP.equals("1")) { - baseStringCfg.setIsCaseInsenstive(0); - } else if (hexP.equals("2")) { - baseStringCfg.setIsCaseInsenstive(1); - } else { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("is_case_insenstive")) + ";"); - } - } - } else { - if (isHex.intValue() != 0 && isHex.intValue() != 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (isCaseInsenstive.intValue() != 0 && isCaseInsenstive.intValue() != 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - if (hexP.indexOf("1") == -1 && isHex.intValue() == 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (hexP.equals("1") && isHex.intValue() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (hexP.indexOf("2") == -1 && isCaseInsenstive.intValue() == 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - if (hexP.equals("2") && isCaseInsenstive.intValue() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - } - isHex = baseStringCfg.getIsHex(); - isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); - if (isHex != null && isCaseInsenstive != null) { - if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 0) { - baseStringCfg.setIsHexbin(0); - } else if (isHex.intValue() == 1 && isCaseInsenstive.intValue() == 0) { - baseStringCfg.setIsHexbin(1); - } else if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 1) { - baseStringCfg.setIsHexbin(2); - }else { - errInfo.append(prop.getProperty("hex_case_insensitive")+ ";"); - } - } - + service.shutdown(); + while(!service.isTerminated()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - if (errInfo.toString().length() > 0) {// - errTip.append(String.format(prop.getProperty("line"), i + 2) + ","); - errTip.append(errInfo); - errTip.append("
"); + } + for(Future future:futures) { + String msg = future.get(); + if(StringUtils.isNotBlank(msg)) { + throw new ServiceException(msg); } - stringList.add(baseStringCfg); } - if (errTip.toString().length() > 0) { - throw new ServiceException(errTip.toString()); + long end=System.currentTimeMillis(); + logger.warn("checkIpCfgMulity finish,cost:"+(end-start)+",size:"+queue.size()); + return queue; + } + /** + * 多线程验证 + * @param serviceDict + * @param regionDict + * @param list + * @return + * @throws InterruptedException + * @throws ExecutionException + * @throws ServiceException + */ + public BlockingQueue> checkStringCfgMulity(FunctionServiceDict serviceDict, FunctionRegionDict regionDict, BlockingQueue list) throws InterruptedException, ExecutionException, ServiceException{ + logger.warn("start checkStringCfgMulity ,size "+list.size()); + long start=System.currentTimeMillis(); + BlockingQueue> queue=new ArrayBlockingQueue<>(list.size()); + ExecutorService service=Executors.newFixedThreadPool(Constants.MULITY_THREAD_SIZE); + List> futures=new ArrayList<>(); + for(int i=0;i future:futures) { + String msg = future.get(); + if(StringUtils.isNotBlank(msg)) { + throw new ServiceException(msg); + } + } + long end=System.currentTimeMillis(); + logger.warn("checkStringCfgMulity finish,cost:"+(end-start)+",size:"+queue.size()); + return queue; + } + /** + * 多线程验证 + * @param serviceDict + * @param regionDict + * @param list + * @return + * @throws InterruptedException + * @throws ExecutionException + * @throws ServiceException + */ + public BlockingQueue checkComplexStringCfgMulity(FunctionServiceDict serviceDict, FunctionRegionDict regionDict,BlockingQueue list) throws InterruptedException, ExecutionException, ServiceException{ + logger.warn("start checkComplexStringCfgMulity ,size "+list.size()); + long start=System.currentTimeMillis(); + BlockingQueue queue=new ArrayBlockingQueue<>(list.size()); + ExecutorService service=Executors.newFixedThreadPool(Constants.MULITY_THREAD_SIZE); + List> futures=new ArrayList<>(); + for(int i=0;i future:futures) { + String msg = future.get(); + if(StringUtils.isNotBlank(msg)) { + throw new ServiceException(msg); + } + } + long end=System.currentTimeMillis(); + logger.warn("checkComplexStringCfgMulity finish,cost:"+(end-start)+",size:"+queue.size()); + return queue; + } + /** + * 多线程验证 + * @param serviceDict + * @param regionDict + * @param list + * @return + * @throws InterruptedException + * @throws ExecutionException + * @throws ServiceException + */ + public BlockingQueue checkDnsResStrategyCfgMulity(FunctionServiceDict serviceDict, FunctionRegionDict regionDict,BlockingQueue list) throws InterruptedException, ExecutionException, ServiceException{ + logger.warn("start checkComplexStringCfgMulity ,size "+list.size()); + long start=System.currentTimeMillis(); + BlockingQueue queue=new ArrayBlockingQueue<>(list.size()); + ExecutorService service=Executors.newFixedThreadPool(Constants.MULITY_THREAD_SIZE); + List> futures=new ArrayList<>(); + for(int i=0;i future:futures) { + String msg = future.get(); + if(StringUtils.isNotBlank(msg)) { + throw new ServiceException(msg); + } + } + long end=System.currentTimeMillis(); + logger.warn("checkComplexStringCfgMulity finish,cost:"+(end-start)+",size:"+queue.size()); + return queue; } - public List checkComplexStringCfg(FunctionServiceDict serviceDict, FunctionRegionDict regionDict, - List list) throws ServiceException { - List stringList = new ArrayList(); - Properties prop = this.getMsgProp(); - String exprTypeP = regionDict.getConfigExprType(); - if (StringUtil.isEmpty(exprTypeP)) { - throw new RuntimeException("Found String region,but exprType is Empty"); - } - String matchMethodP = regionDict.getConfigMatchMethod(); - if (StringUtil.isEmpty(matchMethodP)) { - throw new RuntimeException("Found String region,but matchMethod is Empty"); - } - String hexP = regionDict.getConfigHex(); - if (StringUtil.isEmpty(hexP)) { - throw new RuntimeException("Found String region,but hex is Empty"); - } - String mulityKeywordsP = regionDict.getConfigMultiKeywords(); - if (StringUtil.isEmpty(mulityKeywordsP)) { - throw new RuntimeException("Found String region,but mulityKeywords is Empty"); - } - String dirtrictP = regionDict.getConfigDistrict(); - StringBuffer errTip = new StringBuffer(); - Pattern pattern = Pattern.compile("\t|\r|\n|\b|\f"); - for (int i = 0; i < list.size(); i++) { - StringBuffer errInfo = new StringBuffer(); - ComplexkeywordCfg baseStringCfg = new ComplexkeywordCfg(); - BeanUtils.copyProperties(list.get(i), baseStringCfg); - if (regionDict.getRegionType().equals(3)) { - if (regionDict.getFunctionId().equals(7)) { - Long dnsStrategyId = baseStringCfg.getDnsStrategyId(); - if (dnsStrategyId != null) { - List dnsStrategys = dnsResStrategyService.findDnsResStrategys(dnsStrategyId, - Constants.VALID_YES, Constants.AUDIT_YES); - if (dnsStrategys == null || dnsStrategys.size() == 0) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("dns_res_strategy")) + ";"); - } - } - } - String keyword = baseStringCfg.getCfgKeywords(); - String district = baseStringCfg.getDistrict(); - if (StringUtils.isBlank(keyword)) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("key_word") + " ") + ";"); - } - if (StringUtils.isNotBlank(dirtrictP)) { - if (StringUtils.isBlank(district)) { - if (dirtrictP.indexOf(",") == -1) { - baseStringCfg.setDistrict(dirtrictP); - } else { - // baseStringCfg.setDistrict(dirtrictP.split(",")[0]); - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("district") + " ") - + ";"); - } - } else if (dirtrictP.indexOf(district) == -1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("district") + " ") - + ";"); - } else if (dirtrictP.indexOf("others")>-1&&district.equals("others")) { - //不允许自定义匹配区域导入 - errInfo.append(prop.getProperty("district")+" "+ - String.format(prop.getProperty("can_not_be"), " 'others'")+ ";"); - } - } - if (mulityKeywordsP.equals("0")) { - if (keyword.indexOf("\n") > -1) { - errInfo.append( - String.format(prop.getProperty("not_multiple"), prop.getProperty("key_word")) + ";"); - } - Matcher m = pattern.matcher(keyword); - if (m.find()) { - errInfo.append(String.format(prop.getProperty("has_invisible_char"), - prop.getProperty("key_word") + " '" + keyword + "'") + ";"); - } - } else { - boolean has = false; - Set keywordSet=Sets.newHashSet(); - - for (String key : keyword.split("\n")) { - Matcher m = pattern.matcher(key); - if (m.find()) { - has = true; - errInfo.append(String.format(prop.getProperty("has_invisible_char"), - prop.getProperty("key_word") + " '" + key + "'") + ";"); - break; - } - if(!keywordSet.contains(key)) { - keywordSet.add(key); - }else { - errInfo.append(prop.getProperty("key_word") + " '" + key + "'"+" "+prop.getProperty("repeat") + ";"); - } - } - if (!has) { - if(keyword.replaceAll("\n","").length()>1024) { - errInfo.append(String.format(prop.getProperty("most_keywords"), - prop.getProperty("key_word")) + ";"); - }else { - String reWord = keyword.replaceAll("\n", Constants.KEYWORD_EXPR); - baseStringCfg.setCfgKeywords(reWord); - } - } - } - Integer exprType = baseStringCfg.getExprType(); - boolean has = false; - if (exprType == null) { - if (exprTypeP.indexOf(",") == -1) { - if (mulityKeywordsP.equals("0") && exprTypeP.equals("1")) { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - baseStringCfg.setExprType(Integer.parseInt(exprTypeP)); - } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("0")) { - baseStringCfg.setExprType(0); - } else if (exprTypeP.indexOf("1") > -1 && mulityKeywordsP.equals("1") - && keyword.indexOf("\n") > -1) { - baseStringCfg.setExprType(1); - } else if (exprTypeP.indexOf("0") > -1 && mulityKeywordsP.equals("1") - && keyword.indexOf("\n") == -1) { - baseStringCfg.setExprType(0); - } else { - baseStringCfg.setExprType(Integer.parseInt(exprTypeP.split(",")[0])); - } - // errInfo.append(String.format(prop.getProperty("can_not_null"), - // prop.getProperty("expression_type"))+";"); - } else { - for (String exp : exprTypeP.split(",")) { - if (exp.equals(exprType.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("expression_type")) - + ";"); - } - has = false; - } - exprType = baseStringCfg.getExprType(); - Integer matchMethod = baseStringCfg.getMatchMethod(); - if (matchMethod == null) { - if (matchMethodP.indexOf(",") == -1) { - if (exprTypeP.equals("1") && !matchMethodP.equals("0")) { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP)); - } else if (exprType != null && exprType.intValue() == 1) { - if (matchMethodP.indexOf("0") > -1) { - baseStringCfg.setMatchMethod(0); - } else { - throw new RuntimeException("region dict config error,dict id is " + regionDict.getDictId()); - } - - } else { - baseStringCfg.setMatchMethod(Integer.parseInt(matchMethodP.split(",")[0])); - } - // errInfo.append(String.format(prop.getProperty("can_not_null"), - // prop.getProperty("match_method"))+";"); - } else { - for (String exp : matchMethodP.split(",")) { - if (exp.equals(matchMethod.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("match_method")) - + ";"); - } - } - Integer isHex = baseStringCfg.getIsHex(); - Integer isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); - if (isHex == null || isCaseInsenstive == null) { - if (isHex == null) { - if (hexP.equals("0") || hexP.equals("2")) { - baseStringCfg.setIsHex(0); - } else if (hexP.equals("1")) { - baseStringCfg.setIsHex(1); - } else { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("is_hex")) + ";"); - } - } - if (isCaseInsenstive == null) { - if (hexP.equals("0") || hexP.equals("1")) { - baseStringCfg.setIsCaseInsenstive(0); - } else if (hexP.equals("2")) { - baseStringCfg.setIsCaseInsenstive(1); - } else { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("is_case_insenstive")) + ";"); - } - } - } else { - if (isHex.intValue() != 0 && isHex.intValue() != 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (isCaseInsenstive.intValue() != 0 && isCaseInsenstive.intValue() != 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - if (hexP.indexOf("1") == -1 && isHex.intValue() == 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (hexP.equals("1") && isHex.intValue() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_hex")) + ";"); - } - if (hexP.indexOf("2") == -1 && isCaseInsenstive.intValue() == 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - if (hexP.equals("2") && isCaseInsenstive.intValue() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("is_case_insenstive")) - + ";"); - } - } - isHex = baseStringCfg.getIsHex(); - isCaseInsenstive = baseStringCfg.getIsCaseInsenstive(); - if (isHex != null && isCaseInsenstive != null) { - if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 0) { - baseStringCfg.setIsHexbin(0); - } else if (isHex.intValue() == 1 && isCaseInsenstive.intValue() == 0) { - baseStringCfg.setIsHexbin(1); - } else if (isHex.intValue() == 0 && isCaseInsenstive.intValue() == 1) { - baseStringCfg.setIsHexbin(2); - }else { - errInfo.append(prop.getProperty("hex_case_insensitive")+ ";"); - } - } - } - if (errInfo.toString().length() > 0) {// - errTip.append(String.format(prop.getProperty("line"), i + 2) + ","); - errTip.append(errInfo); - errTip.append("
"); - } - stringList.add(baseStringCfg); - } - if (errTip.toString().length() > 0) { - throw new ServiceException(errTip.toString()); - } - return stringList; - } - public List checkIpCfg(FunctionServiceDict serviceDict, FunctionRegionDict regionDict, List list) - throws ServiceException { - List ipList = new ArrayList(); - Properties prop = this.getMsgProp(); - // ip必填一个,configIpPortShow不能为空,否则字典配错; - String configIpPortShow = regionDict.getConfigIpPortShow(); - if (StringUtil.isEmpty(configIpPortShow)) { - throw new RuntimeException("Found IP region,but configIpPortShow is Empty"); - } - String ipType = regionDict.getConfigIpType(); - if (StringUtil.isEmpty(ipType)) { - throw new RuntimeException("Found IP region,but ipType is Empty"); - } - boolean canIPv4 = ipType.indexOf("4") > -1; - boolean canIPv6 = ipType.indexOf("6") > -1; - String ipPattern = regionDict.getConfigIpPattern(); - if (StringUtil.isEmpty(ipPattern)) { - throw new RuntimeException("Found IP region,but ipPattern is Empty"); - } - boolean canIPFormat = ipType.indexOf("3") > -1; - boolean canIPSubnetFormat = ipType.indexOf("1") > -1; - boolean canIPRangeFormat = ipType.indexOf("2") > -1; - String portPattern = regionDict.getConfigPortPattern(); - String protocol = regionDict.getConfigProtocol(); - String direction = regionDict.getConfigDirection(); - StringBuffer errTip = new StringBuffer(); - for (int i = 0; i < list.size(); i++) { - StringBuffer errInfo = new StringBuffer(); - IpPortCfg baseIpCfg = new IpPortCfg(); - BeanUtils.copyProperties(list.get(i), baseIpCfg); - // 特殊字段验证 - // packet ip ratelimit - if (serviceDict!=null&&serviceDict.getFunctionId().intValue() == 5 && serviceDict.getAction().intValue() == 64) { - String userRegion1 = IpRateLimitTemplate.userRegion1; - baseIpCfg.setUserRegion1(userRegion1); - if("0".equals(userRegion1)) { - String userRegion2 = baseIpCfg.getUserRegion2(); - try { - // Double ratelimt= Double.parseDouble(userRegion2); - List ratelimtList = DictUtils.getDictList("RATE_LIMIT"); - boolean has = false; - for (SysDataDictionaryItem ratelimit : ratelimtList) { - if (ratelimit.getItemCode().equals(userRegion2)) { - has = true; - break; - } - } - if (!has) { - errInfo.append(prop.getProperty("ratelimit") + "." - + String.format(prop.getProperty("must_between"), 0.001, 0.009) + ";"); - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("ratelimit")) + ";"); - } - }else if("1".equals(userRegion1)) { - String userRegion3 = baseIpCfg.getUserRegion3(); - if (StringUtils.isNotBlank(userRegion3)) { - try { - Integer ratelimt=Integer.parseInt(userRegion3); - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("bandwith")) + ";"); - } - }else { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("bandwith")) + ";"); - } - - } - - } - // payload ip - if (regionDict.getFunctionId().equals(212)) { - // replace_zone - String userRegion1 = baseIpCfg.getUserRegion1(); - if (StringUtils.isNotBlank(userRegion1)) { - Object val = DictUtils.getDictLabel("INTERCEPT_REPLACE_ZONE", userRegion1); - if (StringUtils.isBlank(val.toString())) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("replace_zone")) - + ";"); - } - } else { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("replace_zone")) + ";"); - } - - // replaced_content - String userRegion2 = baseIpCfg.getUserRegion2(); - if (StringUtils.isBlank(userRegion2)) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("replaced_content")) - + ";"); - } - // replace_content - String userRegion3 = baseIpCfg.getUserRegion3(); - if (StringUtils.isBlank(userRegion3)) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("replace_content")) + ";"); - } - } - // dns ip - if (regionDict.getFunctionId().equals(7)) { - Integer dnsStrategyId = baseIpCfg.getDnsStrategyId(); - if (dnsStrategyId != null) { - List dnsStrategys = dnsResStrategyService - .findDnsResStrategys(Long.valueOf(dnsStrategyId), Constants.VALID_YES, Constants.AUDIT_YES); - if (dnsStrategys == null || dnsStrategys.size() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("dns_res_strategy")) - + ";"); - } - } - } - // dns fake ip - if (regionDict.getFunctionId().equals(401)) { - Integer dnsStrategyId = baseIpCfg.getDnsStrategyId(); - if (dnsStrategyId != null) { - if(dnsStrategyId>0) { - List dnsStrategys = dnsResStrategyService - .findDnsResStrategys(Long.valueOf(dnsStrategyId), Constants.VALID_YES, Constants.AUDIT_YES); - if (dnsStrategys == null || dnsStrategys.size() == 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("dns_res_strategy")) - + ";"); - } - } - }else { - baseIpCfg.setDnsStrategyId(0); - } - } - //p2p ip - if (regionDict.getFunctionId().equals(510) && "p2p_ip".equals(regionDict.getConfigServiceType())) { // P2p - // IP - String userRegion1 = baseIpCfg.getUserRegion1(); - List ipTypes = DictUtils.getDictList("P2P_IP_TYPE"); - boolean has = false; - for (SysDataDictionaryItem type : ipTypes) { - if (type.getItemCode().equals(userRegion1)) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("p2p_ip_config_type")) - + ";"); - } - } - if (regionDict.getFunctionId().equals(600)) {//asn - //asn id - String userRegion1 = baseIpCfg.getUserRegion1(); - if(StringUtils.isBlank(userRegion1)) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("asn_no")) - + ";"); - }else { - try { - Long asnNo=Long.parseLong(userRegion1); - /*ConfigGroupInfo configGroupInfo=asnIpCfgService.getConfigGroupInfoByAsnNo(asnNo); - if(configGroupInfo==null) {//不存在则新增 - PolicyGroupInfo info=new PolicyGroupInfo(); - info.setAsnNo(asnNo); - info.setDescription("Auto generated"); - info.setGroupName(baseIpCfg.getCfgDesc()+(i+1)); - info.setGroupType(4); - policyGroupInfoService.saveOrUpdate(info); - configGroupInfo=asnIpCfgService.getConfigGroupInfoByAsnNo(asnNo); - } - baseIpCfg.setAsnIpGroup(configGroupInfo.getGroupId());*/ - if(asnNo.longValue()>4294967295l||asnNo.longValue()<1) { - errInfo.append(prop.getProperty("asn_no")+" "+ - String.format(prop.getProperty("must_between"),"1","4294967295" ) - + ";"); - } - }catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("not_number"), prop.getProperty("asn_no")) - + ";"); - } - } - } - if (regionDict.getFunctionId().equals(301)) { - String antiddosProtocol=baseIpCfg.getAntiddosProtocol(); - Long bpsThreadshold= baseIpCfg.getBpsThreadshold(); - Long ppsThreadshold= baseIpCfg.getPpsThreadshold(); - if(StringUtils.isNotBlank(antiddosProtocol)) { - List antiddosProtocolDicts=DictUtils.getDictList("ANTIDDOS_PROTOCOL"); - boolean has=false; - for(SysDataDictionaryItem ditc:antiddosProtocolDicts) { - if(ditc.getItemCode().equals(antiddosProtocol)) { - has=true; - break; - } - } - if(!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("antiddos_protocol")) - + ";"); - } - if(bpsThreadshold==null) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("bps_threadshold")) - + ";"); - } - if(ppsThreadshold==null) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("pps_threadshold")) - + ";"); - } - } - } - if (regionDict.getRegionType().equals(1)) { - // 校验必填的IP,端口 - /* - * for (String code : configIpPortShow.split(",")) { if ("1".equals(code)) {// - * 源IP if (StringUtil.isEmpty(baseIpCfg.getSrcIpAddress())) { - * errInfo.append(String.format(prop.getProperty("can_not_null"), - * prop.getProperty("client_ip", "Client IP")) + ";"); } } else if - * ("2".equals(code)) {// 源端口 if (StringUtil.isEmpty(baseIpCfg.getSrcPort())) { - * errInfo.append(String.format(prop.getProperty("can_not_null"), - * prop.getProperty("client_port", "Client Port")) + ";"); } } else if - * ("3".equals(code)) {// 目的IP if - * (StringUtil.isEmpty(baseIpCfg.getDestIpAddress())) { - * errInfo.append(String.format(prop.getProperty("can_not_null"), - * prop.getProperty("server_ip", "Server IP")) + ";"); } } else if - * ("4".equals(code)) {// 目的端口 if (StringUtil.isEmpty(baseIpCfg.getDestPort())) - * { errInfo.append(String.format(prop.getProperty("can_not_null"), - * prop.getProperty("server_port", "Server Port")) + ";"); } } } - */ - boolean srcIpEmpty = false; - boolean destIpEmpty = false; - boolean srcPortEmpty = false; - boolean destPortEmpty = false; - Pattern ipv4IpRangeRegexpNew = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP_NEW); - if (StringUtil.isEmpty(baseIpCfg.getSrcIpAddress())) { - srcIpEmpty = true; - }else { - //处理0.0.0.1-2这种格式,转换为0.0.0.1-0.0.0.2 - Matcher m=ipv4IpRangeRegexpNew.matcher(baseIpCfg.getSrcIpAddress()); - if(m.matches()) { - String prefix=baseIpCfg.getSrcIpAddress().split("-")[0]; - String subfix=baseIpCfg.getSrcIpAddress().split("-")[1]; - baseIpCfg.setSrcIpAddress(prefix+"-"+prefix.substring(0, prefix.lastIndexOf(".")+1)+subfix); - } - } - if (StringUtil.isEmpty(baseIpCfg.getDestIpAddress())) { - destIpEmpty = true; - }else { - //处理0.0.0.1-2这种格式,转换为0.0.0.1-0.0.0.2 - Matcher m=ipv4IpRangeRegexpNew.matcher(baseIpCfg.getDestIpAddress()); - if(m.matches()) { - String prefix=baseIpCfg.getDestIpAddress().split("-")[0]; - String subfix=baseIpCfg.getDestIpAddress().split("-")[1]; - baseIpCfg.setDestIpAddress(prefix+"-"+prefix.substring(0, prefix.lastIndexOf(".")+1)+subfix); - } - } - if (StringUtil.isEmpty(baseIpCfg.getSrcPort())) { - srcPortEmpty = true; - } - if (StringUtil.isEmpty(baseIpCfg.getDestPort())) { - destPortEmpty = true; - } - if (srcIpEmpty && destIpEmpty) { - if (configIpPortShow.indexOf("1") > -1 && configIpPortShow.indexOf("3") > -1) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("client_ip", "Client IP")) + ";"); - } else if (configIpPortShow.indexOf("1") > -1) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("client_ip", "Client IP")) + ";"); - } else if (configIpPortShow.indexOf("3") > -1) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("server_ip", "Server IP")) + ";"); - } - } else if (destIpEmpty) {// 目的IP为空 - if (configIpPortShow.indexOf("3") > -1 && configIpPortShow.indexOf("1") == -1) {// 目的IP必填 - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("server_ip", "Server IP")) + ";"); - } else if (configIpPortShow.indexOf("1") > -1) { - /* - * if(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.equals(baseIpCfg.getSrcIpAddress())) - * { errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_RANGE_VALUE+"'")+";"); }else - * if(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.equals(baseIpCfg.getSrcIpAddress()) - * ) { errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_SUBNET_VALUE+"'")+";"); }else - * if(Constants.IPV6_DEFAULT_IP_VALUE.equals(baseIpCfg.getSrcIpAddress())) { - * errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.equals(baseIpCfg.getSrcIpAddress())) - * { errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_RANGE_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.equals(baseIpCfg.getSrcIpAddress()) - * ) { errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_SUBNET_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_VALUE.equals(baseIpCfg.getSrcIpAddress())) { - * errInfo.append(prop.getProperty("client_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_VALUE+"'")+";"); } - */ - String matchType = this.validIPAddress("client_ip", errInfo, baseIpCfg.getSrcIpAddress(), - ipType, ipPattern); - if (matchType.startsWith("IPV6")) { - if (matchType.endsWith("RANGE")) { - // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - baseIpCfg.setIpPattern(2); - if (baseIpCfg.getSrcIpAddress() - .startsWith(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { - errInfo.append(prop.getProperty("client_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0]) - + ";"); - } - } else if (matchType.endsWith("SUBNET")) { - // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - baseIpCfg.setIpPattern(1); - if (baseIpCfg.getSrcIpAddress() - .startsWith(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { - errInfo.append( - prop.getProperty("client_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) - + ";"); - } - } else { - // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - baseIpCfg.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - baseIpCfg.setIpPattern(3); - if (baseIpCfg.getSrcIpAddress().equals(Constants.IPV6_DEFAULT_IP_VALUE)) { - errInfo.append(prop.getProperty("client_ip")+" " + String.format( - prop.getProperty("can_not_be"), Constants.IPV6_DEFAULT_IP_VALUE) + ";"); - } - } - baseIpCfg.setIpType(6); - } else { - if (matchType.endsWith("RANGE")) { - this.validIPv4Range("client_ip", errInfo, baseIpCfg.getSrcIpAddress()); - // baseIpCfg.setSrcIpAddress("0.0.0.0-"+baseIpCfg.getSrcIpAddress().split("-")[0].substring(0,baseIpCfg.getSrcIpAddress().indexOf("-")+1)+baseIpCfg.getSrcIpAddress().split("-")[1]); - baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - baseIpCfg.setIpPattern(2); - if (baseIpCfg.getSrcIpAddress() - .startsWith(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { - errInfo.append(prop.getProperty("client_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0]) - + ";"); - } - } else if (matchType.endsWith("SUBNET")) { - // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - baseIpCfg.setIpPattern(1); - if (baseIpCfg.getSrcIpAddress() - .startsWith(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { - errInfo.append( - prop.getProperty("client_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) - + ";"); - } - } else { - baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - baseIpCfg.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - baseIpCfg.setIpPattern(3); - if (baseIpCfg.getSrcIpAddress().equals(Constants.IPV4_DEFAULT_IP_VALUE)) { - errInfo.append(prop.getProperty("client_ip")+" " + String.format( - prop.getProperty("can_not_be"), Constants.IPV4_DEFAULT_IP_VALUE) + ";"); - } - } - baseIpCfg.setIpType(4); - } - } - - } else if (srcIpEmpty) {// 源IP为空 - if (configIpPortShow.indexOf("1") > -1 && configIpPortShow.indexOf("3") == -1) {// 源IP必填 - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("client_ip", "Client IP")) + ";"); - } else if (configIpPortShow.indexOf("3") > -1) { - /* - * if(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.equals(baseIpCfg.getDestIpAddress()) - * ) { errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_RANGE_VALUE+"'")+";"); }else - * if(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.equals(baseIpCfg.getDestIpAddress() - * )) { errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_SUBNET_VALUE+"'")+";"); }else - * if(Constants.IPV6_DEFAULT_IP_VALUE.equals(baseIpCfg.getDestIpAddress())) { - * errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV6_DEFAULT_IP_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.equals(baseIpCfg.getDestIpAddress()) - * ) { errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_RANGE_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.equals(baseIpCfg.getDestIpAddress() - * )) { errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_SUBNET_VALUE+"'")+";"); }else - * if(Constants.IPV4_DEFAULT_IP_VALUE.equals(baseIpCfg.getDestIpAddress())) { - * errInfo.append(prop.getProperty("server_ip") + - * String.format(prop.getProperty("can_not_be"), - * "'"+Constants.IPV4_DEFAULT_IP_VALUE+"'")+";"); } - */ - String matchType = this.validIPAddress("server_ip", errInfo, baseIpCfg.getDestIpAddress(), - ipType, ipPattern); - if (matchType.startsWith("IPV6")) { - if (matchType.endsWith("RANGE")) { - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - baseIpCfg.setIpPattern(2); - if (baseIpCfg.getDestIpAddress() - .startsWith(Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { - errInfo.append(prop.getProperty("server_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV6_DEFAULT_IP_RANGE_VALUE.split("-")[0]) - + ";"); - } - } else if (matchType.endsWith("SUBNET")) { - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - baseIpCfg.setIpPattern(1); - if (baseIpCfg.getDestIpAddress() - .startsWith(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { - errInfo.append( - prop.getProperty("server_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV6_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) - + ";"); - } - } else { - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - baseIpCfg.setSrcIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - baseIpCfg.setIpPattern(3); - if (baseIpCfg.getDestIpAddress().equals(Constants.IPV6_DEFAULT_IP_VALUE)) { - errInfo.append(prop.getProperty("server_ip")+" " + String.format( - prop.getProperty("can_not_be"), Constants.IPV6_DEFAULT_IP_VALUE) + ";"); - } - } - baseIpCfg.setIpType(6); - } else { - if (matchType.endsWith("RANGE")) { - this.validIPv4Range("server_ip", errInfo, baseIpCfg.getDestIpAddress()); - // baseIpCfg.setDestIpAddress("0.0.0.0-"+baseIpCfg.getDestIpAddress().split("-")[0] - // .substring(0,baseIpCfg.getDestIpAddress().indexOf("-")+1) - // +baseIpCfg.getDestIpAddress().split("-")[1]); - baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - baseIpCfg.setIpPattern(2); - if (baseIpCfg.getDestIpAddress() - .startsWith(Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0])) { - errInfo.append(prop.getProperty("server_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV4_DEFAULT_IP_RANGE_VALUE.split("-")[0]) - + ";"); - } - } else if (matchType.endsWith("SUBNET")) { - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - baseIpCfg.setIpPattern(1); - if (baseIpCfg.getDestIpAddress() - .startsWith(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0])) { - errInfo.append( - prop.getProperty("server_ip")+" " - + String.format(prop.getProperty("can_not_be"), - Constants.IPV4_DEFAULT_IP_SUBNET_VALUE.split("/")[0]) - + ";"); - } - } else { - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - baseIpCfg.setSrcIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - baseIpCfg.setIpPattern(3); - if (baseIpCfg.getDestIpAddress().equals(Constants.IPV4_DEFAULT_IP_VALUE)) { - errInfo.append(prop.getProperty("server_ip")+" " + String.format( - prop.getProperty("can_not_be"), Constants.IPV4_DEFAULT_IP_VALUE) + ";"); - } - } - baseIpCfg.setIpType(4); - } - } - } else {// 全不为空 - String matchType = this.validIPAddress("client_ip", errInfo, baseIpCfg.getSrcIpAddress(), ipType, - ipPattern); - String matchType1 = this.validIPAddress("server_ip", errInfo, baseIpCfg.getDestIpAddress(), ipType, - ipPattern); - if (StringUtils.isNotBlank(matchType) && StringUtils.isNotBlank(matchType1) - && !matchType.equals(matchType1)) { - errInfo.append(prop.get("the_same_ip_type_pattern") + ";"); - } - // baseIpCfg.setSrcIpAddress(baseIpCfg.getSrcIpAddress()); - // baseIpCfg.setDestIpAddress(baseIpCfg.getDestIpAddress()); - if (matchType.startsWith("IPV6")) { - baseIpCfg.setIpType(6); - } else { - baseIpCfg.setIpType(4); - } - if (matchType.endsWith("RANGE")) { - if (matchType.startsWith("IPV4")) { - this.validIPv4Range("client_ip", errInfo, baseIpCfg.getSrcIpAddress()); - this.validIPv4Range("server_ip", errInfo, baseIpCfg.getDestIpAddress()); - } - - if (baseIpCfg.getSrcIpAddress().split("-")[0] - .equals(baseIpCfg.getDestIpAddress().split("-")[0])) { - errInfo.append(String.format(prop.getProperty("are_the_same"), - baseIpCfg.getSrcIpAddress().split("-")[0], - baseIpCfg.getDestIpAddress().split("-")[0]) + ";"); - } - baseIpCfg.setIpPattern(2); - } else if (matchType.endsWith("SUBNET")) { - if (baseIpCfg.getSrcIpAddress().split("/")[0] - .equals(baseIpCfg.getDestIpAddress().split("/")[0])) { - errInfo.append(String.format(prop.getProperty("are_the_same"), - baseIpCfg.getSrcIpAddress().split("/")[0], - baseIpCfg.getDestIpAddress().split("/")[0]) + ";"); - } - baseIpCfg.setIpPattern(1); - } else { - if (baseIpCfg.getSrcIpAddress().equals(baseIpCfg.getDestIpAddress())) { - errInfo.append(String.format(prop.getProperty("are_the_same"), - prop.getProperty("client_ip"), prop.getProperty("server_ip")) + ";"); - } - baseIpCfg.setIpPattern(3); - } - } - - // TODO 判断源IP和目的IP的值 - // TODO 判断源IP和目的IP格式 - if (destPortEmpty) { - if (srcPortEmpty) { - baseIpCfg.setSrcPort("0"); - baseIpCfg.setDestPort("0"); - baseIpCfg.setPortPattern(1); - } else { - if (baseIpCfg.getSrcPort().indexOf("/") > -1) { - baseIpCfg.setDestPort("0/0"); - baseIpCfg.setPortPattern(2); - } else { - baseIpCfg.setDestPort("0"); - baseIpCfg.setPortPattern(1); - } - baseIpCfg.setSrcPort(baseIpCfg.getSrcPort().trim()); - } - } else { - if (srcPortEmpty) { - if (baseIpCfg.getDestPort().indexOf("/") > -1) { - baseIpCfg.setSrcPort("0/0"); - baseIpCfg.setPortPattern(2); - } else { - baseIpCfg.setSrcPort("0"); - baseIpCfg.setPortPattern(1); - } - baseIpCfg.setDestPort(baseIpCfg.getSrcPort().trim()); - } else { - if (baseIpCfg.getSrcPort().indexOf("/") > -1) { - baseIpCfg.setPortPattern(2); - } else { - baseIpCfg.setPortPattern(1); - } - baseIpCfg.setSrcPort(baseIpCfg.getSrcPort().trim()); - baseIpCfg.setDestPort(baseIpCfg.getDestPort().trim()); - - } - } - // TODO 判断源端口和目的端口格式 - // TODO 判断源和目的端口的值 - boolean validPort = this.validPort(errInfo, baseIpCfg.getSrcPort(), baseIpCfg.getDestPort(), - portPattern); - if (StringUtil.isEmpty(baseIpCfg.getProtocol())) { - //packet ip reject - if(regionDict.getFunctionId().equals(5)&&serviceDict!=null&&serviceDict.getServiceId().equals(16)) { - baseIpCfg.setProtocol(6); - }else { - if (StringUtil.isEmpty(regionDict.getConfigProtocol())) { - baseIpCfg.setProtocol(0); - } else { - baseIpCfg.setProtocol(Integer.parseInt(regionDict.getConfigProtocol().split(",")[0])); - } - - } - - baseIpCfg.setProtocolId(serviceDict==null?0:serviceDict.getProtocolId()); - } else { - baseIpCfg.setProtocol(baseIpCfg.getProtocol()); - baseIpCfg.setProtocolId(serviceDict==null?0:serviceDict.getProtocolId()); - } - // TODO 判断Protocol的值 - boolean validProtocol = this.validProtocol(errInfo, baseIpCfg.getProtocol(), protocol); - // 验证protocol与port是否符合逻辑 - if (validProtocol && validPort) { - validProtocolAndPort(errInfo, baseIpCfg.getProtocol(), baseIpCfg.getSrcPort(), - baseIpCfg.getDestPort()); - } - if (StringUtil.isEmpty(baseIpCfg.getDirection())) { - if (StringUtil.isEmpty(regionDict.getConfigDirection())) { - baseIpCfg.setDirection(0); - } else { - baseIpCfg.setDirection(Integer.parseInt(regionDict.getConfigDirection().split(",")[0])); - } - } else { - baseIpCfg.setDirection(baseIpCfg.getDirection()); - } - - // TODO 判断Direction的值 - this.validDirection(errInfo, baseIpCfg.getDirection(), direction); - } - if (errInfo.toString().length() > 0) {// - errTip.append(String.format(prop.getProperty("line"), i + 2) + ","); - errTip.append(errInfo); - errTip.append("
"); - } - ipList.add(baseIpCfg); - } - if (errTip.toString().length() > 0) { - throw new ServiceException(errTip.toString()); - } - return ipList; - } - public List checkDnsResStrategyCfg(FunctionServiceDict serviceDict, FunctionRegionDict regionDict, List list) - throws ServiceException { - List dnsResStrategies=Lists.newArrayList(); - Properties prop = this.getMsgProp(); - StringBuffer errTip = new StringBuffer(); - for (int i = 0; i < list.size(); i++) { - StringBuffer errInfo = new StringBuffer(); - DnsResStrategy dnsResStrategy=new DnsResStrategy(); - BeanUtils.copyProperties(list.get(i), dnsResStrategy); - String groupName=dnsResStrategy.getCfgDesc(); - if(StringUtils.isBlank(groupName)) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("policy_name")) + ";"); - } - Integer resGroup1Id=dnsResStrategy.getResGroup1Id(); - if(resGroup1Id==null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("group")) + ";"); - }else { - PolicyGroupInfo info=policyGroupInfoService.getById(resGroup1Id); - if(info==null) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("group")) + ";"); - } - } - Integer resGroup1Num=dnsResStrategy.getResGroup1Num(); - if(resGroup1Num==null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("res_group_num")) + ";"); - } - String ttl=dnsResStrategy.getTtl(); - if(StringUtils.isBlank(ttl)) { - errInfo.append(String.format(prop.getProperty("can_not_null"), - prop.getProperty("min_ttl")) + ";"); - }else { - Pattern p=Pattern.compile("\\d+\\-\\d+"); - Matcher m=p.matcher(ttl); - if(!m.matches()) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("min_ttl")) + ";"); - }else { - String minTtl=ttl.split("-")[0]; - String maxTtl=ttl.split("-")[1]; - Integer min=null,max=null; - try { - min=Integer.parseInt(minTtl); - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("min_ttl")) + ";"); - } - try { - max=Integer.parseInt(maxTtl); - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("min_ttl")) + ";"); - } - if(min.intValue()>max.intValue()) - {errInfo.append(String.format(prop.getProperty("is_incorrect"), - prop.getProperty("min_ttl")) + ";"); - }else { - dnsResStrategy.setMinTtl(min); - dnsResStrategy.setMaxTtl(max); - } - } - } - if (errInfo.toString().length() > 0) {// - errTip.append(String.format(prop.getProperty("line"), i + 2) + ","); - errTip.append(errInfo); - errTip.append("
"); - } - dnsResStrategies.add(dnsResStrategy); - } - if (errTip.toString().length() > 0) { - throw new ServiceException(errTip.toString()); - } - return dnsResStrategies; - } - /** - * 协议与端口验证规则 - * - * @param errInfo - * @param protocol - * @param srcPort - * @param destPort - */ - public void validProtocolAndPort(StringBuffer errInfo, Integer protocol, String srcPort, String destPort) { - Properties prop = this.getMsgProp(); - if (srcPort.indexOf("/") > -1) { - srcPort = srcPort.split("/")[0]; - } - if (destPort.indexOf("/") > -1) { - destPort = destPort.split("/")[0]; - } - if (protocol == 0) { - return; - } else if (protocol != 6 && protocol != 17) { - if (!"0".equals(srcPort) || !"0".equals(destPort)) { - errInfo.append(prop.get("protocol_and_port") + ";"); - } - } - - } - - /** - * 验证方向 - * - * @param errInfo - * @param direction - * @param directionPattern - * @throws ServiceException - */ - public boolean validDirection(StringBuffer errInfo, Integer direction, String directionPattern) - throws ServiceException { - Properties prop = this.getMsgProp(); - if (direction == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("direction")) + ";"); - return false; - } - boolean has = false; - for (String p : directionPattern.split(",")) { - if (p.equals(direction.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("direction")) + "." - + String.format(prop.getProperty("alternative_values"), directionPattern) + ";"); - } - return has; - } - - /** - * 验证协议 - * - * @param errInfo - * @param protocol - * @param protocolPattern - * @return - * @throws ServiceException - */ - public boolean validProtocol(StringBuffer errInfo, Integer protocol, String protocolPattern) - throws ServiceException { - Properties prop = this.getMsgProp(); - if (protocol == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("protocol")) + ";"); - return false; - } - boolean has = false; - for (String p : protocolPattern.split(",")) { - if (p.equals(protocol.toString())) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("protocol")) + "." - + String.format(prop.getProperty("alternative_values"), protocolPattern) + ";"); - } - return has; - } - - /** - * 验证端口格式 - * - * @param srcPortStr - * @param destPortStr - * @param portPattern - * @throws ServiceException - */ - public boolean validPort(StringBuffer errInfo, String srcPortStr, String destPortStr, String portPattern) - throws ServiceException { - boolean valid = true; - Properties prop = this.getMsgProp(); - if (portPattern.equals("1")) { - try { - Integer srcPort = Integer.parseInt(srcPortStr); - if (srcPort < 0 || srcPort > 65535) { - errInfo.append(prop.getProperty("client_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("client_port")) + ";"); - valid = false; - } - try { - Integer destPort = Integer.parseInt(destPortStr); - if (destPort < 0 || destPort > 65535) { - errInfo.append(prop.getProperty("server_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append(String.format(prop.getProperty("not_number"), prop.getProperty("server_port")) + ";"); - valid = false; - } - } else if (portPattern.equals("2")) { - Pattern p = Pattern.compile("^[0-9]+/[0-9]+$"); - Matcher m = p.matcher(srcPortStr); - if (!m.matches()) { - errInfo.append( - String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("client_port")) + ";"); - valid = false; - } - m = p.matcher(destPortStr); - if (!m.matches()) { - errInfo.append( - String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("server_port")) + ";"); - valid = false; - } - Integer srcPort = Integer.parseInt(srcPortStr.split("/")[0]); - Integer srcPortMask = Integer.parseInt(srcPortStr.split("/")[1]); - Integer destPort = Integer.parseInt(destPortStr.split("/")[0]); - Integer destPortMask = Integer.parseInt(destPortStr.split("/")[1]); - if (srcPort < 0 || srcPort > 65535) { - errInfo.append(prop.getProperty("client_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - if (srcPortMask < 0 || srcPortMask > 65535) { - errInfo.append(prop.getProperty("client_port_mask") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - if (destPort < 0 || destPort > 65535) { - errInfo.append(prop.getProperty("server_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - if (destPortMask < 0 || destPortMask > 65535) { - errInfo.append(prop.getProperty("server_port_mask") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } else if (portPattern.indexOf("1") > -1 && portPattern.indexOf("2") > -1) { - Pattern p = Pattern.compile("^[0-9]+/[0-9]+$"); - Pattern p1 = Pattern.compile("^[0-9]+$"); - Matcher m = p.matcher(srcPortStr);// 源端口是端口掩码格式 - Matcher m1 = p.matcher(destPortStr);// 目的端口是端口掩码格式 - Matcher m2 = p1.matcher(srcPortStr);// 源端口是端口格式 - Matcher m3 = p1.matcher(destPortStr);// 目的端口是端口格式 - if (m.matches()) { - Integer srcPort = Integer.parseInt(srcPortStr.split("/")[0]); - Integer srcPortMask = Integer.parseInt(srcPortStr.split("/")[1]); - if (srcPort < 0 || srcPort > 65535) { - errInfo.append(prop.getProperty("client_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - if (srcPortMask < 0 || srcPortMask > 65535) { - errInfo.append(prop.getProperty("client_port_mask") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } - if (m1.matches()) { - Integer destPort = Integer.parseInt(destPortStr.split("/")[0]); - Integer destPortMask = Integer.parseInt(destPortStr.split("/")[1]); - - if (destPort < 0 || destPort > 65535) { - errInfo.append(prop.getProperty("server_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - if (destPortMask < 0 || destPortMask > 65535) { - errInfo.append(prop.getProperty("server_port_mask") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } - if (m2.matches()) { - try { - Integer srcPort = Integer.parseInt(srcPortStr); - if (srcPort < 0 || srcPort > 65535) { - errInfo.append(prop.getProperty("client_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("not_number"), prop.getProperty("client_port")) + ";"); - valid = false; - } - } - if (m3.matches()) { - try { - Integer destPort = Integer.parseInt(destPortStr); - if (destPort < 0 || destPort > 65535) { - errInfo.append(prop.getProperty("server_port") - + String.format(prop.getProperty("must_between"), 0, 65535) + ";"); - valid = false; - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("not_number"), prop.getProperty("server_port")) + ";"); - valid = false; - } - } - if ((m.matches() && m3.matches()) || (m2.matches() && m1.matches())) { - errInfo.append(prop.getProperty("the_same_port_pattern") + ";"); - valid = false; - } - if (!m.matches() && !m2.matches()) { - errInfo.append( - String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("client_port")) + ";"); - valid = false; - } - if (!m1.matches() && !m3.matches()) { - errInfo.append( - String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty("server_port")) + ";"); - valid = false; - } - - } - return valid; - } - - /** - * IPv4 范围验证 - * - * @param ipName - * @param errInfo - * @param ip - */ - public void validIPv4Range(String ipName, StringBuffer errInfo, String ip) { - Properties prop = this.getMsgProp(); - String prefix = ip.split("-")[0]; - String subfix = ip.split("-")[1]; - if ((Integer.parseInt(prefix.split("\\.")[0])<128&&Integer.parseInt(subfix.split("\\.")[0])<128) - ||(Integer.parseInt(prefix.split("\\.")[0])>127&&Integer.parseInt(subfix.split("\\.")[0])>127&&Integer.parseInt(prefix.split("\\.")[0])<192&&Integer.parseInt(subfix.split("\\.")[0])<192) - ||(Integer.parseInt(prefix.split("\\.")[0])>191&&Integer.parseInt(subfix.split("\\.")[0])>191&&Integer.parseInt(prefix.split("\\.")[0])<224&&Integer.parseInt(subfix.split("\\.")[0])<224) - ||(Integer.parseInt(prefix.split("\\.")[0])>223&&Integer.parseInt(subfix.split("\\.")[0])>223&&Integer.parseInt(prefix.split("\\.")[0])<240&&Integer.parseInt(subfix.split("\\.")[0])<240) - ||(Integer.parseInt(prefix.split("\\.")[0])>239&&Integer.parseInt(subfix.split("\\.")[0])>239&&Integer.parseInt(prefix.split("\\.")[0])<256&&Integer.parseInt(subfix.split("\\.")[0])<256)) { - }else { - errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_in_the_same") + ";"); - } -// if (Integer.parseInt(prefix.split("\\.")[0]) < 192 || Integer.parseInt(prefix.split("\\.")[0]) > 223 -// || Integer.parseInt(subfix.split("\\.")[0]) < 192 || Integer.parseInt(subfix.split("\\.")[0]) > 223) { -// errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_in_c") + ";"); -// } - if (!prefix.split("\\.")[0].equals(subfix.split("\\.")[0]) - || !prefix.split("\\.")[1].equals(subfix.split("\\.")[1]) - || !prefix.split("\\.")[2].equals(subfix.split("\\.")[2])) { - errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_bit_field") + ";"); - } - if (ipToInt(prefix) >= ipToInt(subfix)) { - errInfo.append(prop.get(ipName) + " " + prop.get("ip_range_smaller") + ";"); - } - } - - /** - * 根据位运算把 byte[] -> int - * - * @param bytes - * @return int - */ - public static int bytesToInt(byte[] bytes) { - int addr = bytes[3] & 0xFF; - addr |= ((bytes[2] << 8) & 0xFF00); - addr |= ((bytes[1] << 16) & 0xFF0000); - addr |= ((bytes[0] << 24) & 0xFF000000); - return addr; - } - - public static int ipToInt(String ipAddr) { - try { - return bytesToInt(ipToBytesByReg(ipAddr)); - } catch (Exception e) { - throw new IllegalArgumentException(ipAddr + " is invalid IP"); - } - } - - /** - * 把IP地址转化为int - * - * @param ipAddr - * @return int - */ - public static byte[] ipToBytesByReg(String ipAddr) { - byte[] ret = new byte[4]; - try { - String[] ipArr = ipAddr.split("\\."); - ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF); - ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF); - ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF); - ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF); - return ret; - } catch (Exception e) { - throw new IllegalArgumentException(ipAddr + " is invalid IP"); - } - - } - - /** - * 校验ip - * - * @param ipName - * @param errInfo - * @param ip - * @param ipType - * @param ipPattern - * @return - * @throws ServiceException - */ - public String validIPAddress(String ipName, StringBuffer errInfo, String ip, String ipType, String ipPattern) - throws ServiceException { - Properties prop = this.getMsgProp(); - String matchType = ""; - Pattern ipv4IpSubnetOriginalRegexp = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP_ORIGINAL); - Pattern ipv4IpSubnetRegexp = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); - Pattern ipv6IpSubnetRegexp = Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); - Pattern ipv4IpRangeRegexp = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); - Pattern ipv6IpRangeRegexp = Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); - Pattern ipv4IpRegexp = Pattern.compile(Constants.IPV4_IP_REGEXP); - Pattern ipv6IpRegexp = Pattern.compile(Constants.IPV6_IP_REGEXP); - Matcher matcher = ipv4IpRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV4_IP"; - } else { - matcher = ipv6IpRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV6_IP"; - } else { - matcher = ipv4IpSubnetRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV4_IP_SUBNET"; - } else { - matcher = ipv6IpSubnetRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV6_IP_SUBNET"; - } else { - matcher = ipv4IpRangeRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV4_IP_RANGE"; - } else { - matcher = ipv6IpRangeRegexp.matcher(ip); - if (matcher.matches()) { - matchType = "IPV6_IP_RANGE"; - } else { - matcher = ipv4IpSubnetOriginalRegexp.matcher(ip); - if(matcher.matches()) { - errInfo.append(prop.getProperty(ipName)+" "+prop.getProperty("ipv4_mask_range_tip")+ ";"); - }else { - errInfo.append( - String.format(prop.getProperty("is_in_wrong_format"), prop.getProperty(ipName)) - + ";"); - } - } - } - } - } - } - } - if (ipType.indexOf("4") == -1 && matchType.startsWith("IPV4")) { - errInfo.append( - prop.getProperty("ip_type") + " " + String.format(prop.getProperty("can_not_be"), "IPv4") + ",4;"); - } - if (ipType.indexOf("6") == -1 && matchType.startsWith("IPV6")) { - errInfo.append( - prop.getProperty("ip_type") + " " + String.format(prop.getProperty("can_not_be"), "IPv6") + ",6;"); - } - if (ipPattern.indexOf("3") == -1 && matchType.endsWith("IP")) { - errInfo.append( - prop.getProperty("ip_pattern") + " " + String.format(prop.getProperty("can_not_be"), "IP") + ",3;"); - } - if (ipPattern.indexOf("1") == -1 && matchType.endsWith("SUBNET")) { - errInfo.append(prop.getProperty("ip_pattern") + " " - + String.format(prop.getProperty("can_not_be"), prop.getProperty("ip_subnet")) + ",1;"); - } - if (ipPattern.indexOf("2") == -1 && matchType.endsWith("RANGE")) { - errInfo.append(prop.getProperty("ip_pattern") + " " - + String.format(prop.getProperty("can_not_be"), prop.getProperty("ip_range")) + ",2;"); - } - return matchType; - } - - @Deprecated - public void checkIpCfg(int action, int functionId, List list) throws ServiceException { - Properties prop = this.getMsgProp(); - List ipTypeList = DictUtils.getDictList("IP_TYPE"); - List ipPatternList = DictUtils.getDictList("IP_PATTERN"); - List portPatternList = DictUtils.getDictList("PORT_PATTERN"); - List directionList = DictUtils.getDictList("DIRECTION"); - List protocolList = DictUtils.getDictList("PROTOCOL"); - List ipsecProrocolList = DictUtils.getDictList("IPSEC_PROTOCOL"); - List tunnelProrocolList = DictUtils.getDictList("TUNNEL_PROTOCOL"); - List specialFunctionIdList = DictUtils.getDictList("SPECIAL_FUNCTION_ID"); - List irTypeList = DictUtils.getDictList("IR_TYPE"); - String specialItem = null; - for (SysDataDictionaryItem sfuncItem : specialFunctionIdList) { - if (functionId == Integer.parseInt(sfuncItem.getItemCode())) { - specialItem = sfuncItem.getItemValue(); - } - } - if ("ipaddr".equals(specialItem)) { - List icmpprotocolList = DictUtils.getDictList("ICMP_PROTOCOL"); - if (action == Constants.DROP_ACTION) { - protocolList.addAll(icmpprotocolList); - } - } - StringBuffer msg = new StringBuffer(); - int line = 1; - for (IpCfgTemplate value : list) { - String lineStart = String.format(prop.getProperty("line"), line) + ":"; - StringBuffer errInfo = new StringBuffer(); - // ip_type check start - Integer ipType = value.getIpType(); - if (ipType == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("ip_type")) + ";"); - } else { - boolean has = false; - for (SysDataDictionaryItem ipTypeItem : ipTypeList) { - if (ipType == Integer.parseInt(ipTypeItem.getItemCode())) { - has = true; - break; - } - } - if (!has) { - errInfo.append(String.format(prop.getProperty("is_incorrect"), prop.getProperty("ip_type")) + ";"); - } - } - // ip_type check end - // ip_pattern check start - Integer ipPattern = value.getIpPattern(); - if (ipPattern == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("ip_pattern")) + ";"); - } else { - boolean has = false; - for (SysDataDictionaryItem ipPatternItem : ipPatternList) { - if (ipPattern == Integer.parseInt(ipPatternItem.getItemCode())) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("ip_pattern")) + ";"); - } - } - // ip_pattern check end - // client_ip check start - String srcIp = value.getSrcIpAddress(); - String _msg = ""; - if ("ipmulitiplex".equals(specialItem)) { - _msg = checkIP(prop, prop.getProperty("IP"), srcIp, ipType, ipPattern); - } else { - _msg = checkIP(prop, prop.getProperty("client_ip"), srcIp, ipType, ipPattern); - } - if (StringUtils.isNotBlank(_msg)) { - errInfo.append(_msg); - } - // client_ip check end - // server_ip check start - String destIp = value.getDestIpAddress(); - if ("ipmulitiplex".equals(specialItem)) {// set 默认值 - if (ipPattern != null && ipType != null) { - if (1 == ipPattern.intValue()) { - if (4 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - } else if (6 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - } else if (46 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_SUBNET_VALUE); - } else if (64 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - } else if (10 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_SUBNET_VALUE); - } - } else if (2 == ipPattern.intValue()) { - if (4 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - } else if (6 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - } else if (46 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_RANGE_VALUE); - } else if (64 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - } else if (10 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_RANGE_VALUE); - } - } else if (3 == ipPattern.intValue()) { - if (4 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - } else if (6 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - } else if (46 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV6_DEFAULT_IP_VALUE); - } else if (64 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - } else if (10 == ipType.intValue()) { - value.setDestIpAddress(Constants.IPV4_DEFAULT_IP_VALUE); - } - } - } - } else { - if (srcIp.equals(destIp)) { - String info = String.format(prop.getProperty("are_the_same"), prop.getProperty("client_ip"), - prop.getProperty("server_ip")); - errInfo.append(info + ";"); - } - _msg = checkIP(prop, prop.getProperty("server_ip"), destIp, ipType, ipPattern); - if (StringUtils.isNotBlank(_msg)) { - errInfo.append(_msg); - } - } - if (!"ipmulitiplex".equals(specialItem) - && (46 == ipType.intValue() || 64 == ipType.intValue() || 10 == ipType.intValue())) { - _msg = checkIp(prop, prop.getProperty("client_ip"), srcIp, prop.getProperty("server_ip"), destIp, - ipType, ipPattern); - if (StringUtils.isNotBlank(_msg)) { - errInfo.append(_msg); - } - } - // server_ip check end - // port_pattern check start - Integer portPattern = value.getPortPattern(); - if (portPattern == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("port_pattern")) + ";"); - } else { - boolean has = false; - for (SysDataDictionaryItem portPatternItem : portPatternList) { - if (portPattern == Integer.parseInt(portPatternItem.getItemCode())) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("port_pattern")) + ";"); - } - } - // port_pattern check end - // src_port check start - String srcPort = value.getSrcPort(); - if ("ipmulitiplex".equals(specialItem)) { - _msg = checkPort(prop, prop.getProperty("port"), srcPort, portPattern); - } else { - _msg = checkPort(prop, prop.getProperty("client_port"), srcPort, portPattern); - } - - if (StringUtils.isNotBlank(_msg)) { - errInfo.append(_msg); - } - // src_port check end - // dest_port check start - String destPort = value.getDestPort(); - if ("ipmulitiplex".equals(specialItem)) {// set 默认值 - if (portPattern != null) { - if (2 == portPattern.intValue()) { - value.setDestPort(Constants.PORT_MASK_DEFAULT); - } else if (1 == portPattern.intValue()) { - value.setDestPort(Constants.PORT_DEFAULT); - } - } - } else { - _msg = checkPort(prop, prop.getProperty("server_port"), destPort, portPattern); - } - if (StringUtils.isNotBlank(_msg)) { - errInfo.append(_msg); - } - // dest_port check end - // direction check start - Integer direction = value.getDirection(); - if (direction == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("direction")) + ";"); - } else { - boolean has = false; - for (SysDataDictionaryItem directionItem : directionList) { - if (Integer.parseInt(directionItem.getItemCode()) == direction.intValue()) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("direction")) + ";"); - } - } - // direction check end - // protocol check start - Integer protocol = value.getProtocol(); - if (protocol == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("protocol")) + ";"); - } else { - if (specialItem != null && ("ipsec".equals(specialItem) || "tunnel".equals(specialItem))) { - if ("ipsec".equals(specialItem)) { - if (action == Constants.DROP_ACTION.intValue()) { - boolean has = false; - for (SysDataDictionaryItem protocolItem : ipsecProrocolList) { - if (Integer.parseInt(protocolItem.getItemCode()) == protocol.intValue()) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("protocol")) - + ";"); - } - } else { - if (protocol != 0) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("protocol")) - + ";"); - } - } - } else if ("tunnel".equals(specialItem)) { - for (SysDataDictionaryItem protocolItem : tunnelProrocolList) { - if ("default".equals(protocolItem.getItemValue())) { - if (protocol.intValue() != Integer.parseInt(protocolItem.getItemCode())) { - errInfo.append( - String.format(prop.getProperty("must_be"), prop.getProperty("protocol"), - Integer.parseInt(protocolItem.getItemCode())) + ";"); - } - break; - } - } - - } - } else { - boolean has = false; - for (SysDataDictionaryItem protocolItem : protocolList) { - if (Integer.parseInt(protocolItem.getItemCode()) == protocol.intValue()) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("protocol")) + ";"); - } - } - - } - // protocol check end - // requestId check start - Integer requestId = value.getRequestId(); - if (requestId == null) { - if ("whitelistip".equals(specialItem)) { - value.setRequestId(0); - } else { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("letter")) + ";"); - } - - } else {// 查询数据库是否存在ID - if ("whitelistip".equals(specialItem)) { - value.setRequestId(0); - } else { - RequestInfo info = requestInfoService.getRequestInfoById((long) requestId.intValue()); - if (info == null) { - errInfo.append( - String.format(prop.getProperty("id_not_exists"), requestId, prop.getProperty("letter")) - + ";"); - } else if (info.getIsValid() == Constants.VALID_NO || info.getIsValid() == Constants.VALID_DEL) { - errInfo.append( - String.format(prop.getProperty("id_not_valid"), requestId, prop.getProperty("letter")) - + ";"); - } - } - } - // requestId check end - // classification check start - String classification = value.getClassify(); - if (StringUtils.isNotBlank(classification)) { - try { - for (String classify : classification.split(",")) { - if (StringUtils.isNotBlank(classify)) { - int c = Integer.parseInt(classify); - ServiceDictInfo dict = serviceDictInfoService.getDictById(c); - if (dict == null) { - errInfo.append(String.format(prop.getProperty("id_not_exists"), classify, - prop.getProperty("classification")) + ";"); - } else if (dict.getIsValid() == Constants.VALID_NO - || dict.getIsValid() == Constants.VALID_DEL) { - errInfo.append(String.format(prop.getProperty("id_not_valid"), classify, - prop.getProperty("classification")) + ";"); - } - } - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("num_split_by_comma"), prop.getProperty("classification")) - + ";"); - } - } - // classification check end - // attribute check start - String attributes = value.getAttribute(); - if (StringUtils.isNotBlank(attributes)) { - try { - for (String attribute : attributes.split(",")) { - if (StringUtils.isNotBlank(attribute)) { - int c = Integer.parseInt(attribute); - ServiceDictInfo dict = serviceDictInfoService.getDictById(c); - if (dict == null) { - errInfo.append(String.format(prop.getProperty("id_not_exists"), attribute, - prop.getProperty("attribute")) + ";"); - } else if (dict.getIsValid() == Constants.VALID_NO - || dict.getIsValid() == Constants.VALID_DEL) { - errInfo.append(String.format(prop.getProperty("id_not_valid"), attribute, - prop.getProperty("attribute")) + ";"); - } - } - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("num_split_by_comma"), prop.getProperty("attribute")) + ";"); - } - } - // attribute check end - // attribute check start - String labels = value.getLable(); - if (StringUtils.isNotBlank(labels)) { - try { - for (String label : labels.split(",")) { - if (StringUtils.isNotBlank(label)) { - int c = Integer.parseInt(label); - ServiceDictInfo dict = serviceDictInfoService.getDictById(c); - if (dict == null) { - errInfo.append(String.format(prop.getProperty("id_not_exists"), label, - prop.getProperty("label")) + ";"); - } else if (dict.getIsValid() == Constants.VALID_NO - || dict.getIsValid() == Constants.VALID_DEL) { - errInfo.append(String.format(prop.getProperty("id_not_valid"), label, - prop.getProperty("label")) + ";"); - } - } - } - } catch (Exception e) { - // TODO: handle exception - errInfo.append( - String.format(prop.getProperty("num_split_by_comma"), prop.getProperty("label")) + ";"); - } - } - // group and ir_type check start - if ("ipmulitiplex".equals(specialItem)) { - Integer irType = value.getIrType(); - if (irType == null) { - errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("ir_type")) + ";"); - } else { - boolean has = false; - for (SysDataDictionaryItem irTypeItem : irTypeList) { - if (Integer.parseInt(irTypeItem.getItemCode()) == irType.intValue()) { - has = true; - break; - } - } - if (!has) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("ir_type")) + ";"); - } - } - Integer dnsStrategyId = value.getDnsStrategyId(); - if (dnsStrategyId == null) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("group_name")) + ";"); - } else { - PolicyGroupInfo group = policyGroupInfoService.getById(dnsStrategyId.intValue()); - if (group == null) { - errInfo.append(String.format(prop.getProperty("id_not_exists"), dnsStrategyId, - prop.getProperty("group_name")) + ";"); - } else if (group.getIsValid() == Constants.VALID_DEL || group.getIsValid() == Constants.VALID_NO) { - errInfo.append(String.format(prop.getProperty("id_not_valid"), dnsStrategyId, - prop.getProperty("group_name")) + ";"); - } - } - } - if ("ipratelimit".equals(specialItem)) { - String ratelimit = value.getRatelimit(); - if (ratelimit == null) { - errInfo.append( - String.format(prop.getProperty("can_not_null"), prop.getProperty("ratelimit")) + ";"); - } else { - try { - if (Double.parseDouble(ratelimit) < 0 || Double.parseDouble(ratelimit) > 1) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("ratelimit_limit")) - + ";"); - } - } catch (Exception e) { - errInfo.append( - String.format(prop.getProperty("is_incorrect"), prop.getProperty("ratelimit_limit")) - + ";"); - } - - } - } - // attribute check end - if (StringUtils.isNotBlank(errInfo.toString())) { - msg.append(lineStart).append(errInfo); - if (line < list.size()) { - msg.append("
"); - } - } - // group and ir_type check end - line++; - } - if (StringUtils.isNotBlank(msg.toString())) { - throw new MaatConvertException(prop.getProperty("save_failed") + "
" + msg.toString()); - } - } - - /** - * - * checkIP(IP格式验证) (这里描述这个方法适用条件 – 可选) - * - * @param ipName - * @param ip - * @param ipType - * @param ipPattern - * @return String - * @exception @since - * 1.0.0 - */ - @Deprecated - public static String checkIP(Properties prop, String ipName, String ip, Integer ipType, Integer ipPattern) { - StringBuffer msg = new StringBuffer(); - if (StringUtils.isBlank(ip)) { - msg.append(String.format(prop.getProperty("can_not_null"), ipName) + ";"); - } else { - if (4 == ipType.intValue()) { - if (1 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } else if (2 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } else if (3 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV4_IP_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } - } else if (6 == ipType.intValue()) { - if (1 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } else if (2 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } else if (3 == ipPattern.intValue()) { - Pattern pattern = Pattern.compile(Constants.IPV6_IP_REGEXP); - Matcher matcher = pattern.matcher(ip); - if (!matcher.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), ipName) + ";"); - } - } - } - } - return msg.toString(); - } - @Deprecated - public static String checkIp(Properties prop, String srcIpName, String srcIp, String destIpName, String destIp, - Integer ipType, Integer ipPattern) { - StringBuffer msg = new StringBuffer(); - if (46 == ipType.intValue()) { - if (1 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); - Matcher matcherV4 = patternV4.matcher(srcIp); - Matcher matcherV6 = patternV6.matcher(destIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } else if (2 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); - Matcher matcherV4 = patternV4.matcher(srcIp); - Matcher matcherV6 = patternV6.matcher(destIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } else if (3 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_REGEXP); - Matcher matcherV4 = patternV4.matcher(srcIp); - Matcher matcherV6 = patternV6.matcher(destIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } - } else if (64 == ipType.intValue()) { - if (1 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); - Matcher matcherV4 = patternV4.matcher(destIp); - Matcher matcherV6 = patternV6.matcher(srcIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - } else if (2 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); - Matcher matcherV4 = patternV4.matcher(destIp); - Matcher matcherV6 = patternV6.matcher(srcIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - } else if (3 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_REGEXP); - Matcher matcherV4 = patternV4.matcher(destIp); - Matcher matcherV6 = patternV6.matcher(srcIp); - if (!matcherV4.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - if (!matcherV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - } - } else if (10 == ipType.intValue()) { - if (1 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_SUBNET_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_SUBNET_REGEXP); - Matcher matcherSrcIpV4 = patternV4.matcher(srcIp); - Matcher matcherSrcIpV6 = patternV6.matcher(srcIp); - Matcher matcherDestIpV4 = patternV4.matcher(destIp); - Matcher matcherDestIpV6 = patternV6.matcher(destIp); - if (!matcherSrcIpV4.matches() && !matcherSrcIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherDestIpV4.matches() && !matcherDestIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } else if (2 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_RANGE_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_RANGE_REGEXP); - Matcher matcherSrcIpV4 = patternV4.matcher(srcIp); - Matcher matcherSrcIpV6 = patternV6.matcher(srcIp); - Matcher matcherDestIpV4 = patternV4.matcher(destIp); - Matcher matcherDestIpV6 = patternV6.matcher(destIp); - if (!matcherSrcIpV4.matches() && !matcherSrcIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherDestIpV4.matches() && !matcherDestIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } else if (3 == ipPattern.intValue()) { - Pattern patternV4 = Pattern.compile(Constants.IPV4_IP_REGEXP); - Pattern patternV6 = Pattern.compile(Constants.IPV6_IP_REGEXP); - Matcher matcherSrcIpV4 = patternV4.matcher(srcIp); - Matcher matcherSrcIpV6 = patternV6.matcher(srcIp); - Matcher matcherDestIpV4 = patternV4.matcher(destIp); - Matcher matcherDestIpV6 = patternV6.matcher(destIp); - if (!matcherSrcIpV4.matches() && !matcherSrcIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), srcIpName) + ";"); - } - if (!matcherDestIpV4.matches() && !matcherDestIpV6.matches()) {// 完全匹配 - msg.append(String.format(prop.getProperty("is_in_wrong_format"), destIpName) + ";"); - } - } - } - return msg.toString(); - } - - @Deprecated - public static String checkPort(Properties prop, String portName, String port, Integer portPattern) { - StringBuffer msg = new StringBuffer(); - if (StringUtils.isBlank(port)) { - msg.append(String.format(prop.getProperty("can_not_null"), portName) + ";"); - } else { - if (1 == portPattern.intValue()) { - try { - Integer portInt = Integer.parseInt(port); - if (portInt < 0 || portInt > 65535) { - msg.append( - portName + " " + String.format(prop.getProperty("must_between"), "0", "65535") + ";"); - } - } catch (Exception e) { - // TODO: handle exception - msg.append(String.format(prop.getProperty("not_number"), portName) + ";"); - } - } else if (2 == portPattern.intValue()) { - if (port.indexOf("/") == -1) { - msg.append(String.format(prop.getProperty("is_in_wrong_format"), portName) + ";"); - } else { - int index = 0; - String[] portArray = port.split("/"); - for (String portPart : portArray) { - try { - Integer portInt = Integer.parseInt(portPart); - if (portInt < 0 || portInt > 65535) { - if (index == 0) { - - msg.append( - String.format(prop.getProperty("must_between"), portName + " port") + ";"); - } else { - msg.append( - String.format(prop.getProperty("must_between"), portName + " mask") + ";"); - } - } - } catch (Exception e) { - // TODO: handle exception - if (index == 0) { - msg.append(String.format(prop.getProperty("not_number"), portName + " port") + ";"); - } else { - msg.append(String.format(prop.getProperty("not_number"), portName + " mask") + ";"); - } - } - index++; - } - } - } - } - return msg.toString(); - } - /** * * setReportSearchTime(报表查询设置开始时间与结束时间) (这里描述这个方法适用条件 – 可选) @@ -3234,14 +1134,22 @@ public class BaseController { */ public void _import(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes, MultipartFile[] files, Integer serviceDictId, String regionDictIds, Integer requestId) { + logger.warn("import start..."); + long start=System.currentTimeMillis(); + ImportBigExcel ei=null; try { FunctionServiceDict serviceDict = DictUtils.getFunctionServiceDict(serviceDictId); - List ipPortCfgs = new ArrayList(); - List> stringCfgs = new ArrayList>(); - List complexkeywordCfgs = new ArrayList(); + BlockingQueue ipPortCfgs =null; + BlockingQueue> stringCfgs =null; + BlockingQueue complexkeywordCfgs =null; + BlockingQueue dnsResStrategies=null; List cfgIndexInfos = new ArrayList(); List appPolicyCfgs = new ArrayList(); - List dnsResStrategies=Lists.newArrayList(); + List> asnNoMaps=Lists.newArrayList(); + Map newAsnNoMap=Maps.newConcurrentMap(); + Map OldAsnNoMap=Maps.newConcurrentMap(); + asnNoMaps.add(newAsnNoMap); + asnNoMaps.add(OldAsnNoMap); FunctionRegionDict appRegion = null; if(serviceDict!=null) { String regionCode = serviceDict.getRegionCode(); @@ -3260,76 +1168,98 @@ public class BaseController { } for (int i = 0; i < files.length; i++) { MultipartFile file = files[i]; - ImportExcel ei = new ImportExcel(file, 0, 0); - + ei = new ImportBigExcel(file, 0, 1); FunctionRegionDict regionDict = DictUtils .getFunctionRegionDict(Integer.parseInt(regionDictIds.split(",")[i])); + //加载模板 + loadTemplate(ei,regionDict, serviceDict); if (regionDict.getRegionType().equals(1)) {// IP if (regionDict.getFunctionId().equals(5)) { if (serviceDict!=null&&serviceDict.getAction().equals(64)) { - List list = ei.getDataList(IpRateLimitTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(IpRateLimitTemplate.class + ); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); } else { - List list = ei.getDataList(IpAllTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(IpAllTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); } } else if (regionDict.getFunctionId().equals(7)&&serviceDict!=null&&serviceDict.getAction().intValue()==16) { - List list = ei.getDataList(DnsIpTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(DnsIpTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); }else if(regionDict.getFunctionId().equals(401)) { - List list = ei.getDataList(DnsIpTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(DnsIpTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); }else if (regionDict.getFunctionId().equals(212)) { - List list = ei.getDataList(IpPayloadTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(IpPayloadTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); } else if (regionDict.getFunctionId().equals(510) && "p2p_ip".equals(regionDict.getConfigServiceType())) { // P2p IP - List list = ei.getDataList(P2pIpTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); - } else if (regionDict.getFunctionId().equals(600)) {// ANS IP - List list = ei.getDataList(AsnIpTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); - } else if (regionDict.getFunctionId().equals(301)) {// ANS IP - List list = ei.getDataList(DdosIpTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(P2pIpTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); + } else if (regionDict.getFunctionId().equals(600)) {// ASN IP + //加载asn缓存 + AsnCacheUtils.init(); + BlockingQueue list = ei.getDataList(AsnIpTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,asnNoMaps, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, asnNoMaps, list); + } else if (regionDict.getFunctionId().equals(301)) {// DDOS IP + BlockingQueue list = ei.getDataList(DdosIpTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); } else { - List list = ei.getDataList(IpAllTemplate.class, this.getMsgProp(),regionDict,serviceDict); - ipPortCfgs = this.checkIpCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(IpAllTemplate.class ); +// ipPortCfgs = this.checkIpCfg(serviceDict, regionDict,null, list); + ipPortCfgs=this.checkIpCfgMulity(serviceDict, regionDict, null, list); } } else if (regionDict.getRegionType().equals(2)) {// 字符串类 if (regionDict.getFunctionId().equals(510) && "p2p_hash".equals(regionDict.getConfigServiceType())) { // P2p hash - List list = ei.getDataList(P2pHashStringTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(P2pHashStringTemplate.class + ); +// stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + stringCfgs=this.checkStringCfgMulity(serviceDict, regionDict, list); }else if (regionDict.getFunctionId().equals(403)) { - List list = ei.getDataList(AppDomainTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(AppDomainTemplate.class + ); +// stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + stringCfgs=this.checkStringCfgMulity(serviceDict, regionDict, list); } else { - List list = ei.getDataList(StringAllTemplate.class, this.getMsgProp(),regionDict,serviceDict); - stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(StringAllTemplate.class ); +// stringCfgs = this.checkStringCfg(serviceDict, regionDict, list); + stringCfgs=this.checkStringCfgMulity(serviceDict, regionDict, list); } } else if (regionDict.getRegionType().equals(3)) {// 增强字符串类 if (regionDict.getFunctionId().equals(7)&&serviceDict!=null&&serviceDict.getAction().intValue()==16) { - List list = ei.getDataList(DnsComplexStringTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - complexkeywordCfgs = this.checkComplexStringCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(DnsComplexStringTemplate.class + ); +// complexkeywordCfgs = this.checkComplexStringCfg(serviceDict, regionDict, list); + complexkeywordCfgs = this.checkComplexStringCfgMulity(serviceDict, regionDict, list); } else { - List list = ei.getDataList(ComplexStringAllTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - complexkeywordCfgs = this.checkComplexStringCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(ComplexStringAllTemplate.class + ); +// complexkeywordCfgs = this.checkComplexStringCfg(serviceDict, regionDict, list); + complexkeywordCfgs = this.checkComplexStringCfgMulity(serviceDict, regionDict, list); } }else if (regionDict.getRegionType().equals(6)) {// 回调类 if (regionDict.getFunctionId().equals(400)) { - List list = ei.getDataList(DnsResStrategyTemplate.class, - this.getMsgProp(),regionDict,serviceDict); - dnsResStrategies = this.checkDnsResStrategyCfg(serviceDict, regionDict, list); + BlockingQueue list = ei.getDataList(DnsResStrategyTemplate.class + ); +// dnsResStrategies = this.checkDnsResStrategyCfg(serviceDict, regionDict, list); + dnsResStrategies=this.checkDnsResStrategyCfgMulity(serviceDict, regionDict, list); } } + //删除文件 + if(ei.getUploadFile()!=null&&ei.getUploadFile().exists()) { + ei.getUploadFile().delete(); + } Date date = new Date(); String specServiceId = request.getParameter("appId"); String behaviorId = request.getParameter("behaviorId"); @@ -3351,342 +1281,213 @@ public class BaseController { } if (regionDict.getRegionType().equals(1)) {// IP if(regionDict.getFunctionId().intValue()==600) {//ans ip - Map> asnIpCfgs=new HashMap<>(); - int total=ipPortCfgs.size(); - for (BaseIpCfg cfg : ipPortCfgs) { - AsnIpCfg _cfg=new AsnIpCfg(); - BeanUtils.copyProperties(cfg, _cfg); - _cfg.setTableName(AsnIpCfg.getTablename()); - _cfg.setAction(0); - _cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); -// _cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); - _cfg.setCfgType(regionDict.getConfigRegionValue()); - _cfg.setCreateTime(date); - _cfg.setCreatorId(UserUtils.getUser().getId()); - _cfg.setDoLog(1); - _cfg.setFunctionId(regionDict.getFunctionId()); - _cfg.setIsAudit(0); - _cfg.setIsValid(0); - _cfg.setIsAreaEffective(0); - _cfg.setAttribute("0"); - _cfg.setClassify("0"); - _cfg.setLable("0"); - _cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); - _cfg.setServiceId(0); - if(asnIpCfgs.containsKey(_cfg.getUserRegion1())) { - asnIpCfgs.get(_cfg.getUserRegion1()).add(_cfg); - }else { - List ips=Lists.newArrayList(); - ips.add(_cfg); - asnIpCfgs.put(_cfg.getUserRegion1(), ips); - } - } - ipPortCfgs.clear(); - asnIpCfgService.saveAsnIpBatch(asnIpCfgs,total); - }else { - List compileIds=Lists.newArrayList(); - List regionIds=Lists.newArrayList(); - if(!regionDict.getFunctionId().equals(405)) {//app ip compileId 从config_group_info中取 + if(asnNoMaps.get(0).size()>0) { try { - compileIds = ConfigServiceUtil.getId(1,ipPortCfgs.size()); - } catch (Exception e) { - e.printStackTrace(); - logger.info("获取编译ID出错"); - throw new MaatConvertException(":"+e.getMessage()); - } - }else { - try { - regionIds = ConfigServiceUtil.getId(3,ipPortCfgs.size()); + //将未在缓存中找到对应config的asn no分配group ID + List groupIds = ConfigServiceUtil.getId(2,asnNoMaps.get(0).size()); + int ind=0; + for(Long key:asnNoMaps.get(0).keySet()) { + asnNoMaps.get(0).put(key, groupIds.get(ind)); + ind++; + } } catch (Exception e) { e.printStackTrace(); logger.info("获取编译ID出错"); throw new MaatConvertException(":"+e.getMessage()); } } - int ind=0; - for (BaseIpCfg cfg : ipPortCfgs) { - cfg.setAction(serviceDict==null?null:serviceDict.getAction()); - cfg.setAuditorId(UserUtils.getUser().getId()); - cfg.setAuditTime(date); - cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); - cfg.setCfgType(regionDict.getConfigRegionValue()); - cfg.setCreateTime(date); - cfg.setCreatorId(UserUtils.getUser().getId()); - cfg.setDoLog(1); - cfg.setFunctionId(regionDict.getFunctionId()); - cfg.setIsAudit(0); - cfg.setIsValid(0); - cfg.setIsAreaEffective(0); - cfg.setAttribute("0"); - cfg.setClassify("0"); - cfg.setLable("0"); - cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); - cfg.setServiceId(serviceDict==null?null:serviceDict.getServiceId()); - cfg.setTableName("ip_port_cfg"); - if(compileIds.size()==ipPortCfgs.size()) { - cfg.setCompileId(compileIds.get(ind)); + + List isImportAll=DictUtils.getDictList("IS_ASN_IP_IMPORT_ALL"); + //处理组,新的组会在这里保存 + asnIpCfgService.processGroup(asnNoMaps.get(0)); + //全量下发,删除asnNo对应的已有的IP + if(isImportAll.get(0).getItemCode().equals("1")) { + asnIpCfgService.deleteIps(asnNoMaps.get(1)); + } + Map fullMap=Maps.newConcurrentMap(); + for(Long asnNo:asnNoMaps.get(0).keySet()) { + fullMap.put(asnNo, false); + } + for(Long asnNo:asnNoMaps.get(1).keySet()) { + ConfigGroupInfo info=AsnCacheUtils.get(asnNo); + if(info==null) { + info=asnIpCfgService.getConfigGroupInfoByAsnNo(asnNo); } - if(regionDict.getFunctionId().equals(405)&®ionIds.size()==ipPortCfgs.size()) { - cfg.setUserRegion1(regionIds.get(ind).toString()); + fullMap.put(asnNo, info.getIsIssued().intValue()==1); + } + List asnIpCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + List _ipPortCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + while(!ipPortCfgs.isEmpty()) { + ipPortCfgs.drainTo(_ipPortCfgs, Constants.MAAT_JSON_SEND_SIZE); + List regionIds=Lists.newArrayList(); + try { + regionIds = ConfigServiceUtil.getId(3,_ipPortCfgs.size()); + } catch (Exception e) { + e.printStackTrace(); + logger.info("获取域ID出错"); + throw new MaatConvertException(":"+e.getMessage()); } - if(specificServiceCfg!=null) { - cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - if(StringUtils.isNotBlank(behaviorId)) - cfg.setBehavCode(Integer.parseInt(behaviorId)); - } - if (serviceDict!=null&&(serviceDict.getServiceId().intValue() == 33 - || serviceDict.getServiceId().intValue() == 145 - || serviceDict.getServiceId().intValue() == 35 - || serviceDict.getServiceId().intValue() == 147 - || serviceDict.getServiceId().intValue() == 36 - || serviceDict.getServiceId().intValue() == 148)) { - if (appRegion != null) { - AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); - BeanUtils.copyProperties(cfg, appPolicyCfg); - //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - appPolicyCfg.setMatchMethod(0); - appPolicyCfg.setIsHexbin(0); - appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); - appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); - if (StringUtils.isNotBlank(behaviorId)) { - //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); - appPolicyCfg.setExprType(1); - } else { - appPolicyCfg.setExprType(0); + int ind=0; + for (BaseIpCfg cfg : _ipPortCfgs) { + AsnIpCfg _cfg=new AsnIpCfg(); + BeanUtils.copyProperties(cfg, _cfg,new String[] {"cfgId"}); + _cfg.setTableName(AsnIpCfg.getTablename()); + _cfg.setAction(0); + _cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); + _cfg.setCfgType(regionDict.getConfigRegionValue()); + _cfg.setCreateTime(date); + _cfg.setCreatorId(UserUtils.getUser().getId()); + _cfg.setDoLog(1); + _cfg.setFunctionId(regionDict.getFunctionId()); + _cfg.setIsAudit(0); + if(fullMap.get(Long.parseLong(_cfg.getUserRegion1()))) { + _cfg.setIsValid(Constants.VALID_YES); + }else { + _cfg.setIsValid(Constants.VALID_NO); + } + _cfg.setIsAreaEffective(0); + _cfg.setAttribute("0"); + _cfg.setClassify("0"); + _cfg.setLable("0"); + _cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); + _cfg.setServiceId(0); + //设置region id + if(regionIds!=null&®ionIds.size()==_ipPortCfgs.size()) { + _cfg.setRegionId(regionIds.get(ind)); + } + //设置group id + if(asnNoMaps.get(0).containsKey(Long.parseLong(_cfg.getUserRegion1()))) { + _cfg.setAsnIpGroup(asnNoMaps.get(0).get(Long.parseLong(_cfg.getUserRegion1()))); + }else if(asnNoMaps.get(1).containsKey(Long.parseLong(_cfg.getUserRegion1()))) { + _cfg.setAsnIpGroup(asnNoMaps.get(1).get(Long.parseLong(_cfg.getUserRegion1()))); + }else { +// ConfigGroupInfo info=asnIpCfgService.getConfigGroupInfoByAsnNo(Long.parseLong(_cfg.getUserRegion1())); + ConfigGroupInfo info=AsnCacheUtils.get(Long.parseLong(_cfg.getUserRegion1())); + if(info==null) { + info=asnIpCfgService.getConfigGroupInfoByAsnNo(Long.parseLong(_cfg.getUserRegion1())); } - appPolicyCfgs.add(appPolicyCfg); - } - } else { - if(regionDict.getFunctionId().intValue()!=600 - &®ionDict.getFunctionId().intValue()!=301 - &®ionDict.getFunctionId().intValue()!=401 - &®ionDict.getFunctionId().intValue()!=405) { - CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); - BeanUtils.copyProperties(cfg, cfgIndexInfo); - cfgIndexInfos.add(cfgIndexInfo); + _cfg.setAsnIpGroup(info.getGroupId()); } + asnIpCfgs.add(_cfg); + ind++; } - ind++; + this.splitAndSave(regionDict, serviceDict, specificServiceCfg, asnIpCfgs, cfgIndexInfos, appPolicyCfgs); + cfgIndexInfos.clear(); + appPolicyCfgs.clear(); + _ipPortCfgs.clear(); + asnIpCfgs.clear(); } - if(ipPortCfgs!=null&&ipPortCfgs.size()>0) { - if (regionDict.getFunctionId().equals(301)) { - ddosCfgService.saveDdosIpCfg(ipPortCfgs); - }else if (regionDict.getFunctionId().equals(401)) { - dnsIpCfgService.saveDnsIpCfg(ipPortCfgs); - }else if (specificServiceCfg!=null&®ionDict.getFunctionId().equals(405)) { - appCfgService.saveAppIpCfg(ipPortCfgs,specificServiceCfg); + }else { + List _ipPortCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + while(!ipPortCfgs.isEmpty()) { + ipPortCfgs.drainTo(_ipPortCfgs, Constants.MAAT_JSON_SEND_SIZE); + List compileIds=Lists.newArrayList(); + List regionIds=Lists.newArrayList(); + if(!regionDict.getFunctionId().equals(405)) {//app ip compileId 从config_group_info中取 + try { + compileIds = ConfigServiceUtil.getId(1,_ipPortCfgs.size()); + } catch (Exception e) { + e.printStackTrace(); + logger.info("获取编译ID出错"); + throw new MaatConvertException(":"+e.getMessage()); + } }else { - ipCfgService.saveBatch(ipPortCfgs, IpCfgDao.class); + try { + regionIds = ConfigServiceUtil.getId(3,_ipPortCfgs.size()); + } catch (Exception e) { + e.printStackTrace(); + logger.info("获取域ID出错"); + throw new MaatConvertException(":"+e.getMessage()); + } } + int ind=0; + for (BaseIpCfg cfg : _ipPortCfgs) { + cfg.setAction(serviceDict==null?null:serviceDict.getAction()); + cfg.setAuditorId(UserUtils.getUser().getId()); + cfg.setAuditTime(date); + cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); + cfg.setCfgType(regionDict.getConfigRegionValue()); + cfg.setCreateTime(date); + cfg.setCreatorId(UserUtils.getUser().getId()); + cfg.setDoLog(1); + cfg.setFunctionId(regionDict.getFunctionId()); + cfg.setIsAudit(0); + cfg.setIsValid(0); + cfg.setIsAreaEffective(0); + cfg.setAttribute("0"); + cfg.setClassify("0"); + cfg.setLable("0"); + cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); + cfg.setServiceId(serviceDict==null?null:serviceDict.getServiceId()); + cfg.setTableName("ip_port_cfg"); + if(compileIds.size()==_ipPortCfgs.size()) { + cfg.setCompileId(compileIds.get(ind)); + } + if(regionDict.getFunctionId().equals(405)&®ionIds.size()==_ipPortCfgs.size()) { + cfg.setUserRegion1(regionIds.get(ind).toString()); + } + if(specificServiceCfg!=null) { + cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + if(StringUtils.isNotBlank(behaviorId)) + cfg.setBehavCode(Integer.parseInt(behaviorId)); + } + if (serviceDict!=null&&(serviceDict.getServiceId().intValue() == 33 + || serviceDict.getServiceId().intValue() == 145 + || serviceDict.getServiceId().intValue() == 35 + || serviceDict.getServiceId().intValue() == 147 + || serviceDict.getServiceId().intValue() == 36 + || serviceDict.getServiceId().intValue() == 148)) { + if (appRegion != null) { + AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); + BeanUtils.copyProperties(cfg, appPolicyCfg,new String[] {"cfgId"}); + //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + appPolicyCfg.setMatchMethod(0); + appPolicyCfg.setIsHexbin(0); + appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); + appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); + if (StringUtils.isNotBlank(behaviorId)) { + //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); + appPolicyCfg.setExprType(1); + } else { + appPolicyCfg.setExprType(0); + } + appPolicyCfgs.add(appPolicyCfg); + } + } else { + if(regionDict.getFunctionId().intValue()!=600 + &®ionDict.getFunctionId().intValue()!=301 + &®ionDict.getFunctionId().intValue()!=401 + &®ionDict.getFunctionId().intValue()!=405) { + CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); + BeanUtils.copyProperties(cfg, cfgIndexInfo,new String[] {"cfgId"}); + cfgIndexInfos.add(cfgIndexInfo); + } + } + ind++; + } + this.splitAndSave(regionDict, serviceDict, specificServiceCfg, _ipPortCfgs, cfgIndexInfos, appPolicyCfgs); + cfgIndexInfos.clear(); + appPolicyCfgs.clear(); + _ipPortCfgs.clear(); } } } else if (regionDict.getRegionType().equals(2)) { - List compileIds=Lists.newArrayList(); - try { - compileIds = ConfigServiceUtil.getId(1,stringCfgs.size()); - } catch (Exception e) { - e.printStackTrace(); - logger.info("获取编译ID出错"); - throw new MaatConvertException(":"+e.getMessage()); - } - int ind=0; - for (BaseStringCfg cfg : stringCfgs) { - cfg.setAction(serviceDict.getAction()); - cfg.setAuditorId(UserUtils.getUser().getId()); - cfg.setAuditTime(date); - cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); - cfg.setCfgType(regionDict.getConfigRegionValue()); - cfg.setCreateTime(date); - cfg.setCreatorId(UserUtils.getUser().getId()); - cfg.setDoLog(1); - cfg.setFunctionId(regionDict.getFunctionId()); - cfg.setIsAudit(0); - cfg.setIsValid(0); - cfg.setIsAreaEffective(0); - cfg.setAttribute("0"); - cfg.setClassify("0"); - cfg.setLable("0"); - cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); - cfg.setServiceId(serviceDict.getServiceId()); - if(compileIds!=null&&compileIds.size()==stringCfgs.size()) { - cfg.setCompileId(compileIds.get(i)); - } - if(specificServiceCfg!=null) { - cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - if(StringUtils.isNotBlank(behaviorId)) - cfg.setBehavCode(Integer.parseInt(behaviorId)); - } - if (serviceDict.getServiceId().intValue() == 33 - || serviceDict.getServiceId().intValue() == 145 - || serviceDict.getServiceId().intValue() == 35 - || serviceDict.getServiceId().intValue() == 147 - || serviceDict.getServiceId().intValue() == 36 - || serviceDict.getServiceId().intValue() == 148) { - if (appRegion != null) { - AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); - BeanUtils.copyProperties(cfg, appPolicyCfg); - //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - appPolicyCfg.setMatchMethod(0); - appPolicyCfg.setIsHexbin(0); - appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); - appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); - if (StringUtils.isNotBlank(behaviorId)) { - //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); - appPolicyCfg.setExprType(1); - } else { - appPolicyCfg.setExprType(0); - } - appPolicyCfgs.add(appPolicyCfg); - } - } else { - if(!regionDict.getFunctionId().equals(403)) { - CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); - BeanUtils.copyProperties(cfg, cfgIndexInfo, new String[] {"cfgId"}); - cfgIndexInfos.add(cfgIndexInfo); - } - - } - ind++; - } - // 调用对应配置的service - if (regionDict.getDictId().intValue() == 167) { - interceptCfgService.saveInterceptCfg(stringCfgs); - } - if (regionDict.getDictId().intValue() == 9||regionDict.getDictId().intValue() == 15 || regionDict.getDictId().intValue() == 596) { - websiteCfgService.saveHttpUrlCfg(stringCfgs); - } - if (regionDict.getDictId().intValue() == 36||regionDict.getDictId().intValue() == 37) { - fileTransferCfgService.saveFtpCfg(stringCfgs); - } - if (regionDict.getFunctionId().equals(510)) { // P2p hash - /* - * if("p2p_hash".equals(regionDict.getConfigServiceType())) { - * fileTransferCfgService.saveP2pHashCfg(stringCfgs); }else { - */ - fileTransferCfgService.saveP2pCfg(stringCfgs); - /* } */ - - } - if (regionDict.getFunctionId().equals(61)) { - bgpCfgService.saveBgpAsCfg(stringCfgs); - } - if (regionDict.getFunctionId().intValue() == 34) { - websiteCfgService.saveSslCfg(stringCfgs); - } - if(regionDict.getDictId().intValue()==12||regionDict.getDictId().intValue()==13||regionDict.getDictId().intValue()==601||regionDict.getDictId().intValue()==602) { - websiteCfgService.saveHttpBodyCfg(stringCfgs); - } - if(regionDict.getDictId().intValue()==25){ - avContentCfgService.saveContUrlCfg(stringCfgs); - } - if(regionDict.getDictId().intValue()==16){ - avContentCfgService.saveVoIpAccountCfg(stringCfgs); - } - if(regionDict.getDictId().intValue()==64){ - appCfgService.saveAppDomainCfg(stringCfgs); - } - } else if (regionDict.getRegionType().equals(3)) { - List compileIds=Lists.newArrayList(); - try { - compileIds = ConfigServiceUtil.getId(1,complexkeywordCfgs.size()); - } catch (Exception e) { - e.printStackTrace(); - logger.info("获取编译ID出错"); - throw new MaatConvertException(":"+e.getMessage()); - } - int ind=0; - for (ComplexkeywordCfg cfg : complexkeywordCfgs) { - cfg.setAction(serviceDict.getAction()); - cfg.setAuditorId(UserUtils.getUser().getId()); - cfg.setAuditTime(date); - cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); - cfg.setCfgType(regionDict.getConfigRegionValue()); - cfg.setCreateTime(date); - cfg.setCreatorId(UserUtils.getUser().getId()); - cfg.setDoLog(1); - cfg.setFunctionId(regionDict.getFunctionId()); - cfg.setIsAudit(0); - cfg.setIsValid(0); - cfg.setIsAreaEffective(0); - cfg.setAttribute("0"); - cfg.setClassify("0"); - cfg.setLable("0"); - cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); - cfg.setServiceId(serviceDict.getServiceId()); - if(compileIds!=null&&compileIds.size()==complexkeywordCfgs.size()) { - cfg.setCompileId(compileIds.get(ind)); - } - if(specificServiceCfg!=null) { - cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - if(StringUtils.isNotBlank(behaviorId)) - cfg.setBehavCode(Integer.parseInt(behaviorId)); - } - if (serviceDict.getServiceId().intValue() == 33 - || serviceDict.getServiceId().intValue() == 145 - || serviceDict.getServiceId().intValue() == 35 - || serviceDict.getServiceId().intValue() == 147 - || serviceDict.getServiceId().intValue() == 36 - || serviceDict.getServiceId().intValue() == 148) { - if (appRegion != null) { - AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); - BeanUtils.copyProperties(cfg, appPolicyCfg); - //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); - //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); - appPolicyCfg.setMatchMethod(0); - appPolicyCfg.setIsHexbin(0); - appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); - appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); - if (StringUtils.isNotBlank(behaviorId)) { - //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); - appPolicyCfg.setExprType(1); - } else { - appPolicyCfg.setExprType(0); - } - appPolicyCfgs.add(appPolicyCfg); - } - } else { - if(!regionDict.getFunctionId().equals(402)) { - CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); - BeanUtils.copyProperties(cfg, cfgIndexInfo); - cfgIndexInfos.add(cfgIndexInfo); - } - - } - ind++; - } - if (regionDict.getDictId().intValue() == 28) { - websiteCfgService.saveDnsCfg(complexkeywordCfgs); - } - if (regionDict.getDictId().intValue() == 30 || regionDict.getDictId().intValue() == 31||regionDict.getDictId().intValue() == 599) { - mailCfgService.saveMailCfg(complexkeywordCfgs); - } - if(regionDict.getDictId().intValue()==10) { - websiteCfgService.saveHttpReqHeadCfg(complexkeywordCfgs); - } - if(regionDict.getDictId().intValue()==11) { - websiteCfgService.saveHttpResHeadCfg(complexkeywordCfgs); - } - if(regionDict.getDictId().intValue()==63) { - appCfgService.saveAppHttpCfg(complexkeywordCfgs); - } - }else if(regionDict.getRegionType().equals(6)) { - if(regionDict.getFunctionId().equals(400)) { + List _stringCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + while(!stringCfgs.isEmpty()) { + stringCfgs.drainTo(_stringCfgs, Constants.MAAT_JSON_SEND_SIZE); List compileIds=Lists.newArrayList(); try { - compileIds = ConfigServiceUtil.getId(1,dnsResStrategies.size()); + compileIds = ConfigServiceUtil.getId(1,_stringCfgs.size()); } catch (Exception e) { e.printStackTrace(); logger.info("获取编译ID出错"); throw new MaatConvertException(":"+e.getMessage()); } int ind=0; - for(DnsResStrategy cfg:dnsResStrategies) { + for (BaseStringCfg cfg : _stringCfgs) { cfg.setAction(serviceDict.getAction()); cfg.setAuditorId(UserUtils.getUser().getId()); cfg.setAuditTime(date); @@ -3704,28 +1505,350 @@ public class BaseController { cfg.setLable("0"); cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); cfg.setServiceId(serviceDict.getServiceId()); - cfg.setCompileId(compileIds.get(ind)); + if(compileIds!=null&&compileIds.size()==_stringCfgs.size()) { + cfg.setCompileId(compileIds.get(ind)); + } + if(specificServiceCfg!=null) { + cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + if(StringUtils.isNotBlank(behaviorId)) + cfg.setBehavCode(Integer.parseInt(behaviorId)); + } + if (serviceDict.getServiceId().intValue() == 33 + || serviceDict.getServiceId().intValue() == 145 + || serviceDict.getServiceId().intValue() == 35 + || serviceDict.getServiceId().intValue() == 147 + || serviceDict.getServiceId().intValue() == 36 + || serviceDict.getServiceId().intValue() == 148) { + if (appRegion != null) { + AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); + BeanUtils.copyProperties(cfg, appPolicyCfg,new String[] {"cfgId"}); + //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + appPolicyCfg.setMatchMethod(0); + appPolicyCfg.setIsHexbin(0); + appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); + appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); + if (StringUtils.isNotBlank(behaviorId)) { + //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); + appPolicyCfg.setExprType(1); + } else { + appPolicyCfg.setExprType(0); + } + appPolicyCfgs.add(appPolicyCfg); + } + } else { + if(!regionDict.getFunctionId().equals(403)) { + CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); + BeanUtils.copyProperties(cfg, cfgIndexInfo, new String[] {"cfgId"}); + cfgIndexInfos.add(cfgIndexInfo); + } + + } ind++; } - if(dnsResStrategies.size()>0) { - dnsResStrategyService.saveDnsResStrategies(dnsResStrategies); + this.splitAndSave(regionDict, serviceDict, specificServiceCfg, _stringCfgs, cfgIndexInfos, appPolicyCfgs); + cfgIndexInfos.clear(); + appPolicyCfgs.clear(); + _stringCfgs.clear(); + } + } else if (regionDict.getRegionType().equals(3)) { + List _complexkeywordCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + while(!complexkeywordCfgs.isEmpty()) { + complexkeywordCfgs.drainTo(_complexkeywordCfgs, Constants.MAAT_JSON_SEND_SIZE); + List compileIds=Lists.newArrayList(); + try { + compileIds = ConfigServiceUtil.getId(1,_complexkeywordCfgs.size()); + } catch (Exception e) { + e.printStackTrace(); + logger.info("获取编译ID出错"); + throw new MaatConvertException(":"+e.getMessage()); + } + int ind=0; + for (ComplexkeywordCfg cfg : _complexkeywordCfgs) { + cfg.setAction(serviceDict.getAction()); + cfg.setAuditorId(UserUtils.getUser().getId()); + cfg.setAuditTime(date); + cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); + cfg.setCfgType(regionDict.getConfigRegionValue()); + cfg.setCreateTime(date); + cfg.setCreatorId(UserUtils.getUser().getId()); + cfg.setDoLog(1); + cfg.setFunctionId(regionDict.getFunctionId()); + cfg.setIsAudit(0); + cfg.setIsValid(0); + cfg.setIsAreaEffective(0); + cfg.setAttribute("0"); + cfg.setClassify("0"); + cfg.setLable("0"); + cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); + cfg.setServiceId(serviceDict.getServiceId()); + if(compileIds!=null&&compileIds.size()==_complexkeywordCfgs.size()) { + cfg.setCompileId(compileIds.get(ind)); + } + if(specificServiceCfg!=null) { + cfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + cfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + if(StringUtils.isNotBlank(behaviorId)) + cfg.setBehavCode(Integer.parseInt(behaviorId)); + } + if (serviceDict.getServiceId().intValue() == 33 + || serviceDict.getServiceId().intValue() == 145 + || serviceDict.getServiceId().intValue() == 35 + || serviceDict.getServiceId().intValue() == 147 + || serviceDict.getServiceId().intValue() == 36 + || serviceDict.getServiceId().intValue() == 148) { + if (appRegion != null) { + AppPolicyCfg appPolicyCfg = new AppPolicyCfg(); + BeanUtils.copyProperties(cfg, appPolicyCfg,new String[] {"cfgId"}); + //appPolicyCfg.setSpecServiceId(specificServiceCfg.getSpecServiceId()); + //appPolicyCfg.setAppCode(specificServiceCfg.getSpecServiceCode()); + appPolicyCfg.setMatchMethod(0); + appPolicyCfg.setIsHexbin(0); + appPolicyCfg.setCfgType(appRegion.getConfigRegionValue()); + appPolicyCfg.setCfgRegionCode(appRegion.getConfigRegionCode()); + if (StringUtils.isNotBlank(behaviorId)) { + //appPolicyCfg.setBehavCode(Integer.parseInt(behaviorId)); + appPolicyCfg.setExprType(1); + } else { + appPolicyCfg.setExprType(0); + } + appPolicyCfgs.add(appPolicyCfg); + } + } else { + if(!regionDict.getFunctionId().equals(402)) { + CfgIndexInfo cfgIndexInfo = new CfgIndexInfo(); + BeanUtils.copyProperties(cfg, cfgIndexInfo,new String[] {"cfgId"}); + cfgIndexInfos.add(cfgIndexInfo); + } + + } + ind++; + } + this.splitAndSave(regionDict, serviceDict, specificServiceCfg, _complexkeywordCfgs, cfgIndexInfos, appPolicyCfgs); + cfgIndexInfos.clear(); + appPolicyCfgs.clear(); + _complexkeywordCfgs.clear(); + } + }else if(regionDict.getRegionType().equals(6)) { + if(regionDict.getFunctionId().equals(400)) { + List _dnsResStrategies=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE); + while(!dnsResStrategies.isEmpty()) { + dnsResStrategies.drainTo(_dnsResStrategies, Constants.MAAT_JSON_SEND_SIZE); + List compileIds=Lists.newArrayList(); + try { + compileIds = ConfigServiceUtil.getId(1,_dnsResStrategies.size()); + } catch (Exception e) { + e.printStackTrace(); + logger.info("获取编译ID出错"); + throw new MaatConvertException(":"+e.getMessage()); + } + int ind=0; + for(DnsResStrategy cfg:_dnsResStrategies) { + cfg.setAction(serviceDict.getAction()); + cfg.setAuditorId(UserUtils.getUser().getId()); + cfg.setAuditTime(date); + cfg.setCfgRegionCode(regionDict.getConfigRegionCode()); + cfg.setCfgType(regionDict.getConfigRegionValue()); + cfg.setCreateTime(date); + cfg.setCreatorId(UserUtils.getUser().getId()); + cfg.setDoLog(1); + cfg.setFunctionId(regionDict.getFunctionId()); + cfg.setIsAudit(0); + cfg.setIsValid(0); + cfg.setIsAreaEffective(0); + cfg.setAttribute("0"); + cfg.setClassify("0"); + cfg.setLable("0"); + cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId); + cfg.setServiceId(serviceDict.getServiceId()); + cfg.setCompileId(compileIds.get(ind)); + ind++; + } + this.splitAndSave(regionDict, serviceDict, specificServiceCfg, _dnsResStrategies, cfgIndexInfos, appPolicyCfgs); + _dnsResStrategies.clear(); + cfgIndexInfos.clear(); + appPolicyCfgs.clear(); } } } - if (cfgIndexInfos != null && cfgIndexInfos.size() > 0) { - ipCfgService.saveCfgIndexOf(cfgIndexInfos); - } - if (appPolicyCfgs != null && appPolicyCfgs.size() > 0) { - appCfgService.savePolicyList(appPolicyCfgs); - } - } + } catch (Exception e) { + if(ei!=null) { + if(ei.getUploadFile().exists()) { + ei.getUploadFile().delete(); + } + } addMessage(redirectAttributes,"error", e.getMessage()); e.printStackTrace(); } + long end=System.currentTimeMillis(); + logger.warn("import finish,cost:"+(end-start)); } + /** + * 加载模板 + * @param ei + * @param regionDict + * @param serviceDict + */ + public void loadTemplate(ImportBigExcel ei,FunctionRegionDict regionDict,FunctionServiceDict serviceDict) { + if (regionDict.getRegionType().equals(1)) {// IP + if (regionDict.getFunctionId().equals(5)) { + if (serviceDict!=null&&serviceDict.getAction().equals(64)) { + ei.loadInitParams(IpRateLimitTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else { + ei.loadInitParams(IpAllTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } + } else if (regionDict.getFunctionId().equals(7)&&serviceDict!=null&&serviceDict.getAction().intValue()==16) { + ei.loadInitParams(DnsIpTemplate.class, this.getMsgProp(), regionDict, serviceDict); + }else if(regionDict.getFunctionId().equals(401)) { + ei.loadInitParams(DnsIpTemplate.class, this.getMsgProp(), regionDict, serviceDict); + }else if (regionDict.getFunctionId().equals(212)) { + ei.loadInitParams(IpPayloadTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else if (regionDict.getFunctionId().equals(510) + && "p2p_ip".equals(regionDict.getConfigServiceType())) { // P2p IP + ei.loadInitParams(P2pIpTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else if (regionDict.getFunctionId().equals(600)) {// ASN IP + ei.loadInitParams(AsnIpTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else if (regionDict.getFunctionId().equals(301)) {// DDOS IP + ei.loadInitParams(DdosIpTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else { + ei.loadInitParams(IpAllTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } + } else if (regionDict.getRegionType().equals(2)) {// 字符串类 + if (regionDict.getFunctionId().equals(510) + && "p2p_hash".equals(regionDict.getConfigServiceType())) { // P2p hash + ei.loadInitParams(P2pHashStringTemplate.class, this.getMsgProp(), regionDict, serviceDict); + }else if (regionDict.getFunctionId().equals(403)) { + ei.loadInitParams(AppDomainTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else { + ei.loadInitParams(StringAllTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } + } else if (regionDict.getRegionType().equals(3)) {// 增强字符串类 + if (regionDict.getFunctionId().equals(7)&&serviceDict!=null&&serviceDict.getAction().intValue()==16) { + ei.loadInitParams(DnsComplexStringTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } else { + ei.loadInitParams(ComplexStringAllTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } + + }else if (regionDict.getRegionType().equals(6)) {// 回调类 + if (regionDict.getFunctionId().equals(400)) { + ei.loadInitParams(DnsResStrategyTemplate.class, this.getMsgProp(), regionDict, serviceDict); + } + } + + } + /** + * 分割并保存 + * @param regionDict + * @param serviceDict + * @param specificServiceCfg + * @param asnNoMap + * @param list + * @param cfgIndexInfos + * @param appPolicyCfgs + */ + public void splitAndSave(FunctionRegionDict regionDict,FunctionServiceDict serviceDict, + SpecificServiceCfg specificServiceCfg, + List list,List cfgIndexInfos, + List appPolicyCfgs){ + if(list!=null&&list.size()>0) { + String type=null; + if (list.get(0) instanceof BaseIpCfg) { + type="BaseIpCfg"; + }else if(list.get(0) instanceof BaseStringCfg) { + type="BaseStringCfg"; + }else if(list.get(0) instanceof ComplexkeywordCfg) { + type="ComplexkeywordCfg"; + }else if(list.get(0) instanceof DnsResStrategy) { + type="DnsResStrategy"; + }else if(list.get(0) instanceof AsnIpCfg) { + type="AsnIpCfg"; + } + if("BaseIpCfg".equals(type)) { + List listPage = (List) list; + if (regionDict.getFunctionId().equals(301)) { + ddosCfgService.saveDdosIpCfg((List)listPage); + }else if (regionDict.getFunctionId().equals(401)) { + dnsIpCfgService.saveDnsIpCfg((List)listPage); + }else if (specificServiceCfg!=null&®ionDict.getFunctionId().equals(405)) { + appCfgService.saveAppIpCfg((List)listPage,specificServiceCfg); + }else { + ipCfgService.saveBatch((List)listPage, IpCfgDao.class); + } + }else if("AsnIpCfg".equals(type)) { + List listPage = (List) list; + asnIpCfgService.saveAsnIpBatch(listPage); + }else if("BaseStringCfg".equals(type)) { + List> listPage = (List>) list; + // 调用对应配置的service + if (regionDict.getDictId().intValue() == 167) { + interceptCfgService.saveInterceptCfg(listPage); + } + if (regionDict.getDictId().intValue() == 9||regionDict.getDictId().intValue() == 15 || regionDict.getDictId().intValue() == 596) { + websiteCfgService.saveHttpUrlCfg(listPage); + } + if (regionDict.getDictId().intValue() == 36||regionDict.getDictId().intValue() == 37) { + fileTransferCfgService.saveFtpCfg(listPage); + } + if (regionDict.getFunctionId().equals(510)) { // P2p hash + /* + * if("p2p_hash".equals(regionDict.getConfigServiceType())) { + * fileTransferCfgService.saveP2pHashCfg(stringCfgs); }else { + */ + fileTransferCfgService.saveP2pCfg(listPage); + /* } */ + + } + if (regionDict.getFunctionId().equals(61)) { + bgpCfgService.saveBgpAsCfg(listPage); + } + if (regionDict.getFunctionId().intValue() == 34) { + websiteCfgService.saveSslCfg(listPage); + } + if(regionDict.getDictId().intValue()==12||regionDict.getDictId().intValue()==13||regionDict.getDictId().intValue()==601||regionDict.getDictId().intValue()==602) { + websiteCfgService.saveHttpBodyCfg(listPage); + } + if(regionDict.getDictId().intValue()==25){ + avContentCfgService.saveContUrlCfg(listPage); + } + if(regionDict.getDictId().intValue()==16){ + avContentCfgService.saveVoIpAccountCfg(listPage); + } + if(regionDict.getDictId().intValue()==64){ + appCfgService.saveAppDomainCfg(listPage); + } + }else if("ComplexkeywordCfg".equals(type)) { + List listPage = (List) list; + if (regionDict.getDictId().intValue() == 28) { + websiteCfgService.saveDnsCfg(listPage); + } + if (regionDict.getDictId().intValue() == 30 || regionDict.getDictId().intValue() == 31||regionDict.getDictId().intValue() == 599) { + mailCfgService.saveMailCfg(listPage); + } + if(regionDict.getDictId().intValue()==10) { + websiteCfgService.saveHttpReqHeadCfg(listPage); + } + if(regionDict.getDictId().intValue()==11) { + websiteCfgService.saveHttpResHeadCfg(listPage); + } + if(regionDict.getDictId().intValue()==63) { + appCfgService.saveAppHttpCfg(listPage); + } + }else if("DnsResStrategy".equals(type)) { + List listPage = (List) list; + dnsResStrategyService.saveDnsResStrategies(listPage); + } + } + if (cfgIndexInfos != null && cfgIndexInfos.size() > 0) { + ipCfgService.saveCfgIndexOf(cfgIndexInfos); + } + if (appPolicyCfgs != null && appPolicyCfgs.size() > 0) { + appCfgService.savePolicyList(appPolicyCfgs); + } + } /** * 多域配置导出 * diff --git a/src/main/java/com/nis/web/controller/basics/AsnIpController.java b/src/main/java/com/nis/web/controller/basics/AsnIpController.java index 42dec5ee5..7e64592f3 100644 --- a/src/main/java/com/nis/web/controller/basics/AsnIpController.java +++ b/src/main/java/com/nis/web/controller/basics/AsnIpController.java @@ -25,9 +25,11 @@ import com.nis.domain.Page; import com.nis.domain.basics.AsnIpCfg; import com.nis.domain.basics.PolicyGroupInfo; import com.nis.domain.configuration.CfgIndexInfo; -import com.nis.domain.configuration.DnsResStrategy; import com.nis.domain.configuration.IpPortCfg; +import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.domain.specific.SpecificServiceCfg; import com.nis.exceptions.MaatConvertException; +import com.nis.util.AsnCacheUtils; import com.nis.util.Constants; import com.nis.util.DictUtils; import com.nis.web.controller.BaseController; @@ -110,11 +112,10 @@ public class AsnIpController extends BaseController{ ,String ids,Integer functionId ,RedirectAttributes redirectAttributes){ try{ - asnIpCfgService.delete(functionId,isValid,ids); + asnIpCfgService.delete(isValid,ids); addMessage(redirectAttributes,"delete_success"); }catch(Exception e){ - logger.error("信息保存失败",e); - e.printStackTrace(); + logger.error("Delete failed",e); if(e instanceof MaatConvertException) { addMessage(redirectAttributes,e.getMessage()); }else { @@ -124,6 +125,25 @@ public class AsnIpController extends BaseController{ return "redirect:" + adminPath +"/basics/asn/list?functionId="+functionId; } + /** + * ajax设置Name + * @param model + * @param ids + * @return + */ + @RequestMapping(value="ajaxGroupName",method=RequestMethod.POST) + @ResponseBody + public Map ajaxGroupName(Model model,String ids){ + AsnCacheUtils.init(); + Map groupMap=new HashMap<>(); + for(String id:ids.split(",")) { + ConfigGroupInfo info=AsnCacheUtils.get(Long.parseLong(id)); + if(info!=null) { + groupMap.put(id, info.getGroupName()); + } + } + return groupMap; + } @RequestMapping(value="ajaxServiceIdState",method=RequestMethod.POST) @ResponseBody public boolean ajaxServiceIdState(Model model,@RequestParam(required=true,value="serviceGroupIds")String serviceGroupIds){ diff --git a/src/main/java/com/nis/web/controller/configuration/CommonController.java b/src/main/java/com/nis/web/controller/configuration/CommonController.java index 0f1eea490..b820c8ba5 100644 --- a/src/main/java/com/nis/web/controller/configuration/CommonController.java +++ b/src/main/java/com/nis/web/controller/configuration/CommonController.java @@ -292,6 +292,7 @@ public class CommonController extends BaseController { RedirectAttributes redirectAttributes,Integer functionId,Integer cfgRegionCode) { this.importCfgTemplate(request, response, redirectAttributes, functionId, cfgRegionCode); } + @Deprecated public void _importFileTemplate(HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes,Integer functionId,Integer cfgRegionCode,Class clazz) { this.importCfgTemplate(request, response, redirectAttributes, functionId, cfgRegionCode,clazz); diff --git a/src/main/java/com/nis/web/controller/configuration/ntc/AsnPolicyCfgController.java b/src/main/java/com/nis/web/controller/configuration/ntc/AsnPolicyCfgController.java index ee9375f16..b35f187f0 100644 --- a/src/main/java/com/nis/web/controller/configuration/ntc/AsnPolicyCfgController.java +++ b/src/main/java/com/nis/web/controller/configuration/ntc/AsnPolicyCfgController.java @@ -21,6 +21,7 @@ import com.nis.util.StringUtil; import com.nis.web.controller.BaseController; @Controller @RequestMapping("${adminPath}/cfg/asnPolicy") +@Deprecated public class AsnPolicyCfgController extends BaseController { @RequestMapping(value = {"/list"}) public String list(Model model,HttpServletRequest request @@ -76,29 +77,29 @@ public class AsnPolicyCfgController extends BaseController { asnPolicyCfgService.delete(functionId, isValid, ids); return "redirect:" + adminPath +"/cfg/asnPolicy/list?functionId="+functionId; } - @RequestMapping(value = {"/audit"}) - @RequiresPermissions(value={"asn:policy:confirm"}) - public String audit(Integer isAudit,Integer isValid,String ids - ,Integer functionId, RedirectAttributes redirectAttributes) { - if(!StringUtil.isEmpty(ids)){ - String[] idArray = ids.split(","); - Date auditTime=new Date(); - for(String id :idArray){ - try { - asnPolicyCfgService.audit(isAudit,isValid,functionId,id,auditTime); - } catch (MaatConvertException e) { - e.printStackTrace(); - logger.error("dns fake ip配置下发失败:"+e.getMessage()); - addMessage(redirectAttributes,"error","request_service_failed"); - } catch (Exception e) { - e.printStackTrace(); - logger.error("dns fake ip配置下发失败:"+e.getMessage()); - addMessage(redirectAttributes,"error","audit_failed"); - } - - } - - } - return "redirect:" + adminPath +"/cfg/asnPolicy/list?functionId="+functionId; - } +// @RequestMapping(value = {"/audit"}) +// @RequiresPermissions(value={"asn:policy:confirm"}) +// public String audit(Integer isAudit,Integer isValid,String ids +// ,Integer functionId, RedirectAttributes redirectAttributes) { +// if(!StringUtil.isEmpty(ids)){ +// String[] idArray = ids.split(","); +// Date auditTime=new Date(); +// for(String id :idArray){ +// try { +// asnPolicyCfgService.audit(isAudit,isValid,functionId,id,auditTime); +// } catch (MaatConvertException e) { +// e.printStackTrace(); +// logger.error("dns fake ip配置下发失败:"+e.getMessage()); +// addMessage(redirectAttributes,"error","request_service_failed"); +// } catch (Exception e) { +// e.printStackTrace(); +// logger.error("dns fake ip配置下发失败:"+e.getMessage()); +// addMessage(redirectAttributes,"error","audit_failed"); +// } +// +// } +// +// } +// return "redirect:" + adminPath +"/cfg/asnPolicy/list?functionId="+functionId; +// } } diff --git a/src/main/java/com/nis/web/controller/configuration/ntc/IpController.java b/src/main/java/com/nis/web/controller/configuration/ntc/IpController.java index 6bdf47795..0da999180 100644 --- a/src/main/java/com/nis/web/controller/configuration/ntc/IpController.java +++ b/src/main/java/com/nis/web/controller/configuration/ntc/IpController.java @@ -358,7 +358,7 @@ public class IpController extends BaseController{ this._import(request,response,redirectAttributes, files,serviceDictId,regionDictIds,requestId); return "redirect:" + adminPath+ importPath; } - //ip配置导出 + //ip配置导出 @RequestMapping(value = "exportIpAddr") public void exportIpAddr(Model model,HttpServletRequest request,HttpServletResponse response, @ModelAttribute("cfg")CfgIndexInfo entity,String ids,RedirectAttributes redirectAttributes){ diff --git a/src/main/java/com/nis/web/controller/configuration/ntc/WebsiteController.java b/src/main/java/com/nis/web/controller/configuration/ntc/WebsiteController.java index 2564380c5..bb4e06525 100644 --- a/src/main/java/com/nis/web/controller/configuration/ntc/WebsiteController.java +++ b/src/main/java/com/nis/web/controller/configuration/ntc/WebsiteController.java @@ -418,6 +418,7 @@ public class WebsiteController extends BaseController{ } return "redirect:" + adminPath +"/ntc/website/dnsList?functionId="+functionId; } + @Deprecated //下载导入模板 @RequestMapping(value = "import/template") public void importFileTemplate(HttpServletRequest request,HttpServletResponse response, diff --git a/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.java b/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.java index 09a46b25c..b074a2a2c 100644 --- a/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.java +++ b/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.java @@ -5,13 +5,14 @@ import java.util.List; import org.apache.ibatis.annotations.Param; import com.nis.domain.basics.AsnIpCfg; -import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.domain.basics.Varibles; import com.nis.web.dao.CrudDao; import com.nis.web.dao.MyBatisDao; @MyBatisDao public interface AsnIpCfgDao extends CrudDao{ public List findPage(AsnIpCfg cfg); - public void updateValid(@Param("isValid")Integer isValid,@Param("ids")String ids); + //public void updateValid(@Param("isValid")Integer isValid,@Param("ids")String ids); + public void delete(@Param("ids")String ids); public void updateIssued(AsnIpCfg cfg); public List getByIds(@Param("ids")String ids); public List hasGroupIds(@Param("ids")String ids); @@ -19,7 +20,9 @@ public interface AsnIpCfgDao extends CrudDao{ // public List findPolicyGroupInfosByType(@Param("groupId")Integer groupId); public List findOtherIps(@Param("groupId")Integer groupId,@Param("cfgId")Integer cfgId); public List countValidIPs(@Param("groups")String groups,@Param("ids")String ids); - public ConfigGroupInfo getInfoByAsnNo(@Param("asnId")Long asnNo); - public List getByAsnGroup(@Param("groupId")long groupId,@Param("asnId")long asnNo); - public void deleteByAsnGroup(@Param("groupId")long groupId,@Param("asnId")long asnNo); + + public List getByAsnNo(@Param("asnId")long asnNo); + public int deleteByAsnGroup(@Param("asnId")String asnNo); + public int insertBatch(List list); + public Varibles getVaribles(@Param("name")String name); } diff --git a/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.xml b/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.xml index e5ece76e5..25d5f62ec 100644 --- a/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.xml +++ b/src/main/java/com/nis/web/dao/basics/AsnIpCfgDao.xml @@ -35,14 +35,17 @@ - + - + + + + r.cfg_id,r.cfg_desc,r.ip_type,r.src_ip_address,r.ip_pattern,r.port_pattern,r.src_port ,r.protocol,r.protocol_id,r.direction,r.cfg_type,r.action,r.dest_port,r.dest_ip_address @@ -58,10 +61,10 @@ ,s.name as creator_name,e.name as editor_name,u.name as auditor_name - ,asn.group_name as asn_ip_group_name + from asn_ip_cfg r - left join config_group_info asn on asn.group_id=r.asn_ip_group + left join sys_user s on r.creator_id=s.id left join sys_user e on r.editor_id=e.id left join sys_user u on r.auditor_id=u.id @@ -206,10 +209,10 @@ from asn_ip_cfg r where r.cfg_id in (${ids}) and is_valid !=-1 - select - from asn_ip_cfg r where r.is_valid!=-1 and r.asn_ip_group=#{groupId} and r.user_region1=#{asnId} + from asn_ip_cfg r where r.is_valid!=-1 and r.user_region1=#{asnId} insert into asn_ip_cfg ( @@ -393,15 +396,18 @@ and cfg_id = #{cfgId,jdbcType=INTEGER} - + - - delete from asn_ip_cfg where asn_ip_group=#{groupId} and user_region1=#{asnId} + + delete from asn_ip_cfg where user_region1 in(${asnId}) limit 10000 + + delete from asn_ip_cfg where cfg_id in (${ids}) + @@ -411,8 +417,55 @@ - + show VARIABLES like #{name} + + INSERT INTO asn_ip_cfg + (CFG_DESC,ACTION,IS_VALID,IS_AUDIT,CREATOR_ID,CREATE_TIME,EDITOR_ID,EDIT_TIME,AUDITOR_ID, + AUDIT_TIME,SERVICE_ID,REQUEST_ID,region_id,IS_AREA_EFFECTIVE,CLASSIFY,ATTRIBUTE,LABLE, + AREA_EFFECTIVE_IDS,function_id,ip_type,src_ip_address,ip_pattern,port_pattern,src_port, + protocol,protocol_id,direction,dest_port,dest_ip_address,cfg_type,cfg_region_code, + asn_ip_group,user_region1,user_region2,user_region3,user_region4,user_region5) + VALUES + + (#{asnIp.cfgDesc,jdbcType=VARCHAR}, + #{asnIp.action,jdbcType=INTEGER}, + #{asnIp.isValid,jdbcType=INTEGER}, + 0, + #{asnIp.creatorId,jdbcType=INTEGER}, + #{asnIp.createTime,jdbcType=TIMESTAMP}, + #{asnIp.editorId,jdbcType=INTEGER}, + #{asnIp.editTime,jdbcType=TIMESTAMP}, + #{asnIp.auditorId,jdbcType=INTEGER}, + #{asnIp.auditTime,jdbcType=TIMESTAMP}, + #{asnIp.serviceId,jdbcType=INTEGER}, + #{asnIp.requestId,jdbcType=INTEGER}, + #{asnIp.regionId,jdbcType=INTEGER}, + #{asnIp.isAreaEffective,jdbcType=INTEGER}, + #{asnIp.classify,jdbcType=VARCHAR}, + #{asnIp.attribute,jdbcType=VARCHAR}, + #{asnIp.lable,jdbcType=VARCHAR}, + #{asnIp.areaEffectiveIds,jdbcType=VARCHAR}, + #{asnIp.functionId,jdbcType=INTEGER}, + #{asnIp.ipType,jdbcType=INTEGER}, + #{asnIp.srcIpAddress,jdbcType=VARCHAR}, + #{asnIp.ipPattern,jdbcType=INTEGER}, + #{asnIp.portPattern,jdbcType=INTEGER}, + #{asnIp.srcPort,jdbcType=VARCHAR}, + #{asnIp.protocol,jdbcType=INTEGER}, + #{asnIp.protocolId,jdbcType=INTEGER}, + #{asnIp.direction,jdbcType=INTEGER}, + #{asnIp.destPort,jdbcType=VARCHAR}, + #{asnIp.destIpAddress,jdbcType=VARCHAR}, + #{asnIp.cfgType,jdbcType=VARCHAR}, + #{asnIp.cfgRegionCode,jdbcType=INTEGER}, + #{asnIp.asnIpGroup,jdbcType=INTEGER}, + #{asnIp.userRegion1,jdbcType=VARCHAR}, + #{asnIp.userRegion2,jdbcType=VARCHAR}, + #{asnIp.userRegion3,jdbcType=VARCHAR}, + #{asnIp.userRegion4,jdbcType=VARCHAR}, + #{asnIp.userRegion5,jdbcType=VARCHAR}) + + \ No newline at end of file diff --git a/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.java b/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.java index c1cc3b1f0..4176f3971 100644 --- a/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.java +++ b/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.java @@ -19,4 +19,5 @@ public interface PolicyGroupInfoDao extends CrudDao { PolicyGroupInfo getInfoByAsnNo(PolicyGroupInfo policyGroupInfo); List getHasAreaPolicyGroups(int groupType); + int insertBatch(List list); } \ No newline at end of file diff --git a/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.xml b/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.xml index fbb21273e..16a1929e7 100644 --- a/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.xml +++ b/src/main/java/com/nis/web/dao/basics/PolicyGroupInfoDao.xml @@ -113,6 +113,30 @@ #{asnNo,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR} ) + + + insert into policy_group_info( + IS_VALID, + CREATOR_ID, + CREATE_TIME, + GROUP_NAME, + GROUP_TYPE, + SERVICE_GROUP_ID, + ASN_NO, + DESCRIPTION + )values + + ( + 1, + #{info.creatorId,jdbcType=INTEGER}, + #{info.createTime,jdbcType=TIMESTAMP}, + #{info.groupName,jdbcType=VARCHAR}, + #{info.groupType,jdbcType=INTEGER}, + #{info.serviceGroupId,jdbcType=INTEGER}, + #{info.asnNo,jdbcType=INTEGER}, + #{info.description,jdbcType=VARCHAR} + ) + update policy_group_info diff --git a/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.java b/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.java new file mode 100644 index 000000000..21b28e139 --- /dev/null +++ b/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.java @@ -0,0 +1,22 @@ +package com.nis.web.dao.specific; + +import java.util.List; + +import org.apache.ibatis.annotations.Param; +import org.springframework.cache.annotation.Cacheable; + +import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.web.dao.CrudDao; +import com.nis.web.dao.MyBatisDao; +@MyBatisDao +public interface ConfigGroupInfoDao extends CrudDao{ + List findAllList(int groupType); + void insertConfigGroupInfo(ConfigGroupInfo entity); + int insertBatch(List list); + void updateConfigGroupInfobyGroupId(ConfigGroupInfo entity); + ConfigGroupInfo getConfigGroupInfoByGroupId(Integer groupId); + @Cacheable(value="asnNoCache",key="#asnNo") + ConfigGroupInfo getInfoByAsnNo(@Param("asnId")Long asnNo); + Integer getIssuedConfigGroupInfoByGroupIds(@Param("groupIds")String groupIds); + Long getCountByType(@Param("groupType")Integer groupType); +} diff --git a/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.xml b/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.xml new file mode 100644 index 000000000..644cf2c1f --- /dev/null +++ b/src/main/java/com/nis/web/dao/specific/ConfigGroupInfoDao.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + id,group_id,group_name,is_issued,insert_time,update_time,group_type,compile_id,asn_id + + + + + insert into config_group_info (id,group_id,group_name,is_issued,insert_time,group_type,compile_id,asn_id) + values(#{id},#{groupId},#{groupName},#{isIssued},now(),#{groupType},#{compileId},#{asnId}) + + + insert into config_group_info (id,group_id,group_name,is_issued,insert_time,group_type,compile_id,asn_id) + values + + (#{info.id},#{info.groupId},#{info.groupName},#{info.isIssued},now(),#{info.groupType},#{info.compileId},#{info.asnId}) + + + + + UPDATE config_group_info set is_issued = #{isIssued},update_time=now() + + ,compile_id= #{compileId} + + where group_id = #{groupId} + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.java b/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.java index 29565c169..47a47e210 100644 --- a/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.java +++ b/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.java @@ -3,6 +3,8 @@ package com.nis.web.dao.specific; import java.util.List; import org.apache.ibatis.annotations.Param; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; import com.nis.domain.specific.ConfigGroupInfo; import com.nis.domain.specific.SpecificServiceCfg; @@ -42,14 +44,6 @@ public interface SpecificServiceCfgDao extends CrudDao { * @return */ List getChildrenById(Integer specServiceId); - - ConfigGroupInfo getConfigGroupInfoByGroupId(Integer groupId); - ConfigGroupInfo findPolicyGroupInfosByType(@Param("groupType")Integer groupType); - Integer getIssuedConfigGroupInfoByGroupIds(@Param("groupIds")String groupIds); - - Integer insertConfigGroupInfo(ConfigGroupInfo entity); - - Integer updateConfigGroupInfobyGroupId(ConfigGroupInfo entity); Integer getParentType(Integer specServiceId); Integer getParentCode(Integer specServiceId); SpecificServiceCfg getRepeat(@Param("specServiceCode")Integer code, @Param("cfgType")Integer cfgType,@Param("parentId")Integer parentId); diff --git a/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.xml b/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.xml index 1bf00b9a0..031bcc105 100644 --- a/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.xml +++ b/src/main/java/com/nis/web/dao/specific/SpecificServiceCfgDao.xml @@ -213,32 +213,4 @@ - - - - - - - - UPDATE config_group_info set is_issued = #{isIssued},update_time=now() - - ,compile_id= #{compileId} - - where group_id = #{groupId} - - - - - insert into config_group_info (id,group_id,group_name,is_issued,insert_time,group_type,compile_id,asn_id) - values(#{id},#{groupId},#{groupName},#{isIssued},now(),#{groupType},#{compileId},#{asnId}) - - \ No newline at end of file diff --git a/src/main/java/com/nis/web/service/BaseService.java b/src/main/java/com/nis/web/service/BaseService.java index b614838fe..d98e555f4 100644 --- a/src/main/java/com/nis/web/service/BaseService.java +++ b/src/main/java/com/nis/web/service/BaseService.java @@ -1092,33 +1092,48 @@ public abstract class BaseService { } //asn IP 复用转换 public List groupReuseCfgAddRemoveConvert(List> ipCfgList,Integer isValid,Integer groupId) { + logger.warn("convert data start"); + long start=System.currentTimeMillis(); Date opTime=new Date(); List maatIpList=new ArrayList<>(); - for(BaseCfg _cfg:ipCfgList) { - IpCfg cfg = new IpCfg(); - BaseIpCfg baseIpCfg = new BaseIpCfg(); - if(_cfg instanceof AsnIpCfg) { - AsnIpCfg asnIpCfg=(AsnIpCfg)_cfg; - BeanUtils.copyProperties(asnIpCfg, baseIpCfg); - BeanUtils.copyProperties(baseIpCfg, cfg); - cfg.setGroupId(asnIpCfg.getAsnIpGroup()); - cfg.setRegionId(asnIpCfg.getRegionId()); - String userRegion = "ASN_ID=" + asnIpCfg.getUserRegion1(); - cfg.setUserRegion(userRegion); - }else if(_cfg instanceof AppIpCfg) { - AppIpCfg appIpCfg=(AppIpCfg)_cfg; - BeanUtils.copyProperties(appIpCfg, baseIpCfg); - BeanUtils.copyProperties(baseIpCfg, cfg); - cfg.setGroupId(groupId); - cfg.setRegionId(Integer.parseInt(appIpCfg.getUserRegion1())); - String userRegion = "APP_ID=" + appIpCfg.getAppCode(); - cfg.setUserRegion(userRegion); + if(ipCfgList.size()>0) { + //只用一次instanceof,取代循环中每次都用一次instanceof + if(ipCfgList.get(0) instanceof AsnIpCfg) { + for(BaseCfg _cfg:ipCfgList) { + IpCfg cfg = new IpCfg(); + BaseIpCfg baseIpCfg = new BaseIpCfg(); + AsnIpCfg asnIpCfg=(AsnIpCfg)_cfg; + BeanUtils.copyProperties(asnIpCfg, baseIpCfg); + BeanUtils.copyProperties(baseIpCfg, cfg); + cfg.setGroupId(asnIpCfg.getAsnIpGroup()); + cfg.setRegionId(asnIpCfg.getRegionId()); + String userRegion = "ASN_ID=" + asnIpCfg.getUserRegion1(); + cfg.setUserRegion(userRegion); + cfg.setIsValid(isValid); + cfg.setAuditTime(opTime); + List cfgs = ipConvert(cfg,baseIpCfg); + maatIpList.addAll(cfgs); + } + }else if(ipCfgList.get(0) instanceof AppIpCfg) { + for(BaseCfg _cfg:ipCfgList) { + IpCfg cfg = new IpCfg(); + BaseIpCfg baseIpCfg = new BaseIpCfg(); + AppIpCfg appIpCfg=(AppIpCfg)_cfg; + BeanUtils.copyProperties(appIpCfg, baseIpCfg); + BeanUtils.copyProperties(baseIpCfg, cfg); + cfg.setGroupId(groupId); + cfg.setRegionId(Integer.parseInt(appIpCfg.getUserRegion1())); + String userRegion = "APP_ID=" + appIpCfg.getAppCode(); + cfg.setUserRegion(userRegion); + cfg.setIsValid(isValid); + cfg.setAuditTime(opTime); + List cfgs = ipConvert(cfg,baseIpCfg); + maatIpList.addAll(cfgs); + } } - cfg.setIsValid(isValid); - cfg.setAuditTime(opTime); - List cfgs = ipConvert(cfg,baseIpCfg); - maatIpList.addAll(cfgs); } + long end=System.currentTimeMillis(); + logger.warn("convert data finish,cost:"+(end-start)); return maatIpList; } } diff --git a/src/main/java/com/nis/web/service/CrudService.java b/src/main/java/com/nis/web/service/CrudService.java index ba44984e2..96b4e8f41 100644 --- a/src/main/java/com/nis/web/service/CrudService.java +++ b/src/main/java/com/nis/web/service/CrudService.java @@ -105,10 +105,16 @@ public abstract class CrudService, T extends BaseEntity> SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class); SqlSession batchSqlSession = null; try{ +// int batchSize=1000; batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); for(int index = 0; index < data.size();index++){ T t = data.get(index); ((CrudDao) batchSqlSession.getMapper(mClass)).insert(t); +// if(index>0&&index%batchSize==0) { +// batchSqlSession.commit(); +// batchSqlSession.clearCache(); +// logger.warn("session commit"); +// } } batchSqlSession.commit(); }finally { diff --git a/src/main/java/com/nis/web/service/basics/AsnIpCfgService.java b/src/main/java/com/nis/web/service/basics/AsnIpCfgService.java index 252cfcfb2..85513ff3f 100644 --- a/src/main/java/com/nis/web/service/basics/AsnIpCfgService.java +++ b/src/main/java/com/nis/web/service/basics/AsnIpCfgService.java @@ -1,10 +1,14 @@ package com.nis.web.service.basics; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; @@ -14,9 +18,9 @@ import org.springframework.transaction.annotation.Transactional; import com.beust.jcommander.internal.Lists; import com.nis.domain.Page; -import com.nis.domain.SysDataDictionaryItem; import com.nis.domain.basics.AsnIpCfg; import com.nis.domain.basics.PolicyGroupInfo; +import com.nis.domain.basics.Varibles; import com.nis.domain.configuration.CfgIndexInfo; import com.nis.domain.maat.GroupReuseAddBean; import com.nis.domain.maat.GroupReuseCfg; @@ -26,21 +30,23 @@ import com.nis.domain.maat.MaatCfg.StringCfg; import com.nis.domain.maat.ToMaatResult; import com.nis.domain.specific.ConfigGroupInfo; import com.nis.exceptions.MaatConvertException; +import com.nis.util.AsnCacheUtils; import com.nis.util.ConfigServiceUtil; import com.nis.util.Constants; -import com.nis.util.DictUtils; import com.nis.web.dao.CrudDao; import com.nis.web.dao.basics.AsnIpCfgDao; import com.nis.web.dao.basics.PolicyGroupInfoDao; -import com.nis.web.dao.specific.SpecificServiceCfgDao; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.security.UserUtils; import com.nis.web.service.CrudService; @Service public class AsnIpCfgService extends CrudService, AsnIpCfg> { @Autowired private AsnIpCfgDao asnIpCfgDao; +// @Autowired +// private SpecificServiceCfgDao specificServiceCfgDao; @Autowired - private SpecificServiceCfgDao specificServiceCfgDao; + private ConfigGroupInfoDao configGroupInfoDao; @Autowired private PolicyGroupInfoDao policyGroupInfoDao; /** @@ -58,9 +64,38 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { public AsnIpCfg get(Long id ) { return asnIpCfgDao.get(id); } + +// public void save(List entitys){ +// this.saveBatch(entitys, AsnIpCfgDao.class); +// } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) - public void save(List entitys){ - this.saveBatch(entitys, AsnIpCfgDao.class); + public void save(List entitys) { + logger.warn("Start to save IP"); + long start=System.currentTimeMillis(); + int len=0; + List tempList=Lists.newArrayList(); + Varibles maxPacket=asnIpCfgDao.getVaribles("max_allowed_packet"); + for(AsnIpCfg asnIpCfg:entitys) { + int tempLen=asnIpCfg.toString().getBytes(Charset.forName("UTF-8")).length; + if((len+tempLen)0) { + logger.warn("save ip size:"+tempList.size()); + asnIpCfgDao.insertBatch(tempList); + tempList.clear(); + } +// entitys.clear(); + long end=System.currentTimeMillis(); + logger.warn("Save IP finish,cost:"+(end-start)); } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) public void update(AsnIpCfg entity){ @@ -70,7 +105,7 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { if(entity.getAsnIpGroup()==null) { throw new RuntimeException("asn group name not found!"); } - ConfigGroupInfo groupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup()); + ConfigGroupInfo groupInfo=configGroupInfoDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup()); if(groupInfo.getIsIssued()==0) {//未下发,可修改 asnIpCfgDao.update(entity); }else { @@ -79,53 +114,170 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) - public void saveAsnIpBatch(Map> cfgs,int total) { - //需要通过删除域接口删除的ip集合 - List toDelAsnIpCfgs=Lists.newArrayList(); - //需要本地保存的IP集合 - List toSaveAsnIpCfgs=Lists.newArrayList(); + public void processGroup(Map asnNoMap){ + logger.warn("Start to save group"); + long start=System.currentTimeMillis(); + List policyGroupInfos=Lists.newArrayList(); + List configGroupInfoS=Lists.newArrayList(); + Date createTime=new Date(); + int len=0,len1=0; + Varibles maxPacket=asnIpCfgDao.getVaribles("max_allowed_packet"); + for(Entry e:asnNoMap.entrySet()) { + PolicyGroupInfo info=new PolicyGroupInfo(); + info.setAsnNo(e.getKey()); + info.setDescription("Create by import excel"); + info.setGroupName("Import"+e.getKey()); + info.setGroupType(4); + info.setServiceGroupId(asnNoMap.get(e.getKey())); + info.setIsValid(1); + info.setCreatorId(UserUtils.getUser().getId()); + info.setCreateTime(createTime); + int tempLen=info.toString().getBytes(Charset.forName("UTF-8")).length; + if((len+tempLen)0) { + policyGroupInfoDao.insertBatch(policyGroupInfos); + policyGroupInfos.clear(); + } + if(configGroupInfoS.size()>0) { + configGroupInfoDao.insertBatch(configGroupInfoS); + configGroupInfoS.clear(); + } + //刷新缓存 + AsnCacheUtils.init(); + long end=System.currentTimeMillis(); + logger.warn("Save group finish,cost:"+(end-start)); + } + public void deleteIps(Map asnNoMap){ + List toDelAndSendAsnIpCfgs=Lists.newArrayList(); + List asnIds=Lists.newArrayList(asnNoMap.size()); + for(Entry e:asnNoMap.entrySet()) { + ConfigGroupInfo configGroupInfo=AsnCacheUtils.get(e.getKey()); + if(configGroupInfo==null) { + configGroupInfo=this.getConfigGroupInfoByAsnNo(e.getKey()); + } + if(configGroupInfo.getIsIssued()==1) {//已下发 + List _toDelAsnIpCfgs=this.getByAsnNo(configGroupInfo.getAsnId()); + toDelAndSendAsnIpCfgs.addAll(_toDelAsnIpCfgs); + }else { + asnIds.add(e.getKey()); + } + } + if(asnIds.size()>0) { + this.deleteByAsnNo(asnIds); + } + if(toDelAndSendAsnIpCfgs.size()>0) { + int pointsDataLimit = Constants.MAAT_JSON_SEND_SIZE;//限制条数 + Integer size = toDelAndSendAsnIpCfgs.size(); + //判断是否有必要分批 + if(pointsDataLimit listPage = toDelAndSendAsnIpCfgs.subList(0, pointsDataLimit); + delAndSend(listPage); + //剔除 + toDelAndSendAsnIpCfgs.subList(0, pointsDataLimit).clear(); + } + //最后剩下的 + if(!toDelAndSendAsnIpCfgs.isEmpty()){ + delAndSend(toDelAndSendAsnIpCfgs); + + } + }else { + delAndSend(toDelAndSendAsnIpCfgs); + } + } + } + /** + * 根据Id 批量删除 + * @param entities + */ + public void deleteByIdsBatch(List entities) { + StringBuilder sb=new StringBuilder(); + for(AsnIpCfg ip:entities) { + sb.append(ip.getCfgId()); + sb.append(","); + } + sb.deleteCharAt(sb.toString().lastIndexOf(",")); + asnIpCfgDao.delete(sb.toString()); + } + @Transactional(readOnly=false,rollbackFor=RuntimeException.class) + public void delAndSend(List entities) { + deleteByIdsBatch(entities); + asnIPRegionSendToMaat(entities,Constants.VALID_NO); + } + @Transactional(readOnly=false,rollbackFor=RuntimeException.class) + public void saveAsnIpBatch(List cfgs){ //需要通过新增域接口新增的ip集合 List toAddRegionAsnIpCfgs=Lists.newArrayList(); - List isImportAll=DictUtils.getDictList("IS_ASN_IP_IMPORT_ALL"); - List idList=ConfigServiceUtil.getId(3, total); - int ind=0; - for(Entry> entry:cfgs.entrySet()) { - Long asnNo=Long.parseLong(entry.getKey()); - ConfigGroupInfo configGroupInfo=this.getConfigGroupInfoByAsnNo(asnNo); - if(configGroupInfo==null) {//不存在则新增 - PolicyGroupInfo info=new PolicyGroupInfo(); - info.setAsnNo(Long.parseLong(entry.getKey())); - info.setDescription("Create by import excel"); - info.setGroupName("Import"+asnNo); - info.setGroupType(4); - configGroupInfo=this.saveAsnGroup(info); + logger.warn("process configGroupInfo and delete IP start"); + long start=System.currentTimeMillis(); + for(AsnIpCfg cfg:cfgs) { + if(Constants.VALID_YES==cfg.getIsValid().intValue()) { + toAddRegionAsnIpCfgs.add(cfg); } - for(AsnIpCfg cfg:entry.getValue()) { - cfg.setAsnIpGroup(configGroupInfo.getGroupId()); - cfg.setRegionId(idList.get(ind)); - if(configGroupInfo.getIsIssued()==0) - cfg.setIsValid(Constants.VALID_NO); - else { - cfg.setIsValid(Constants.VALID_YES); - toAddRegionAsnIpCfgs.add(cfg); - } - ind++; - } - if(isImportAll.get(0).getItemCode().equals("1")) {//全量下发,删除已有的 - if(configGroupInfo.getIsIssued()==1) {//已下发 - List _toDelAsnIpCfgs=this.getByAsnGroup(configGroupInfo.getGroupId(), configGroupInfo.getAsnId()); - toDelAsnIpCfgs.addAll(_toDelAsnIpCfgs); - } - this.deleteByAsnGroup(configGroupInfo.getGroupId(), configGroupInfo.getAsnId()); - } - toSaveAsnIpCfgs.addAll(entry.getValue()); } - this.save(toSaveAsnIpCfgs); - if(toDelAsnIpCfgs.size()>0) { - asnIPRegionSendToMaat(toDelAsnIpCfgs,Constants.VALID_NO); - } - if(toAddRegionAsnIpCfgs.size()>0) { - asnIPRegionSendToMaat(toAddRegionAsnIpCfgs,Constants.VALID_YES); + long end=System.currentTimeMillis(); + logger.warn("process configGroupInfo and delete IP finish,cost:"+(end-start)); + this.save(cfgs); + cfgs.clear(); + splitAndSend(toAddRegionAsnIpCfgs,Constants.VALID_YES); + toAddRegionAsnIpCfgs.clear(); + } + /** + * 分割IP region分次下发 + * @param asnIpCfgs + * @param valid + */ + public void splitAndSend(List asnIpCfgs,Integer valid){ + if(asnIpCfgs!=null&&asnIpCfgs.size()>0) { +// List asnIpCfgs=Lists.newArrayList(list.size()); +// asnIpCfgs.addAll(list); + int pointsDataLimit = Constants.MAAT_JSON_SEND_SIZE;//限制条数 + Integer size = asnIpCfgs.size(); + //判断是否有必要分批 + if(pointsDataLimit listPage = asnIpCfgs.subList(0, pointsDataLimit); + asnIPRegionSendToMaat(listPage,valid); + //剔除 + asnIpCfgs.subList(0, pointsDataLimit).clear(); + } + //最后剩下的 + if(!asnIpCfgs.isEmpty()){ + asnIPRegionSendToMaat(asnIpCfgs,valid); + } + }else { + asnIPRegionSendToMaat(asnIpCfgs,valid); + } + } } /** @@ -151,7 +303,11 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { maatBean.setOpAction(Constants.INSERT_ACTION); //调用服务接口下发配置数据 String json=gsonToJson(maatBean); - logger.info("asn ip复用域新增配置下发配置参数:"+json); + if(asnIpCfgs.size()<=100) { + logger.info("asn ip复用域新增配置下发配置参数:"+json); + }else { + logger.info("asn ip复用域新增配置下发region条数:"+asnIpCfgs.size()); + } //调用服务接口下发配置 ToMaatResult result =ConfigServiceUtil.postGroupReuseSources(json); logger.info("asn ip复用域新增配置响应信息:"+result.getMsg()); @@ -159,45 +315,63 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { maatBean.setOpAction(Constants.UPDATE_ACTION); //调用服务接口下发配置数据 String json=gsonToJson(maatBean); - logger.info("asn ip复用域删除配置下发配置参数:"+json); + if(asnIpCfgs.size()<=100) { + logger.info("asn ip复用域删除配置下发配置参数:"+json); + }else { + logger.info("asn ip复用域删除配置下发region条数:"+asnIpCfgs.size()); + + } //调用服务接口下发配置 ToMaatResult result = ConfigServiceUtil.put(json,3); logger.info("asn ip复用域删除配置响应信息:"+result.getMsg()); } } + /** + * 批量保存asn PolicyGroupInfo + * @param entities + * @return + * @throws MaatConvertException + */ @Transactional(readOnly=false,rollbackFor=RuntimeException.class) - public ConfigGroupInfo saveAsnGroup(PolicyGroupInfo entity) throws MaatConvertException{ - entity.setIsValid(1); - ConfigGroupInfo group=new ConfigGroupInfo(); - //新增 - Date createTime=new Date(); - entity.setCreatorId(UserUtils.getUser().getId()); - entity.setCreateTime(createTime); - //asn类型从综合服务获取groupId - if(4==entity.getGroupType().intValue()) { - int serviceGroupId=0; - List groupIds= ConfigServiceUtil.getId(2,1); - if(groupIds.size()>0) { - serviceGroupId=groupIds.get(0).intValue(); - }else { - throw new MaatConvertException("Get asn group id failed"); + public Map saveAsnGroupBatch(List entities) throws MaatConvertException{ + Map configGroupInfoMap=new HashMap<>(entities.size()); + List groupIds= ConfigServiceUtil.getId(2,entities.size()); + if(groupIds.size()!=entities.size()) { + throw new MaatConvertException("Get asn group id failed"); + } + int ind=0; + for(PolicyGroupInfo entity:entities) { + entity.setIsValid(1); + ConfigGroupInfo group=new ConfigGroupInfo(); + //新增 + Date createTime=new Date(); + entity.setCreatorId(UserUtils.getUser().getId()); + entity.setCreateTime(createTime); + //asn类型从综合服务获取groupId + if(4!=entity.getGroupType().intValue()) { + throw new RuntimeException("ConfigGroupInfo is not asn type 4!"); } - entity.setServiceGroupId(serviceGroupId); + entity.setServiceGroupId(groupIds.get(ind).intValue()); //新增协议分组 - group.setGroupId(serviceGroupId); + group.setGroupId(groupIds.get(ind).intValue()); group.setGroupName(entity.getGroupName()); group.setIsIssued(0); group.setGroupType(entity.getGroupType()); group.setAsnId(entity.getAsnNo()); - specificServiceCfgDao.insertConfigGroupInfo(group); + configGroupInfoDao.insertConfigGroupInfo(group); + if(group.getGroupType().intValue()==4) { + AsnCacheUtils.put(entity.getAsnNo(), group); + } policyGroupInfoDao.insert(entity); + configGroupInfoMap.put(group.getAsnId(), group); + ind++; } - return group; + return configGroupInfoMap; } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) public void saveAsnIpCfg(CfgIndexInfo entity) { Date createTime=new Date(); - ConfigGroupInfo groupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup().intValue()); + ConfigGroupInfo groupInfo=configGroupInfoDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup().intValue()); if(groupInfo==null) { throw new RuntimeException("ConfigGroupInfo is null!"); } @@ -221,7 +395,7 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { } } this.save(entity.getAsnIpCfgs()); - asnIPRegionSendToMaat(entity.getAsnIpCfgs(),Constants.VALID_YES); + splitAndSend(entity.getAsnIpCfgs(),Constants.VALID_YES); }else { if(entity.getAsnIpCfgs()!=null) { for(AsnIpCfg cfg:entity.getAsnIpCfgs()) { @@ -238,7 +412,7 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { } } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) - public void delete(Integer functionId,Integer isValid,String ids) { + public void delete(Integer isValid,String ids) { for(String id:ids.split(",")) { Long.parseLong(id); } @@ -256,11 +430,10 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { } } } - if(issuedList.size()>0) { - //已经下发过的,调用分组复用配置删除接口 - asnIPRegionSendToMaat(issuedList,Constants.VALID_NO); - } - asnIpCfgDao.updateValid(isValid, ids); + asnIpCfgDao.delete(ids); + //已经下发过的,调用分组复用配置删除接口 + splitAndSend(issuedList,Constants.VALID_NO); + } // public List findPolicyGroupInfosByType(Integer groupId) { // // TODO Auto-generated method stub @@ -288,18 +461,65 @@ public class AsnIpCfgService extends CrudService, AsnIpCfg> { return false; } public ConfigGroupInfo getConfigGroupInfoByAsnNo(Long asnNo) { - return asnIpCfgDao.getInfoByAsnNo(asnNo); + ConfigGroupInfo configGroupInfo=configGroupInfoDao.getInfoByAsnNo(asnNo); + AsnCacheUtils.put(asnNo, configGroupInfo); + return configGroupInfo; } - public List getByAsnGroup(Integer groupId,Long asnNo) { - if(groupId==null||asnNo==null) { + public List getByAsnNo(Long asnNo) { + if(asnNo==null) { throw new RuntimeException("groupId or asnNo is null!"); } - return asnIpCfgDao.getByAsnGroup(groupId.longValue(),asnNo.longValue()); + return asnIpCfgDao.getByAsnNo(asnNo.longValue()); } - public void deleteByAsnGroup(Integer groupId,Long asnNo) { - if(groupId==null||asnNo==null) { - throw new RuntimeException("groupId or asnNo is null!"); +// public void deleteByAsnGroup(Long asnNo) { +// if(asnNo==null) { +// throw new RuntimeException("asnNo is null!"); +// } +// asnIpCfgDao.deleteByAsnGroup(asnNo.longValue()); +// } + @Transactional(readOnly=false,rollbackFor=RuntimeException.class) + public void deleteByAsnNo(List asnNoList) { + int pointsDataLimit = 1000;//限制条数 + Integer size = asnNoList.size(); + if(pointsDataLimit listPage = asnNoList.subList(0, pointsDataLimit); + StringBuilder asnNoStr=new StringBuilder(); + for(Long asnNo:asnNoList) { + asnNoStr.append(asnNo); + asnNoStr.append(","); + } + int result=0; + do { + result=asnIpCfgDao.deleteByAsnGroup(asnNoStr.toString().substring(0,asnNoStr.toString().length()-1)); + }while(result>0); + //剔除 + asnNoList.subList(0, pointsDataLimit).clear(); + } + if(!asnNoList.isEmpty()){ + StringBuilder asnNoStr=new StringBuilder(); + for(Long asnNo:asnNoList) { + asnNoStr.append(asnNo); + asnNoStr.append(","); + } + int result=0; + do { + result=asnIpCfgDao.deleteByAsnGroup(asnNoStr.toString().substring(0,asnNoStr.toString().length()-1)); + }while(result>0); + asnNoList.clear(); + } + }else { + StringBuilder asnNoStr=new StringBuilder(); + for(Long asnNo:asnNoList) { + asnNoStr.append(asnNo); + asnNoStr.append(","); + } + int result=0; + do { + result=asnIpCfgDao.deleteByAsnGroup(asnNoStr.toString().substring(0,asnNoStr.toString().length()-1)); + }while(result>0); + asnNoList.clear(); } - asnIpCfgDao.deleteByAsnGroup(groupId.longValue(),asnNo.longValue()); } } diff --git a/src/main/java/com/nis/web/service/basics/PolicyGroupInfoService.java b/src/main/java/com/nis/web/service/basics/PolicyGroupInfoService.java index c50e4fada..f0d46a0a7 100644 --- a/src/main/java/com/nis/web/service/basics/PolicyGroupInfoService.java +++ b/src/main/java/com/nis/web/service/basics/PolicyGroupInfoService.java @@ -11,10 +11,12 @@ import com.nis.domain.Page; import com.nis.domain.basics.PolicyGroupInfo; import com.nis.domain.specific.ConfigGroupInfo; import com.nis.exceptions.MaatConvertException; +import com.nis.util.AsnCacheUtils; import com.nis.util.ConfigServiceUtil; import com.nis.util.StringUtil; import com.nis.web.dao.basics.AsnIpCfgDao; import com.nis.web.dao.basics.PolicyGroupInfoDao; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.dao.specific.SpecificServiceCfgDao; import com.nis.web.security.UserUtils; import com.nis.web.service.BaseService; @@ -25,7 +27,8 @@ public class PolicyGroupInfoService extends BaseService{ @Autowired private PolicyGroupInfoDao policyGroupInfoDao; @Autowired - private SpecificServiceCfgDao specificServiceCfgDao; +// private SpecificServiceCfgDao specificServiceCfgDao; + private ConfigGroupInfoDao configGroupInfoDao; @Autowired private AsnIpCfgDao asnIpCfgDao; @@ -86,7 +89,7 @@ public class PolicyGroupInfoService extends BaseService{ } entity.setServiceGroupId(serviceGroupId); //新增协议分组 - ConfigGroupInfo group = specificServiceCfgDao.getConfigGroupInfoByGroupId(serviceGroupId); + ConfigGroupInfo group = configGroupInfoDao.getConfigGroupInfoByGroupId(serviceGroupId); if(group==null){ group = new ConfigGroupInfo(); group.setGroupId(serviceGroupId); @@ -94,7 +97,11 @@ public class PolicyGroupInfoService extends BaseService{ group.setIsIssued(0); group.setGroupType(entity.getGroupType()); group.setAsnId(entity.getAsnNo()); - specificServiceCfgDao.insertConfigGroupInfo(group); + configGroupInfoDao.insertConfigGroupInfo(group); + if(group.getGroupType().intValue()==4) { + AsnCacheUtils.put(entity.getAsnNo(), group); + } + } } policyGroupInfoDao.insert(entity); @@ -126,7 +133,7 @@ public class PolicyGroupInfoService extends BaseService{ for(String groupId:groupIds.split(",")) { Integer.parseInt(groupId); } - Integer count=specificServiceCfgDao.getIssuedConfigGroupInfoByGroupIds(groupIds); + Integer count=configGroupInfoDao.getIssuedConfigGroupInfoByGroupIds(groupIds); if(count>0) return true; return false; } diff --git a/src/main/java/com/nis/web/service/configuration/AppCfgService.java b/src/main/java/com/nis/web/service/configuration/AppCfgService.java index 1ae702a8f..0f249c0d6 100644 --- a/src/main/java/com/nis/web/service/configuration/AppCfgService.java +++ b/src/main/java/com/nis/web/service/configuration/AppCfgService.java @@ -52,6 +52,7 @@ import com.nis.web.dao.configuration.AppCfgDao; import com.nis.web.dao.configuration.AreaIpCfgDao; import com.nis.web.dao.configuration.IpCfgDao; import com.nis.web.dao.configuration.StringCfgDao; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.dao.specific.SpecificServiceCfgDao; import com.nis.web.security.UserUtils; import com.nis.web.service.BaseService; @@ -75,6 +76,8 @@ public class AppCfgService extends BaseService { protected IpCfgDao ipCfgDao; @Autowired protected SpecificServiceCfgDao specificServiceCfgDao; + @Autowired + protected ConfigGroupInfoDao configGroupInfoDao; public Page findAppPolicyList(Page page, AppPolicyCfg entity) { entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(), "r")); @@ -322,8 +325,8 @@ public class AppCfgService extends BaseService { * @throws Exception */ @Transactional(readOnly = false, rollbackFor = RuntimeException.class) - public void saveAppIpCfg(List cfgs,SpecificServiceCfg specificServiceCfg) throws Exception { - ConfigGroupInfo configGroupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); + public void saveAppIpCfg(List cfgs,SpecificServiceCfg specificServiceCfg){ + ConfigGroupInfo configGroupInfo=configGroupInfoDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); if(configGroupInfo==null) {//一般特定服务的组号是新增之后就有的,不会进入这一步。 Integer groupId = ConfigServiceUtil.getId(2, 1).get(0); Integer compileId=ConfigServiceUtil.getId(1, 1).get(0); @@ -334,7 +337,7 @@ public class AppCfgService extends BaseService { configGroupInfo.setIsIssued(0); configGroupInfo.setGroupType(1); configGroupInfo.setCompileId(compileId); - specificServiceCfgDao.insertConfigGroupInfo(configGroupInfo); + configGroupInfoDao.insertConfigGroupInfo(configGroupInfo); //更新group_id specificServiceCfg.setOpTime(new Date()); specificServiceCfgDao.update(specificServiceCfg); @@ -355,7 +358,7 @@ public class AppCfgService extends BaseService { if (entity.getCfgId() == null) { SpecificServiceCfg specificServiceCfg=specificServiceCfgDao.getBySpecServiceId(entity.getSpecServiceId()); - ConfigGroupInfo configGroupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); + ConfigGroupInfo configGroupInfo=configGroupInfoDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); if(configGroupInfo==null) {//一般特定服务的组号是新增之后就有的,不会进入这一步。 Integer groupId = ConfigServiceUtil.getId(2, 1).get(0); Integer compileId=ConfigServiceUtil.getId(1, 1).get(0); @@ -366,7 +369,7 @@ public class AppCfgService extends BaseService { configGroupInfo.setIsIssued(0); configGroupInfo.setGroupType(1); configGroupInfo.setCompileId(compileId); - specificServiceCfgDao.insertConfigGroupInfo(configGroupInfo); + configGroupInfoDao.insertConfigGroupInfo(configGroupInfo); //更新group_id specificServiceCfg.setOpTime(new Date()); specificServiceCfgDao.update(specificServiceCfg); @@ -408,7 +411,7 @@ public class AppCfgService extends BaseService { //app ip的编译ID存于configGroupInfo中 if((configGroupInfo.getCompileId()==null)||(compileId.intValue()!=configGroupInfo.getCompileId().intValue())) { configGroupInfo.setCompileId(entity.getCompileId()); - specificServiceCfgDao.updateConfigGroupInfobyGroupId(configGroupInfo); + configGroupInfoDao.updateConfigGroupInfobyGroupId(configGroupInfo); } // 保存区域IP信息 if (entity.getAreaCfg() != null) { @@ -514,7 +517,7 @@ public class AppCfgService extends BaseService { } @Transactional(readOnly = false, rollbackFor = RuntimeException.class) - public void saveAppDomainCfg(List> cfgs) throws Exception { + public void saveAppDomainCfg(List> cfgs){ for(BaseStringCfg cfg:cfgs) { AppDomainCfg entity=new AppDomainCfg(); BeanUtils.copyProperties(cfg, entity); @@ -1169,7 +1172,7 @@ public class AppCfgService extends BaseService { Integer specServiceId= entitys.get(0).getSpecServiceId(); SpecificServiceCfg specificServiceCfg=specificServiceCfgDao.getBySpecServiceId(specServiceId); //String businessType=specificServiceCfg.getBusinessType(); - ConfigGroupInfo configGroupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); + ConfigGroupInfo configGroupInfo=configGroupInfoDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); if(configGroupInfo==null) { Integer groupId = ConfigServiceUtil.getId(2, 1).get(0); specificServiceCfg.setGroupId(groupId); @@ -1179,7 +1182,7 @@ public class AppCfgService extends BaseService { configGroupInfo.setIsIssued(0); configGroupInfo.setCompileId(entitys.get(0).getCompileId()); configGroupInfo.setGroupType(1); - specificServiceCfgDao.insertConfigGroupInfo(configGroupInfo); + configGroupInfoDao.insertConfigGroupInfo(configGroupInfo); //更新group_id specificServiceCfg.setOpTime(new Date()); specificServiceCfgDao.update(specificServiceCfg); @@ -1226,7 +1229,7 @@ public class AppCfgService extends BaseService { logger.info("app协议IP配置下发响应信息:" + result.getMsg()); configGroupInfo.setIsIssued(1); configGroupInfo.setUpdateTime(new Date()); - specificServiceCfgDao.updateConfigGroupInfobyGroupId(configGroupInfo); + configGroupInfoDao.updateConfigGroupInfobyGroupId(configGroupInfo); }else if(configGroupInfo.getIsIssued()==1) {//已经下发,分组复用下发 GroupReuseAddBean maatBean = new GroupReuseAddBean(); List groupReuseList=new ArrayList<>(); diff --git a/src/main/java/com/nis/web/service/configuration/AsnPolicyCfgService.java b/src/main/java/com/nis/web/service/configuration/AsnPolicyCfgService.java index 31bb44e1f..17eb5b66c 100644 --- a/src/main/java/com/nis/web/service/configuration/AsnPolicyCfgService.java +++ b/src/main/java/com/nis/web/service/configuration/AsnPolicyCfgService.java @@ -26,6 +26,7 @@ import com.nis.util.ConfigServiceUtil; import com.nis.util.Constants; import com.nis.web.dao.basics.AsnIpCfgDao; import com.nis.web.dao.configuration.AsnPolicyCfgDao; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.dao.specific.SpecificServiceCfgDao; import com.nis.web.security.UserUtils; import com.nis.web.service.BaseService; @@ -36,7 +37,8 @@ public class AsnPolicyCfgService extends BaseService { @Autowired private AsnIpCfgDao asnIpCfgDao; @Autowired - private SpecificServiceCfgDao specificServiceCfgDao; + //private SpecificServiceCfgDao specificServiceCfgDao; + private ConfigGroupInfoDao configGroupInfoDao; /** * @param page * @param entity @@ -96,124 +98,125 @@ public class AsnPolicyCfgService extends BaseService { * @param ids cfgId * @param functionId */ - @Transactional(readOnly=false,rollbackFor=RuntimeException.class) - public void audit(Integer isAudit,Integer isValid,Integer functionId,String id,Date auditTime){ - CfgIndexInfo entity=asnPolicyCfgDao.get(Long.valueOf(id)); - entity.setIsAudit(isAudit); - entity.setIsValid(isValid); - entity.setAuditorId(UserUtils.getUser().getId()); - entity.setAuditTime(auditTime); - asnPolicyCfgDao.updateCfgIndexInfo(entity); - ToMaatBean maatBean = new ToMaatBean(); - MaatCfg maatCfg = new MaatCfg(); - List configCompileList = new ArrayList<>(); - List groupRelationList = new ArrayList<>(); - List ipRegionList = new ArrayList<>(); - List strRegionList = new ArrayList<>(); - List numRegionList = new ArrayList<>(); - List digestRegionList = new ArrayList<>(); - List areaIpRegionList = new ArrayList<>(); - if(isAudit==Constants.AUDIT_YES) { - ConfigGroupInfo group=specificServiceCfgDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion1())); - if(group.getIsIssued()==1) {//复用,值下编译跟分组 - maatCfg.initDefaultValue(); - BeanUtils.copyProperties(entity, maatCfg); - maatCfg.setAction(entity.getAction()); - maatCfg.setAuditTime(entity.getAuditTime()); - maatCfg.setIpRegionList(ipRegionList); - maatCfg.setStrRegionList(strRegionList); - maatCfg.setNumRegionList(numRegionList); - maatCfg.setDigestRegionList(digestRegionList); - maatCfg.setGroupRelationList(groupRelationList); - maatCfg.setAreaIpRegionList(areaIpRegionList); - maatCfg.setIsValid(entity.getIsValid()); - //group - GroupCfg groupCfg=new GroupCfg(); - groupCfg.setCompileId(entity.getCompileId()); - groupCfg.setGroupId(Integer.parseInt(entity.getUserRegion1())); - groupCfg.setIsValid(Constants.VALID_YES); - groupCfg.setAuditTime(auditTime); - groupRelationList.add(groupCfg); - maatCfg.setGroupNum(groupRelationList.size()); - configCompileList.add(maatCfg); - maatBean.setConfigCompileList(configCompileList); - maatBean.setAuditTime(entity.getAuditTime()); - maatBean.setCreatorName(entity.getCurrentUser().getName()); - maatBean.setVersion(Constants.MAAT_VERSION); - maatBean.setOpAction(Constants.INSERT_ACTION); - //调用服务接口下发配置数据 - String json=gsonToJson(maatBean); - logger.info("asn策略组复用配置下发配置参数:"+json); - //调用服务接口下发配置 - ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); - logger.info("asn策略组复用配置下发响应信息:"+result.getMsg()); - }else {//首次下发 - maatCfg.initDefaultValue(); - BeanUtils.copyProperties(entity, maatCfg); - maatCfg.setAction(entity.getAction()); - maatCfg.setAuditTime(entity.getAuditTime()); - maatCfg.setStrRegionList(strRegionList); - maatCfg.setNumRegionList(numRegionList); - maatCfg.setDigestRegionList(digestRegionList); - maatCfg.setGroupRelationList(groupRelationList); - maatCfg.setAreaIpRegionList(areaIpRegionList); - maatCfg.setIsValid(entity.getIsValid()); - //group - GroupCfg groupCfg=new GroupCfg(); - groupCfg.setCompileId(entity.getCompileId()); - groupCfg.setGroupId(Integer.parseInt(entity.getUserRegion1())); - groupCfg.setIsValid(Constants.VALID_YES); - groupCfg.setAuditTime(auditTime); - groupRelationList.add(groupCfg); - maatCfg.setGroupNum(groupRelationList.size()); - //region - //查询asn group id下所有的 ip - AsnIpCfg asnIpCfg=new AsnIpCfg(); - asnIpCfg.setAsnIpGroup(Integer.parseInt(entity.getUserRegion1())); - List allAsnIpCfgs=asnIpCfgDao.findAllList(asnIpCfg); - ipRegionList.addAll(groupReuseCfgAddRemoveConvert(allAsnIpCfgs,Constants.VALID_YES,null)); - maatCfg.setIpRegionList(ipRegionList); - configCompileList.add(maatCfg); - maatBean.setConfigCompileList(configCompileList); - maatBean.setAuditTime(entity.getAuditTime()); - maatBean.setCreatorName(entity.getCurrentUser().getName()); - maatBean.setVersion(Constants.MAAT_VERSION); - maatBean.setOpAction(Constants.INSERT_ACTION); - - //调用服务接口下发配置数据 - String json=gsonToJson(maatBean); - logger.info("asn策略配置下发配置参数:"+json); - //调用服务接口下发配置 - ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); - logger.info("asn策略配置下发响应信息:"+result.getMsg()); - ConfigGroupInfo info=new ConfigGroupInfo(); - info.setIsIssued(1); - info.setGroupId(Integer.parseInt(entity.getUserRegion1())); - specificServiceCfgDao.updateConfigGroupInfobyGroupId(info); - AsnIpCfg cfg=new AsnIpCfg(); - cfg.setIsValid(Constants.VALID_YES); - cfg.setAsnIpGroup(Integer.parseInt(entity.getUserRegion1())); - asnIpCfgDao.updateIssued(cfg); - } - }else if(isAudit==Constants.AUDIT_NOT_YES) {//取消审核通过 - maatCfg.setCompileId(entity.getCompileId()); - maatCfg.setServiceId(entity.getServiceId()); - maatCfg.setIsValid(0);//无效 - configCompileList.add(maatCfg); - maatBean.setConfigCompileList(configCompileList); - maatBean.setAuditTime(entity.getAuditTime()); - maatBean.setCreatorName(entity.getCurrentUser().getName()); - maatBean.setVersion(Constants.MAAT_VERSION); - maatBean.setOpAction(Constants.UPDATE_ACTION); - //调用服务接口取消配置 - String json=gsonToJson(maatBean); - logger.info("asn策略取消下发配置参数:"+json); - //调用服务接口下发配置 - ToMaatResult result = ConfigServiceUtil.put(json,1); - logger.info("asn策略取消配置响应信息:"+result.getMsg()); - }else { - throw new RuntimeException("unknown isAudit value "+isAudit); - } - - } +// @Transactional(readOnly=false,rollbackFor=RuntimeException.class) +// @Deprecated +// public void audit(Integer isAudit,Integer isValid,Integer functionId,String id,Date auditTime){ +// CfgIndexInfo entity=asnPolicyCfgDao.get(Long.valueOf(id)); +// entity.setIsAudit(isAudit); +// entity.setIsValid(isValid); +// entity.setAuditorId(UserUtils.getUser().getId()); +// entity.setAuditTime(auditTime); +// asnPolicyCfgDao.updateCfgIndexInfo(entity); +// ToMaatBean maatBean = new ToMaatBean(); +// MaatCfg maatCfg = new MaatCfg(); +// List configCompileList = new ArrayList<>(); +// List groupRelationList = new ArrayList<>(); +// List ipRegionList = new ArrayList<>(); +// List strRegionList = new ArrayList<>(); +// List numRegionList = new ArrayList<>(); +// List digestRegionList = new ArrayList<>(); +// List areaIpRegionList = new ArrayList<>(); +// if(isAudit==Constants.AUDIT_YES) { +// ConfigGroupInfo group=configGroupInfoDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion1())); +// if(group.getIsIssued()==1) {//复用,值下编译跟分组 +// maatCfg.initDefaultValue(); +// BeanUtils.copyProperties(entity, maatCfg); +// maatCfg.setAction(entity.getAction()); +// maatCfg.setAuditTime(entity.getAuditTime()); +// maatCfg.setIpRegionList(ipRegionList); +// maatCfg.setStrRegionList(strRegionList); +// maatCfg.setNumRegionList(numRegionList); +// maatCfg.setDigestRegionList(digestRegionList); +// maatCfg.setGroupRelationList(groupRelationList); +// maatCfg.setAreaIpRegionList(areaIpRegionList); +// maatCfg.setIsValid(entity.getIsValid()); +// //group +// GroupCfg groupCfg=new GroupCfg(); +// groupCfg.setCompileId(entity.getCompileId()); +// groupCfg.setGroupId(Integer.parseInt(entity.getUserRegion1())); +// groupCfg.setIsValid(Constants.VALID_YES); +// groupCfg.setAuditTime(auditTime); +// groupRelationList.add(groupCfg); +// maatCfg.setGroupNum(groupRelationList.size()); +// configCompileList.add(maatCfg); +// maatBean.setConfigCompileList(configCompileList); +// maatBean.setAuditTime(entity.getAuditTime()); +// maatBean.setCreatorName(entity.getCurrentUser().getName()); +// maatBean.setVersion(Constants.MAAT_VERSION); +// maatBean.setOpAction(Constants.INSERT_ACTION); +// //调用服务接口下发配置数据 +// String json=gsonToJson(maatBean); +// logger.info("asn策略组复用配置下发配置参数:"+json); +// //调用服务接口下发配置 +// ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); +// logger.info("asn策略组复用配置下发响应信息:"+result.getMsg()); +// }else {//首次下发 +// maatCfg.initDefaultValue(); +// BeanUtils.copyProperties(entity, maatCfg); +// maatCfg.setAction(entity.getAction()); +// maatCfg.setAuditTime(entity.getAuditTime()); +// maatCfg.setStrRegionList(strRegionList); +// maatCfg.setNumRegionList(numRegionList); +// maatCfg.setDigestRegionList(digestRegionList); +// maatCfg.setGroupRelationList(groupRelationList); +// maatCfg.setAreaIpRegionList(areaIpRegionList); +// maatCfg.setIsValid(entity.getIsValid()); +// //group +// GroupCfg groupCfg=new GroupCfg(); +// groupCfg.setCompileId(entity.getCompileId()); +// groupCfg.setGroupId(Integer.parseInt(entity.getUserRegion1())); +// groupCfg.setIsValid(Constants.VALID_YES); +// groupCfg.setAuditTime(auditTime); +// groupRelationList.add(groupCfg); +// maatCfg.setGroupNum(groupRelationList.size()); +// //region +// //查询asn group id下所有的 ip +// AsnIpCfg asnIpCfg=new AsnIpCfg(); +// asnIpCfg.setAsnIpGroup(Integer.parseInt(entity.getUserRegion1())); +// List allAsnIpCfgs=asnIpCfgDao.findAllList(asnIpCfg); +// ipRegionList.addAll(groupReuseCfgAddRemoveConvert(allAsnIpCfgs,Constants.VALID_YES,null)); +// maatCfg.setIpRegionList(ipRegionList); +// configCompileList.add(maatCfg); +// maatBean.setConfigCompileList(configCompileList); +// maatBean.setAuditTime(entity.getAuditTime()); +// maatBean.setCreatorName(entity.getCurrentUser().getName()); +// maatBean.setVersion(Constants.MAAT_VERSION); +// maatBean.setOpAction(Constants.INSERT_ACTION); +// +// //调用服务接口下发配置数据 +// String json=gsonToJson(maatBean); +// logger.info("asn策略配置下发配置参数:"+json); +// //调用服务接口下发配置 +// ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); +// logger.info("asn策略配置下发响应信息:"+result.getMsg()); +// ConfigGroupInfo info=new ConfigGroupInfo(); +// info.setIsIssued(1); +// info.setGroupId(Integer.parseInt(entity.getUserRegion1())); +// configGroupInfoDao.updateConfigGroupInfobyGroupId(info); +// AsnIpCfg cfg=new AsnIpCfg(); +// cfg.setIsValid(Constants.VALID_YES); +// cfg.setAsnIpGroup(Integer.parseInt(entity.getUserRegion1())); +// asnIpCfgDao.updateIssued(cfg); +// } +// }else if(isAudit==Constants.AUDIT_NOT_YES) {//取消审核通过 +// maatCfg.setCompileId(entity.getCompileId()); +// maatCfg.setServiceId(entity.getServiceId()); +// maatCfg.setIsValid(0);//无效 +// configCompileList.add(maatCfg); +// maatBean.setConfigCompileList(configCompileList); +// maatBean.setAuditTime(entity.getAuditTime()); +// maatBean.setCreatorName(entity.getCurrentUser().getName()); +// maatBean.setVersion(Constants.MAAT_VERSION); +// maatBean.setOpAction(Constants.UPDATE_ACTION); +// //调用服务接口取消配置 +// String json=gsonToJson(maatBean); +// logger.info("asn策略取消下发配置参数:"+json); +// //调用服务接口下发配置 +// ToMaatResult result = ConfigServiceUtil.put(json,1); +// logger.info("asn策略取消配置响应信息:"+result.getMsg()); +// }else { +// throw new RuntimeException("unknown isAudit value "+isAudit); +// } +// +// } } diff --git a/src/main/java/com/nis/web/service/configuration/IpCfgService.java b/src/main/java/com/nis/web/service/configuration/IpCfgService.java index b8e830893..3f357e171 100644 --- a/src/main/java/com/nis/web/service/configuration/IpCfgService.java +++ b/src/main/java/com/nis/web/service/configuration/IpCfgService.java @@ -41,7 +41,7 @@ import com.nis.web.dao.basics.AsnIpCfgDao; import com.nis.web.dao.configuration.AreaIpCfgDao; import com.nis.web.dao.configuration.IpCfgDao; import com.nis.web.dao.configuration.StringCfgDao; -import com.nis.web.dao.specific.SpecificServiceCfgDao; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.security.UserUtils; import com.nis.web.service.CrudService; @@ -59,7 +59,8 @@ public class IpCfgService extends CrudService { @Autowired protected AreaIpCfgDao areaIpCfgDao; @Autowired - protected SpecificServiceCfgDao specificServiceCfgDao; + //protected SpecificServiceCfgDao specificServiceCfgDao; + protected ConfigGroupInfoDao configGroupInfoDao; @Autowired protected AsnIpCfgDao asnIpCfgDao; /** @@ -731,7 +732,7 @@ public class IpCfgService extends CrudService { List digestRegionList = new ArrayList<>(); List areaIpRegionList = new ArrayList<>(); if(isAudit==Constants.AUDIT_YES) { - ConfigGroupInfo group=specificServiceCfgDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4())); + ConfigGroupInfo group=configGroupInfoDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4())); if(group.getIsIssued()==1) {//复用,只下编译跟分组 maatCfg.initDefaultValue(); BeanUtils.copyProperties(entity, maatCfg); @@ -810,9 +811,10 @@ public class IpCfgService extends CrudService { ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); logger.info("ipaddr asn配置下发响应信息:"+result.getMsg()); ConfigGroupInfo info=new ConfigGroupInfo(); + BeanUtils.copyProperties(group, info); info.setIsIssued(1); - info.setGroupId(Integer.parseInt(entity.getUserRegion4())); - specificServiceCfgDao.updateConfigGroupInfobyGroupId(info); + //info.setGroupId(Integer.parseInt(entity.getUserRegion4())); + configGroupInfoDao.updateConfigGroupInfobyGroupId(info); AsnIpCfg cfg=new AsnIpCfg(); cfg.setIsValid(Constants.VALID_YES); cfg.setAsnIpGroup(Integer.parseInt(entity.getUserRegion4())); @@ -907,7 +909,7 @@ public class IpCfgService extends CrudService { entity.setIpPortList(ipPortList); entity.setNtcSubscribeIdCfgList(subscribeIdList); if(StringUtils.isNotBlank(entity.getUserRegion4())) { - ConfigGroupInfo info=specificServiceCfgDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4().trim())); + ConfigGroupInfo info=configGroupInfoDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4().trim())); entity.setAsnIpGroupName(info.getGroupName()); } return entity; @@ -916,7 +918,7 @@ public class IpCfgService extends CrudService { List ipPortList = ipCfgDao.getIpPortList(entity); entity.setIpPortList(ipPortList); if(StringUtils.isNotBlank(entity.getUserRegion4())) { - ConfigGroupInfo info=specificServiceCfgDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4().trim())); + ConfigGroupInfo info=configGroupInfoDao.getConfigGroupInfoByGroupId(Integer.parseInt(entity.getUserRegion4().trim())); entity.setAsnIpGroupName(info.getGroupName()); } return entity; diff --git a/src/main/java/com/nis/web/service/specific/ConfigGroupInfoService.java b/src/main/java/com/nis/web/service/specific/ConfigGroupInfoService.java new file mode 100644 index 000000000..f635f9ec1 --- /dev/null +++ b/src/main/java/com/nis/web/service/specific/ConfigGroupInfoService.java @@ -0,0 +1,36 @@ +package com.nis.web.service.specific; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nis.domain.specific.ConfigGroupInfo; +import com.nis.web.dao.specific.ConfigGroupInfoDao; +import com.nis.web.service.BaseService; +@Service +public class ConfigGroupInfoService extends BaseService{ + @Autowired + private ConfigGroupInfoDao configGroupInfoDao; + + public List findAllList(int groupType){ + return configGroupInfoDao.findAllList(groupType); + } + public ConfigGroupInfo insertConfigGroupInfo(ConfigGroupInfo entity) { + configGroupInfoDao.insertConfigGroupInfo(entity); + return entity; + } + public ConfigGroupInfo updateConfigGroupInfobyGroupId(ConfigGroupInfo entity) { + configGroupInfoDao.updateConfigGroupInfobyGroupId(entity); + return entity; + } + public ConfigGroupInfo getConfigGroupInfoByGroupId(Integer groupId) { + return configGroupInfoDao.getConfigGroupInfoByGroupId(groupId); + } + public ConfigGroupInfo getInfoByAsnNo(Long asnNo) { + return configGroupInfoDao.getInfoByAsnNo(asnNo); + } + public Integer getIssuedConfigGroupInfoByGroupIds(String groupIds) { + return configGroupInfoDao.getIssuedConfigGroupInfoByGroupIds(groupIds); + } +} diff --git a/src/main/java/com/nis/web/service/specific/SpecificServiceCfgService.java b/src/main/java/com/nis/web/service/specific/SpecificServiceCfgService.java index 74dd87a42..f296c84a0 100644 --- a/src/main/java/com/nis/web/service/specific/SpecificServiceCfgService.java +++ b/src/main/java/com/nis/web/service/specific/SpecificServiceCfgService.java @@ -11,8 +11,10 @@ import com.beust.jcommander.internal.Lists; import com.nis.domain.Page; import com.nis.domain.specific.ConfigGroupInfo; import com.nis.domain.specific.SpecificServiceCfg; +import com.nis.util.AsnCacheUtils; import com.nis.util.ConfigServiceUtil; import com.nis.util.Constants; +import com.nis.web.dao.specific.ConfigGroupInfoDao; import com.nis.web.dao.specific.SpecificServiceCfgDao; import com.nis.web.service.BaseService; @@ -21,6 +23,8 @@ public class SpecificServiceCfgService extends BaseService{ @Autowired private SpecificServiceCfgDao specificServiceCfgDao; + @Autowired + private ConfigGroupInfoDao configGroupInfoDao; /** * 根据id查询对象 @@ -76,14 +80,14 @@ public class SpecificServiceCfgService extends BaseService{ specificServiceCfg.setGroupId(groupId); } //新增协议分组 - ConfigGroupInfo group = specificServiceCfgDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); + ConfigGroupInfo group = configGroupInfoDao.getConfigGroupInfoByGroupId(specificServiceCfg.getGroupId()); if(group==null){ group = new ConfigGroupInfo(); group.setGroupId(specificServiceCfg.getGroupId()); group.setGroupName(specificServiceCfg.getSpecServiceName()); group.setIsIssued(0); group.setGroupType(1); - specificServiceCfgDao.insertConfigGroupInfo(group); + configGroupInfoDao.insertConfigGroupInfo(group); } if(specificServiceCfg.getSpecServiceId()==null){//新增 if(specificServiceCfg.getIsLeaf()==null){ diff --git a/src/main/resources/cache/ehcache-local.xml b/src/main/resources/cache/ehcache-local.xml index 171114789..d7353f5fe 100644 --- a/src/main/resources/cache/ehcache-local.xml +++ b/src/main/resources/cache/ehcache-local.xml @@ -27,5 +27,6 @@ - + + \ No newline at end of file diff --git a/src/main/resources/cache/ehcache-rmi.xml b/src/main/resources/cache/ehcache-rmi.xml index 2c093c0d3..7962a5da6 100644 --- a/src/main/resources/cache/ehcache-rmi.xml +++ b/src/main/resources/cache/ehcache-rmi.xml @@ -55,5 +55,6 @@ properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true "/> --> - + + \ No newline at end of file diff --git a/src/main/resources/nis.properties b/src/main/resources/nis.properties index bc7d57abd..b63442288 100644 --- a/src/main/resources/nis.properties +++ b/src/main/resources/nis.properties @@ -545,4 +545,8 @@ cert_validate_success_info=Successful ipv4_ip_range_regexp_new=^(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)-(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$ ipv4_ip_subnet_regexp_original=^(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(0|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)/(3[0-2]|1[0-9]|2[0-9]|[0-9])$ #\u57df\u540d\u9a8c\u8bc1\u6b63\u5219 -domain_regexp=^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$ \ No newline at end of file +domain_regexp=^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$ +#IP复用maat json中的ip region单次send 最大个数 +maat_json_send_size=2000 +#导入验证的线程数量 +mulity_thread_size=5 \ No newline at end of file diff --git a/src/main/resources/spring-mvc.xml b/src/main/resources/spring-mvc.xml index fd5a1bea0..26e66edbb 100644 --- a/src/main/resources/spring-mvc.xml +++ b/src/main/resources/spring-mvc.xml @@ -247,7 +247,7 @@ - + --%> + <%-- --%> @@ -274,8 +275,8 @@ ${cfg.cfgDesc } - ${cfg.asnIpGroupName } - + ${cfg.asnIpGroup } + ${cfg.userRegion1} diff --git a/src/main/webapp/static/pages/scripts/appNames.js b/src/main/webapp/static/pages/scripts/appNames.js index de12b64c6..6a37ed096 100644 --- a/src/main/webapp/static/pages/scripts/appNames.js +++ b/src/main/webapp/static/pages/scripts/appNames.js @@ -4,7 +4,7 @@ $(function(){ if($(this).attr("id")&&$(this).attr("id")!=''){ ids.push($(this).attr("id")); } - ids.push($(this).attr("id")); + //ids.push($(this).attr("id")); }); $(".behavCode").each(function(){ if($(this).attr("id")&&$(this).attr("id")!=''){ @@ -32,4 +32,26 @@ $(function(){ } }); } + var asnNos=[]; + $(".configGroup").each(function(){ + if($(this).attr("asnNo")&&$(this).attr("asnNo")!=''){ + asnNos.push($(this).attr("asnNo")); + } + }); + if(asnNos.length>0){ + var pathName=window.document.location.pathname.substring(0,window.document.location.pathname.indexOf("/nis")+4); + $.ajax({ + type:'post', + async:false, + url:pathName+'/basics/asn/ajaxGroupName', + data:{"ids":asnNos.join(",")}, + success:function(data){ + if(data){ + for(var ind in data){ + $("[class='configGroup'][asnNo='"+ind+"']").html(data[ind]); + } + } + } + }); + } }); \ No newline at end of file