Reputation: 156
I'm attempting to dig directly from the Cookie request header to a value I've previously stored in the Play session via
Http.Context.current().session().put("my-fancy-key", "some-interesting-value");
I have ONLY access to the play.mvc.Http.Request from which I'm able to get to the play.mvc.Http.Cookie ... but from there I stumble.
This snippet doesn't work... Hints?
NOTE: I'm totally open to using objects that aren't in the Play framework. I see Netty has cookie r/w functions and am looking into those ... perhaps something directly in the javax?
String playSessionCookieName = Configuration.root().getString("session.cookieName", "PLAY_SESSION");
Http.Cookie playSessionCookie = r.cookie(playSessionCookieName);
if (playSessionCookie != null) {
// FIXME: What to do here to get my value?
Logger.debug("Found the cookie! Serialized value: " + playSessionCookie.value());
try {
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(playSessionCookie.value().getBytes()));
Http.Session session = (Http.Session) objIn.readObject();
// Here's the goal
Logger.debug("Found my value: " + session.get("my-fancy-key"));
} catch (Exception e) {
Logger.warn("Couldn't deserialize the value.", e);
}
}
Upvotes: 1
Views: 3301
Reputation: 127
It may be easier to iterate over the session stored in the result instead of the cookie, if you have access to the result:
Session resultSession = play.test.Helpers.session(result);
for (Entry<String, String> entry : resultSession.entrySet()) {
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
Upvotes: 0
Reputation: 28566
I don't know why You don't use simple session(key)
to get session value, but if You need to get session values from session cookie You can use something like that (play 2.0).
String cookieVal = request().cookies().get("PLAY_SESSION").value();
cookieVal = cookieVal.substring(cookieVal.indexOf("-")+1);
for(String a: cookieVal.split("%00")) {
String[] k = a.split("%3A");
// k[0] - session key; k[1] - session value
Logger.info(k[0] + " = " + k[1]);
}
Upvotes: 2
Reputation: 749
Cookie has a method value(). I dont know if it does what you want but I would start there.
Upvotes: 0