diff --git a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java index 43c8cfdc..23f9784d 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/AppointmentDao.java @@ -176,6 +176,11 @@ public class AppointmentDao { } private HubEntity loginToOdessa(HubEntity hub, ApplicationEntity application) { + + 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(); @@ -195,15 +200,16 @@ public class AppointmentDao { hub.setAreaCode(parsedResponse.getAreaCode()); 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."); } } throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN)); - } - catch (FeignException.Forbidden forbiddenException) { - logForbiddenError(); + } catch (FeignException.Forbidden forbiddenException) { + attempt++; + log.error("Failed to login to odessa due to some error"); // Extract raw response body String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response @@ -239,15 +245,17 @@ public class AppointmentDao { } catch (IOException e) { log.error("Error parsing JSON response: {}", e.getMessage()); } - - // Regenerate the token and retry - loginToOdessa(hub, application); - } - catch (Exception e) { + if (attempt >= maxRetries) { + throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale("Maximum retry attempts reached while trying to login to Odessa.")); + } else { + loginToOdessa(hub, application); + } + } 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; + } + return null; } private void startAsyncNdgProcessing(Long applicationId) {