From 19b67704abef97b9c83728396ab21b03fcb69413 Mon Sep 17 00:00:00 2001 From: "julian.lxs" Date: Tue, 31 Dec 2024 11:10:21 +0100 Subject: [PATCH] add ExternalSystemConfiguration to set basepath in application.properties --- .../config/ExternalSystemConfiguration.java | 17 +++++++++++++++++ .../service/GradingService.java | 18 +++++++++--------- src/main/resources/application.properties | 6 +++++- 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 src/main/java/de/unistuttgart/iste/meitrex/assignment_service/config/ExternalSystemConfiguration.java diff --git a/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/config/ExternalSystemConfiguration.java b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/config/ExternalSystemConfiguration.java new file mode 100644 index 0000000..7cc69e7 --- /dev/null +++ b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/config/ExternalSystemConfiguration.java @@ -0,0 +1,17 @@ +package de.unistuttgart.iste.meitrex.assignment_service.config; + +import lombok.Getter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Getter +@Configuration +public class ExternalSystemConfiguration { + + @Value("${external_system.authToken}") + private String authToken; + + @Value("${external_system.url}") + private String basePath; + +} diff --git a/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java index 4dd9349..cf586ae 100644 --- a/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java +++ b/src/main/java/de/unistuttgart/iste/meitrex/assignment_service/service/GradingService.java @@ -1,6 +1,7 @@ package de.unistuttgart.iste.meitrex.assignment_service.service; +import de.unistuttgart.iste.meitrex.assignment_service.config.ExternalSystemConfiguration; import de.unistuttgart.iste.meitrex.assignment_service.exception.ExternalPlatformConnectionException; import de.unistuttgart.iste.meitrex.assignment_service.exception.ManualMappingRequiredException; import de.unistuttgart.iste.meitrex.user_service.exception.UserServiceConnectionException; @@ -50,9 +51,8 @@ public class GradingService { private final UserServiceClient userServiceClient; private final CourseServiceClient courseServiceClient; - // these need to be set! - private static final String authToken = ""; - private static final String basePath = ""; + private final ExternalSystemConfiguration externalSystemConfiguration; + public Grading getGradingForAssignmentForStudent(final UUID assignmentId, final UUID studentId, final LoggedInUser currentUser) { final AssignmentEntity assignment = assignmentService.requireAssignmentExists(assignmentId); // throws EntityNotFoundException "Assignment with assessmentId %s not found" @@ -86,9 +86,9 @@ public void importGradingsForAssignment(final UUID assignmentId, final LoggedInU String body; CompletableFuture response; try (HttpClient client = HttpClient.newBuilder().build()) { - HttpRequest request = HttpRequest.newBuilder().uri(URI.create(basePath + "api/grading/handIn/" + externalId)) + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(externalSystemConfiguration.getBasePath() + "api/grading/handIn/" + externalId)) //.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("username:password".getBytes())) - .header("Cookie", "connect.sid=" + authToken) + .header("Cookie", "connect.sid=" + externalSystemConfiguration.getAuthToken()) .build(); response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body); body = response.join(); @@ -337,9 +337,9 @@ private JSONObject getExternalStudentInfo(final String externalStudentId) throws String body; CompletableFuture response; try (HttpClient client = HttpClient.newBuilder().build()) { - HttpRequest request = HttpRequest.newBuilder().uri(URI.create(basePath + "api/student/" + externalStudentId)) + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(externalSystemConfiguration.getBasePath() + "api/student/" + externalStudentId)) //.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("username:password".getBytes())) - .header("Cookie", "connect.sid=" + authToken) + .header("Cookie", "connect.sid=" + externalSystemConfiguration.getAuthToken()) .build(); response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body); body = response.join(); @@ -439,9 +439,9 @@ public List getExternalAssignments(final UUID courseId, fina String body; CompletableFuture response; try (HttpClient client = HttpClient.newBuilder().build()) { - HttpRequest request = HttpRequest.newBuilder().uri(URI.create(basePath + "api/sheet")) + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(externalSystemConfiguration.getBasePath() + "api/sheet")) //.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("username:password".getBytes())) - .header("Cookie", "connect.sid=" + authToken) + .header("Cookie", "connect.sid=" + externalSystemConfiguration.getAuthToken()) .build(); response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body); body = response.join(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b408e58..c7443cf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -19,4 +19,8 @@ management.health.readinessState.enabled=true # URLs for course and user service course_service.url=http://localhost:2001/graphql -user_service.url=http://localhost:5001/graphql \ No newline at end of file +user_service.url=http://localhost:5001/graphql + +# URL base path for external system like TMS +external_system.url=http://localhost:1234/ +external_system.authToken="" \ No newline at end of file