From 68605d35aaf1395befafe218c81f16526ee5ed04 Mon Sep 17 00:00:00 2001 From: parvathy00 Date: Thu, 21 Mar 2024 17:51:37 +0530 Subject: [PATCH] [Parvathy, Rahul] | BAH-3703 | Fix. Improper Session Expiry --- .../openerp/web/OdooSessionExpiredException.java | 13 +++++++++++++ .../org/bahmni/openerp/web/ResponseChecker.java | 5 +++-- .../bahmni/openerp/web/http/client/RestClient.java | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 openerp-client/src/main/java/org/bahmni/openerp/web/OdooSessionExpiredException.java diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/OdooSessionExpiredException.java b/openerp-client/src/main/java/org/bahmni/openerp/web/OdooSessionExpiredException.java new file mode 100644 index 0000000..00343a6 --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/OdooSessionExpiredException.java @@ -0,0 +1,13 @@ +package org.bahmni.openerp.web; + +public class OdooSessionExpiredException extends RuntimeException { + + public OdooSessionExpiredException() { + super("Odoo Session Expired"); + } + + public OdooSessionExpiredException(Throwable cause) { + super("Odoo Session Expired", cause); + } + +} diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/ResponseChecker.java b/openerp-client/src/main/java/org/bahmni/openerp/web/ResponseChecker.java index ab782cd..6cd237f 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/ResponseChecker.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/ResponseChecker.java @@ -28,10 +28,11 @@ public static void checkResponse(ResponseEntity responseEntity) throws J else if (jsonResponse.has("error")) { JsonNode error = jsonResponse.get("error"); String errorMsg = error.get("message").asText(); - int status = error.get("status").asInt(); + int status = error.has("status") ? error.get("status").asInt() : error.get("code").asInt(); + if (status == 100 || status == 403) throw new OdooSessionExpiredException(); throw new OdooRestException(String.format("Error found in response. Response status: %s. Error message: %s", status, errorMsg)); } - else{ + else { throw new OdooRestException(String.format("Response is empty")); } } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java index 228a237..5faf407 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.bahmni.openerp.web.OdooSessionExpiredException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.bahmni.openerp.web.OdooRestException; @@ -72,6 +73,10 @@ public String post(String URL, String requestBody) { logger.error("Post call to {} failed", URL, e); logger.error("Post data: {}", requestBody); throw new RuntimeException("Post call to " + URL + " failed", e); + } catch (OdooSessionExpiredException e) { + logger.warn("Session expired for user {}. Recreating user session.", username); + login(); + return post(URL, requestBody); } }