From fcc904853b43457593c70d3f741c2f405deebee4 Mon Sep 17 00:00:00 2001 From: anthonywj Date: Tue, 28 Mar 2023 16:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8F=AF=E4=BB=A5=EF=BC=8C?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8D=E5=8F=AF=E4=BB=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/idc/controller/IdcController.java | 262 +++++++++++------- .../sale/admin/idc/thread/AsyncFetchTask.java | 12 +- .../sale/admin/idc/thread/AsyncIdcTask.java | 12 +- .../main/resources/application-pro.properties | 8 +- 4 files changed, 176 insertions(+), 118 deletions(-) diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java index ecba009..956daa9 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/controller/IdcController.java @@ -6,9 +6,12 @@ import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -28,115 +31,170 @@ import com.glxp.sale.common.res.BaseResponse; /** * 中继服务接口 */ +@Slf4j @RestController public class IdcController { - @Resource - IdcService idcService; - @Resource - DbDao dbDao; - /*通用上传接口*/ - //@Log("中继服务数据接收,下级往上级上传") - @RequestMapping(value = "/spssync/common/upload") - public BaseResponse upload(HttpServletRequest request, - @RequestParam("content") String content, - @RequestParam(value = "files", required = false) MultipartFile[] files) { - // - return idcService.receive("U",request, content, files); - } - /*通用下发接口*/ - //@Log("中继服务数据接收,上级往下级下发") - @RequestMapping(value = "/spssync/common/issued") - public BaseResponse issued(HttpServletRequest request, - @RequestParam("content") String content, - @RequestParam(value = "files", required = false) MultipartFile[] files) { - // - return idcService.receive("I",request, content, files); - } - - //@Log("数据同步测试") - @RequestMapping(value = "/spssync/common/test") - public BaseResponse test(HttpServletRequest request,@RequestBody Map params) { - // - return idcService.send(params); - } - - //@Log("数据同步任务列表拉取") - @RequestMapping(value = "/spssync/common/list") - public BaseResponse list(HttpServletRequest request,@RequestBody(required = false) Map params) { - return idcService.taskList(request,params); - } - - //@Log("数据同步任务根据任务ID下载数据") - @RequestMapping(value = "/spssync/common/download") - @ResponseBody - public BaseResponse download(HttpServletRequest request,@RequestBody Map params) { - return idcService.download(request,params); - } - - //@Log("数据同步任务根据任务ID,返回下载成功标记") - @RequestMapping(value = "/spssync/common/success") - @ResponseBody - public BaseResponse success(HttpServletRequest request,@RequestBody Map params) { - return idcService.downlaodSuccess(request,params); - } - - @RequestMapping("/mapi/**") - public JSONObject mapi(HttpServletRequest request) { - - return redirect(request); - } - @RequestMapping("/zapi/**") - public JSONObject zapi(HttpServletRequest request) { - - return redirect(request); - } - private JSONObject redirect(HttpServletRequest request) { - String uri = parseUri(request.getRequestURL().toString()); - RestTemplate restTemplate = new RestTemplate(); - - - Map headerParam = new HashMap<>(); - Map bodyParam = new HashMap(); - Enumeration pNames=request.getParameterNames(); - while(pNames.hasMoreElements()){ - String name=(String)pNames.nextElement(); - String value=request.getParameter(name); - bodyParam.put(name, value); - } - HttpHeaders headers = new HttpHeaders(); - Enumeration enumeration = request.getHeaderNames(); + @Resource + IdcService idcService; + @Resource + DbDao dbDao; + + /*通用上传接口*/ + //@Log("中继服务数据接收,下级往上级上传") + @RequestMapping(value = "/spssync/common/upload") + public BaseResponse upload(HttpServletRequest request, + @RequestParam("content") String content, + @RequestParam(value = "files", required = false) MultipartFile[] files) { + // + return idcService.receive("U", request, content, files); + } + + /*通用下发接口*/ + //@Log("中继服务数据接收,上级往下级下发") + @RequestMapping(value = "/spssync/common/issued") + public BaseResponse issued(HttpServletRequest request, + @RequestParam("content") String content, + @RequestParam(value = "files", required = false) MultipartFile[] files) { + // + return idcService.receive("I", request, content, files); + } + + //@Log("数据同步测试") + @RequestMapping(value = "/spssync/common/test") + public BaseResponse test(HttpServletRequest request, @RequestBody Map params) { + // + return idcService.send(params); + } + + //@Log("数据同步任务列表拉取") + @RequestMapping(value = "/spssync/common/list") + public BaseResponse list(HttpServletRequest request, @RequestBody(required = false) Map params) { + return idcService.taskList(request, params); + } + + //@Log("数据同步任务根据任务ID下载数据") + @RequestMapping(value = "/spssync/common/download") + @ResponseBody + public BaseResponse download(HttpServletRequest request, @RequestBody Map params) { + return idcService.download(request, params); + } + + //@Log("数据同步任务根据任务ID,返回下载成功标记") + @RequestMapping(value = "/spssync/common/success") + @ResponseBody + public BaseResponse success(HttpServletRequest request, @RequestBody Map params) { + return idcService.downlaodSuccess(request, params); + } + + @RequestMapping("/mapi/**") + public Object mapi(HttpServletRequest request) { + String uri = parseUri(request.getRequestURL().toString()); + + if (uri.contains("image")) { + return redirectIamge(request, uri); + } else { + return redirect(request, uri); + } + } + + + HttpEntity buildHeader(HttpServletRequest request) { + + log.info(request.getMethod()); + Map headerParam = new HashMap<>(); + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + } + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements()) { - String name = enumeration.nextElement(); + String name = enumeration.nextElement(); String value = request.getHeader(name); headerParam.put(name, value); headers.add(name, value); } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(JSON.toJSONString(bodyParam), headers); + return http; + } + + private Resource redirectIamge(HttpServletRequest request, String uri) { + uri = uri.replace("mapi/", ""); + uri = "http://127.0.0.1:9993/" + uri; + RestTemplate restTemplate = new RestTemplate(); +// HttpEntity httpEntity = buildHeader(request); + log.info(request.getMethod()); + Map headerParam = new HashMap<>(); + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + String parm = ""; + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + parm = parm + name + "=" + value + "&"; + } + uri = uri + "?" + parm.substring(0, parm.length() - 1); + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String name = enumeration.nextElement(); + String value = request.getHeader(name); + headerParam.put(name, value); + headers.add(name, value); + } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(JSON.toJSONString(bodyParam), headers); + + + log.info(uri); +// ResponseEntity entity = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, Resource.class); + ResponseEntity responseEntity = restTemplate.exchange(uri, + HttpMethod.GET, http, Resource.class, new Object[0]); +// ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, http, HttpServletResponse.class); + return responseEntity.getBody(); - - //headers.add("Content-Type", "application/json;charset=UTF-8"); - - System.out.print(JSON.toJSONString(headerParam)); - System.out.print(JSON.toJSONString(bodyParam)); - HttpEntity http = new HttpEntity<>(JSON.toJSONString(bodyParam), headers); - ResponseEntity responseBody = restTemplate.postForEntity("http://127.0.0.1:8090/"+uri, http, JSONObject.class); - JSONObject result = responseBody.getBody(); - return result; } - - - - private String parseUri(String url) { - String uri = url; - int a1 = uri.indexOf("://"); - int a2 = 0; - if(a1>0) { - a2 = uri.indexOf("/",a1 +4); - if(a2>0) { - uri = uri.substring(a2 +1); - } - } - return uri; - } - - + + private JSONObject redirect(HttpServletRequest request, String uri) { + + RestTemplate restTemplate = new RestTemplate(); + HttpEntity httpEntity = buildHeader(request); + log.info(uri); + uri = uri.replace("mapi/", ""); + uri = "http://127.0.0.1:9993/" + uri; + log.info(uri); + ResponseEntity responseBody = null; + if (request.getMethod().equals("POST")) + responseBody = restTemplate.postForEntity(uri, httpEntity, JSONObject.class); + else if (request.getMethod().equals("GET")) { + responseBody = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("PUT")) { + responseBody = restTemplate.exchange(uri, HttpMethod.PUT, httpEntity, JSONObject.class); + } else if (request.getMethod().equals("DELETE")) { + responseBody = restTemplate.exchange(uri, HttpMethod.DELETE, httpEntity, JSONObject.class); + } + JSONObject result = responseBody.getBody(); + return result; + } + + + private String parseUri(String url) { + String uri = url; + int a1 = uri.indexOf("://"); + int a2 = 0; + if (a1 > 0) { + a2 = uri.indexOf("/", a1 + 4); + if (a2 > 0) { + uri = uri.substring(a2 + 1); + } + } + return uri; + } + + } diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncFetchTask.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncFetchTask.java index fcab829..f5429d3 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncFetchTask.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncFetchTask.java @@ -20,13 +20,13 @@ import com.glxp.sale.admin.req.info.ScheduledRequest; public class AsyncFetchTask implements SchedulingConfigurer { final Logger logger = LoggerFactory.getLogger(AsyncIdcTask.class); - + @Resource private ScheduledDao scheduledDao; @Resource private IdcService idcService; - + @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(() -> process(), @@ -35,8 +35,8 @@ public class AsyncFetchTask implements SchedulingConfigurer { scheduledRequest.setCronName("syncFetch"); logger.info("syncFetch----------------"); ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "15 * * * * ?"; - + String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "* 3/30 * * * ?"; + if (cron.isEmpty()) { logger.error("cron is null"); } @@ -47,8 +47,8 @@ public class AsyncFetchTask implements SchedulingConfigurer { private void process() { logger.info("syncFetch----process------------"); - + idcService.asyncFetchTask(); } -} \ No newline at end of file +} diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncIdcTask.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncIdcTask.java index 7135d23..9c4aa25 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncIdcTask.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/thread/AsyncIdcTask.java @@ -20,13 +20,13 @@ import com.glxp.sale.admin.req.info.ScheduledRequest; public class AsyncIdcTask implements SchedulingConfigurer { final Logger logger = LoggerFactory.getLogger(AsyncIdcTask.class); - + @Resource private ScheduledDao scheduledDao; @Resource private IdcService idcService; - + @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(() -> process(), @@ -35,8 +35,8 @@ public class AsyncIdcTask implements SchedulingConfigurer { scheduledRequest.setCronName("syncIdc"); logger.info("syncIdc----------------"); ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "5 * * * * ?"; - + String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "* 0/30 * * * ?"; + if (cron.isEmpty()) { logger.error("cron is null"); } @@ -47,8 +47,8 @@ public class AsyncIdcTask implements SchedulingConfigurer { private void process() { logger.info("syncIdc----process------------"); - + idcService.asyncIdcTask(); } -} \ No newline at end of file +} diff --git a/api-admin/src/main/resources/application-pro.properties b/api-admin/src/main/resources/application-pro.properties index daf7148..1f6055f 100644 --- a/api-admin/src/main/resources/application-pro.properties +++ b/api-admin/src/main/resources/application-pro.properties @@ -1,10 +1,10 @@ # \u751F\u4EA7\u73AF\u5883 server.port=10001 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3316/udispsync?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true +spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/udispsync?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root -#spring.datasource.password=root -spring.datasource.password=xiamenswan +spring.datasource.password=123456 +#spring.datasource.password=xiamenswan #server.servlet.context-path= logging.level.com.glxp.sale.admin=debug # \u8DE8\u57DF\u8BBE\u7F6E @@ -56,4 +56,4 @@ spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接超时时间(毫秒) -spring.redis.jedis.timeout=300 \ No newline at end of file +spring.redis.jedis.timeout=300