Updated code for retry authentication for login to odessa.

This commit is contained in:
rajesh
2025-05-13 19:48:48 +05:30
parent 43ff10e7b6
commit b137c9b3ec

View File

@@ -430,7 +430,10 @@ public class AppointmentDao {
}
private HubEntity authenticateAndSaveToken(HubEntity hub) {
int maxRetries = 3;
int attempt = 0;
boolean success = false;
while (attempt < maxRetries && !success) {
try {
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
@@ -455,6 +458,7 @@ public class AppointmentDao {
hubRepository.save(hub);
log.info("Saved new authToken and areaCode for Hub.");
success = true;
return hub;
} else {
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
@@ -463,7 +467,8 @@ public class AppointmentDao {
// Handle non-OK response
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
} catch (FeignException.Forbidden forbiddenException) {
logForbiddenError();
attempt++;
log.error("Failed to login to odessa due to some error occurred.");
// Extract raw response body
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
@@ -495,13 +500,16 @@ public class AppointmentDao {
} catch (IOException e) {
log.error("Error parsing JSON response: {}", e.getMessage());
}
// Regenerate the token and retry
if (attempt >= maxRetries) {
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale("Maximum retry attempts reached while trying to login to Odessa."));
} else {
regenerateTokenAndSave(hub);
}
} catch (Exception e) {
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
throw new RuntimeException("Authentication failed on Odessa. try again", e);
}
}
return null;
}