From 65e94fae91998fe4c7672edec3e9a97c3684b8f3 Mon Sep 17 00:00:00 2001 From: lensferno Date: Sun, 9 Jul 2023 12:11:35 +0800 Subject: [PATCH] errcode --- .../rpc/exception/CommonRpcException.java | 4 - .../rpc/exception/RpcException.java | 30 ++++ .../undergrad/api/json/CookieApi.java | 6 +- .../undergrad/api/json/CourseTableApi.java | 38 ++--- .../undergrad/api/json/CreditStatusApi.java | 39 +++-- .../undergrad/api/json/ExamActivitiesApi.java | 49 +++++++ .../api/json/ExamDelayApplicationApi.java | 52 +++++++ .../internal/undergrad/api/json/ScoreApi.java | 32 ++--- .../undergrad/api/json/StudentInfoApi.java | 29 ++-- .../undergrad/api/json/TrainingPlanApi.java | 31 ++-- .../api/json/handler/ApiExceptionHandler.java | 14 -- .../api/json/handler/RpcExceptionHandler.java | 18 +++ .../exception/UndergradRpcException.java | 72 ++++++++++ .../undergrad/services/LoginService.java | 60 +++++++- .../undergrad/services/ParseService.java | 96 +++++++++++-- .../services/RequestAgentService.java | 134 ++++++++++++++++-- 16 files changed, 564 insertions(+), 140 deletions(-) delete mode 100644 common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/CommonRpcException.java create mode 100644 common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java create mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java create mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java delete mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ApiExceptionHandler.java create mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java create mode 100644 sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java diff --git a/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/CommonRpcException.java b/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/CommonRpcException.java deleted file mode 100644 index 1fe160e..0000000 --- a/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/CommonRpcException.java +++ /dev/null @@ -1,4 +0,0 @@ -package cn.wustlinghang.wusthelper.rpc.exception; - -public class CommonRpcException extends RuntimeException { -} 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 new file mode 100644 index 0000000..bb49a46 --- /dev/null +++ b/common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java @@ -0,0 +1,30 @@ +package cn.wustlinghang.wusthelper.rpc.exception; + +public class RpcException extends RuntimeException { + protected final int code; + + public RpcException(int code, String message) { + super(message); + this.code = code; + } + + public RpcException(int code) { + this(code, String.valueOf(code)); + } + + public RpcException(int module, int type, int exceptionCode) { + this((module * 1000_0000) + (type * 1_0000) + (exceptionCode)); + } + + public RpcException(int module, int type, int exceptionCode, String message) { + this((module * 1000_0000) + (type * 1_0000) + (exceptionCode), message); + } + + public int getCode() { + return code; + } + + public static RpcException ApiNotImplement() { + return new RpcException(-1, "接口未实现"); + } +} diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java index 3c88d95..aa5264d 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java @@ -1,7 +1,7 @@ package cn.wustlinghang.wusthelper.internal.undergrad.api.json; -import cn.wustlinghang.mywust.exception.ApiException; 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/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java index 0ea3310..ec94278 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java @@ -1,12 +1,11 @@ package cn.wustlinghang.wusthelper.internal.undergrad.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 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; @@ -20,33 +19,34 @@ import java.util.List; @Path("/course_table") @ApplicationScoped public class CourseTableApi { - private final UndergradCourseTableApiService studentInfoApiService; - private final UndergradCourseTableParser studentInfoPageParser; + private final RequestAgentService agentService; + private final ParseService parseService; - public CourseTableApi(UndergradCourseTableApiService studentInfoApiService, - UndergradCourseTableParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; + 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 IOException, ApiException, ParseException { - String html = studentInfoApiService.getPage(term, cookie); - return this.parse(html); + 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 IOException, ApiException { - return studentInfoApiService.getPage(term, cookie); + 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 ParseException { - return studentInfoPageParser.parse(html); + public List parse(String html) throws RpcException { + return parseService.parseCourseTable(html); } } diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java index 3c7ac29..258d5f3 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java @@ -1,12 +1,10 @@ package cn.wustlinghang.wusthelper.internal.undergrad.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 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; @@ -19,36 +17,33 @@ import java.io.IOException; @Path("/credit_status") @ApplicationScoped public class CreditStatusApi { - private final UndergradCreditStatusApiService creditStatusApiService; - private final UndergradCreditStatusParser creditStatusParser; + private final RequestAgentService agentService; + private final ParseService parseService; - private final RequestClientOption option; - - public CreditStatusApi(UndergradCreditStatusApiService creditStatusApiService, - UndergradCreditStatusParser creditStatusParser, - RequestClientOption option) { - this.creditStatusApiService = creditStatusApiService; - this.creditStatusParser = creditStatusParser; - this.option = option; + public CreditStatusApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; } - + @GET @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = creditStatusApiService.getPage(cookie, option, false); + 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 IOException, ApiException { - return creditStatusApiService.getPage(cookie, option, false); + throws RpcException { + return agentService.getCreditStatus(cookie); } @POST @Path("/parse") - public String parse(String html) throws ParseException { - return creditStatusParser.parse(html); + public String parse(String html) throws RpcException { + return parseService.parseCreditStatus(html); } } 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 new file mode 100644 index 0000000..d12ba35 --- /dev/null +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java @@ -0,0 +1,49 @@ +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; +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/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 new file mode 100644 index 0000000..2f19684 --- /dev/null +++ b/sub-services/undergrad/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("activities") @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("activities") @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/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java index ce6ffc0..6435534 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java @@ -1,12 +1,11 @@ package cn.wustlinghang.wusthelper.internal.undergrad.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 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; @@ -20,31 +19,32 @@ import java.util.List; @Path("/score") @ApplicationScoped public class ScoreApi { - private final UndergradScoreApiService scoreApiService; - private final UndergradScoreParser scoreParser; + private final RequestAgentService agentService; + private final ParseService parseService; - public ScoreApi(UndergradScoreApiService scoreApiService, UndergradScoreParser scoreParser) { - this.scoreApiService = scoreApiService; - this.scoreParser = scoreParser; + public ScoreApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; } @GET @Path("/") - public List get(String cookie) throws IOException, ApiException, ParseException { - String html = scoreApiService.getPage(cookie); - return this.parse(html); + 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 IOException, ApiException { - return scoreApiService.getPage(cookie); + throws RpcException { + return agentService.getScore(cookie); } @POST @Path("/parse") - public List parse(String html) throws ParseException { - return scoreParser.parse(html); + public List parse(String html) throws RpcException { + return parseService.parseScore(html); } } diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java index 33c4c43..3e09b2b 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java @@ -1,10 +1,12 @@ package cn.wustlinghang.wusthelper.internal.undergrad.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 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; @@ -17,31 +19,32 @@ import java.io.IOException; @Path("/student_info") @ApplicationScoped public class StudentInfoApi { - private final UndergradStudentInfoApiService studentInfoApiService; - private final UndergradStudentInfoPageParser studentInfoPageParser; + private final RequestAgentService agentService; + private final ParseService parseService; - public StudentInfoApi(UndergradStudentInfoApiService studentInfoApiService, - UndergradStudentInfoPageParser studentInfoPageParser) { - this.studentInfoApiService = studentInfoApiService; - this.studentInfoPageParser = studentInfoPageParser; + public StudentInfoApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; } @GET @Path("/") - public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) throws IOException, ApiException, ParseException { + 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 IOException, ApiException { - return studentInfoApiService.getPage(cookie); + throws RpcException { + return agentService.getStudentInfoPage(cookie); } @POST @Path("/parse") - public StudentInfo parse(String html) throws ParseException { - return studentInfoPageParser.parse(html); + public StudentInfo parse(String html) throws RpcException { + return parseService.parseStudentInfo(html); } } diff --git a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java index 9638498..aeebf99 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java @@ -1,9 +1,10 @@ package cn.wustlinghang.wusthelper.internal.undergrad.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 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; @@ -16,32 +17,32 @@ import java.io.IOException; @Path("/training_plan") @ApplicationScoped public class TrainingPlanApi { - private final UndergradTrainingPlanApiService trainingPlanApiService; - private final UndergradTrainingPlanPageParser trainingPlanPageParser; + private final RequestAgentService agentService; + private final ParseService parseService; - public TrainingPlanApi(UndergradTrainingPlanApiService trainingPlanApiService, - UndergradTrainingPlanPageParser trainingPlanPageParser) { - this.trainingPlanApiService = trainingPlanApiService; - this.trainingPlanPageParser = trainingPlanPageParser; + public TrainingPlanApi(RequestAgentService agentService, + ParseService parseService) { + this.agentService = agentService; + this.parseService = parseService; } @GET @Path("/") - public String get(String cookie) throws IOException, ApiException, ParseException { - String html = trainingPlanApiService.getPage(cookie); - return this.parse(html); + 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 IOException, ApiException { - return trainingPlanApiService.getPage(cookie); + throws RpcException { + return agentService.getTrainingPlan(cookie); } @POST @Path("/parse") - public String parse(String html) throws ParseException { - return trainingPlanPageParser.parse(html); + 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/ApiExceptionHandler.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ApiExceptionHandler.java deleted file mode 100644 index e1456ae..0000000 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ApiExceptionHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.wustlinghang.wusthelper.internal.undergrad.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/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 new file mode 100644 index 0000000..3d35211 --- /dev/null +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java @@ -0,0 +1,18 @@ +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 { + @Override + public Response toResponse(RpcException e) { + return super.toResponse( + e.getCode(), + e.toString(), + "ApiExceptionHandler"); + } +} 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 new file mode 100644 index 0000000..722a6be --- /dev/null +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java @@ -0,0 +1,72 @@ +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 { + AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, + STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION + } + + /** + * 具体错误编码 + */ + public enum ErrorCode { + 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/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java index 28c3348..a6d573d 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java @@ -3,10 +3,13 @@ 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; @@ -18,15 +21,58 @@ public class LoginService { 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 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 IOException { - return !undergraduateLogin.checkCookiesFail(cookie); + 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/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java index f90f531..22aae56 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java @@ -6,6 +6,8 @@ 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; @@ -34,27 +36,97 @@ public class ParseService { this.examDelayParser = examDelayParser; } - public List parseCourseTable(String data) throws ParseException { - return courseTableParser.parse(data); + 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 ParseException { - return scoreParser.parse(data); + 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 ParseException { - return studentInfoPageParser.parse(data); + 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 ParseException { - return trainingPlanPageParser.parse(data); + 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 ParseException { - return creditStatusParser.parse(data); + 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 ParseException { - return examDelayParser.parse(data); + 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/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java index 58a62bf..e608b5e 100644 --- a/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java +++ b/sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java @@ -2,10 +2,15 @@ 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; @@ -15,12 +20,15 @@ public class RequestAgentService { 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) { + UndergradExamDelayApiService examDelayApiService, + RequestClientOption requestClientOption) { this.courseTableApiService = courseTableApiService; this.scoreApiService = scoreApiService; @@ -28,35 +36,131 @@ public class RequestAgentService { this.trainingPlanApiService = trainingPlanApiService; this.creditStatusApiService = creditStatusApiService; this.examDelayApiService = examDelayApiService; + + this.requestClientOption = requestClientOption; } - public String getStudentInfoPage(String cookie) throws IOException, ApiException { - return studentInfoApiService.getPage(cookie); + 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 IOException, ApiException { - return courseTableApiService.getPage(term, cookie); + 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 IOException, ApiException { - return scoreApiService.getPage(cookie); + 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 IOException, ApiException { - return trainingPlanApiService.getPage(cookie); + 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 IOException, ApiException { - return creditStatusApiService.getPage(cookie, null, false); + 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 IOException, ApiException { - return examDelayApiService.getActivities(term, cookie); + 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 IOException, ApiException { - return examDelayApiService.getPage(term, activityId, cookie); + 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 + ); + } + }; } }