-
1. Re: Inject HttpSession
mbarkley Feb 27, 2014 9:35 AM (in response to clewis)Hi Carl,
If I understand you correctly, you want to send the HttpSession from the server to the client? Events could work, but I think Errai RPC is a much better fit. Here's what a solution with Errai RPC would look like:
A shared RPC interface:
@Remote public interface HttpSessionProvider { public HttpSession getHttpSession(); }
A server side implementation:
@Service public class HttpSessionProviderImpl implements HttpSessionProvider { public HttpSession getHttpSession() { // Get and return the session here } }
And here is how you call the service from the client:
MessageBuilder.createCall(new RemoteCallback<HttpSession>() { @Override public void callback(HttpSession response) { // Do something with the session... } }, HttpSessionProvider.class).getHttpSession();
I think this is a lot cleaner than using a CDI event. But if I haven't convinced you, it would also be possible with a CDI event. You would have to start by firing an event from the client, and respond with an @Conversational event from the server (unless of course you want to broadcast the session to ALL connected clients).
Cheers.
-
2. Re: Inject HttpSession
clewis Feb 28, 2014 5:14 PM (in response to mbarkley)Hi Max,
Thanks for code samples, but I guess I'm struggling with a more basic problem, I'm unable to successfully access the HttpSession object. I've tried on the server to get a reference to the HttpSession but I always get a NullPointerException. It looks like the proper way to do this in GWT is using
getThreadLocalRequest().getSession(); but I'm not sure of the context in which to make this call. In your code sample, you have a comment "//Get and return the session here", could you please provide an example of how to do this?
Thanks,
Carl
-
3. Re: Inject HttpSession
mbarkley Mar 3, 2014 10:21 AM (in response to clewis)You should be able to @Inject the HttpSession. If you're having issues with that, it may because you're trying to @Inject into a bean outside of a session or request scope. If you were going to use the RPC method I suggested previously, I would try @Inject'ing into the HttpSessionProviderImpl class.