I am using Java Play Framework 2.5 (cannot upgrade to 2.6 until my authentication is updated). When a user logs into my application, I want to send a request to a web service to retrieve information about that user’s geolocation, specifically their zip code. I will then save their zip code into session and use that elsewhere in my application, specifically around search.
The problem I am having is, it appears that I can add zip code to session, and I can see that it has been added, but it only works inside my thenApply scope. Elsewhere in my app, when I call session.get(“user_zip”), that value is not found.
String url = "http://someservice.com/json/123.44.321.123";
WSRequest request = ws.url(url);
final Http.Session session = Http.Context.current().session();
ws.url(url).get().thenApply(response -> {
JsonNode json = response.asJson();
String userId = session.get(UsersService.USER_ID); // works, I can read from session
String zipCode = null;
if (json != null && json.has("zip_code")) {
zipCode = json.get("zip_code").asText();
}
if (zipCode != null) {
session.put("USER_ZIP", zipCode); // works, can add to session
String zip = session.get("USER_ZIP"); // works, can read from session
}
return json; // not using this, but must return
});
I also tried this approach, passing in the httpExecutionContext.current() from my controller action, but my variables saved in session do not persist outside of this method.
ws.url(url).get().thenAcceptAsync(response -> {
JsonNode json = response.asJson();
String zipCode = null;
if (json != null && json.has("zip_code")) {
zipCode = json.get("zip_code").asText();
}
if (zipCode != null) {
ctx().session().put(UsersService.USER_ZIP, zipCode);
String ctxZip = ctx().session().get(UsersService.USER_ZIP);
} else {
ctx().session().remove(UsersService.USER_ZIP);
}
}, httpExecutionContext.current());
Both approaches I have tried, I can read from session just fine, and I can write to session (temporarily) making me think it works, but I am unable to access the variables I write to session outside of this scope.
Any ideas?
Thanks for any help.