Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for wasted access tokens #131

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading