From 4974bb14f1c9f5f739a6d7304e0e9f33f14c39a2 Mon Sep 17 00:00:00 2001 From: lensferno Date: Sun, 9 Jul 2023 16:03:24 +0800 Subject: [PATCH] wow --- .../rpc/exception/RpcException.java | 11 +- .../graduate/api/json/CourseTableApi.java | 52 ------ .../graduate/api/json/CreditStatusApi.java | 54 ------ .../internal/graduate/api/json/ScoreApi.java | 50 ------ .../graduate/api/json/StudentInfoApi.java | 47 ----- .../graduate/api/json/TrainingPlanApi.java | 47 ----- .../api/json/handler/ApiExceptionHandler.java | 14 -- .../graduate/services/LoginService.java | 32 ---- .../graduate/services/ParseService.java | 60 ------- .../services/RequestAgentService.java | 62 ------- .../wusthelper/internal/undergrad/Main.java | 16 ++ .../undergrad}/api/json/CookieApi.java | 10 +- .../undergrad/api/json/CourseTableApi.java | 52 ++++++ .../undergrad/api/json/CreditStatusApi.java | 49 ++++++ .../undergrad/api/json/ExamActivitiesApi.java | 48 +++++ .../api/json/ExamDelayApplicationApi.java | 52 ++++++ .../internal/undergrad/api/json/ScoreApi.java | 50 ++++++ .../undergrad/api/json/StudentInfoApi.java | 50 ++++++ .../undergrad/api/json/TrainingPlanApi.java | 48 +++++ .../json/handler/BaseExceptionHandler.java} | 12 +- .../json/handler/DefaultExceptionHandler.java | 4 +- .../api/json/handler/IOExceptionHandler.java | 4 +- .../json/handler/ParseExceptionHandler.java | 4 +- .../api/json/handler/RpcExceptionHandler.java | 17 ++ .../handler/ValidationExceptionHandler.java | 4 +- .../WebApplicationExceptionHandler.java | 26 +++ .../ResponseWrapperInterceptor.java | 6 +- .../internal/undergrad}/bean/JacksonBean.java | 2 +- .../undergrad}/bean/MywustParserBeans.java | 2 +- .../bean/MywustRequestAgentBeans.java | 2 +- .../exception/UndergradRpcException.java | 73 ++++++++ .../undergrad/services/LoginService.java | 78 ++++++++ .../undergrad/services/ParseService.java | 132 ++++++++++++++ .../services/RequestAgentService.java | 166 ++++++++++++++++++ .../library/api/json/CourseTableApi.java | 52 ------ .../library/api/json/CreditStatusApi.java | 54 ------ .../internal/library/api/json/ScoreApi.java | 50 ------ .../library/api/json/StudentInfoApi.java | 47 ----- .../library/api/json/TrainingPlanApi.java | 47 ----- .../api/json/handler/ApiExceptionHandler.java | 14 -- .../library/services/LoginService.java | 32 ---- .../library/services/ParseService.java | 60 ------- .../library/services/RequestAgentService.java | 62 ------- .../wusthelper/internal/undergrad/Main.java | 16 ++ .../undergrad}/api/json/CookieApi.java | 10 +- .../undergrad/api/json/CourseTableApi.java | 52 ++++++ .../undergrad/api/json/CreditStatusApi.java | 49 ++++++ .../undergrad/api/json/ExamActivitiesApi.java | 48 +++++ .../api/json/ExamDelayApplicationApi.java | 52 ++++++ .../internal/undergrad/api/json/ScoreApi.java | 50 ++++++ .../undergrad/api/json/StudentInfoApi.java | 50 ++++++ .../undergrad/api/json/TrainingPlanApi.java | 48 +++++ .../json/handler/BaseExceptionHandler.java} | 14 +- .../json/handler/DefaultExceptionHandler.java | 4 +- .../api/json/handler/IOExceptionHandler.java | 4 +- .../json/handler/ParseExceptionHandler.java | 4 +- .../api/json/handler/RpcExceptionHandler.java | 17 ++ .../handler/ValidationExceptionHandler.java | 4 +- .../WebApplicationExceptionHandler.java | 26 +++ .../ResponseWrapperInterceptor.java | 6 +- .../internal/undergrad}/bean/JacksonBean.java | 2 +- .../undergrad}/bean/MywustParserBeans.java | 2 +- .../bean/MywustRequestAgentBeans.java | 2 +- .../exception/UndergradRpcException.java | 73 ++++++++ .../undergrad/services/LoginService.java | 78 ++++++++ .../undergrad/services/ParseService.java | 132 ++++++++++++++ .../services/RequestAgentService.java | 166 ++++++++++++++++++ .../physics/api/json/CourseTableApi.java | 52 ------ .../physics/api/json/CreditStatusApi.java | 54 ------ .../internal/physics/api/json/ScoreApi.java | 50 ------ .../physics/api/json/StudentInfoApi.java | 47 ----- .../physics/api/json/TrainingPlanApi.java | 47 ----- .../api/json/handler/ApiExceptionHandler.java | 14 -- .../physics/services/LoginService.java | 32 ---- .../physics/services/ParseService.java | 60 ------- .../physics/services/RequestAgentService.java | 62 ------- .../wusthelper/internal/undergrad/Main.java | 16 ++ .../undergrad}/api/json/CookieApi.java | 10 +- .../undergrad/api/json/CourseTableApi.java | 52 ++++++ .../undergrad/api/json/CreditStatusApi.java | 49 ++++++ .../undergrad/api/json/ExamActivitiesApi.java | 48 +++++ .../api/json/ExamDelayApplicationApi.java | 52 ++++++ .../internal/undergrad/api/json/ScoreApi.java | 50 ++++++ .../undergrad/api/json/StudentInfoApi.java | 50 ++++++ .../undergrad/api/json/TrainingPlanApi.java | 48 +++++ .../json/handler/BaseExceptionHandler.java} | 14 +- .../json/handler/DefaultExceptionHandler.java | 4 +- .../api/json/handler/IOExceptionHandler.java | 4 +- .../json/handler/ParseExceptionHandler.java | 4 +- .../api/json/handler/RpcExceptionHandler.java | 17 ++ .../handler/ValidationExceptionHandler.java | 4 +- .../WebApplicationExceptionHandler.java | 26 +++ .../ResponseWrapperInterceptor.java | 6 +- .../internal/undergrad}/bean/JacksonBean.java | 2 +- .../undergrad}/bean/MywustParserBeans.java | 2 +- .../bean/MywustRequestAgentBeans.java | 2 +- .../exception/UndergradRpcException.java | 73 ++++++++ .../undergrad/services/LoginService.java | 78 ++++++++ .../undergrad/services/ParseService.java | 132 ++++++++++++++ .../services/RequestAgentService.java | 166 ++++++++++++++++++ .../undergrad/api/json/ExamActivitiesApi.java | 1 - .../api/json/ExamDelayApplicationApi.java | 4 +- .../json/handler/BaseExceptionHandler.java} | 14 +- .../json/handler/DefaultExceptionHandler.java | 2 +- .../api/json/handler/IOExceptionHandler.java | 2 +- .../json/handler/ParseExceptionHandler.java | 2 +- .../api/json/handler/RpcExceptionHandler.java | 5 +- .../handler/ValidationExceptionHandler.java | 2 +- .../WebApplicationExceptionHandler.java | 26 +++ .../ResponseWrapperInterceptor.java | 4 +- .../exception/UndergradRpcException.java | 3 +- 111 files changed, 2718 insertions(+), 1337 deletions(-) delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CourseTableApi.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CreditStatusApi.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/ScoreApi.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/StudentInfoApi.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/TrainingPlanApi.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ApiExceptionHandler.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/LoginService.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java delete mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/CookieApi.java (73%) create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java rename sub-services/{undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ExceptionHandlerBase.java => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java} (71%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/DefaultExceptionHandler.java (73%) rename sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/{graduate => undergrad}/api/json/handler/IOExceptionHandler.java (80%) rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/ParseExceptionHandler.java (81%) create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/ValidationExceptionHandler.java (80%) create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/interceptor/ResponseWrapperInterceptor.java (80%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/JacksonBean.java (82%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustParserBeans.java (94%) rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustRequestAgentBeans.java (97%) create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java create mode 100644 sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CourseTableApi.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CreditStatusApi.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/ScoreApi.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/StudentInfoApi.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/TrainingPlanApi.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ApiExceptionHandler.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/LoginService.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/ParseService.java delete mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/RequestAgentService.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/CookieApi.java (73%) create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ExceptionHandlerBase.java => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java} (67%) rename sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/{library => undergrad}/api/json/handler/DefaultExceptionHandler.java (73%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/IOExceptionHandler.java (80%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/ParseExceptionHandler.java (81%) create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/ValidationExceptionHandler.java (80%) create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java rename sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/{library => undergrad}/api/json/interceptor/ResponseWrapperInterceptor.java (80%) rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/JacksonBean.java (82%) rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustParserBeans.java (94%) rename sub-services/{physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics => library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustRequestAgentBeans.java (97%) create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java create mode 100644 sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CourseTableApi.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CreditStatusApi.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/ScoreApi.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/StudentInfoApi.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/TrainingPlanApi.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ApiExceptionHandler.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/LoginService.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/ParseService.java delete mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/RequestAgentService.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/CookieApi.java (73%) create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java rename sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/{physics/api/json/handler/ExceptionHandlerBase.java => undergrad/api/json/handler/BaseExceptionHandler.java} (67%) rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/DefaultExceptionHandler.java (73%) rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/IOExceptionHandler.java (80%) rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/handler/ParseExceptionHandler.java (81%) create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java rename sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/{physics => undergrad}/api/json/handler/ValidationExceptionHandler.java (80%) create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/api/json/interceptor/ResponseWrapperInterceptor.java (80%) rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/JacksonBean.java (82%) rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustParserBeans.java (94%) rename sub-services/{graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate => physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad}/bean/MywustRequestAgentBeans.java (97%) create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java create mode 100644 sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java rename sub-services/{library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ExceptionHandlerBase.java => undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java} (67%) create mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java b/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java index bb49a46..dedc48a 100644 --- a/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java +++ b/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java @@ -3,6 +3,9 @@ package cn.wustlinghang.wusthelper.rpc.exception; public class RpcException extends RuntimeException { protected final int code; + private static final int MODULE_CODE_OFFSET = 100_0000; + private static final int TYPE_CODE_OFFSET = 1_0000; + public RpcException(int code, String message) { super(message); this.code = code; @@ -13,11 +16,11 @@ public class RpcException extends RuntimeException { } public RpcException(int module, int type, int exceptionCode) { - this((module * 1000_0000) + (type * 1_0000) + (exceptionCode)); + this((module * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode)); } public RpcException(int module, int type, int exceptionCode, String message) { - this((module * 1000_0000) + (type * 1_0000) + (exceptionCode), message); + this((module * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode), message); } public int getCode() { @@ -27,4 +30,8 @@ public class RpcException extends RuntimeException { public static RpcException ApiNotImplement() { return new RpcException(-1, "接口未实现"); } + + public static int toIntCode(int module, int type, int exceptionCode) { + return (module * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode); + } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CourseTableApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CourseTableApi.java deleted file mode 100644 index 5ddb822..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CourseTableApi.java +++ /dev/null @@ -1,52 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCourseTableParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCourseTableApiService; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/course_table") -@ApplicationScoped -public class CourseTableApi { - private final UndergradCourseTableApiService studentInfoApiService; - private final UndergradCourseTableParser studentInfoPageParser; - - public CourseTableApi(UndergradCourseTableApiService studentInfoApiService, - UndergradCourseTableParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public List get(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException, ParseException { - String html = studentInfoApiService.getPage(term, cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException { - return studentInfoApiService.getPage(term, cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CreditStatusApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CreditStatusApi.java deleted file mode 100644 index 55f5248..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CreditStatusApi.java +++ /dev/null @@ -1,54 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCreditStatusParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCreditStatusApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/credit_status") -@ApplicationScoped -public class CreditStatusApi { - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradCreditStatusParser creditStatusParser; - - private final RequestClientOption option; - - public CreditStatusApi(UndergradCreditStatusApiService creditStatusApiService, - UndergradCreditStatusParser creditStatusParser, - RequestClientOption option) { - this.creditStatusApiService = creditStatusApiService; - this.creditStatusParser = creditStatusParser; - this.option = option; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = creditStatusApiService.getPage(cookie, option, false); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, option, false); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return creditStatusParser.parse(html); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/ScoreApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/ScoreApi.java deleted file mode 100644 index 305c742..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/ScoreApi.java +++ /dev/null @@ -1,50 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradScoreParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradScoreApiService; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/score") -@ApplicationScoped -public class ScoreApi { - private final UndergradScoreApiService scoreApiService; - private final UndergradScoreParser scoreParser; - - public ScoreApi(UndergradScoreApiService scoreApiService, UndergradScoreParser scoreParser) { - this.scoreApiService = scoreApiService; - this.scoreParser = scoreParser; - } - - @GET - @Path("/") - public List get(String cookie) throws IOException, ApiException, ParseException { - String html = scoreApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return scoreParser.parse(html); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/StudentInfoApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/StudentInfoApi.java deleted file mode 100644 index 2b5eaf1..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/StudentInfoApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradStudentInfoPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradStudentInfoApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/student_info") -@ApplicationScoped -public class StudentInfoApi { - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradStudentInfoPageParser studentInfoPageParser; - - public StudentInfoApi(UndergradStudentInfoApiService studentInfoApiService, - UndergradStudentInfoPageParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) throws IOException, ApiException, ParseException { - return this.parse(this.agent(cookie)); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public StudentInfo parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/TrainingPlanApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/TrainingPlanApi.java deleted file mode 100644 index e85e4c0..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/TrainingPlanApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradTrainingPlanPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradTrainingPlanApiService; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/training_plan") -@ApplicationScoped -public class TrainingPlanApi { - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - - public TrainingPlanApi(UndergradTrainingPlanApiService trainingPlanApiService, - UndergradTrainingPlanPageParser trainingPlanPageParser) { - this.trainingPlanApiService = trainingPlanApiService; - this.trainingPlanPageParser = trainingPlanPageParser; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = trainingPlanApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return trainingPlanPageParser.parse(html); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ApiExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ApiExceptionHandler.java deleted file mode 100644 index b737a8d..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ApiExceptionHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; - -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -@Provider -public class ApiExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { - @Override - public Response toResponse(ApiException e) { - return super.toResponse(e.getCodeValue(), e.toString(), "ApiExceptionHandler"); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/LoginService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/LoginService.java deleted file mode 100644 index 5dc9f78..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/LoginService.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.services; - -import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class LoginService { - private final RequestClientOption option; - private final UndergraduateLogin undergraduateLogin; - - public LoginService(RequestClientOption option, - UndergraduateLogin undergraduateLogin) { - this.option = option; - this.undergraduateLogin = undergraduateLogin; - } - - public String login(String username, String password, boolean legacy) throws IOException, ApiException { - if (legacy) { - return undergraduateLogin.getLoginCookieLegacy(username, password, option); - } else { - return undergraduateLogin.getLoginCookie(username, password, option); - } - } - - public boolean verify(String cookie) throws IOException { - return !undergraduateLogin.checkCookiesFail(cookie); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java deleted file mode 100644 index 841cc97..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/ParseService.java +++ /dev/null @@ -1,60 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.services; - -import cn.wustlinghang.mywust.core.parser.undergraduate.*; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.util.List; - -@ApplicationScoped -public class ParseService { - private final UndergradCourseTableParser courseTableParser; - private final UndergradScoreParser scoreParser; - private final UndergradStudentInfoPageParser studentInfoPageParser; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - private final UndergradCreditStatusParser creditStatusParser; - private final UndergradExamDelayParser examDelayParser; - - public ParseService(UndergradCourseTableParser courseTableParser, - UndergradScoreParser scoreParser, - UndergradStudentInfoPageParser studentInfoPageParser, - UndergradTrainingPlanPageParser trainingPlanPageParser, - UndergradCreditStatusParser creditStatusParser, - UndergradExamDelayParser examDelayParser) { - - this.courseTableParser = courseTableParser; - this.scoreParser = scoreParser; - this.studentInfoPageParser = studentInfoPageParser; - this.trainingPlanPageParser = trainingPlanPageParser; - this.creditStatusParser = creditStatusParser; - this.examDelayParser = examDelayParser; - } - - public List parseCourseTable(String data) throws ParseException { - return courseTableParser.parse(data); - } - - public List parseScore(String data) throws ParseException { - return scoreParser.parse(data); - } - - public StudentInfo parseStudentInfo(String data) throws ParseException { - return studentInfoPageParser.parse(data); - } - - public String parseTrainingPlan(String data) throws ParseException { - return trainingPlanPageParser.parse(data); - } - - public String parseCreditStatus(String data) throws ParseException { - return creditStatusParser.parse(data); - } - - public List parseExamDelayApplications(String data) throws ParseException { - return examDelayParser.parse(data); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java deleted file mode 100644 index 08803d8..0000000 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/services/RequestAgentService.java +++ /dev/null @@ -1,62 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.graduate.services; - -import cn.wustlinghang.mywust.core.request.service.undergraduate.*; -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class RequestAgentService { - private final UndergradCourseTableApiService courseTableApiService; - private final UndergradScoreApiService scoreApiService; - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradExamDelayApiService examDelayApiService; - - public RequestAgentService(UndergradCourseTableApiService courseTableApiService, - UndergradScoreApiService scoreApiService, - UndergradStudentInfoApiService studentInfoApiService, - UndergradTrainingPlanApiService trainingPlanApiService, - UndergradCreditStatusApiService creditStatusApiService, - UndergradExamDelayApiService examDelayApiService) { - - this.courseTableApiService = courseTableApiService; - this.scoreApiService = scoreApiService; - this.studentInfoApiService = studentInfoApiService; - this.trainingPlanApiService = trainingPlanApiService; - this.creditStatusApiService = creditStatusApiService; - this.examDelayApiService = examDelayApiService; - } - - public String getStudentInfoPage(String cookie) throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - public String getCourseTable(String cookie, String term) throws IOException, ApiException { - return courseTableApiService.getPage(term, cookie); - } - - public String getScore(String cookie) throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - public String getTrainingPlan(String cookie) throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - public String getCreditStatus(String cookie) throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, null, false); - } - - public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) - throws IOException, ApiException { - return examDelayApiService.getActivities(term, cookie); - } - - public String getExamDelayApplications(String cookie, String term, String activityId) - throws IOException, ApiException { - return examDelayApiService.getPage(term, activityId, cookie); - } -} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java new file mode 100644 index 0000000..edcd35f --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java @@ -0,0 +1,16 @@ +package cn.wustlinghang.wusthelper.internal.undergrad; + +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.inject.Singleton; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@Singleton +public class Main { + @PostConstruct + public void run() { + log.info("Undergrad service started."); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CookieApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java similarity index 73% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CookieApi.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java index 5995610..aa5264d 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CookieApi.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java @@ -1,7 +1,7 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.wusthelper.internal.library.services.LoginService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.LoginService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -20,13 +20,13 @@ public class CookieApi { @GET @Path("/") public String login(@QueryParam("username") @NotNull String username, - @QueryParam("password") @NotNull String password) throws IOException, ApiException { + @QueryParam("password") @NotNull String password) throws RpcException { return loginService.login(username, password, false); } @GET @Path("/verify") - public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws IOException { + public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws RpcException { return loginService.verify(cookie); } } diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java new file mode 100644 index 0000000..ec94278 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/course_table") +@ApplicationScoped +public class CourseTableApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CourseTableApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return this.parse(this.agent(cookie, term)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return agentService.getCourseTable(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseCourseTable(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java new file mode 100644 index 0000000..258d5f3 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java @@ -0,0 +1,49 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/credit_status") +@ApplicationScoped +public class CreditStatusApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CreditStatusApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + String html = this.agent(cookie); + return this.parse(html); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getCreditStatus(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseCreditStatus(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java new file mode 100644 index 0000000..9f0011d --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_activities") +@ApplicationScoped +public class ExamActivitiesApi { + private final RequestAgentService agentService; + + public ExamActivitiesApi(RequestAgentService agentService) { + this.agentService = agentService; + } + + @GET + @Path("/") + public UndergradExamDelayApiService.ExamActivity[] get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return this.agent(cookie, term); + } + + @GET + @Path("/agent") + public UndergradExamDelayApiService.ExamActivity[] agent( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return agentService.getExamActivities(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + throw RpcException.ApiNotImplement(); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java new file mode 100644 index 0000000..8cc0287 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_delay_application") +@ApplicationScoped +public class ExamDelayApplicationApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ExamDelayApplicationApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return this.parse(this.agent(cookie, term, activityId)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return agentService.getExamDelayApplications(cookie, term, activityId); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseExamDelayApplications(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java new file mode 100644 index 0000000..6435534 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/score") +@ApplicationScoped +public class ScoreApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ScoreApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getScore(cookie); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseScore(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java new file mode 100644 index 0000000..3e09b2b --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/student_info") +@ApplicationScoped +public class StudentInfoApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public StudentInfoApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getStudentInfoPage(cookie); + } + + @POST + @Path("/parse") + public StudentInfo parse(String html) throws RpcException { + return parseService.parseStudentInfo(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java new file mode 100644 index 0000000..aeebf99 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/training_plan") +@ApplicationScoped +public class TrainingPlanApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public TrainingPlanApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getTrainingPlan(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseTrainingPlan(html); + } +} diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ExceptionHandlerBase.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java similarity index 71% rename from sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ExceptionHandlerBase.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java index a875ca6..7b284d7 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ExceptionHandlerBase.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -public abstract class ExceptionHandlerBase { +public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -17,6 +17,14 @@ public abstract class ExceptionHandlerBase { } public Response toResponse(int code, String msg, String handlerName) { + return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + } + + public Response toResponse(int status, int code, String msg, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + } + + public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); @@ -24,7 +32,7 @@ public abstract class ExceptionHandlerBase { response = RpcResponse.error(ResponseCode.SERVER_INTERNAL_ERROR); } - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + return Response.status(status) .entity(response) .header(EXCEPTION_HEADER_KEY, handlerName) .type(MediaType.APPLICATION_JSON) diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/DefaultExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java similarity index 73% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/DefaultExceptionHandler.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java index 07cdfbe..47ae2c2 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/DefaultExceptionHandler.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class DefaultExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/IOExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java similarity index 80% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/IOExceptionHandler.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java index 6a55b11..73274f1 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/IOExceptionHandler.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -10,7 +10,7 @@ import java.io.IOException; @Slf4j @Provider -public class IOExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ParseExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java similarity index 81% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ParseExceptionHandler.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java index fad268f..8d15675 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ParseExceptionHandler.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ParseExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java new file mode 100644 index 0000000..a5652b3 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java @@ -0,0 +1,17 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(RpcException e) { + return super.toResponse( + e.getCode(), + e.toString(), + "RpcExceptionHandler"); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ValidationExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java similarity index 80% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ValidationExceptionHandler.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java index 591abbd..a5c3e15 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ValidationExceptionHandler.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.validation.ValidationException; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ValidationExceptionHandler extends ExceptionHandlerBase +public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java new file mode 100644 index 0000000..bed20bd --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java @@ -0,0 +1,26 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(WebApplicationException e) { + return super.toResponse( + e.getResponse().getStatus(), + RpcException.toIntCode( + UndergradRpcException.UNDERGRAD_MODULE, + UndergradRpcException.SubModuleCode.COMMON.ordinal(), + UndergradRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 + + UndergradRpcException.ErrorCode.REQUEST_INVALID.ordinal() + ), + e.getMessage(), + "WebApplicationExceptionHandler" + ); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/interceptor/ResponseWrapperInterceptor.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java similarity index 80% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/interceptor/ResponseWrapperInterceptor.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java index 5c7fcbf..a6b16f4 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java @@ -1,6 +1,6 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.interceptor; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.interceptor; -import cn.wustlinghang.wusthelper.internal.physics.api.json.handler.ExceptionHandlerBase; +import cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler.BaseExceptionHandler; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; @@ -22,7 +22,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { - boolean hasException = context.getHeaders().get(ExceptionHandlerBase.EXCEPTION_HEADER_KEY) != null; + boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); RpcResponse wrappedResponse = RpcResponse.success(data); diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/JacksonBean.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java similarity index 82% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/JacksonBean.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java index 3eafad3..c95e91d 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/JacksonBean.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustParserBeans.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java similarity index 94% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustParserBeans.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java index 7577969..a9fbc00 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustParserBeans.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.parser.undergraduate.*; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustRequestAgentBeans.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java similarity index 97% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustRequestAgentBeans.java rename to sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java index c67d89d..b959e45 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustRequestAgentBeans.java +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; import cn.wustlinghang.mywust.core.request.service.undergraduate.*; diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java new file mode 100644 index 0000000..a8ed179 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java @@ -0,0 +1,73 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.exception; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; + +import java.util.StringJoiner; + +public class UndergradRpcException extends RpcException { + public static final int UNDERGRAD_MODULE = 1; + + public UndergradRpcException(TypeCode typeCode, + SubModuleCode subModuleCode, + ErrorCode errorCode) { + super(UNDERGRAD_MODULE, + typeCode.ordinal(), + subModuleCode.ordinal() * 100 + errorCode.ordinal(), + new StringJoiner("-") + .add("UNDERGRAD") + .add(typeCode.name()) + .add(subModuleCode.name()) + .add(errorCode.name()) + .toString() + ); + } + + /** + * 异常类型编码 + */ + public enum TypeCode { + // 网络异常类型 + NETWORK_EXCEPTION, + // 参数异常类型 + PARAM_EXCEPTION, + // 权限认证异常类型 + AUTH_EXCEPTION, + // 网页解析异常类型 + PARSE_EXCEPTION, + // 其他的异常类型 + OTHER_EXCEPTION + } + + /** + * 子模块编码 + */ + public enum SubModuleCode { + COMMON, AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, + STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION + } + + /** + * 具体错误编码 + */ + public enum ErrorCode { + REQUEST_INVALID, + PARAM_INVALID, + COOKIE_INVALID, + NETWORK_ERROR, + PARSE_ERROR, + + // 需要评教 + NEED_EVALUATE, + + AUTH_PASSWORD_WRONG, + // 用户不存在 + AUTH_USER_NOT_EXISTS, + // 封号 + AUTH_USER_BANNED, + // 用户账号禁用 + AUTH_USER_DISABLED, + // 用户账号需要更改 + AUTH_NEED_CHANGE_PASSWORD, + AUTH_UNKNOWN_ERROR + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java new file mode 100644 index 0000000..a6d573d --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java @@ -0,0 +1,78 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class LoginService { + private final RequestClientOption option; + private final UndergraduateLogin undergraduateLogin; + + public LoginService(RequestClientOption option, + UndergraduateLogin undergraduateLogin) { + this.option = option; + this.undergraduateLogin = undergraduateLogin; + } + + public String login(String username, String password, boolean legacy) throws UndergradRpcException { + try { + if (legacy) { + return undergraduateLogin.getLoginCookieLegacy(username, password, option); + } else { + return undergraduateLogin.getLoginCookie(username, password, option); + } + } catch (ApiException e) { + throw wrapApiException(e); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public boolean verify(String cookie) throws UndergradRpcException { + try { + return !undergraduateLogin.checkCookiesFail(cookie); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e) { + UndergradRpcException.ErrorCode errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> UndergradRpcException.ErrorCode.NETWORK_ERROR; + case UNI_LOGIN_PASSWORD_WRONG -> UndergradRpcException.ErrorCode.AUTH_PASSWORD_WRONG; + case UNI_LOGIN_USER_NOT_EXISTS -> UndergradRpcException.ErrorCode.AUTH_USER_NOT_EXISTS; + case UNI_LOGIN_USER_BANNED -> UndergradRpcException.ErrorCode.AUTH_USER_BANNED; + case UNI_LOGIN_USER_DISABLED -> UndergradRpcException.ErrorCode.AUTH_USER_DISABLED; + case UNI_LOGIN_NEED_CHANGE_PASSWORD -> UndergradRpcException.ErrorCode.AUTH_NEED_CHANGE_PASSWORD; + case UNI_LOGIN_USER_NOT_ONLY -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NO_REGISTER -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NEED_TFA -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + default -> { + log.error("本科生:登录代理请求异常,异常未处理"); + log.error("异常:", e); + yield UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + } + }; + + return new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + errorCode + ); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java new file mode 100644 index 0000000..22aae56 --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java @@ -0,0 +1,132 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.parser.undergraduate.*; +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.util.List; + +@ApplicationScoped +public class ParseService { + private final UndergradCourseTableParser courseTableParser; + private final UndergradScoreParser scoreParser; + private final UndergradStudentInfoPageParser studentInfoPageParser; + private final UndergradTrainingPlanPageParser trainingPlanPageParser; + private final UndergradCreditStatusParser creditStatusParser; + private final UndergradExamDelayParser examDelayParser; + + public ParseService(UndergradCourseTableParser courseTableParser, + UndergradScoreParser scoreParser, + UndergradStudentInfoPageParser studentInfoPageParser, + UndergradTrainingPlanPageParser trainingPlanPageParser, + UndergradCreditStatusParser creditStatusParser, + UndergradExamDelayParser examDelayParser) { + + this.courseTableParser = courseTableParser; + this.scoreParser = scoreParser; + this.studentInfoPageParser = studentInfoPageParser; + this.trainingPlanPageParser = trainingPlanPageParser; + this.creditStatusParser = creditStatusParser; + this.examDelayParser = examDelayParser; + } + + public List parseCourseTable(String data) throws UndergradRpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return courseTableParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseScore(String data) throws RpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return scoreParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public StudentInfo parseStudentInfo(String data) throws RpcException { + try { + return studentInfoPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseTrainingPlan(String data) throws RpcException { + try { + return trainingPlanPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseCreditStatus(String data) throws RpcException { + try { + return creditStatusParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseExamDelayApplications(String data) throws RpcException { + try { + return examDelayParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java new file mode 100644 index 0000000..e608b5e --- /dev/null +++ b/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java @@ -0,0 +1,166 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.*; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class RequestAgentService { + private final UndergradCourseTableApiService courseTableApiService; + private final UndergradScoreApiService scoreApiService; + private final UndergradStudentInfoApiService studentInfoApiService; + private final UndergradTrainingPlanApiService trainingPlanApiService; + private final UndergradCreditStatusApiService creditStatusApiService; + private final UndergradExamDelayApiService examDelayApiService; + + private final RequestClientOption requestClientOption; + + public RequestAgentService(UndergradCourseTableApiService courseTableApiService, + UndergradScoreApiService scoreApiService, + UndergradStudentInfoApiService studentInfoApiService, + UndergradTrainingPlanApiService trainingPlanApiService, + UndergradCreditStatusApiService creditStatusApiService, + UndergradExamDelayApiService examDelayApiService, + RequestClientOption requestClientOption) { + + this.courseTableApiService = courseTableApiService; + this.scoreApiService = scoreApiService; + this.studentInfoApiService = studentInfoApiService; + this.trainingPlanApiService = trainingPlanApiService; + this.creditStatusApiService = creditStatusApiService; + this.examDelayApiService = examDelayApiService; + + this.requestClientOption = requestClientOption; + } + + public String getStudentInfoPage(String cookie) throws RpcException { + try { + return studentInfoApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.STUDENT_INFO); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCourseTable(String cookie, String term) throws RpcException { + try { + return courseTableApiService.getPage(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.COURSE_TABLE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getScore(String cookie) throws RpcException { + try { + return scoreApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.SCORE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getTrainingPlan(String cookie) throws RpcException { + try { + return trainingPlanApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.TRAINING_PLAN); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCreditStatus(String cookie) throws RpcException { + try { + return creditStatusApiService.getPage(cookie, requestClientOption, false); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.CREDIT_STATUS); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) + throws RpcException { + try { + return examDelayApiService.getActivities(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getExamDelayApplications(String cookie, String term, String activityId) + throws RpcException { + try { + return examDelayApiService.getPage(term, activityId, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e, UndergradRpcException.SubModuleCode subModuleCode) { + return switch (e.getCode()) { + case NETWORK_EXCEPTION -> new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + case COOKIE_INVALID -> new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + default -> { + log.error("本科生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("异常:", e); + yield new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + } + }; + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CourseTableApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CourseTableApi.java deleted file mode 100644 index 5b51909..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CourseTableApi.java +++ /dev/null @@ -1,52 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCourseTableParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCourseTableApiService; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/course_table") -@ApplicationScoped -public class CourseTableApi { - private final UndergradCourseTableApiService studentInfoApiService; - private final UndergradCourseTableParser studentInfoPageParser; - - public CourseTableApi(UndergradCourseTableApiService studentInfoApiService, - UndergradCourseTableParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public List get(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException, ParseException { - String html = studentInfoApiService.getPage(term, cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException { - return studentInfoApiService.getPage(term, cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CreditStatusApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CreditStatusApi.java deleted file mode 100644 index bc15c96..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/CreditStatusApi.java +++ /dev/null @@ -1,54 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCreditStatusParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCreditStatusApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/credit_status") -@ApplicationScoped -public class CreditStatusApi { - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradCreditStatusParser creditStatusParser; - - private final RequestClientOption option; - - public CreditStatusApi(UndergradCreditStatusApiService creditStatusApiService, - UndergradCreditStatusParser creditStatusParser, - RequestClientOption option) { - this.creditStatusApiService = creditStatusApiService; - this.creditStatusParser = creditStatusParser; - this.option = option; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = creditStatusApiService.getPage(cookie, option, false); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, option, false); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return creditStatusParser.parse(html); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/ScoreApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/ScoreApi.java deleted file mode 100644 index 61690af..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/ScoreApi.java +++ /dev/null @@ -1,50 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradScoreParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradScoreApiService; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/score") -@ApplicationScoped -public class ScoreApi { - private final UndergradScoreApiService scoreApiService; - private final UndergradScoreParser scoreParser; - - public ScoreApi(UndergradScoreApiService scoreApiService, UndergradScoreParser scoreParser) { - this.scoreApiService = scoreApiService; - this.scoreParser = scoreParser; - } - - @GET - @Path("/") - public List get(String cookie) throws IOException, ApiException, ParseException { - String html = scoreApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return scoreParser.parse(html); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/StudentInfoApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/StudentInfoApi.java deleted file mode 100644 index d676834..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/StudentInfoApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradStudentInfoPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradStudentInfoApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/student_info") -@ApplicationScoped -public class StudentInfoApi { - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradStudentInfoPageParser studentInfoPageParser; - - public StudentInfoApi(UndergradStudentInfoApiService studentInfoApiService, - UndergradStudentInfoPageParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) throws IOException, ApiException, ParseException { - return this.parse(this.agent(cookie)); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public StudentInfo parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/TrainingPlanApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/TrainingPlanApi.java deleted file mode 100644 index c228e48..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/TrainingPlanApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradTrainingPlanPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradTrainingPlanApiService; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/training_plan") -@ApplicationScoped -public class TrainingPlanApi { - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - - public TrainingPlanApi(UndergradTrainingPlanApiService trainingPlanApiService, - UndergradTrainingPlanPageParser trainingPlanPageParser) { - this.trainingPlanApiService = trainingPlanApiService; - this.trainingPlanPageParser = trainingPlanPageParser; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = trainingPlanApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return trainingPlanPageParser.parse(html); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ApiExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ApiExceptionHandler.java deleted file mode 100644 index 517164d..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ApiExceptionHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; - -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -@Provider -public class ApiExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { - @Override - public Response toResponse(ApiException e) { - return super.toResponse(e.getCodeValue(), e.toString(), "ApiExceptionHandler"); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/LoginService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/LoginService.java deleted file mode 100644 index e8c0774..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/LoginService.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.services; - -import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class LoginService { - private final RequestClientOption option; - private final UndergraduateLogin undergraduateLogin; - - public LoginService(RequestClientOption option, - UndergraduateLogin undergraduateLogin) { - this.option = option; - this.undergraduateLogin = undergraduateLogin; - } - - public String login(String username, String password, boolean legacy) throws IOException, ApiException { - if (legacy) { - return undergraduateLogin.getLoginCookieLegacy(username, password, option); - } else { - return undergraduateLogin.getLoginCookie(username, password, option); - } - } - - public boolean verify(String cookie) throws IOException { - return !undergraduateLogin.checkCookiesFail(cookie); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/ParseService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/ParseService.java deleted file mode 100644 index 4094e1b..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/ParseService.java +++ /dev/null @@ -1,60 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.services; - -import cn.wustlinghang.mywust.core.parser.undergraduate.*; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.util.List; - -@ApplicationScoped -public class ParseService { - private final UndergradCourseTableParser courseTableParser; - private final UndergradScoreParser scoreParser; - private final UndergradStudentInfoPageParser studentInfoPageParser; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - private final UndergradCreditStatusParser creditStatusParser; - private final UndergradExamDelayParser examDelayParser; - - public ParseService(UndergradCourseTableParser courseTableParser, - UndergradScoreParser scoreParser, - UndergradStudentInfoPageParser studentInfoPageParser, - UndergradTrainingPlanPageParser trainingPlanPageParser, - UndergradCreditStatusParser creditStatusParser, - UndergradExamDelayParser examDelayParser) { - - this.courseTableParser = courseTableParser; - this.scoreParser = scoreParser; - this.studentInfoPageParser = studentInfoPageParser; - this.trainingPlanPageParser = trainingPlanPageParser; - this.creditStatusParser = creditStatusParser; - this.examDelayParser = examDelayParser; - } - - public List parseCourseTable(String data) throws ParseException { - return courseTableParser.parse(data); - } - - public List parseScore(String data) throws ParseException { - return scoreParser.parse(data); - } - - public StudentInfo parseStudentInfo(String data) throws ParseException { - return studentInfoPageParser.parse(data); - } - - public String parseTrainingPlan(String data) throws ParseException { - return trainingPlanPageParser.parse(data); - } - - public String parseCreditStatus(String data) throws ParseException { - return creditStatusParser.parse(data); - } - - public List parseExamDelayApplications(String data) throws ParseException { - return examDelayParser.parse(data); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/RequestAgentService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/RequestAgentService.java deleted file mode 100644 index 4675c2a..0000000 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/services/RequestAgentService.java +++ /dev/null @@ -1,62 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.library.services; - -import cn.wustlinghang.mywust.core.request.service.undergraduate.*; -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class RequestAgentService { - private final UndergradCourseTableApiService courseTableApiService; - private final UndergradScoreApiService scoreApiService; - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradExamDelayApiService examDelayApiService; - - public RequestAgentService(UndergradCourseTableApiService courseTableApiService, - UndergradScoreApiService scoreApiService, - UndergradStudentInfoApiService studentInfoApiService, - UndergradTrainingPlanApiService trainingPlanApiService, - UndergradCreditStatusApiService creditStatusApiService, - UndergradExamDelayApiService examDelayApiService) { - - this.courseTableApiService = courseTableApiService; - this.scoreApiService = scoreApiService; - this.studentInfoApiService = studentInfoApiService; - this.trainingPlanApiService = trainingPlanApiService; - this.creditStatusApiService = creditStatusApiService; - this.examDelayApiService = examDelayApiService; - } - - public String getStudentInfoPage(String cookie) throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - public String getCourseTable(String cookie, String term) throws IOException, ApiException { - return courseTableApiService.getPage(term, cookie); - } - - public String getScore(String cookie) throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - public String getTrainingPlan(String cookie) throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - public String getCreditStatus(String cookie) throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, null, false); - } - - public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) - throws IOException, ApiException { - return examDelayApiService.getActivities(term, cookie); - } - - public String getExamDelayApplications(String cookie, String term, String activityId) - throws IOException, ApiException { - return examDelayApiService.getPage(term, activityId, cookie); - } -} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java new file mode 100644 index 0000000..edcd35f --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java @@ -0,0 +1,16 @@ +package cn.wustlinghang.wusthelper.internal.undergrad; + +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.inject.Singleton; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@Singleton +public class Main { + @PostConstruct + public void run() { + log.info("Undergrad service started."); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CookieApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java similarity index 73% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CookieApi.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java index 5b7bcc9..aa5264d 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CookieApi.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java @@ -1,7 +1,7 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.wusthelper.internal.physics.services.LoginService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.LoginService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -20,13 +20,13 @@ public class CookieApi { @GET @Path("/") public String login(@QueryParam("username") @NotNull String username, - @QueryParam("password") @NotNull String password) throws IOException, ApiException { + @QueryParam("password") @NotNull String password) throws RpcException { return loginService.login(username, password, false); } @GET @Path("/verify") - public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws IOException { + public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws RpcException { return loginService.verify(cookie); } } diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java new file mode 100644 index 0000000..ec94278 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/course_table") +@ApplicationScoped +public class CourseTableApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CourseTableApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return this.parse(this.agent(cookie, term)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return agentService.getCourseTable(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseCourseTable(html); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java new file mode 100644 index 0000000..258d5f3 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java @@ -0,0 +1,49 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/credit_status") +@ApplicationScoped +public class CreditStatusApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CreditStatusApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + String html = this.agent(cookie); + return this.parse(html); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getCreditStatus(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseCreditStatus(html); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java new file mode 100644 index 0000000..9f0011d --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_activities") +@ApplicationScoped +public class ExamActivitiesApi { + private final RequestAgentService agentService; + + public ExamActivitiesApi(RequestAgentService agentService) { + this.agentService = agentService; + } + + @GET + @Path("/") + public UndergradExamDelayApiService.ExamActivity[] get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return this.agent(cookie, term); + } + + @GET + @Path("/agent") + public UndergradExamDelayApiService.ExamActivity[] agent( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return agentService.getExamActivities(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + throw RpcException.ApiNotImplement(); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java new file mode 100644 index 0000000..8cc0287 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_delay_application") +@ApplicationScoped +public class ExamDelayApplicationApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ExamDelayApplicationApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return this.parse(this.agent(cookie, term, activityId)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return agentService.getExamDelayApplications(cookie, term, activityId); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseExamDelayApplications(html); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java new file mode 100644 index 0000000..6435534 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/score") +@ApplicationScoped +public class ScoreApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ScoreApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getScore(cookie); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseScore(html); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java new file mode 100644 index 0000000..3e09b2b --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/student_info") +@ApplicationScoped +public class StudentInfoApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public StudentInfoApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getStudentInfoPage(cookie); + } + + @POST + @Path("/parse") + public StudentInfo parse(String html) throws RpcException { + return parseService.parseStudentInfo(html); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java new file mode 100644 index 0000000..aeebf99 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/training_plan") +@ApplicationScoped +public class TrainingPlanApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public TrainingPlanApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getTrainingPlan(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseTrainingPlan(html); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ExceptionHandlerBase.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java similarity index 67% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ExceptionHandlerBase.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java index b11c2b1..7b284d7 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ExceptionHandlerBase.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -public abstract class ExceptionHandlerBase { +public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -17,6 +17,14 @@ public abstract class ExceptionHandlerBase { } public Response toResponse(int code, String msg, String handlerName) { + return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + } + + public Response toResponse(int status, int code, String msg, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + } + + public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); @@ -24,7 +32,7 @@ public abstract class ExceptionHandlerBase { response = RpcResponse.error(ResponseCode.SERVER_INTERNAL_ERROR); } - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + return Response.status(status) .entity(response) .header(EXCEPTION_HEADER_KEY, handlerName) .type(MediaType.APPLICATION_JSON) diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/DefaultExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java similarity index 73% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/DefaultExceptionHandler.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java index f3442bf..47ae2c2 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/DefaultExceptionHandler.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class DefaultExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/IOExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java similarity index 80% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/IOExceptionHandler.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java index 4175a13..73274f1 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/IOExceptionHandler.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -10,7 +10,7 @@ import java.io.IOException; @Slf4j @Provider -public class IOExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ParseExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java similarity index 81% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ParseExceptionHandler.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java index 0d12553..8d15675 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ParseExceptionHandler.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ParseExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java new file mode 100644 index 0000000..a5652b3 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java @@ -0,0 +1,17 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(RpcException e) { + return super.toResponse( + e.getCode(), + e.toString(), + "RpcExceptionHandler"); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ValidationExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java similarity index 80% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ValidationExceptionHandler.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java index 36ca773..a5c3e15 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ValidationExceptionHandler.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.validation.ValidationException; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ValidationExceptionHandler extends ExceptionHandlerBase +public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java new file mode 100644 index 0000000..bed20bd --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java @@ -0,0 +1,26 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(WebApplicationException e) { + return super.toResponse( + e.getResponse().getStatus(), + RpcException.toIntCode( + UndergradRpcException.UNDERGRAD_MODULE, + UndergradRpcException.SubModuleCode.COMMON.ordinal(), + UndergradRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 + + UndergradRpcException.ErrorCode.REQUEST_INVALID.ordinal() + ), + e.getMessage(), + "WebApplicationExceptionHandler" + ); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/interceptor/ResponseWrapperInterceptor.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java similarity index 80% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/interceptor/ResponseWrapperInterceptor.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java index 408f87b..a6b16f4 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java @@ -1,6 +1,6 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.interceptor; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.interceptor; -import cn.wustlinghang.wusthelper.internal.library.api.json.handler.ExceptionHandlerBase; +import cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler.BaseExceptionHandler; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; @@ -22,7 +22,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { - boolean hasException = context.getHeaders().get(ExceptionHandlerBase.EXCEPTION_HEADER_KEY) != null; + boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); RpcResponse wrappedResponse = RpcResponse.success(data); diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/JacksonBean.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java similarity index 82% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/JacksonBean.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java index d8b06f9..c95e91d 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/JacksonBean.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustParserBeans.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java similarity index 94% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustParserBeans.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java index d833e13..a9fbc00 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustParserBeans.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.parser.undergraduate.*; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustRequestAgentBeans.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java similarity index 97% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustRequestAgentBeans.java rename to sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java index df15ceb..b959e45 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/bean/MywustRequestAgentBeans.java +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; import cn.wustlinghang.mywust.core.request.service.undergraduate.*; diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java new file mode 100644 index 0000000..a8ed179 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java @@ -0,0 +1,73 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.exception; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; + +import java.util.StringJoiner; + +public class UndergradRpcException extends RpcException { + public static final int UNDERGRAD_MODULE = 1; + + public UndergradRpcException(TypeCode typeCode, + SubModuleCode subModuleCode, + ErrorCode errorCode) { + super(UNDERGRAD_MODULE, + typeCode.ordinal(), + subModuleCode.ordinal() * 100 + errorCode.ordinal(), + new StringJoiner("-") + .add("UNDERGRAD") + .add(typeCode.name()) + .add(subModuleCode.name()) + .add(errorCode.name()) + .toString() + ); + } + + /** + * 异常类型编码 + */ + public enum TypeCode { + // 网络异常类型 + NETWORK_EXCEPTION, + // 参数异常类型 + PARAM_EXCEPTION, + // 权限认证异常类型 + AUTH_EXCEPTION, + // 网页解析异常类型 + PARSE_EXCEPTION, + // 其他的异常类型 + OTHER_EXCEPTION + } + + /** + * 子模块编码 + */ + public enum SubModuleCode { + COMMON, AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, + STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION + } + + /** + * 具体错误编码 + */ + public enum ErrorCode { + REQUEST_INVALID, + PARAM_INVALID, + COOKIE_INVALID, + NETWORK_ERROR, + PARSE_ERROR, + + // 需要评教 + NEED_EVALUATE, + + AUTH_PASSWORD_WRONG, + // 用户不存在 + AUTH_USER_NOT_EXISTS, + // 封号 + AUTH_USER_BANNED, + // 用户账号禁用 + AUTH_USER_DISABLED, + // 用户账号需要更改 + AUTH_NEED_CHANGE_PASSWORD, + AUTH_UNKNOWN_ERROR + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java new file mode 100644 index 0000000..a6d573d --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java @@ -0,0 +1,78 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class LoginService { + private final RequestClientOption option; + private final UndergraduateLogin undergraduateLogin; + + public LoginService(RequestClientOption option, + UndergraduateLogin undergraduateLogin) { + this.option = option; + this.undergraduateLogin = undergraduateLogin; + } + + public String login(String username, String password, boolean legacy) throws UndergradRpcException { + try { + if (legacy) { + return undergraduateLogin.getLoginCookieLegacy(username, password, option); + } else { + return undergraduateLogin.getLoginCookie(username, password, option); + } + } catch (ApiException e) { + throw wrapApiException(e); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public boolean verify(String cookie) throws UndergradRpcException { + try { + return !undergraduateLogin.checkCookiesFail(cookie); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e) { + UndergradRpcException.ErrorCode errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> UndergradRpcException.ErrorCode.NETWORK_ERROR; + case UNI_LOGIN_PASSWORD_WRONG -> UndergradRpcException.ErrorCode.AUTH_PASSWORD_WRONG; + case UNI_LOGIN_USER_NOT_EXISTS -> UndergradRpcException.ErrorCode.AUTH_USER_NOT_EXISTS; + case UNI_LOGIN_USER_BANNED -> UndergradRpcException.ErrorCode.AUTH_USER_BANNED; + case UNI_LOGIN_USER_DISABLED -> UndergradRpcException.ErrorCode.AUTH_USER_DISABLED; + case UNI_LOGIN_NEED_CHANGE_PASSWORD -> UndergradRpcException.ErrorCode.AUTH_NEED_CHANGE_PASSWORD; + case UNI_LOGIN_USER_NOT_ONLY -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NO_REGISTER -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NEED_TFA -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + default -> { + log.error("本科生:登录代理请求异常,异常未处理"); + log.error("异常:", e); + yield UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + } + }; + + return new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + errorCode + ); + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java new file mode 100644 index 0000000..22aae56 --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java @@ -0,0 +1,132 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.parser.undergraduate.*; +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.util.List; + +@ApplicationScoped +public class ParseService { + private final UndergradCourseTableParser courseTableParser; + private final UndergradScoreParser scoreParser; + private final UndergradStudentInfoPageParser studentInfoPageParser; + private final UndergradTrainingPlanPageParser trainingPlanPageParser; + private final UndergradCreditStatusParser creditStatusParser; + private final UndergradExamDelayParser examDelayParser; + + public ParseService(UndergradCourseTableParser courseTableParser, + UndergradScoreParser scoreParser, + UndergradStudentInfoPageParser studentInfoPageParser, + UndergradTrainingPlanPageParser trainingPlanPageParser, + UndergradCreditStatusParser creditStatusParser, + UndergradExamDelayParser examDelayParser) { + + this.courseTableParser = courseTableParser; + this.scoreParser = scoreParser; + this.studentInfoPageParser = studentInfoPageParser; + this.trainingPlanPageParser = trainingPlanPageParser; + this.creditStatusParser = creditStatusParser; + this.examDelayParser = examDelayParser; + } + + public List parseCourseTable(String data) throws UndergradRpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return courseTableParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseScore(String data) throws RpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return scoreParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public StudentInfo parseStudentInfo(String data) throws RpcException { + try { + return studentInfoPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseTrainingPlan(String data) throws RpcException { + try { + return trainingPlanPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseCreditStatus(String data) throws RpcException { + try { + return creditStatusParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseExamDelayApplications(String data) throws RpcException { + try { + return examDelayParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java new file mode 100644 index 0000000..e608b5e --- /dev/null +++ b/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java @@ -0,0 +1,166 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.*; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class RequestAgentService { + private final UndergradCourseTableApiService courseTableApiService; + private final UndergradScoreApiService scoreApiService; + private final UndergradStudentInfoApiService studentInfoApiService; + private final UndergradTrainingPlanApiService trainingPlanApiService; + private final UndergradCreditStatusApiService creditStatusApiService; + private final UndergradExamDelayApiService examDelayApiService; + + private final RequestClientOption requestClientOption; + + public RequestAgentService(UndergradCourseTableApiService courseTableApiService, + UndergradScoreApiService scoreApiService, + UndergradStudentInfoApiService studentInfoApiService, + UndergradTrainingPlanApiService trainingPlanApiService, + UndergradCreditStatusApiService creditStatusApiService, + UndergradExamDelayApiService examDelayApiService, + RequestClientOption requestClientOption) { + + this.courseTableApiService = courseTableApiService; + this.scoreApiService = scoreApiService; + this.studentInfoApiService = studentInfoApiService; + this.trainingPlanApiService = trainingPlanApiService; + this.creditStatusApiService = creditStatusApiService; + this.examDelayApiService = examDelayApiService; + + this.requestClientOption = requestClientOption; + } + + public String getStudentInfoPage(String cookie) throws RpcException { + try { + return studentInfoApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.STUDENT_INFO); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCourseTable(String cookie, String term) throws RpcException { + try { + return courseTableApiService.getPage(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.COURSE_TABLE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getScore(String cookie) throws RpcException { + try { + return scoreApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.SCORE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getTrainingPlan(String cookie) throws RpcException { + try { + return trainingPlanApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.TRAINING_PLAN); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCreditStatus(String cookie) throws RpcException { + try { + return creditStatusApiService.getPage(cookie, requestClientOption, false); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.CREDIT_STATUS); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) + throws RpcException { + try { + return examDelayApiService.getActivities(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getExamDelayApplications(String cookie, String term, String activityId) + throws RpcException { + try { + return examDelayApiService.getPage(term, activityId, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e, UndergradRpcException.SubModuleCode subModuleCode) { + return switch (e.getCode()) { + case NETWORK_EXCEPTION -> new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + case COOKIE_INVALID -> new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + default -> { + log.error("本科生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("异常:", e); + yield new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + } + }; + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CourseTableApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CourseTableApi.java deleted file mode 100644 index cf29158..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CourseTableApi.java +++ /dev/null @@ -1,52 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCourseTableParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCourseTableApiService; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/course_table") -@ApplicationScoped -public class CourseTableApi { - private final UndergradCourseTableApiService studentInfoApiService; - private final UndergradCourseTableParser studentInfoPageParser; - - public CourseTableApi(UndergradCourseTableApiService studentInfoApiService, - UndergradCourseTableParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public List get(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException, ParseException { - String html = studentInfoApiService.getPage(term, cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) - throws IOException, ApiException { - return studentInfoApiService.getPage(term, cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CreditStatusApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CreditStatusApi.java deleted file mode 100644 index 2bf505c..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/CreditStatusApi.java +++ /dev/null @@ -1,54 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCreditStatusParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCreditStatusApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/credit_status") -@ApplicationScoped -public class CreditStatusApi { - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradCreditStatusParser creditStatusParser; - - private final RequestClientOption option; - - public CreditStatusApi(UndergradCreditStatusApiService creditStatusApiService, - UndergradCreditStatusParser creditStatusParser, - RequestClientOption option) { - this.creditStatusApiService = creditStatusApiService; - this.creditStatusParser = creditStatusParser; - this.option = option; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = creditStatusApiService.getPage(cookie, option, false); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, option, false); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return creditStatusParser.parse(html); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/ScoreApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/ScoreApi.java deleted file mode 100644 index ddfd83d..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/ScoreApi.java +++ /dev/null @@ -1,50 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradScoreParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradScoreApiService; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; -import java.util.List; - -@Path("/score") -@ApplicationScoped -public class ScoreApi { - private final UndergradScoreApiService scoreApiService; - private final UndergradScoreParser scoreParser; - - public ScoreApi(UndergradScoreApiService scoreApiService, UndergradScoreParser scoreParser) { - this.scoreApiService = scoreApiService; - this.scoreParser = scoreParser; - } - - @GET - @Path("/") - public List get(String cookie) throws IOException, ApiException, ParseException { - String html = scoreApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public List parse(String html) throws ParseException { - return scoreParser.parse(html); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/StudentInfoApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/StudentInfoApi.java deleted file mode 100644 index ba46686..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/StudentInfoApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradStudentInfoPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradStudentInfoApiService; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/student_info") -@ApplicationScoped -public class StudentInfoApi { - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradStudentInfoPageParser studentInfoPageParser; - - public StudentInfoApi(UndergradStudentInfoApiService studentInfoApiService, - UndergradStudentInfoPageParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; - } - - @GET - @Path("/") - public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) throws IOException, ApiException, ParseException { - return this.parse(this.agent(cookie)); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public StudentInfo parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/TrainingPlanApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/TrainingPlanApi.java deleted file mode 100644 index 01f8a99..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/TrainingPlanApi.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json; - -import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradTrainingPlanPageParser; -import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradTrainingPlanApiService; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.QueryParam; - -import java.io.IOException; - -@Path("/training_plan") -@ApplicationScoped -public class TrainingPlanApi { - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - - public TrainingPlanApi(UndergradTrainingPlanApiService trainingPlanApiService, - UndergradTrainingPlanPageParser trainingPlanPageParser) { - this.trainingPlanApiService = trainingPlanApiService; - this.trainingPlanPageParser = trainingPlanPageParser; - } - - @GET - @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = trainingPlanApiService.getPage(cookie); - return this.parse(html); - } - - @GET - @Path("/agent") - public String agent(@QueryParam("cookie") @NotNull String cookie) - throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - @POST - @Path("/parse") - public String parse(String html) throws ParseException { - return trainingPlanPageParser.parse(html); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ApiExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ApiExceptionHandler.java deleted file mode 100644 index 76ba132..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ApiExceptionHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; - -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -@Provider -public class ApiExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { - @Override - public Response toResponse(ApiException e) { - return super.toResponse(e.getCodeValue(), e.toString(), "ApiExceptionHandler"); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/LoginService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/LoginService.java deleted file mode 100644 index 5d9f969..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/LoginService.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.services; - -import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.network.RequestClientOption; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class LoginService { - private final RequestClientOption option; - private final UndergraduateLogin undergraduateLogin; - - public LoginService(RequestClientOption option, - UndergraduateLogin undergraduateLogin) { - this.option = option; - this.undergraduateLogin = undergraduateLogin; - } - - public String login(String username, String password, boolean legacy) throws IOException, ApiException { - if (legacy) { - return undergraduateLogin.getLoginCookieLegacy(username, password, option); - } else { - return undergraduateLogin.getLoginCookie(username, password, option); - } - } - - public boolean verify(String cookie) throws IOException { - return !undergraduateLogin.checkCookiesFail(cookie); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/ParseService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/ParseService.java deleted file mode 100644 index 4387185..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/ParseService.java +++ /dev/null @@ -1,60 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.services; - -import cn.wustlinghang.mywust.core.parser.undergraduate.*; -import cn.wustlinghang.mywust.data.global.Course; -import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.mywust.data.global.StudentInfo; -import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; -import cn.wustlinghang.mywust.exception.ParseException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.util.List; - -@ApplicationScoped -public class ParseService { - private final UndergradCourseTableParser courseTableParser; - private final UndergradScoreParser scoreParser; - private final UndergradStudentInfoPageParser studentInfoPageParser; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; - private final UndergradCreditStatusParser creditStatusParser; - private final UndergradExamDelayParser examDelayParser; - - public ParseService(UndergradCourseTableParser courseTableParser, - UndergradScoreParser scoreParser, - UndergradStudentInfoPageParser studentInfoPageParser, - UndergradTrainingPlanPageParser trainingPlanPageParser, - UndergradCreditStatusParser creditStatusParser, - UndergradExamDelayParser examDelayParser) { - - this.courseTableParser = courseTableParser; - this.scoreParser = scoreParser; - this.studentInfoPageParser = studentInfoPageParser; - this.trainingPlanPageParser = trainingPlanPageParser; - this.creditStatusParser = creditStatusParser; - this.examDelayParser = examDelayParser; - } - - public List parseCourseTable(String data) throws ParseException { - return courseTableParser.parse(data); - } - - public List parseScore(String data) throws ParseException { - return scoreParser.parse(data); - } - - public StudentInfo parseStudentInfo(String data) throws ParseException { - return studentInfoPageParser.parse(data); - } - - public String parseTrainingPlan(String data) throws ParseException { - return trainingPlanPageParser.parse(data); - } - - public String parseCreditStatus(String data) throws ParseException { - return creditStatusParser.parse(data); - } - - public List parseExamDelayApplications(String data) throws ParseException { - return examDelayParser.parse(data); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/RequestAgentService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/RequestAgentService.java deleted file mode 100644 index ca02d79..0000000 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/services/RequestAgentService.java +++ /dev/null @@ -1,62 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.physics.services; - -import cn.wustlinghang.mywust.core.request.service.undergraduate.*; -import cn.wustlinghang.mywust.exception.ApiException; -import jakarta.enterprise.context.ApplicationScoped; - -import java.io.IOException; - -@ApplicationScoped -public class RequestAgentService { - private final UndergradCourseTableApiService courseTableApiService; - private final UndergradScoreApiService scoreApiService; - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradExamDelayApiService examDelayApiService; - - public RequestAgentService(UndergradCourseTableApiService courseTableApiService, - UndergradScoreApiService scoreApiService, - UndergradStudentInfoApiService studentInfoApiService, - UndergradTrainingPlanApiService trainingPlanApiService, - UndergradCreditStatusApiService creditStatusApiService, - UndergradExamDelayApiService examDelayApiService) { - - this.courseTableApiService = courseTableApiService; - this.scoreApiService = scoreApiService; - this.studentInfoApiService = studentInfoApiService; - this.trainingPlanApiService = trainingPlanApiService; - this.creditStatusApiService = creditStatusApiService; - this.examDelayApiService = examDelayApiService; - } - - public String getStudentInfoPage(String cookie) throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); - } - - public String getCourseTable(String cookie, String term) throws IOException, ApiException { - return courseTableApiService.getPage(term, cookie); - } - - public String getScore(String cookie) throws IOException, ApiException { - return scoreApiService.getPage(cookie); - } - - public String getTrainingPlan(String cookie) throws IOException, ApiException { - return trainingPlanApiService.getPage(cookie); - } - - public String getCreditStatus(String cookie) throws IOException, ApiException { - return creditStatusApiService.getPage(cookie, null, false); - } - - public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) - throws IOException, ApiException { - return examDelayApiService.getActivities(term, cookie); - } - - public String getExamDelayApplications(String cookie, String term, String activityId) - throws IOException, ApiException { - return examDelayApiService.getPage(term, activityId, cookie); - } -} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java new file mode 100644 index 0000000..edcd35f --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/Main.java @@ -0,0 +1,16 @@ +package cn.wustlinghang.wusthelper.internal.undergrad; + +import io.quarkus.runtime.Startup; +import jakarta.annotation.PostConstruct; +import jakarta.inject.Singleton; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Startup +@Singleton +public class Main { + @PostConstruct + public void run() { + log.info("Undergrad service started."); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CookieApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java similarity index 73% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CookieApi.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java index 7f0a205..aa5264d 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/CookieApi.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java @@ -1,7 +1,7 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.wusthelper.internal.graduate.services.LoginService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.LoginService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -20,13 +20,13 @@ public class CookieApi { @GET @Path("/") public String login(@QueryParam("username") @NotNull String username, - @QueryParam("password") @NotNull String password) throws IOException, ApiException { + @QueryParam("password") @NotNull String password) throws RpcException { return loginService.login(username, password, false); } @GET @Path("/verify") - public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws IOException { + public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws RpcException { return loginService.verify(cookie); } } diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java new file mode 100644 index 0000000..ec94278 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/course_table") +@ApplicationScoped +public class CourseTableApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CourseTableApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return this.parse(this.agent(cookie, term)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") String term) + throws RpcException { + return agentService.getCourseTable(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseCourseTable(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java new file mode 100644 index 0000000..258d5f3 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java @@ -0,0 +1,49 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/credit_status") +@ApplicationScoped +public class CreditStatusApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public CreditStatusApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + String html = this.agent(cookie); + return this.parse(html); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getCreditStatus(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseCreditStatus(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java new file mode 100644 index 0000000..9f0011d --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_activities") +@ApplicationScoped +public class ExamActivitiesApi { + private final RequestAgentService agentService; + + public ExamActivitiesApi(RequestAgentService agentService) { + this.agentService = agentService; + } + + @GET + @Path("/") + public UndergradExamDelayApiService.ExamActivity[] get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return this.agent(cookie, term); + } + + @GET + @Path("/agent") + public UndergradExamDelayApiService.ExamActivity[] agent( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term) + throws RpcException { + return agentService.getExamActivities(cookie, term); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + throw RpcException.ApiNotImplement(); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java new file mode 100644 index 0000000..8cc0287 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java @@ -0,0 +1,52 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.util.List; + +@Path("/exam_delay_application") +@ApplicationScoped +public class ExamDelayApplicationApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ExamDelayApplicationApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get( + @QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return this.parse(this.agent(cookie, term, activityId)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie, + @QueryParam("term") @NotNull String term, + @QueryParam("activity_id") @NotNull String activityId) + throws RpcException { + return agentService.getExamDelayApplications(cookie, term, activityId); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseExamDelayApplications(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java new file mode 100644 index 0000000..6435534 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; +import java.util.List; + +@Path("/score") +@ApplicationScoped +public class ScoreApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public ScoreApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public List get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getScore(cookie); + } + + @POST + @Path("/parse") + public List parse(String html) throws RpcException { + return parseService.parseScore(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java new file mode 100644 index 0000000..3e09b2b --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java @@ -0,0 +1,50 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/student_info") +@ApplicationScoped +public class StudentInfoApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public StudentInfoApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getStudentInfoPage(cookie); + } + + @POST + @Path("/parse") + public StudentInfo parse(String html) throws RpcException { + return parseService.parseStudentInfo(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java new file mode 100644 index 0000000..aeebf99 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java @@ -0,0 +1,48 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; +import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; + +import java.io.IOException; + +@Path("/training_plan") +@ApplicationScoped +public class TrainingPlanApi { + private final RequestAgentService agentService; + private final ParseService parseService; + + public TrainingPlanApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; + } + + @GET + @Path("/") + public String get(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return this.parse(this.agent(cookie)); + } + + @GET + @Path("/agent") + public String agent(@QueryParam("cookie") @NotNull String cookie) + throws RpcException { + return agentService.getTrainingPlan(cookie); + } + + @POST + @Path("/parse") + public String parse(String html) throws RpcException { + return parseService.parseTrainingPlan(html); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ExceptionHandlerBase.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java similarity index 67% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ExceptionHandlerBase.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java index e483fc1..7b284d7 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ExceptionHandlerBase.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -public abstract class ExceptionHandlerBase { +public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -17,6 +17,14 @@ public abstract class ExceptionHandlerBase { } public Response toResponse(int code, String msg, String handlerName) { + return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + } + + public Response toResponse(int status, int code, String msg, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + } + + public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); @@ -24,7 +32,7 @@ public abstract class ExceptionHandlerBase { response = RpcResponse.error(ResponseCode.SERVER_INTERNAL_ERROR); } - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + return Response.status(status) .entity(response) .header(EXCEPTION_HEADER_KEY, handlerName) .type(MediaType.APPLICATION_JSON) diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/DefaultExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java similarity index 73% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/DefaultExceptionHandler.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java index 937a50b..47ae2c2 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/DefaultExceptionHandler.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class DefaultExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/IOExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java similarity index 80% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/IOExceptionHandler.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java index dcf9ad3..73274f1 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/IOExceptionHandler.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.ws.rs.core.Response; @@ -10,7 +10,7 @@ import java.io.IOException; @Slf4j @Provider -public class IOExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ParseExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java similarity index 81% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ParseExceptionHandler.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java index ddc94fc..8d15675 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/handler/ParseExceptionHandler.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ParseExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java new file mode 100644 index 0000000..a5652b3 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java @@ -0,0 +1,17 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(RpcException e) { + return super.toResponse( + e.getCode(), + e.toString(), + "RpcExceptionHandler"); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ValidationExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java similarity index 80% rename from sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ValidationExceptionHandler.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java index ab3ed70..a5c3e15 100644 --- a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/physics/api/json/handler/ValidationExceptionHandler.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.physics.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import jakarta.validation.ValidationException; @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ValidationExceptionHandler extends ExceptionHandlerBase +public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java new file mode 100644 index 0000000..bed20bd --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java @@ -0,0 +1,26 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(WebApplicationException e) { + return super.toResponse( + e.getResponse().getStatus(), + RpcException.toIntCode( + UndergradRpcException.UNDERGRAD_MODULE, + UndergradRpcException.SubModuleCode.COMMON.ordinal(), + UndergradRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 + + UndergradRpcException.ErrorCode.REQUEST_INVALID.ordinal() + ), + e.getMessage(), + "WebApplicationExceptionHandler" + ); + } +} diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/interceptor/ResponseWrapperInterceptor.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java similarity index 80% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/interceptor/ResponseWrapperInterceptor.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java index 64cb735..a6b16f4 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/api/json/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java @@ -1,6 +1,6 @@ -package cn.wustlinghang.wusthelper.internal.graduate.api.json.interceptor; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.interceptor; -import cn.wustlinghang.wusthelper.internal.graduate.api.json.handler.ExceptionHandlerBase; +import cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler.BaseExceptionHandler; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; @@ -22,7 +22,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { - boolean hasException = context.getHeaders().get(ExceptionHandlerBase.EXCEPTION_HEADER_KEY) != null; + boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); RpcResponse wrappedResponse = RpcResponse.success(data); diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/JacksonBean.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java similarity index 82% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/JacksonBean.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java index f49d315..c95e91d 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/JacksonBean.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/JacksonBean.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustParserBeans.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java similarity index 94% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustParserBeans.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java index a337fe6..a9fbc00 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/bean/MywustParserBeans.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustParserBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.parser.undergraduate.*; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustRequestAgentBeans.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java similarity index 97% rename from sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustRequestAgentBeans.java rename to sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java index d4b29d5..b959e45 100644 --- a/sub-services/graduate/src/main/java/cn/wustlinghang/wusthelper/internal/graduate/bean/MywustRequestAgentBeans.java +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/bean/MywustRequestAgentBeans.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.graduate.bean; +package cn.wustlinghang.wusthelper.internal.undergrad.bean; import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; import cn.wustlinghang.mywust.core.request.service.undergraduate.*; diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java new file mode 100644 index 0000000..a8ed179 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java @@ -0,0 +1,73 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.exception; + +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; + +import java.util.StringJoiner; + +public class UndergradRpcException extends RpcException { + public static final int UNDERGRAD_MODULE = 1; + + public UndergradRpcException(TypeCode typeCode, + SubModuleCode subModuleCode, + ErrorCode errorCode) { + super(UNDERGRAD_MODULE, + typeCode.ordinal(), + subModuleCode.ordinal() * 100 + errorCode.ordinal(), + new StringJoiner("-") + .add("UNDERGRAD") + .add(typeCode.name()) + .add(subModuleCode.name()) + .add(errorCode.name()) + .toString() + ); + } + + /** + * 异常类型编码 + */ + public enum TypeCode { + // 网络异常类型 + NETWORK_EXCEPTION, + // 参数异常类型 + PARAM_EXCEPTION, + // 权限认证异常类型 + AUTH_EXCEPTION, + // 网页解析异常类型 + PARSE_EXCEPTION, + // 其他的异常类型 + OTHER_EXCEPTION + } + + /** + * 子模块编码 + */ + public enum SubModuleCode { + COMMON, AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, + STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION + } + + /** + * 具体错误编码 + */ + public enum ErrorCode { + REQUEST_INVALID, + PARAM_INVALID, + COOKIE_INVALID, + NETWORK_ERROR, + PARSE_ERROR, + + // 需要评教 + NEED_EVALUATE, + + AUTH_PASSWORD_WRONG, + // 用户不存在 + AUTH_USER_NOT_EXISTS, + // 封号 + AUTH_USER_BANNED, + // 用户账号禁用 + AUTH_USER_DISABLED, + // 用户账号需要更改 + AUTH_NEED_CHANGE_PASSWORD, + AUTH_UNKNOWN_ERROR + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java new file mode 100644 index 0000000..a6d573d --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java @@ -0,0 +1,78 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class LoginService { + private final RequestClientOption option; + private final UndergraduateLogin undergraduateLogin; + + public LoginService(RequestClientOption option, + UndergraduateLogin undergraduateLogin) { + this.option = option; + this.undergraduateLogin = undergraduateLogin; + } + + public String login(String username, String password, boolean legacy) throws UndergradRpcException { + try { + if (legacy) { + return undergraduateLogin.getLoginCookieLegacy(username, password, option); + } else { + return undergraduateLogin.getLoginCookie(username, password, option); + } + } catch (ApiException e) { + throw wrapApiException(e); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public boolean verify(String cookie) throws UndergradRpcException { + try { + return !undergraduateLogin.checkCookiesFail(cookie); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e) { + UndergradRpcException.ErrorCode errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> UndergradRpcException.ErrorCode.NETWORK_ERROR; + case UNI_LOGIN_PASSWORD_WRONG -> UndergradRpcException.ErrorCode.AUTH_PASSWORD_WRONG; + case UNI_LOGIN_USER_NOT_EXISTS -> UndergradRpcException.ErrorCode.AUTH_USER_NOT_EXISTS; + case UNI_LOGIN_USER_BANNED -> UndergradRpcException.ErrorCode.AUTH_USER_BANNED; + case UNI_LOGIN_USER_DISABLED -> UndergradRpcException.ErrorCode.AUTH_USER_DISABLED; + case UNI_LOGIN_NEED_CHANGE_PASSWORD -> UndergradRpcException.ErrorCode.AUTH_NEED_CHANGE_PASSWORD; + case UNI_LOGIN_USER_NOT_ONLY -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NO_REGISTER -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + case UNI_LOGIN_NEED_TFA -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + default -> { + log.error("本科生:登录代理请求异常,异常未处理"); + log.error("异常:", e); + yield UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + } + }; + + return new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.AUTH, + errorCode + ); + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java new file mode 100644 index 0000000..22aae56 --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java @@ -0,0 +1,132 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.parser.undergraduate.*; +import cn.wustlinghang.mywust.data.global.Course; +import cn.wustlinghang.mywust.data.global.Score; +import cn.wustlinghang.mywust.data.global.StudentInfo; +import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import cn.wustlinghang.mywust.exception.ParseException; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; + +import java.util.List; + +@ApplicationScoped +public class ParseService { + private final UndergradCourseTableParser courseTableParser; + private final UndergradScoreParser scoreParser; + private final UndergradStudentInfoPageParser studentInfoPageParser; + private final UndergradTrainingPlanPageParser trainingPlanPageParser; + private final UndergradCreditStatusParser creditStatusParser; + private final UndergradExamDelayParser examDelayParser; + + public ParseService(UndergradCourseTableParser courseTableParser, + UndergradScoreParser scoreParser, + UndergradStudentInfoPageParser studentInfoPageParser, + UndergradTrainingPlanPageParser trainingPlanPageParser, + UndergradCreditStatusParser creditStatusParser, + UndergradExamDelayParser examDelayParser) { + + this.courseTableParser = courseTableParser; + this.scoreParser = scoreParser; + this.studentInfoPageParser = studentInfoPageParser; + this.trainingPlanPageParser = trainingPlanPageParser; + this.creditStatusParser = creditStatusParser; + this.examDelayParser = examDelayParser; + } + + public List parseCourseTable(String data) throws UndergradRpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return courseTableParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseScore(String data) throws RpcException { + try { + if (data.contains("评教")) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NEED_EVALUATE + ); + } + + return scoreParser.parse(data); + + } catch (UndergradRpcException e) { + throw e; + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public StudentInfo parseStudentInfo(String data) throws RpcException { + try { + return studentInfoPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseTrainingPlan(String data) throws RpcException { + try { + return trainingPlanPageParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public String parseCreditStatus(String data) throws RpcException { + try { + return creditStatusParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } + + public List parseExamDelayApplications(String data) throws RpcException { + try { + return examDelayParser.parse(data); + } catch (Exception e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.PARSE_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.PARSE_ERROR + ); + } + } +} diff --git a/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java new file mode 100644 index 0000000..e608b5e --- /dev/null +++ b/sub-services/physics/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java @@ -0,0 +1,166 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.services; + +import cn.wustlinghang.mywust.core.request.service.undergraduate.*; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.network.RequestClientOption; +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +@Slf4j +@ApplicationScoped +public class RequestAgentService { + private final UndergradCourseTableApiService courseTableApiService; + private final UndergradScoreApiService scoreApiService; + private final UndergradStudentInfoApiService studentInfoApiService; + private final UndergradTrainingPlanApiService trainingPlanApiService; + private final UndergradCreditStatusApiService creditStatusApiService; + private final UndergradExamDelayApiService examDelayApiService; + + private final RequestClientOption requestClientOption; + + public RequestAgentService(UndergradCourseTableApiService courseTableApiService, + UndergradScoreApiService scoreApiService, + UndergradStudentInfoApiService studentInfoApiService, + UndergradTrainingPlanApiService trainingPlanApiService, + UndergradCreditStatusApiService creditStatusApiService, + UndergradExamDelayApiService examDelayApiService, + RequestClientOption requestClientOption) { + + this.courseTableApiService = courseTableApiService; + this.scoreApiService = scoreApiService; + this.studentInfoApiService = studentInfoApiService; + this.trainingPlanApiService = trainingPlanApiService; + this.creditStatusApiService = creditStatusApiService; + this.examDelayApiService = examDelayApiService; + + this.requestClientOption = requestClientOption; + } + + public String getStudentInfoPage(String cookie) throws RpcException { + try { + return studentInfoApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.STUDENT_INFO); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.STUDENT_INFO, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCourseTable(String cookie, String term) throws RpcException { + try { + return courseTableApiService.getPage(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.COURSE_TABLE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.COURSE_TABLE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getScore(String cookie) throws RpcException { + try { + return scoreApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.SCORE); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.SCORE, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getTrainingPlan(String cookie) throws RpcException { + try { + return trainingPlanApiService.getPage(cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.TRAINING_PLAN); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.TRAINING_PLAN, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getCreditStatus(String cookie) throws RpcException { + try { + return creditStatusApiService.getPage(cookie, requestClientOption, false); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.CREDIT_STATUS); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.CREDIT_STATUS, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) + throws RpcException { + try { + return examDelayApiService.getActivities(term, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + public String getExamDelayApplications(String cookie, String term, String activityId) + throws RpcException { + try { + return examDelayApiService.getPage(term, activityId, cookie); + } catch (ApiException e) { + throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION); + } catch (IOException e) { + throw new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + } + } + + private UndergradRpcException wrapApiException(ApiException e, UndergradRpcException.SubModuleCode subModuleCode) { + return switch (e.getCode()) { + case NETWORK_EXCEPTION -> new UndergradRpcException( + UndergradRpcException.TypeCode.NETWORK_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.NETWORK_ERROR + ); + case COOKIE_INVALID -> new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + default -> { + log.error("本科生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("异常:", e); + yield new UndergradRpcException( + UndergradRpcException.TypeCode.AUTH_EXCEPTION, + subModuleCode, + UndergradRpcException.ErrorCode.COOKIE_INVALID + ); + } + }; + } +} diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java index d12ba35..9f0011d 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java @@ -2,7 +2,6 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.json; import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; import cn.wustlinghang.mywust.data.global.Score; -import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService; import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java index 2f19684..8cc0287 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java @@ -30,7 +30,7 @@ public class ExamDelayApplicationApi { public List get( @QueryParam("cookie") @NotNull String cookie, @QueryParam("term") @NotNull String term, - @QueryParam("activities") @NotNull String activityId) + @QueryParam("activity_id") @NotNull String activityId) throws RpcException { return this.parse(this.agent(cookie, term, activityId)); } @@ -39,7 +39,7 @@ public class ExamDelayApplicationApi { @Path("/agent") public String agent(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") @NotNull String term, - @QueryParam("activities") @NotNull String activityId) + @QueryParam("activity_id") @NotNull String activityId) throws RpcException { return agentService.getExamDelayApplications(cookie, term, activityId); } diff --git a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ExceptionHandlerBase.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java similarity index 67% rename from sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ExceptionHandlerBase.java rename to sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java index b52897e..7b284d7 100644 --- a/sub-services/library/src/main/java/cn/wustlinghang/wusthelper/internal/library/api/json/handler/ExceptionHandlerBase.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/BaseExceptionHandler.java @@ -1,4 +1,4 @@ -package cn.wustlinghang.wusthelper.internal.library.api.json.handler; +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; import cn.wustlinghang.wusthelper.rpc.response.ResponseCode; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -public abstract class ExceptionHandlerBase { +public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -17,6 +17,14 @@ public abstract class ExceptionHandlerBase { } public Response toResponse(int code, String msg, String handlerName) { + return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + } + + public Response toResponse(int status, int code, String msg, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + } + + public Response toResponse(Response.Status status, int code, String msg, String handlerName) { Object response; try { response = objectMapper.writeValueAsString(RpcResponse.error(code, msg)); @@ -24,7 +32,7 @@ public abstract class ExceptionHandlerBase { response = RpcResponse.error(ResponseCode.SERVER_INTERNAL_ERROR); } - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + return Response.status(status) .entity(response) .header(EXCEPTION_HEADER_KEY, handlerName) .type(MediaType.APPLICATION_JSON) diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java index eb82227..47ae2c2 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/DefaultExceptionHandler.java @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class DefaultExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java index e6c0815..73274f1 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/IOExceptionHandler.java @@ -10,7 +10,7 @@ import java.io.IOException; @Slf4j @Provider -public class IOExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java index 66e77b1..8d15675 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ParseExceptionHandler.java @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ParseExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java index 3d35211..a5652b3 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java @@ -1,18 +1,17 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; -import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.wusthelper.rpc.exception.RpcException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; @Provider -public class RpcExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper { +public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(RpcException e) { return super.toResponse( e.getCode(), e.toString(), - "ApiExceptionHandler"); + "RpcExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java index 8c3dcf0..a5c3e15 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ValidationExceptionHandler.java @@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider -public class ValidationExceptionHandler extends ExceptionHandlerBase +public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java new file mode 100644 index 0000000..bed20bd --- /dev/null +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/WebApplicationExceptionHandler.java @@ -0,0 +1,26 @@ +package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler; + +import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException; +import cn.wustlinghang.wusthelper.rpc.exception.RpcException; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { + @Override + public Response toResponse(WebApplicationException e) { + return super.toResponse( + e.getResponse().getStatus(), + RpcException.toIntCode( + UndergradRpcException.UNDERGRAD_MODULE, + UndergradRpcException.SubModuleCode.COMMON.ordinal(), + UndergradRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 + + UndergradRpcException.ErrorCode.REQUEST_INVALID.ordinal() + ), + e.getMessage(), + "WebApplicationExceptionHandler" + ); + } +} diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java index a162242..a6b16f4 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/interceptor/ResponseWrapperInterceptor.java @@ -1,6 +1,6 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.json.interceptor; -import cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler.ExceptionHandlerBase; +import cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler.BaseExceptionHandler; import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.WebApplicationException; @@ -22,7 +22,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { - boolean hasException = context.getHeaders().get(ExceptionHandlerBase.EXCEPTION_HEADER_KEY) != null; + boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); RpcResponse wrappedResponse = RpcResponse.success(data); diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java index 722a6be..a8ed179 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java @@ -42,7 +42,7 @@ public class UndergradRpcException extends RpcException { * 子模块编码 */ public enum SubModuleCode { - AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, + COMMON, AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION } @@ -50,6 +50,7 @@ public class UndergradRpcException extends RpcException { * 具体错误编码 */ public enum ErrorCode { + REQUEST_INVALID, PARAM_INVALID, COOKIE_INVALID, NETWORK_ERROR,