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

[Rahul] | BAH-3417 | Add. Session Expiry Handler #97

Merged
merged 4 commits into from
Jan 3, 2024
Merged
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 @@ -9,7 +9,7 @@
import org.bahmni.openerp.web.ResponseChecker;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseCookie;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
Expand All @@ -24,7 +24,7 @@ public class RestClient {
private final String username;
private final String password;
private final int connectionTimeout;
private String sessionId;
private static ResponseCookie session;

public RestClient(String baseURL, String username, String password, int connectionTimeout, String database) {
this.database = database;
Expand All @@ -35,55 +35,50 @@ public RestClient(String baseURL, String username, String password, int connecti
}

private void login() {
rahu1ramesh marked this conversation as resolved.
Show resolved Hide resolved
if (sessionId == null) {
String requestBody = buildLoginRequest();
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer < HttpHeaders > consumer = httpHeaders -> httpHeaders.addAll(headers);
try {
client.post().uri("web/session/authenticate").headers(consumer).bodyValue(requestBody).exchangeToMono(loginResponse -> {
if(loginResponse.statusCode().is2xxSuccessful()) {
try {
sessionId = loginResponse.cookies().get("session_id").get(0).getValue();
logger.debug("Login session_id: {}", sessionId);
} catch (Exception e) {
logger.warn("Failed to login for user {}", username);
throw new OpenERPException(String.format("Failed to login. The login user is : %s", username));
}
String requestBody = buildLoginRequest();
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
try {
client.post().uri("web/session/authenticate").headers(consumer).bodyValue(requestBody).exchangeToMono(loginResponse -> {
if (loginResponse.statusCode().is2xxSuccessful()) {
session = loginResponse.cookies().get("session_id").get(0);
}
return loginResponse.bodyToMono(String.class);
}).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------Login Initiated-----------------------------------------------------\n* Request : {}\n* Session Id : {}\n-----------------------------------------------------End of Login-----------------------------------------------------", requestBody, sessionId);
} catch (Exception e) {
logger.warn("Failed to login for user {}", username);
throw new OpenERPException(String.format("Failed to login. The login user is : %s", username));
}
}).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------Login Initiated-----------------------------------------------------\n* Request : {}\n* Session Id : {}\n-----------------------------------------------------End of Login-----------------------------------------------------", requestBody, session);
} catch (Exception e) {
logger.warn("Failed to login for user {}", username);
throw new OpenERPException(String.format("Failed to login. The login user is : %s, no session cookie set.", username));
}
}

public String post(String URL, String requestBody) {
try {
if(!isSessionValid())
login();
try {
logger.debug("Post Data: {}", requestBody);
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer < HttpHeaders > consumer = httpHeaders -> httpHeaders.addAll(headers);
ResponseEntity<String> responseEntity = client.post().uri(URL).headers(consumer).cookie("session_id", sessionId).bodyValue(requestBody).retrieve().toEntity(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
ResponseChecker.checkResponse(responseEntity);
String response = responseEntity.getBody();
logger.debug("\n-----------------------------------------------------{} Initiated-----------------------------------------------------\n* Cookies : {}\n* Request : {}\n* Response : {}\n-----------------------------------------------------End of {}-----------------------------------------------------", URL, sessionId, requestBody, response, URL);
String response = client.post().uri(URL).headers(consumer).cookie("session_id", session.getValue()).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------{} Initiated-----------------------------------------------------\n* Session : {}\n* Request : {}\n* Response : {}\n-----------------------------------------------------End of {}-----------------------------------------------------", URL, session, requestBody, response, URL);
if (response == null) {
throw new OpenERPException(String.format("Could not post to %s", URL));
}
logger.debug("Post Data output: {}", response);
return response;
} catch (OdooRestException e) {
logger.error("Post call to {} failed", URL, e);
logger.error("Post data: {}", requestBody);
throw new RuntimeException("Post call to " + URL + " failed", e);
} catch (Exception e) {
logger.error("Could not post to {}", URL, e);
logger.error("Post data: {}", requestBody);
throw new RuntimeException("Could not post message", e);
}
}

private boolean isSessionValid() {
return session != null && !session.getMaxAge().isNegative();
}

private WebClient getWebClient(String baseURL) {
if (webClient == null) {
webClient = WebClient.builder()
Expand Down
Loading