package com.nis.web.controller;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jets3t.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
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.basics.ServiceDictInfo;
import com.nis.domain.basics.SysDictInfo;
import com.nis.domain.configuration.AreaBean;
import com.nis.domain.configuration.AreaIpCfg;
import com.nis.domain.configuration.BaseCfg;
import com.nis.domain.configuration.BaseIpCfg;
import com.nis.domain.configuration.ComplexStringCfgTemplate;
import com.nis.domain.configuration.IpCfgTemplate;
import com.nis.domain.configuration.RequestInfo;
import com.nis.domain.configuration.StringCfgTemplate;
import com.nis.domain.log.BaseLogEntity;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Configurations;
//import com.nis.main.ConvertTool;
import com.nis.util.Constants;
import com.nis.util.DateUtils;
import com.nis.util.DictUtils;
import com.nis.util.JsonMapper;
import com.nis.util.excel.ExportExcel;
import com.nis.web.service.ArchiveServcie;
import com.nis.web.service.AreaService;
import com.nis.web.service.DictService;
import com.nis.web.service.LogService;
import com.nis.web.service.MenuService;
import com.nis.web.service.OfficeService;
import com.nis.web.service.RoleService;
import com.nis.web.service.SystemService;
import com.nis.web.service.UserService;
import com.nis.web.service.basics.PolicyGroupInfoService;
import com.nis.web.service.basics.ServiceDictInfoService;
import com.nis.web.service.basics.SysDictInfoService;
import com.nis.web.service.configuration.AppCfgService;
import com.nis.web.service.configuration.AvCfgService;
import com.nis.web.service.configuration.AvContentCfgService;
import com.nis.web.service.configuration.BgpCfgService;
import com.nis.web.service.configuration.ComplexStringCfgService;
import com.nis.web.service.configuration.ControlPolicyService;
import com.nis.web.service.configuration.DdosCfgService;
import com.nis.web.service.configuration.DnsIpCfgService;
import com.nis.web.service.configuration.DnsResStrategyService;
import com.nis.web.service.configuration.DomainService;
import com.nis.web.service.configuration.FileTransferCfgService;
import com.nis.web.service.configuration.HttpRedirectCfgService;
import com.nis.web.service.configuration.IpCfgService;
import com.nis.web.service.configuration.IpMultiplexPoolCfgService;
import com.nis.web.service.configuration.MailCfgService;
import com.nis.web.service.configuration.NumCfgService;
import com.nis.web.service.configuration.RequestInfoService;
import com.nis.web.service.configuration.WebsiteCfgService;
import com.nis.web.service.configuration.XmppCfgService;
import com.nis.web.service.specific.SpecificServiceCfgService;
import com.nis.web.service.specific.SpecificServiceHostCfgService;
import com.nis.web.service.systemService.ServiceConfigInfoService;
public class BaseController {
@Autowired
protected UserService userService;
@Autowired
protected SystemService systemService;
@Autowired
protected OfficeService officeService;
@Autowired
protected AreaService areaService;
@Autowired
protected RoleService roleService;
@Autowired
protected MenuService menuService;
@Autowired
protected LogService logService;
@Autowired
protected ArchiveServcie archiveServcie;
@Autowired
protected DictService dictService;
@Autowired
protected IpCfgService ipCfgService;
@Autowired
protected DnsIpCfgService dnsIpCfgService;
@Autowired
protected MailCfgService mailCfgService;
@Autowired
protected FileTransferCfgService fileTransferCfgService;
@Autowired
protected AppCfgService appCfgService;
@Autowired
protected ControlPolicyService controlPolicyService;
@Autowired
protected ServiceDictInfoService serviceDictInfoService;
@Autowired
protected SysDictInfoService sysDictInfoService;
protected final Logger logger = Logger.getLogger(this.getClass());
@Autowired
protected ServiceConfigInfoService serviceConfigInfoService;
@Autowired
protected RequestInfoService requestInfoService;
@Autowired
protected DomainService domainService;
@Autowired
protected NumCfgService numCfgService;
@Autowired
protected ComplexStringCfgService complexStringCfgService;
@Autowired
protected SpecificServiceCfgService specificServiceCfgService;
@Autowired
protected SpecificServiceHostCfgService specificServiceHostCfgService;
@Autowired
protected AvCfgService avCfgService;
@Autowired
protected AvContentCfgService avContentCfgService;
@Autowired
protected WebsiteCfgService websiteCfgService;
@Autowired
protected BgpCfgService bgpCfgService;
@Autowired
protected XmppCfgService xmppCfgService;
@Autowired
protected PolicyGroupInfoService policyGroupInfoService;
@Autowired
protected DnsResStrategyService dnsResStrategyService;
@Autowired
protected HttpRedirectCfgService httpRedirectCfgService;
@Autowired
protected DdosCfgService ddosCfgService;
@Autowired
protected IpMultiplexPoolCfgService ipMultiplexPoolCfgService;
/**
* 管理基础路径
*/
@Value("${adminPath}")
protected String adminPath;
/**
* 前端基础路径
*/
@Value("${frontPath}")
protected String frontPath;
/**
* 前端URL后缀
*/
@Value("${urlSuffix}")
protected String urlSuffix;
/**
* 客户端返回JSON字符串
* @param response
* @param object
* @return
*/
protected String renderString(HttpServletResponse response, Object object) {
return renderString(response, JsonMapper.toJsonString(object), "application/json");
}
/**
* 客户端返回字符串
* @param response
* @param string
* @return
*/
protected String renderString(HttpServletResponse response, String string, String type) {
try {
response.reset();
response.setContentType(type);
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
return null;
} catch (IOException e) {
return null;
}
}
/**
* 添加Model消息
* @param message
*/
protected void addMessage(Model model, String... messages) {
StringBuilder sb = new StringBuilder();
for (String message : messages){
sb.append(message).append(messages.length>1?"
":"");
}
model.addAttribute("message", sb.toString());
}
/**
* 添加Flash消息
* @param message
*/
protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
StringBuilder sb = new StringBuilder();
for (String message : messages){
sb.append(message).append(messages.length>1?"
":"");
}
redirectAttributes.addFlashAttribute("message", sb.toString());
}
/**
* 初始化数据绑定
* 1. 将所有传递进来的String进行HTML编码,防止XSS攻击
* 2. 将字段中Date类型转换为String类型
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
// String类型转换,将所有传递进来的String进行HTML编码,防止XSS攻击
binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
}
@Override
public String getAsText() {
Object value = getValue();
return value != null ? value.toString() : "";
}
});
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateUtils.parseDate(text));
}
});
}
protected void initPageCondition(Model model){
List requestInfos=requestInfoService.getAllRequestInfo();
model.addAttribute("requestInfos", requestInfos);
List fls=serviceDictInfoService.findAllFlDict();
model.addAttribute("fls", fls);
List xzs=serviceDictInfoService.findAllXzDict();
model.addAttribute("xzs", xzs);
List lables=serviceDictInfoService.findAllLableDict();
model.addAttribute("lables", lables);
}
protected void initPageCondition(Model model,BaseCfg cfg){
List requestInfos=requestInfoService.getAllRequestInfo();
model.addAttribute("requestInfos", requestInfos);
List fls=serviceDictInfoService.findAllFlDict();
model.addAttribute("fls", fls);
List xzs=serviceDictInfoService.findAllXzDict();
model.addAttribute("xzs", xzs);
List lables=serviceDictInfoService.findAllLableDict();
model.addAttribute("lables", lables);
List regionList = DictUtils.getFunctionRegionDictList(cfg.getFunctionId());
model.addAttribute("regionList", regionList);
List serviceList = DictUtils.getFunctionServiceDictList(cfg.getFunctionId());
model.addAttribute("serviceList", serviceList);
}
protected void initFormCondition(Model model){
List requestInfos=requestInfoService.getValidRequestInfo();
model.addAttribute("requestInfos", requestInfos);
List fls=serviceDictInfoService.findFlDict();
model.addAttribute("fls", fls);
List xzs=serviceDictInfoService.findXzDict();
model.addAttribute("xzs", xzs);
List lables=serviceDictInfoService.findLableDict();
model.addAttribute("lables", lables);
List itTypeList=new ArrayList();
itTypeList.add(Constants.ITEM_TYPE_AREA);
List areas = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("areas", areas);
itTypeList.clear();
itTypeList.add(Constants.ITEM_TYPE_ISP);
List isps = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("isps", isps);
//初始化一个默认的地域ip给界面
AreaIpCfg areaIpCfg=new AreaIpCfg();
areaIpCfg.initDefaultValue();
model.addAttribute("_areaCfg", areaIpCfg);
}
protected void initFormCondition(Model model,BaseCfg cfg){
List requestInfos=requestInfoService.getValidRequestInfo();
model.addAttribute("requestInfos", requestInfos);
List fls=serviceDictInfoService.findFlDict();
model.addAttribute("fls", fls);
List xzs=serviceDictInfoService.findXzDict();
model.addAttribute("xzs", xzs);
List lables=serviceDictInfoService.findLableDict();
model.addAttribute("lables", lables);
List itTypeList=new ArrayList();
itTypeList.add(Constants.ITEM_TYPE_AREA);
List areas = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("areas", areas);
itTypeList.clear();
itTypeList.add(Constants.ITEM_TYPE_ISP);
List isps = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("isps", isps);
//初始化一个默认的地域ip给界面
AreaIpCfg areaIpCfg=new AreaIpCfg();
areaIpCfg.initDefaultValue();
model.addAttribute("_areaCfg", areaIpCfg);
List regionList = DictUtils.getFunctionRegionDictList(cfg.getFunctionId());
model.addAttribute("regionList", regionList);
List serviceList = DictUtils.getFunctionServiceDictList(cfg.getFunctionId());
model.addAttribute("serviceList", serviceList);
//ip配置设置protocolId
if(cfg instanceof BaseIpCfg){
for(FunctionServiceDict dict:serviceList){
if(cfg.getFunctionId().intValue()==dict.getFunctionId().intValue()){
((BaseIpCfg) cfg).setProtocolId(dict.getProtocolId().intValue());
break;
}
}
}
}
protected void initUpdateFormCondition(Model model,BaseCfg cfg){
List itTypeList=new ArrayList();
itTypeList.add(Constants.ITEM_TYPE_AREA);
List areas = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("areas", areas);
itTypeList.clear();
itTypeList.add(Constants.ITEM_TYPE_ISP);
List isps = sysDictInfoService.findAllSysDictInfo(new SysDictInfo(),itTypeList,null);
model.addAttribute("isps", isps);
//查询配置对应的地域ip
List areaCfgs=ipCfgService.getAreaCfgByCompileId(cfg.getCompileId());
model.addAttribute("areaCfgs", areaCfgs);
StringBuffer areaCfgIds=new StringBuffer();
for(AreaIpCfg areaCfg:areaCfgs){
areaCfgIds.append(areaCfg.getCfgId()+",");
}
//update时判断是否有删除地域ip
model.addAttribute("areaCfgIds", areaCfgIds.toString());
//初始化一个默认的地域ip给界面
AreaIpCfg areaIpCfg=new AreaIpCfg();
areaIpCfg.initDefaultValue();
model.addAttribute("_areaCfg", areaIpCfg);
//从区域生效ID中获取区域与运营商
String areaEffectiveIds=cfg.getAreaEffectiveIds();
List areaIspList=new ArrayList();
getAreaIsps(areaEffectiveIds,areaIspList,isps,areas);
model.addAttribute("_areaIsps", areaIspList);
List requestInfos=requestInfoService.getValidRequestInfo();//只查询有效的
model.addAttribute("requestInfos", requestInfos);
List fls=serviceDictInfoService.findFlDict();//只查询有效分类字典
model.addAttribute("fls", fls);
List xzs=serviceDictInfoService.findXzDict();//只查询有效性质字典
model.addAttribute("xzs", xzs);
List lables=serviceDictInfoService.findLableDict();//只查询有效标签字典
model.addAttribute("lables", lables);
List regionList = DictUtils.getFunctionRegionDictList(cfg.getFunctionId());
model.addAttribute("regionList", regionList);
List serviceList = DictUtils.getFunctionServiceDictList(cfg.getFunctionId());
model.addAttribute("serviceList", serviceList);
}
public List getAreaIsps(String areaEffectiveIds,List areaIspList,List isps,List areas) {
Map areaMap=new HashMap();
for (SysDictInfo areaDict : areas) {
areaMap.put(areaDict.getItemCode(), areaDict);
}
Map ispMap=new HashMap();
for (SysDictInfo ispDict : isps) {
ispMap.put(ispDict.getItemCode(), ispDict);
}
// : 存在冒号,则冒号前为area后为isp;不存在冒号,判断id是否为isp,否则为area
if(StringUtils.isNotBlank(areaEffectiveIds)){
String[] areaIsps=areaEffectiveIds.split(",");
for(String areaIsp:areaIsps){
AreaBean area=new AreaBean();
if(areaIsp.contains(":")){
area.setArea(areaIsp.split(":")[0]);
area.setIsp(areaIsp.split(":")[1]);
area.setAreaName(areaMap.get(areaIsp.split(":")[0]).getItemValue());
area.setIspName(ispMap.get(areaIsp.split(":")[1]).getItemValue());
}else{
if(ispMap.keySet().contains(areaIsp)){
area.setIsp(areaIsp);
area.setIspName(ispMap.get(areaIsp).getItemValue());
area.setAreaName("");
}else{
area.setArea(areaIsp);
area.setIspName("");
area.setAreaName(areaMap.get(areaIsp).getItemValue());
}
}
areaIspList.add(area);
}
}
return areaIspList;
}
/**
* 获取国际化配置文件
* @return
*/
public Properties getMsgProp(){
Properties msgProp = new Properties();
try {
String language = LocaleContextHolder.getLocale().getLanguage();
if(language.equals("zh_cn")||language.equals("zh")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_zh_CN.properties"));
}else if(language.equals("ru")){
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_ru.properties"));
}else{
msgProp.load(Configurations.class.getResourceAsStream("/messages/message_en.properties"));
}
} catch (Exception e) {
msgProp = null;
logger.error("未知i18n消息配置文件,请确定文件是否存在!",e);
}
return msgProp;
}
public void importCfgTemplate(HttpServletRequest request,HttpServletResponse response,
RedirectAttributes redirectAttributes,Integer functionId,Integer cfgRegionCode){
Properties msgProp = getMsgProp();
try {
List dictList = DictUtils.getFunctionRegionDictList(functionId);
for(FunctionRegionDict dict:dictList){
if(dict.getConfigRegionCode()==cfgRegionCode){
String fileName = dict.getConfigRegionValue()+".xlsx";
if(dict.getRegionType()==1){
List list = Lists.newArrayList();
list.add(new IpCfgTemplate());
new ExportExcel(msgProp,null, IpCfgTemplate.class, 2).setDataList(msgProp,list,null).
write(request,response, fileName).dispose();
}else if(dict.getRegionType()==2){
List list = Lists.newArrayList();
list.add(new StringCfgTemplate());
new ExportExcel(msgProp,null, StringCfgTemplate.class, 2).setDataList(msgProp,list,null).
write(request,response, fileName).dispose();
}else if(dict.getRegionType()==3){
List list = Lists.newArrayList();
list.add(new ComplexStringCfgTemplate());
new ExportExcel(msgProp,null, ComplexStringCfgTemplate.class, 2).setDataList(msgProp,list,null).
write(request,response, fileName).dispose();
}
}
}
} catch (Exception e) {
msgProp = null;
e.printStackTrace();
}
}
public void setLogAction(BaseLogEntity log) {
List serviceList = DictUtils.getFunctionServiceDictList(log.getFunctionId());
for (FunctionServiceDict dict : serviceList) {
if (dict.getServiceId().intValue() == log.getService().intValue()) {
log.setAction(dict.getAction());
}
}
}
//初始化查询值判断
public void initLogSearchValue(BaseLogEntity entry,Map params){
if (StringUtils.isNotBlank(entry.getSearchFoundStartTime())&&StringUtils.isNotBlank(entry.getSearchFoundEndTime())) {
params.put("searchFoundStartTime", entry.getSearchFoundStartTime());
params.put("searchFoundEndTime", entry.getSearchFoundEndTime());
}else{
//设置默认查询当前时间及前五分钟
String endTime = DateUtils.getDateTime();
Date dateStart = new Date(new Date().getTime() - Constants.LOG_TIME_RANGE);
String startTime = DateUtils.formatDateTime(dateStart);
params.put("searchFoundStartTime", startTime);
params.put("searchFoundEndTime",endTime );
entry.setSearchFoundStartTime(startTime);
entry.setSearchFoundEndTime(endTime);
}
if (StringUtils.isNotBlank(entry.getTransProto())) {
params.put("searchTransProto", entry.getTransProto());
}
if (entry.getService()!=null) {
params.put("searchService", entry.getService());
}
if (StringUtils.isNotBlank(entry.getdIp())) {
params.put("searchDIp", entry.getdIp());
}
if (StringUtils.isNotBlank(entry.getsIp())) {
params.put("searchSIp", entry.getsIp());
}
if (StringUtils.isNotBlank(entry.getCapIp())) {
params.put("searchCapIp", entry.getCapIp());
}
if (entry.getDirection()!=null) {
params.put("searchDirection", entry.getDirection());
}
if (entry.getEntranceId()!=null) {
params.put("searchEntranceId", entry.getEntranceId());
}
if (entry.getCfgId()!=null) {
params.put("searchCfgId", entry.getCfgId());
}
}
public void checkIpCfg(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");
String specialItem=null;
for(SysDataDictionaryItem sfuncItem:specialFunctionIdList){
if(functionId==Integer.parseInt(sfuncItem.getItemCode())){
specialItem=sfuncItem.getItemValue();
}
}
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();
String ipTypeString=null;
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())){
ipTypeString=ipTypeItem.getItemValue();
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();
String ipPatternString=null;
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())){
ipPatternString=ipPatternItem.getItemValue();
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=checkIP(prop,prop.getProperty("client_ip"),srcIp,ipTypeString,ipPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//client_ip check end
//server_ip check start
String destIp=value.getDestIpAddress();
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,ipTypeString,ipPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//server_ip check end
//port_pattern check start
Integer portPattern=value.getPortPattern();
String portPatternString=null;
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())){
portPatternString=portPatternItem.getItemValue();
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();
_msg=checkPort(prop,prop.getProperty("client_port"), srcPort, portPatternString);
if(StringUtils.isNotBlank(_msg)){
errInfo.append(_msg);
}
//src_port check end
//dest_port check start
String destPort=value.getDestPort();
_msg=checkPort(prop,prop.getProperty("server_port"), destPort, portPatternString);
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)){
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("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){
errInfo.append(String.format(prop.getProperty("can_not_null"), prop.getProperty("letter"))+";");
}else{//查询数据库是否存在ID
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"))+";");
}
}
//attribute check end
if(StringUtils.isNotBlank(errInfo.toString())){
msg.append(lineStart).append(errInfo);
if(line");
}
}
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
*/
public static String checkIP(Properties prop,String ipName,String ip,String ipType,String ipPattern){
StringBuffer msg=new StringBuffer();
if(StringUtils.isBlank(ip)){
msg.append(String.format(prop.getProperty("can_not_null"), ipName)+";");
}else{
if("ipv4".equals(ipType)){
if("ip_subnet".equals(ipPattern)){
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("ip_range".equals(ipPattern)){
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("ip".equals(ipPattern)){
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("ipv6".equals(ipType)){
if("ip_subnet".equals(ipPattern)){
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("ip_range".equals(ipPattern)){
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("ip".equals(ipPattern)){
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();
}
public static String checkPort(Properties prop,String portName,String port,String portPattern){
StringBuffer msg=new StringBuffer();
if(StringUtils.isBlank(port)){
msg.append(String.format(prop.getProperty("can_not_null"), portName)+";");
}else{
if("port".equals(portPattern)){
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("port_mask".equals(portPattern)){
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();
}
}