添加 回调配置新增和样例文件上传 接口

This commit is contained in:
zhangdongxu
2018-05-24 17:22:38 +08:00
parent 2391170301
commit 27d3b82c2b
11 changed files with 847 additions and 5 deletions

View File

@@ -43,7 +43,7 @@ import com.nis.web.service.ServicesRequestLogService;
*/
public class BaseRestController {
protected final Logger logger = Logger.getLogger(this.getClass());
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHssSSS");
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
/**
*
* @Title: serviceResponse

View File

@@ -1,5 +1,6 @@
package com.nis.web.controller.restful;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@@ -9,15 +10,20 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.nis.domain.restful.ConfigCompile;
import com.nis.domain.restful.ConfigSource;
import com.nis.domain.restful.FileDesc;
import com.nis.domain.restful.MaatConfig;
import com.nis.restful.RestBusinessCode;
import com.nis.restful.RestServiceException;
@@ -28,8 +34,9 @@ import com.nis.util.StringUtils;
import com.nis.web.controller.BaseRestController;
import com.nis.web.service.SaveRequestLogThread;
import com.nis.web.service.ServicesRequestLogService;
import com.nis.web.service.fdfs.FastDFSFile;
import com.nis.web.service.fdfs.FileManager;
import com.nis.web.service.restful.ConfigRedisService;
import com.nis.web.service.restful.ConfigRedisServiceimpl;
import com.nis.web.service.restful.ConfigSourcesService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@@ -433,7 +440,66 @@ public class ConfigSourcesController extends BaseRestController {
}
}
@RequestMapping(value = "/cfg/v1/commonSources", method = RequestMethod.POST, produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "回调配置存储", httpMethod = "POST", response = Map.class, notes = "回调配置存储服务")
//@ApiParam(value = "回调配置数据源", name = "JSONObject", requirerue)
public Map createCommonConfigSource(@RequestBody String jsonString , HttpServletRequest request,
HttpServletResponse response) {
ConfigSourcesService.setMsgList(new ArrayList<Exception>());// 清除上次记录的日志信息
long start = System.currentTimeMillis();
SaveRequestLogThread thread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_POST, request,null);
StringBuffer sb = new StringBuffer();
configSourcesService.savaCommonSources(thread, start, jsonString, sb);
return compileServiceResponse(thread, System.currentTimeMillis() - start, request, response,
"配置数据插入成功" , Constants.IS_DEBUG ? jsonString : null);
}
@RequestMapping(value = "/cfg/v1/fileUploadSources", method = RequestMethod.POST)
@ApiOperation(value = "样例文件上传服务", httpMethod = "POST", response = Map.class, notes = "样例文件上传服务")
@ApiParam(value = "样例文件上传服务", name = "MultipartFile", required = true)
public Map fileUploadSource(@RequestBody MultipartFile file, HttpServletRequest request,
HttpServletResponse response) {
ConfigSourcesService.setMsgList(new ArrayList<Exception>());// 清除上次记录的日志信息
long start = System.currentTimeMillis();
SaveRequestLogThread thread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_POST, request,null);
String filePath ="";
try {
FileDesc fileDesc = (FileDesc) JSONObject.toBean(JSONObject.fromObject(request.getHeader("File-Desc")),FileDesc.class);
if (!StringUtil.isEmpty(fileDesc.getChecksum())) {
//验证Md5
String md5 = DigestUtils.md5Hex(file.getBytes());
System.out.println("----------------------------MD5:'"+md5+"'==='"+fileDesc.getChecksum()+"'");
if (!md5.equals(fileDesc.getChecksum())) {
thread.setExceptionInfo("checksum与文件MD5值不一致");
throw new RestServiceException(thread, System.currentTimeMillis() - start, "checksum与文件MD5值不一致",
RestBusinessCode.config_integrity_error.getValue());
}
String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
FastDFSFile fdsfile = new FastDFSFile(file.getBytes(),file.getOriginalFilename(), ext);
// NameValuePair[] meta_list = new NameValuePair[5];
// meta_list[0] = new NameValuePair("fileName", file.getOriginalFilename());
// meta_list[1] = new NameValuePair("fileLength", String.valueOf(file.getSize()));
// meta_list[2] = new NameValuePair("fileExt", ext);
// meta_list[3] = new NameValuePair("fileAuthor", "rkg");
// meta_list[4] = new NameValuePair("fileMd5", md5);
filePath = FileManager.upload(fdsfile, null);
}else{
thread.setExceptionInfo("请求头信息中缺少checksum参数");
throw new RestServiceException(thread, System.currentTimeMillis() - start, "请求头信息中缺少checksum参数",
RestBusinessCode.missing_args.getValue());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonObj = new JSONObject();
jsonObj.put("accessUrl", filePath.substring(filePath.indexOf("group")));
return compileServiceResponse(thread, System.currentTimeMillis() - start, request, response,
"文件上传成功" ,jsonObj);
}
private boolean isBlank(Date datetime) {
if (null != datetime) {
return true;

View File

@@ -4,15 +4,19 @@
package com.nis.web.service.restful;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nis.domain.restful.ConfigPzIdSource;
import com.nis.util.StringUtil;
import com.nis.web.dao.ConfigPzIdDao;
import com.nis.web.service.BaseLogService;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.Static;
/**
* @ClassName:ConfigPzIdService
@@ -31,11 +35,23 @@ public class ConfigPzIdService extends BaseLogService {
@Autowired
protected ConfigPzIdDao dao;
@Autowired
ConfigRedisService configRedisService;
private static Map<String, String> sourceNameMap = new HashMap<String, String>();
static {
sourceNameMap.put("CONFIG_COMPILE", "SEQ_COMPILEID");
sourceNameMap.put("CONFIG_GROUP", "SEQ_GROUPID");
sourceNameMap.put("CONFIG_REGION", "SEQ_REGIONID");
}
public ConfigPzIdSource getConfigPzIdList(ConfigPzIdSource entity){
List<Long> pzIdList = new ArrayList<Long>();
entity.setSourceName(entity.getSourceName().toUpperCase());
String seqName= StringUtil.isEmpty(sourceNameMap.get(entity.getSourceName()))?"SEQ_COMPILEID":sourceNameMap.get(entity.getSourceName());
for (int i = 0; i < entity.getNum(); i++) {
pzIdList.add(dao.getConfigPzIdList(entity));
//pzIdList.add(dao.getConfigPzIdList(entity)); //直接从数据库序列号获取
pzIdList.add(configRedisService.getIncrId(seqName));
}
entity.setPzIdList(pzIdList);
return entity;

View File

@@ -2,12 +2,20 @@ package com.nis.web.service.restful;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
@@ -16,7 +24,11 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.nis.domain.Page;
import com.nis.domain.restful.CommonSourceFieldCfg;
import com.nis.domain.restful.ConfigCompile;
import com.nis.domain.restful.ConfigCompileTest;
import com.nis.domain.restful.ConfigGroupRelation;
@@ -27,9 +39,11 @@ import com.nis.domain.restful.StrRegion;
import com.nis.restful.CompileJudgeCode;
import com.nis.restful.RestBusinessCode;
import com.nis.restful.RestServiceException;
import com.nis.util.BasicProvingUtil;
import com.nis.util.CompileVal;
import com.nis.util.Configurations;
import com.nis.util.OracleErrorCodeUtil;
import com.nis.util.ReadCommSourceXmlUtil;
import com.nis.util.StringUtil;
import com.nis.util.StringUtils;
import com.nis.web.dao.ConfigCompileDao;
@@ -78,6 +92,11 @@ public class ConfigSourcesService extends BaseService {
@Autowired
private StrRegionDao strRegionDao;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
@Autowired
ConfigRedisService configRedisService;
public static Map<Integer, Map<String, String>> getTableRelation() {
Map<Integer, Map<String, String>> tableMap = new HashMap<Integer, Map<String, String>>();
Map<String, String> typeMap = new HashMap<String, String>();
@@ -2076,7 +2095,136 @@ public class ConfigSourcesService extends BaseService {
}
return "ok";
}
public String savaCommonSources(SaveRequestLogThread thread, long start,String jsonString,
StringBuffer sb) {
JsonArray jsonObjectList = new JsonParser().parse(jsonString).getAsJsonArray();
Map<Integer,List<Map<String, String>>> dstMaps = new HashMap<Integer, List<Map<String,String>>>();
for (int i = 0; i < jsonObjectList.size(); i++) {
JsonObject jsonObj=(JsonObject) jsonObjectList.get(i);
Map<String, Object> srcMap = JSONObject.fromObject(JSONObject.fromObject((jsonObj.toString())));
if(srcMap.containsKey("service")){
Map<String,String> dstMap = new HashMap<String, String>();
List<CommonSourceFieldCfg> commonSourceFieldCfgList = ReadCommSourceXmlUtil.getCommonSourceCfgByService(srcMap.get("service").toString().trim());
if (StringUtil.isEmpty(commonSourceFieldCfgList)) {
logger.error("service请检查service配置是否正确");
thread.setExceptionInfo("请检查service配置是否正确");
throw new RestServiceException(thread, System.currentTimeMillis() - start,"请检查service配置是否正确",
RestBusinessCode.wrong_range.getValue());
}
for (CommonSourceFieldCfg commonSourceFieldCfg : commonSourceFieldCfgList) {
//是否必填
if(commonSourceFieldCfg.getIsRequired()&&!srcMap.containsKey(commonSourceFieldCfg.getSrcName())){
logger.error(commonSourceFieldCfg.getSrcName()+"参数不能为空");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数不能为空");
throw new RestServiceException(thread, System.currentTimeMillis() - start,commonSourceFieldCfg.getSrcName()+"参数不能为空",
RestBusinessCode.missing_args.getValue());
}
//字段类型 String Number Date Ip Port
String dstStr = StringUtil.isEmpty(srcMap.get(commonSourceFieldCfg.getSrcName()))?commonSourceFieldCfg.getDefaultVal():srcMap.get(commonSourceFieldCfg.getSrcName()).toString();
if (!StringUtil.isEmpty(dstStr)&&dstStr.startsWith("[")&&dstStr.endsWith("]")) {
dstStr = srcMap.get(dstStr.substring(1, dstStr.length()-1)).toString();
}
switch (commonSourceFieldCfg.getFieldType()) {
case "Number":
if(!StringUtil.isNumeric(dstStr)){
logger.error(commonSourceFieldCfg.getSrcName()+"参数不能格式不正确,必需是数值型");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数不能格式不正确,必需是数值型");
throw new RestServiceException(thread, System.currentTimeMillis() - start,commonSourceFieldCfg.getSrcName()+"参数不能格式不正确",
RestBusinessCode.missing_args.getValue());
}
break;
case "Date":
try {
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(dstStr);
dstStr = date.getTime()+"000";
} catch (ParseException e) {
// TODO Auto-generated catch block
logger.error(commonSourceFieldCfg.getSrcName()+"参数格式不正确,必须是日期型");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数格式不正确,必须是日期型");
throw new RestServiceException(thread, System.currentTimeMillis() - start,commonSourceFieldCfg.getSrcName()+"参数不能格式不正确,必须是日期型",
RestBusinessCode.missing_args.getValue());
}
break;
case "Ip":
if (!isIp(dstStr)) {
logger.error(commonSourceFieldCfg.getSrcName()+"参数格式不正确不是合法的IP地址");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数格式不正确不是合法的IP地址");
throw new RestServiceException(thread, System.currentTimeMillis() - start,commonSourceFieldCfg.getSrcName()+"参数不能格式不正确不是合法的IP地址",
RestBusinessCode.missing_args.getValue());
}
break;
case "Port":
if (!BasicProvingUtil.isPortOrPortMask(dstStr)) {
logger.error(commonSourceFieldCfg.getSrcName()+"参数格式不正确不是合法的Port");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数格式不正确不是合法的Port");
throw new RestServiceException(thread, System.currentTimeMillis() - start,commonSourceFieldCfg.getSrcName()+"参数不能格式不正确不是合法的Port",
RestBusinessCode.missing_args.getValue());
}
break;
}
//range取值范围验证
if(!StringUtil.isEmpty(commonSourceFieldCfg.getRange())){
String [] range= commonSourceFieldCfg.getRange().split("-");
if(!(Long.valueOf(range[0]).compareTo(Long.valueOf(dstStr))<=0&&Long.valueOf(range[1]).compareTo(Long.valueOf(dstStr))>=0)){
logger.error(commonSourceFieldCfg.getSrcName()+"参数不在有效范围");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数不在有效范围");
throw new RestServiceException(thread, System.currentTimeMillis() - start, commonSourceFieldCfg.getSrcName()+"参数不在有效范围",
RestBusinessCode.wrong_range.getValue());
}
}
//regexp 特殊格式正则验证
Boolean valFlag = true;
if(!StringUtil.isEmpty(commonSourceFieldCfg.getRegexp())){
Pattern pattern = Pattern.compile(commonSourceFieldCfg.getRegexp());
Matcher matcher = pattern.matcher(dstStr);
valFlag = valFlag&matcher.matches();
}
if (valFlag) {
dstMap.put(commonSourceFieldCfg.getDstName(),dstStr);
}else{
logger.error(commonSourceFieldCfg.getSrcName()+"参数格式与正则不匹配");
thread.setExceptionInfo(commonSourceFieldCfg.getSrcName()+"参数格式与正则不匹配");
throw new RestServiceException(thread, System.currentTimeMillis() - start, commonSourceFieldCfg.getSrcName()+"参数格式不正确或数据不在有效范围",
RestBusinessCode.param_formate_error.getValue());
}
}
if (StringUtil.isEmpty(dstMaps.get(Integer.valueOf(srcMap.get("service").toString())))) {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
list.add(dstMap);
dstMaps.put(Integer.valueOf(srcMap.get("service").toString()), list);
}else{
List<Map<String, String>> list = dstMaps.get(Integer.valueOf(srcMap.get("service").toString()));
list.add(dstMap);
}
}else{
thread.setExceptionInfo("service参数不能为空");
throw new RestServiceException(thread, System.currentTimeMillis() - start, "service参数不能为空",
RestBusinessCode.missing_args.getValue());
}
}
Iterator iterator =dstMaps.keySet().iterator();
while (iterator.hasNext()) {
Integer key = Integer.valueOf(iterator.next().toString());
configRedisService.saveUnMaatConfig(dstMaps.get(key),key);
// configRedisService.saveConfigYSPDemoCompile(key,);
}
return "ok";
}
private boolean isIp(String ipStr){
if (!(BasicProvingUtil.isIpOrIpMask(ipStr,4)||BasicProvingUtil.isIpOrIpMask(ipStr,6))) {
return false;
}
return true;
}
public String setCompileInvalid(ConfigCompile configCompile) throws Exception {
configCompileDao.setCompileInvalid(configCompile.getCompileId(), 0);
return "ok";