From ebc965fa21f588d1a429901a022ad9d649fdebbc Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 17 Feb 2020 19:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/component/ASESpringUtil.java | 1 + .../DataModelRepositorySearcher.java | 13 +++- .../datamanager/DataModelSearcher.java | 8 +++ .../ase/component/error/ErrorResponse.java | 22 ++++++ .../ase/controller/ASEControllerAdvice.java | 23 ++++++ .../ase/controller/ASEErrorController.java | 24 ++++++- .../ase/controller/DataManagerController.java | 38 +++++----- .../codesdream/ase/test/DataManagerTest.java | 8 +++ src/main/resources/templates/error.html | 72 +++++++++++++++++++ src/main/resources/templates/query.html | 11 +++ 10 files changed, 196 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/component/error/ErrorResponse.java create mode 100644 src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java create mode 100644 src/main/resources/templates/query.html diff --git a/src/main/java/com/codesdream/ase/component/ASESpringUtil.java b/src/main/java/com/codesdream/ase/component/ASESpringUtil.java index d60f1bf..e4b44cf 100644 --- a/src/main/java/com/codesdream/ase/component/ASESpringUtil.java +++ b/src/main/java/com/codesdream/ase/component/ASESpringUtil.java @@ -18,4 +18,5 @@ public class ASESpringUtil { public T getBean(Class tClass){ return applicationContext.getBean(tClass); } + } diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java index d3061b7..00d9da7 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java @@ -24,6 +24,7 @@ public class DataModelRepositorySearcher { private boolean present; public void getDataModelRepositoryClass(String subSystem, String dataModel){ + dataModel = doCheckName(dataModel); try { this.repositoryClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel)); this.setPresent(true); @@ -32,13 +33,21 @@ public class DataModelRepositorySearcher { } } - public T getDataModelRepositoryInstance(Class dataModeRepositoryClass) { + public T getDataModelRepositoryInstance() { if(isPresent()) { - return springUtil.getBean(dataModeRepositoryClass); + return (T) springUtil.getBean(repositoryClass); } return null; } + + public static String doCheckName(String string) { + char[] charArray = string.toCharArray(); + if(Character.isLowerCase(charArray[0])) charArray[0] -= 32; + else return string; + return String.valueOf(charArray); + } + private String dataModelFullNameGenerator(String subSystem, String dataModel){ return new String(repositoryPath + subSystem + "." + dataModel + "Repository"); } diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java index abb898c..113d963 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java @@ -22,6 +22,7 @@ public class DataModelSearcher { private boolean present; public void getDataModelClass(String subSystem, String dataModel) { + dataModel = doCheckName(dataModel); try { dataModelClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel)); this.setPresent(true); @@ -56,6 +57,13 @@ public class DataModelSearcher { return null; } + public static String doCheckName(String string) { + char[] charArray = string.toCharArray(); + if(Character.isLowerCase(charArray[0])) charArray[0] -= 32; + else return string; + return String.valueOf(charArray); + } + private String dataModelFullNameGenerator(String subSystem, String dataModel){ return new String(modelPath + subSystem + "." + dataModel); } diff --git a/src/main/java/com/codesdream/ase/component/error/ErrorResponse.java b/src/main/java/com/codesdream/ase/component/error/ErrorResponse.java new file mode 100644 index 0000000..42d5bbf --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/error/ErrorResponse.java @@ -0,0 +1,22 @@ +package com.codesdream.ase.component.error; + +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "error") +public class ErrorResponse +{ + public ErrorResponse(String message, List details) { + super(); + this.message = message; + this.details = details; + } + + //General error message about nature of error + private String message; + + //Specific errors in API request processing + private List details; + + //Getter and setters +} diff --git a/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java new file mode 100644 index 0000000..d5166ff --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java @@ -0,0 +1,23 @@ +package com.codesdream.ase.controller; + +import com.codesdream.ase.component.error.ErrorResponse; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; + +import java.util.ArrayList; +import java.util.List; + +@ControllerAdvice +public class ASEControllerAdvice { + @ExceptionHandler(value = {RuntimeException.class}) + public final ResponseEntity handleRuntimeException(RuntimeException e, WebRequest webRequest){ + List details = new ArrayList<>(); + details.add(e.getLocalizedMessage()); + ErrorResponse errorResponse = new ErrorResponse("Runtime Error", details); + return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + } + +} diff --git a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java index 189d0cd..2ceedcc 100644 --- a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java +++ b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java @@ -1,26 +1,44 @@ package com.codesdream.ase.controller; +import com.codesdream.ase.component.error.ErrorResponse; import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.request.WebRequest; import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; @Controller public class ASEErrorController implements ErrorController { - @RequestMapping("/error") public String handleError(HttpServletRequest request, Model model){ Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception"); // 页面未找到 - if(statusCode == 404){ + if(statusCode == HttpStatus.NOT_FOUND.value()){ return "not_found"; } + model.addAttribute("http_status", statusCode); + model.addAttribute("exception_name", exception.getClass().getName()); + model.addAttribute("exception_message", exception.getMessage()); + List stack_infos = new ArrayList<>(); + + for(StackTraceElement element : exception.getStackTrace()){ + String s = element.toString(); + stack_infos.add(s); + } + model.addAttribute("error_stack", stack_infos); + + return "error"; - return"error"; } @Override diff --git a/src/main/java/com/codesdream/ase/controller/DataManagerController.java b/src/main/java/com/codesdream/ase/controller/DataManagerController.java index 9bde2db..92de3f8 100644 --- a/src/main/java/com/codesdream/ase/controller/DataManagerController.java +++ b/src/main/java/com/codesdream/ase/controller/DataManagerController.java @@ -1,5 +1,8 @@ package com.codesdream.ase.controller; +import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher; +import com.codesdream.ase.component.datamanager.DataModelSearcher; import org.apache.catalina.core.ApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -10,31 +13,28 @@ import org.w3c.dom.Entity; import javax.annotation.Resource; import java.lang.reflect.Array; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; @Controller @RequestMapping(value = "/database") public class DataManagerController { + @Resource + ASESpringUtil springUtil; + @RequestMapping(value = "{subSystem}/{dataModel}/query") - private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem) - throws InstantiationException, IllegalAccessException, ClassNotFoundException { - String dataModelFullName = "com.codesdream.ase.model." + subSystem + "." + dataModel; - String repositoryFullName = "com.codesdream.ase.repository." + subSystem + "." + dataModel + "Repository"; - try { - Class entityModelClass = Class.forName(dataModelFullName); - Collection paramArgs = new ArrayList<>(); - for(Field field :entityModelClass.getDeclaredFields()){ - paramArgs.add(field.getName()); - } - model.addAttribute("paramArgs", paramArgs); - Object entityModel = entityModelClass.newInstance(); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { - throw e; + private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem) { + DataModelRepositorySearcher dataModelRepositorySearcher = springUtil.getBean(DataModelRepositorySearcher.class); + DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class); + + dataModelSearcher.getDataModelClass(subSystem, dataModel); + if(!dataModelSearcher.isPresent()){ + throw new RuntimeException("Data Model Not Found"); } - return null; + dataModelRepositorySearcher.getDataModelRepositoryClass(subSystem, dataModel); + if(!dataModelRepositorySearcher.isPresent()){ + throw new RuntimeException("Data Model Repository Not Found"); + } + return "query"; } } diff --git a/src/main/java/com/codesdream/ase/test/DataManagerTest.java b/src/main/java/com/codesdream/ase/test/DataManagerTest.java index 49e4308..9404bb5 100644 --- a/src/main/java/com/codesdream/ase/test/DataManagerTest.java +++ b/src/main/java/com/codesdream/ase/test/DataManagerTest.java @@ -1,7 +1,9 @@ package com.codesdream.ase.test; import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher; import com.codesdream.ase.component.datamanager.DataModelSearcher; +import com.codesdream.ase.repository.permission.UserRepository; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,6 +33,12 @@ public class DataManagerTest { @Test public void dataModelRepositorySearcherTest(){ + DataModelRepositorySearcher dataModelRepositorySearcher = + springUtil.getBean(DataModelRepositorySearcher.class); + + dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User"); + Assert.assertTrue(dataModelRepositorySearcher.isPresent()); + UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance(); } diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 3c55a1c..9795c21 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -11,6 +11,78 @@

Error

+ +
+
+
+

+ 检测到异常发生 +

+

+ 当服务器端程序内部某个模块抛出异常时,该界面就会被显示。此次异常的简要信息已被异常管理子系统收集并概括如下表: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 编号 + + 信息项 + + 值 +
+ 0 + + 服务器返回HTTP状态 + + +
+ 1 + + 抛出的异常类型 + + +
+ 2 + + 异常附带消息 + + +
+

+ 运行栈相关信息补充 +

+

+ +

+
+
+
diff --git a/src/main/resources/templates/query.html b/src/main/resources/templates/query.html new file mode 100644 index 0000000..de567c1 --- /dev/null +++ b/src/main/resources/templates/query.html @@ -0,0 +1,11 @@ + + + + + Title + + + + + \ No newline at end of file