diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java index 57d58f2..4382f96 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java @@ -292,9 +292,10 @@ public ResponseEntity getStudentTranscriptPDFByType(String pen, String t private void getStudentAchievementReports(List> partitions, List locations) { logger.debug("******** Getting Student Achievement Reports ******"); for(List studentList: partitions) { + String accessToken = tokenUtils.getAccessToken(); logger.debug("******** Run partition with {} students ******", studentList.size()); List> futures = studentList.stream() - .map(studentGuid -> CompletableFuture.supplyAsync(() -> getStudentAchievementReport(studentGuid))) + .map(studentGuid -> CompletableFuture.supplyAsync(() -> getStudentAchievementReport(studentGuid, accessToken))) .toList(); CompletableFuture allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); CompletableFuture> result = allFutures.thenApply(v -> futures.stream() @@ -305,10 +306,10 @@ private void getStudentAchievementReports(List> partitions, List h.setBearerAuth(accessTokenNext)).retrieve().bodyToMono(InputStreamResource.class).block(); + String finalAccessToken = tokenUtils.isTokenExpired() ? tokenUtils.getAccessToken() : accessToken; + InputStreamResource result = webClient.get().uri(String.format(educGraduationApiConstants.getStudentCredentialByType(), studentGuid, "ACHV")).headers(h -> h.setBearerAuth(finalAccessToken)).retrieve().bodyToMono(InputStreamResource.class).block(); if (result != null) { logger.debug("******** Fetched Achievement Report for {} ******", studentGuid); return result.getInputStream(); @@ -367,8 +368,7 @@ private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) th String pathToFile = TMP + File.separator + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); File fileToSave = new File(pathToFile); - boolean isDeleted = Files.deleteIfExists(fileToSave.toPath()); - if(isDeleted) { + if(Files.deleteIfExists(fileToSave.toPath())) { logger.debug("Delete existing PDF {}", reportFile); } Files.write(fileToSave.toPath(), resultBinary); diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/TokenUtils.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/TokenUtils.java index 18ea101..d3d64c3 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/TokenUtils.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/TokenUtils.java @@ -37,8 +37,12 @@ public String getAccessToken() { return this.fetchAccessToken(); } + public boolean isTokenExpired() { + return responseObjCache.isExpired(); + } + private ResponseObj getTokenResponseObject() { - if(responseObjCache.isExpired()){ + if(isTokenExpired()){ responseObjCache.setResponseObj(getResponseObj()); } return responseObjCache.getResponseObj();