Skip to content

Commit

Permalink
Merge pull request #131 from bcgov/develop/alex-GRAD2-2410-2
Browse files Browse the repository at this point in the history
Fix for wasted access tokens
  • Loading branch information
kamal-mohammed authored Nov 29, 2023
2 parents 59cc347 + 4902c58 commit 8e10d4c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,10 @@ public ResponseEntity<byte[]> getStudentTranscriptPDFByType(String pen, String t
private void getStudentAchievementReports(List<List<UUID>> partitions, List<InputStream> locations) {
logger.debug("******** Getting Student Achievement Reports ******");
for(List<UUID> studentList: partitions) {
String accessToken = tokenUtils.getAccessToken();
logger.debug("******** Run partition with {} students ******", studentList.size());
List<CompletableFuture<InputStream>> futures = studentList.stream()
.map(studentGuid -> CompletableFuture.supplyAsync(() -> getStudentAchievementReport(studentGuid)))
.map(studentGuid -> CompletableFuture.supplyAsync(() -> getStudentAchievementReport(studentGuid, accessToken)))
.toList();
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
CompletableFuture<List<InputStream>> result = allFutures.thenApply(v -> futures.stream()
Expand All @@ -305,10 +306,10 @@ private void getStudentAchievementReports(List<List<UUID>> partitions, List<Inpu
logger.debug("******** Fetched All {} Student Achievement Reports ******", locations.size());
}

private InputStream getStudentAchievementReport(UUID studentGuid) {
String accessTokenNext = tokenUtils.getAccessToken();
private InputStream getStudentAchievementReport(UUID studentGuid, String accessToken) {
try {
InputStreamResource result = webClient.get().uri(String.format(educGraduationApiConstants.getStudentCredentialByType(), studentGuid, "ACHV")).headers(h -> 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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8e10d4c

Please sign in to comment.