This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/controller/basics/IpCommGroupController.java
2019-06-03 14:53:39 +08:00

479 lines
22 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.controller.basics;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jets3t.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
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.Page;
import com.nis.domain.basics.CommonGroupInfo;
import com.nis.domain.basics.IpCommCfg;
import com.nis.domain.basics.PolicyGroupInfo;
import com.nis.domain.configuration.BaseIpCfg;
import com.nis.domain.configuration.CfgIndexInfo;
import com.nis.domain.configuration.IpPortCfg;
import com.nis.domain.configuration.template.IpCommCfgTemplate;
import com.nis.domain.specific.ConfigGroupInfo;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Constants;
import com.nis.util.DictUtils;
import com.nis.util.StringUtil;
import com.nis.util.excel.ImportBigExcel;
import com.nis.web.controller.BaseController;
import com.nis.web.security.UserUtils;
import com.nis.web.service.basics.IpCommGroupCfgService;
@Controller
@RequestMapping(value = "${adminPath}/basics/ip")
public class IpCommGroupController extends BaseController {
@Autowired
private IpCommGroupCfgService ipCommGroupCfgService;
@RequestMapping(value = { "/list" })
public String list(Model model, @ModelAttribute("cfg") IpCommCfg entity, HttpServletRequest request, HttpServletResponse response) {
Page<IpCommCfg> page = ipCommGroupCfgService.findPage(new Page<IpCommCfg>(request, response, "r"),entity);
List<FunctionRegionDict> regionList = DictUtils.getFunctionRegionDictList(entity.getFunctionId());
List<FunctionServiceDict> serviceList = DictUtils.getFunctionServiceDictList(entity.getFunctionId());
List<CommonGroupInfo> policyGroupInfos=commonGroupManageService.findCommonGroupInfosByType(5);
model.addAttribute("page", page);
model.addAttribute("regionList", regionList);
model.addAttribute("serviceList", serviceList);
model.addAttribute("policyGroupInfos", policyGroupInfos);
return "/basics/ipCommonGroupCfgList";
}
@RequestMapping(value = { "/addForm" })
public String addForm(Model model, @ModelAttribute("cfg") CfgIndexInfo cfg, RedirectAttributes redirectAttributes,
HttpServletRequest request, HttpServletResponse response) {
// IP Group Tyep为5
cfg.setGroupType(5);
initFormCondition(model, cfg);
List<CommonGroupInfo> groupInfos=commonGroupManageService.findCommonGroupInfosByType(5);
model.addAttribute("policyGroupInfos", groupInfos);
model.addAttribute("_cfg", cfg);
return "/basics/ipCommGroupFormAdd";
}
@RequestMapping(value = { "/save" })
@RequiresPermissions(value = { "ip:common:config" })
public String save(Model model, @ModelAttribute("cfg") CfgIndexInfo cfg, RedirectAttributes redirectAttributes,
HttpServletRequest request, HttpServletResponse response) {
try {
ipCommGroupCfgService.saveIpCommGroupCfg(cfg);
} catch (Exception e) {
logger.error("信息保存失败", e);
if (e instanceof MaatConvertException) {
addMessage(redirectAttributes, "error", e.getMessage());
} else {
addMessage(redirectAttributes, "error", "save_failed");
}
}
return "redirect:" + adminPath + "/basics/ip/list?functionId=" + cfg.getFunctionId();
}
@RequestMapping(value = { "/updateForm" })
public String updateForm(Model model, @ModelAttribute("cfg") IpCommCfg cfg, RedirectAttributes redirectAttributes, String ids,
HttpServletRequest request, HttpServletResponse response) {
cfg = ipCommGroupCfgService.get(Long.parseLong(ids));
// initUpdateFormCondition(model, cfg);
cfg.setGroupType(5);
initUpdateFormCondition(model, cfg);
List<CommonGroupInfo> groupInfos=commonGroupManageService.findCommonGroupInfosByType(5);
model.addAttribute("policyGroupInfos", groupInfos);
model.addAttribute("_cfg", cfg);
return "/basics/ipCommGroupFormUpdate";
}
@RequestMapping(value = { "/update" })
@RequiresPermissions(value = { "ip:common:config" })
public String update(Model model, HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cfg") IpCommCfg cfg, RedirectAttributes redirectAttributes) {
try {
ipCommGroupCfgService.update(cfg);
addMessage(redirectAttributes, "success", "save_success");
} catch (Exception e) {
logger.error("信息保存失败", e);
addMessage(redirectAttributes, "error", "save_failed");
}
return "redirect:" + adminPath + "/basics/ip/list?functionId=" + cfg.getFunctionId();
}
// @RequestMapping(value = { "/audit" })
// @RequiresPermissions(value = { "ip:common:config" })
// public String audit(Model model, @ModelAttribute("cfg") IpCommCfg cfg, Integer isAudit, Integer isValid,
// String ids, Integer functionId, HttpServletRequest request, HttpServletResponse response,
// RedirectAttributes redirectAttributes) {
// // 选中配置审核
// if (!StringUtil.isEmpty(ids)) {
// List<IpCommCfg> ipGroupCfgs = ipCommGroupCfgService.getByIds(ids);
// List<IpCommCfg> temp = Lists.newArrayList();
// try {
// ipCommGroupCfgService.audit(ipGroupCfgs, isAudit, isValid);
// } catch (Exception e) {
// logger.error("审核失败", e);
// addMessage(redirectAttributes, "error", "audit_failed");
// } finally {
// temp.clear();
// }
// return "redirect:" + adminPath + "/basics/ip/list?functionId=" + cfg.getFunctionId();
// } else {// 全部审核
// // 条件下所有配置审核
// Page<IpCommCfg> searchPage = new Page<IpCommCfg>(request, response, "a");
// Page<IpCommCfg> auditPage = new Page<IpCommCfg>(request, response, "a");
// BeanUtils.copyProperties(searchPage, auditPage);
//
// try {
// auditAll(auditPage, isValid, cfg);
// addMessage(redirectAttributes, "success", "audit_success");
// } catch (Exception e) {
// logger.error("配置下发失败:", e);
// if (e instanceof MaatConvertException) {
// addMessage(redirectAttributes, "error", "request_service_failed");
// } else {
// addMessage(redirectAttributes, "error", "audit_failed");
// }
//
// }
//
// return list(model, request, response, cfg);
// }
// }
@RequestMapping(value = { "/delete" })
@RequiresPermissions(value = { "ip:common:config" })
public String delete(Integer isValid, String ids, Integer functionId, RedirectAttributes redirectAttributes) {
try {
ipCommGroupCfgService.delete(ids);
addMessage(redirectAttributes, "success", "delete_success");
} catch (Exception e) {
logger.error("Delete failed", e);
if (e instanceof MaatConvertException) {
addMessage(redirectAttributes, "error", e.getMessage());
} else {
addMessage(redirectAttributes, "error", "delete_failed");
}
}
return "redirect:" + adminPath + "/basics/ip/list?functionId=" + functionId;
}
// ipComm配置导出
@RequestMapping(value = "/exportIpComm")
public void exportIpCommonCfg(Model model, HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cfg") IpCommCfg entity, String ids, RedirectAttributes redirectAttributes) {
try {
// export data info
List<String> titleList = new ArrayList<String>();
Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
Map<String, List> dataMap = new HashMap<String, List>();
Map<String, String> noExportMap = new HashMap<String, String>();
List<IpCommCfg> list = new ArrayList<IpCommCfg>();
if (!StringUtil.isEmpty(ids)) {
list = ipCommGroupCfgService.findByPage(ids);
} else {
Page<IpCommCfg> pageInfo = new Page<IpCommCfg>(request, response, "r");
pageInfo.setPageNo(1);
pageInfo.setPageSize(Constants.MAX_EXPORT_SIZE);
Page<IpCommCfg> page = ipCommGroupCfgService.findPage(pageInfo, entity);
list = page.getList();
}
//
titleList.add(entity.getMenuNameCode());
classMap.put(entity.getMenuNameCode(), IpPortCfg.class);
String cfgIndexInfoNoExport = ",block_type,do_log,action"
+ ",letter,whether_area_block,classification,attribute,label"
+ ",userregion1,userregion2,userregion3,userregion4,userregion5,src_ip_pattern,client_ip,src_port_pattern,client_port,dest_ip_pattern,dest_port_pattern,dest_port,";
// 时间过滤
if (entity.getSearch_create_time_start() == null) {
cfgIndexInfoNoExport = ",config_time" + cfgIndexInfoNoExport;
}
if (entity.getSearch_edit_time_start() == null) {
cfgIndexInfoNoExport = ",edit_time" + cfgIndexInfoNoExport;
}
if (entity.getSearch_audit_time_start() == null) {
cfgIndexInfoNoExport = ",audit_time" + cfgIndexInfoNoExport;
}
if (!StringUtil.isEmpty(entity.gethColumns())) {
cfgIndexInfoNoExport = "," + entity.gethColumns() + "," + cfgIndexInfoNoExport;
}
noExportMap.put(entity.getMenuNameCode(), cfgIndexInfoNoExport);
dataMap.put(entity.getMenuNameCode(), list);
String timeRange = initTimeMap(entity);
noExportMap.put("timeRange", timeRange);
if ("csv".equals(entity.getExType())) {
this._exportCsv(model, request, response, redirectAttributes, entity.getMenuNameCode(), titleList,
classMap, dataMap, noExportMap);
} else {
this._export(model, request, response, redirectAttributes, entity.getMenuNameCode(), titleList,
classMap, dataMap, noExportMap);
}
} catch (Exception e) {
logger.error("IpCommGroupCfg export failed", e);
addMessage(redirectAttributes, "error", "export_failed");
}
// return "redirect:" + adminPath
// +"/ntc/iplist/list?functionId="+entity.getFunctionId();
}
/**
* 批量审核
*
* @param isAudit
* @param isValid
* @param ids
* @param functionId
* @param redirectAttributes
* @return
*/
// @Override
// public void auditAll(Page page, Integer auditType, Object entity) throws Exception {
// long start = System.currentTimeMillis();
// page.setOrderBy("");
// page.setPageSize(Constants.MAAT_JSON_SEND_SIZE);
// page.setPageNo(1);
// page.setLastPage(false);
// // 携带审核状态信息的BaseCfg
// BaseCfg auditBatchCfg = new BaseCfg();
// // 携带审核条件的BaseCfg
// IpCommCfg searchIpCommGroupCfg = new IpCommCfg();
// BeanUtils.copyProperties(entity, auditBatchCfg);
// BeanUtils.copyProperties(entity, searchIpCommGroupCfg);
//
// auditType = (Integer) Reflections.invokeGetter(entity, "isAudit");
//
// // 全部审核通过,只查询当前条件下的所有未审核的配置 -批量审核通过/不通过
// if (auditType.equals(1) || auditType.equals(2)) {
//
// searchIpCommGroupCfg.setIsValid(0);
// searchIpCommGroupCfg.setIsAudit(0);
//
// if (auditType.equals(1)) {
// auditBatchCfg.setIsAudit(1);
// auditBatchCfg.setIsValid(1);
// } else {
// auditBatchCfg.setIsAudit(2);
// auditBatchCfg.setIsValid(0);
// }
// auditBatchCfg.setAuditTime(new Date());
// auditBatchCfg.setAuditorId(UserUtils.getUser().getId());
// } else {
// // 全部取消通过,只查询当前条件下的所有审核通过的配置
// searchIpCommGroupCfg.setIsValid(1);
// searchIpCommGroupCfg.setIsAudit(1);
//
// auditBatchCfg.setIsAudit(3);
// auditBatchCfg.setIsValid(0);
// auditBatchCfg.setAuditTime(new Date());
// auditBatchCfg.setAuditorId(UserUtils.getUser().getId());
// }
//
// ServiceConfigTemplateUtil serviceTemplate = new ServiceConfigTemplateUtil();
// Integer functionId = 0;
// if (auditBatchCfg != null && !StringUtil.isEmpty(auditBatchCfg.getFunctionId())) {
// functionId = auditBatchCfg.getFunctionId();
// }
// List<Map<String, Object>> serviceList = serviceTemplate.getServiceListByFunctionId(functionId);
// for (Map<String, Object> service : serviceList) {
// String tableNameXml = service.get("tableName").toString(); // 获取业务主配置表
// String serviceTypeXml = service.get("serviceType").toString(); // 业务类型 1maat 2callback
// String classNameXml = service.get("className").toString(); // 主配置Java类
// String serviceIdXml = service.get("id").toString(); // service字典表 service_id字段
// auditBatchCfg.setServiceId(Integer.valueOf(serviceIdXml));
// auditBatchCfg.setTableName(tableNameXml);
// searchIpCommGroupCfg.setServiceId(Integer.valueOf(serviceIdXml));
// searchIpCommGroupCfg.setTableName(tableNameXml);
//
// if ("1".equals(serviceTypeXml)) {// maat类配置
// // 存放域配置类型 及 对应表名
// List<Map<String, Object>> cfgList = (List<Map<String, Object>>) service.get("cfgList");
// List<Map<String, Object>> userRegionList = (List<Map<String, Object>>) service.get("userRegionList");
// int cfgType = Integer.parseInt(service.get("cfgType").toString());
// if (auditBatchCfg.getIsAudit() == 1) {
// boolean hasData = true;
//
// while (hasData) {
// page.setPageNo(1);
// page.setLastPage(false);
// List list = ipCommGroupCfgService.findPage(page, searchIpCommGroupCfg).getList();
// if(CollectionUtils.isNotEmpty(list)){
// Map<Integer,List> groupIdMap=new HashMap();
// // 配置生效处理
// if(auditBatchCfg.getIsAudit().equals(1)) {
// hasData=commonPolicyService.auditReuseCommonConfigData(page, auditBatchCfg,groupIdMap,hasData);
// Map<Integer,List> groupIpMap=new HashMap();
// if(CollectionUtils.isNotEmpty(list)) {
// for (IpCommCfg obj : (List<IpCommCfg>)list) {
// BaseCfg baseCfg=(BaseCfg)obj;
// if(!StringUtil.isEmpty(obj.getGroupId())) {
// groupIdMap.get(obj.getGroupId()).add(baseCfg);
// }else {
// List newList=new ArrayList<>();
// newList.add(baseCfg);
// groupIdMap.put(obj.getGroupId(),newList);
// }
// }
// }
//
// }
// if(hasData) {
// page.setPageNo(1);
// page.setLastPage(false);
// }
// }else{
// hasData = false;
// }
// }
// }
// } else {
// throw new RuntimeException("wrong service type " + serviceTypeXml);
// }
// }
//
// // 批量审核通过时如果没有携带isValid检索条件返回界面需要将isValid置为null
// if (!StringUtil.isEmpty(entity)) {
// BaseCfg base = (BaseCfg) entity;
// if (!StringUtil.isEmpty(base.getSeltype()) && base.getSeltype().equals("isValid")) {
// base.setIsValid(null);
// BeanUtils.copyProperties(base, entity);
// }
// }
// long end = System.currentTimeMillis();
// logger.warn("配置批量生效/失效耗时:" + (end - start));
// }
@RequestMapping(value = "import", method=RequestMethod.POST)
public String importIp(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes,
@RequestParam("files") MultipartFile[] files, Integer serviceDictId ,Integer requestId ,String attribute ,String classify
,String regionDictIds ,String importPath) {
logger.warn("import start...");
long start=System.currentTimeMillis();
ImportBigExcel ei=null;
try {
FunctionServiceDict serviceDict = DictUtils.getFunctionServiceDict(serviceDictId);
StringBuffer errTip=new StringBuffer();
BlockingQueue<BaseIpCfg> ipPortCfgs =null;
//List<CfgIndexInfo> cfgIndexInfos = new ArrayList<CfgIndexInfo>();
for (int i = 0; i < files.length; i++) {
MultipartFile file = files[i];
ei = new ImportBigExcel(file, 0, 1);
FunctionRegionDict regionDict = DictUtils.getFunctionRegionDict(Integer.parseInt(regionDictIds.split(",")[i]));
//加载模板
loadTemplate(ei,regionDict, serviceDict);
//------------------------------------check format start----------------------------
if (regionDict.getRegionType().equals(1)) {// IP
BlockingQueue<IpCommCfgTemplate> list = ei.getDataList(IpCommCfgTemplate.class );
ipPortCfgs=this.checkIpCfgMulity(errTip,serviceDict, regionDict, null,null, list);
}
//删除文件
if(ei.getUploadFile()!=null&&ei.getUploadFile().exists()) {
ei.getUploadFile().delete();
}
//------------------------------------check format end----------------------------
Date date = new Date();
String isSend = request.getParameter("isSend")==null?"":request.getParameter("isSend");
if (regionDict.getRegionType().equals(1)) {// IP
List<BaseIpCfg> _ipPortCfgs=Lists.newArrayList(Constants.MAAT_JSON_SEND_SIZE);
while(!ipPortCfgs.isEmpty()) {
ipPortCfgs.drainTo(_ipPortCfgs, Constants.MAAT_JSON_SEND_SIZE);
int ind=0;
for (BaseIpCfg cfg : _ipPortCfgs) {
cfg.setAction(serviceDict==null?null:serviceDict.getAction());
cfg.setCfgRegionCode(regionDict.getConfigRegionCode());
cfg.setCfgType(regionDict.getConfigRegionValue());
cfg.setCreateTime(date);
cfg.setCreatorId(UserUtils.getUser().getId());
//cfg.setDoLog(2);
cfg.setCompileId(0);
cfg.setFunctionId(regionDict.getFunctionId());
if(isSend.equals("1")) {
cfg.setIsAudit(Constants.AUDIT_YES);
cfg.setIsValid(Constants.VALID_YES);
cfg.setAuditorId(UserUtils.getUser().getId());
cfg.setAuditTime(date);
}else {
cfg.setIsAudit(Constants.AUDIT_NOT_YET);
cfg.setIsValid(Constants.VALID_NO);
}
cfg.setIsAreaEffective(0);
cfg.setLable("0");
cfg.setRequestId(StringUtil.isEmpty(requestId) ? 0 : requestId);
cfg.setAttribute(attribute);
cfg.setClassify(classify);
cfg.setServiceId(serviceDict==null?null:serviceDict.getServiceId());
cfg.setTableName("ip_comm_cfg");
ind++;
}
ipCommGroupCfgService.saveAndSend(regionDict, serviceDict, _ipPortCfgs,isSend.equals("1"));
_ipPortCfgs.clear();
}
}
}
if(errTip.toString().length()>0) {
addMessage(redirectAttributes,"error", errTip.toString());
}
} catch (Exception e) {
if(ei!=null) {
if(ei.getUploadFile().exists()) {
ei.getUploadFile().delete();
}
}
if(e instanceof MaatConvertException) {
addMessage(redirectAttributes,"error", "request_service_failed");
}else if(e instanceof ServiceException) {
addMessage(redirectAttributes,"error", e.getMessage());
}else if(e instanceof IndexOutOfBoundsException){
addMessage(redirectAttributes,"error", "template_error");
}else {
addMessage(redirectAttributes,"error", "import_failed");
}
logger.error("import failed", e);
}
long end=System.currentTimeMillis();
logger.warn("import finish,cost:"+(end-start));
return "redirect:" + adminPath+ importPath;
}
@RequestMapping(value="ajaxGetGroups",method=RequestMethod.POST)
@ResponseBody
public Map<Integer,String> ajaxGetGroups(Model model,@RequestParam(required=true,value="groupIds")String groupIds){
Map<Integer,String> groupIdList=new HashMap<Integer,String>();
if(StringUtils.isNotBlank(groupIds)) {
List<PolicyGroupInfo> list=policyGroupInfoService.findPolicyByGroupInfoList(groupIds);
for(PolicyGroupInfo p:list) {
groupIdList.put(p.getGroupId(), p.getGroupName());
}
}
return groupIdList;
}
}