From a158488a402ef742029a34fd31cbf6d8bee4a117 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期一, 19 九月 2022 00:41:19 +0800
Subject: [PATCH] 优化代码

---
 java110-doc/src/main/java/com/java110/doc/controller/DocController.java |  215 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 206 insertions(+), 9 deletions(-)

diff --git a/java110-doc/src/main/java/com/java110/doc/controller/DocController.java b/java110-doc/src/main/java/com/java110/doc/controller/DocController.java
index 7af6b92..5c1f638 100644
--- a/java110-doc/src/main/java/com/java110/doc/controller/DocController.java
+++ b/java110-doc/src/main/java/com/java110/doc/controller/DocController.java
@@ -1,27 +1,224 @@
 package com.java110.doc.controller;
 
 
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.doc.annotation.*;
+import com.java110.doc.entity.ApiDocDto;
+import com.java110.doc.entity.CmdDocDto;
+import com.java110.doc.entity.RequestMappingsDocDto;
+import com.java110.doc.registrar.ApiDocCmdPublishing;
+import com.java110.doc.registrar.ApiDocPublishing;
+import com.java110.utils.factory.ApplicationContextFactory;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.client.RestTemplate;
 
 import javax.servlet.http.HttpServletRequest;
+import java.lang.annotation.Annotation;
+import java.util.List;
 
 @RestController
-
 @RequestMapping(value = "/doc")
 public class DocController {
 
+
     @RequestMapping(path = "/api", method = RequestMethod.GET)
     public ResponseEntity<String> html(
-            HttpServletRequest request){
-        return new ResponseEntity<>("<html></html>", HttpStatus.OK);
+            HttpServletRequest request) {
+
+
+        ApiDocDto apiDocDto = ApiDocPublishing.getApiDocDto();
+
+        List<RequestMappingsDocDto> mappingsDocDtos = ApiDocPublishing.getMappingsDocDtos();
+
+        JSONObject param = new JSONObject();
+        param.put("api", apiDocDto);
+        param.put("mappings", mappingsDocDtos);
+        return new ResponseEntity<>(param.toString(), HttpStatus.OK);
+    }
+
+    @RequestMapping(path = "/api/page", method = RequestMethod.GET)
+    public ResponseEntity<String> pages(
+            @RequestParam("name") String name,
+            @RequestParam("resource") String resource,
+            HttpServletRequest request) {
+
+        List<RequestMappingsDocDto> mappingsDocDtos = ApiDocPublishing.getMappingsDocDtos();
+
+        RequestMappingsDocDto newMappingsDocDto = null;
+        for (RequestMappingsDocDto mappingsDocDto : mappingsDocDtos) {
+            if (mappingsDocDto.getName().equals(name)) {
+                newMappingsDocDto = mappingsDocDto;
+            }
+        }
+
+        if (newMappingsDocDto == null) {
+            return new ResponseEntity<>("[]", HttpStatus.OK);
+        }
+
+        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        if ("boot".equals(newMappingsDocDto.getStartWay())) {
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        }
+        ResponseEntity<String> responseEntity = null;
+        HttpEntity<String> httpEntity = new HttpEntity<String>("", new HttpHeaders());
+        try {
+            responseEntity = restTemplate.exchange(newMappingsDocDto.getUrl()+"/doc/api/"+resource, HttpMethod.GET, httpEntity, String.class);
+        } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
+            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
+        } catch (Exception e) {
+            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+        return responseEntity;
+    }
+
+    @RequestMapping(path = "/api/{resource}", method = RequestMethod.GET)
+    public ResponseEntity<String> resourcePages(
+            @PathVariable String resource,
+            HttpServletRequest request) {
+
+        List<CmdDocDto> cmdDocDtos = ApiDocCmdPublishing.getCmdDocs(resource);
+
+        return new ResponseEntity<>(JSONArray.toJSONString(cmdDocDtos), HttpStatus.OK);
+    }
+
+
+    @RequestMapping(path = "/api/pageContent", method = RequestMethod.GET)
+    public ResponseEntity<String> pageContent(
+            @RequestParam("name") String name,
+            @RequestParam("serviceCode") String serviceCode,
+            @RequestParam("resource") String resource,
+            HttpServletRequest request) {
+
+        List<RequestMappingsDocDto> mappingsDocDtos = ApiDocPublishing.getMappingsDocDtos();
+
+        RequestMappingsDocDto newMappingsDocDto = null;
+        for (RequestMappingsDocDto mappingsDocDto : mappingsDocDtos) {
+            if (mappingsDocDto.getName().equals(name)) {
+                newMappingsDocDto = mappingsDocDto;
+            }
+        }
+
+        if (newMappingsDocDto == null) {
+            return new ResponseEntity<>("{}", HttpStatus.OK);
+        }
+
+        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        if ("boot".equals(newMappingsDocDto.getStartWay())) {
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        }
+        ResponseEntity<String> responseEntity = null;
+        HttpEntity<String> httpEntity = new HttpEntity<String>("", new HttpHeaders());
+        try {
+            responseEntity = restTemplate.exchange(newMappingsDocDto.getUrl()+"/doc/api/"+resource+"/"+serviceCode, HttpMethod.GET, httpEntity, String.class);
+        } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�
+            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
+        } catch (Exception e) {
+            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+        return responseEntity;
     }
 
     @RequestMapping(path = "/api/{resource}/{serviceCode}", method = RequestMethod.GET)
-    public ResponseEntity<String> api( @PathVariable String resource,
-                                       @PathVariable String serviceCode,
-                                       HttpServletRequest request){
-        return null;
+    public ResponseEntity<String> api(@PathVariable String resource,
+                                      @PathVariable String serviceCode,
+                                      HttpServletRequest request) {
+        CmdDocDto cmdDocDto = ApiDocCmdPublishing.getCmdDocs(resource,serviceCode);
+
+        if(cmdDocDto == null){
+            return new ResponseEntity<>("{}", HttpStatus.OK);
+        }
+
+        JSONObject param = JSONObject.parseObject(JSONObject.toJSONString(cmdDocDto));
+
+        Class clazz = cmdDocDto.getCmdClass();
+
+
+        doJava110ParamsDoc(clazz,param);
+
+
+        doJava110ResponseDoc(param, clazz);
+
+        doJava110ExampleDoc(param, clazz);
+
+        return new ResponseEntity<>(param.toJSONString(), HttpStatus.OK);
+    }
+
+    private void doJava110ExampleDoc(JSONObject param, Class clazz) {
+        Java110ExampleDoc java110ExampleDoc = AnnotationUtils.findAnnotation(clazz,Java110ExampleDoc.class);
+
+        if(java110ExampleDoc == null){
+            return ;
+        }
+
+        param.put("reqBody",java110ExampleDoc.reqBody());
+        param.put("resBody",java110ExampleDoc.resBody());
+    }
+
+    private void doJava110ResponseDoc(JSONObject param, Class clazz) {
+        Java110ResponseDoc java110ResponseDoc = AnnotationUtils.findAnnotation(clazz,Java110ResponseDoc.class);
+
+        if(java110ResponseDoc == null){
+            return ;
+        }
+
+        Java110ParamDoc[] java110ParamDocs = java110ResponseDoc.params();
+
+        JSONArray params = new JSONArray();
+        JSONObject p = null;
+        for(Java110ParamDoc java110ParamDoc : java110ParamDocs){
+            p = new JSONObject();
+            p.put("name",java110ParamDoc.name());
+            p.put("defaultValue",java110ParamDoc.defaultValue());
+            p.put("remark",java110ParamDoc.remark());
+            p.put("type",java110ParamDoc.type());
+            p.put("length",java110ParamDoc.length());
+            p.put("parentNodeName",java110ParamDoc.parentNodeName());
+            params.add(p);
+        }
+        param.put("resParam",params);
+    }
+
+    private void doJava110ParamsDoc(Class clazz, JSONObject param) {
+
+        Java110ParamsDoc java110ParamsDoc = AnnotationUtils.findAnnotation(clazz,Java110ParamsDoc.class);
+
+        if(java110ParamsDoc == null){
+            return ;
+        }
+
+        Java110HeaderDoc[] java110HeaderDocs = java110ParamsDoc.headers();
+
+        JSONArray headers = new JSONArray();
+        JSONObject header = null;
+        for(Java110HeaderDoc java110HeaderDoc : java110HeaderDocs){
+            header = new JSONObject();
+            header.put("name",java110HeaderDoc.name());
+            header.put("defaultValue",java110HeaderDoc.defaultValue());
+            header.put("description",java110HeaderDoc.description());
+            headers.add(header);
+        }
+
+        param.put("headers",headers);
+
+
+        Java110ParamDoc[] java110ParamDocs = java110ParamsDoc.params();
+
+        JSONArray params = new JSONArray();
+        JSONObject p = null;
+        for(Java110ParamDoc java110ParamDoc : java110ParamDocs){
+            p = new JSONObject();
+            p.put("name",java110ParamDoc.name());
+            p.put("defaultValue",java110ParamDoc.defaultValue());
+            p.put("remark",java110ParamDoc.remark());
+            p.put("type",java110ParamDoc.type());
+            p.put("length",java110ParamDoc.length());
+            p.put("parentNodeName",java110ParamDoc.parentNodeName());
+            params.add(p);
+        }
+        param.put("reqParam",params);
     }
 }

--
Gitblit v1.8.0