3 Replies Latest reply on Mar 3, 2014 10:21 AM by Max Barkley

    Inject HttpSession

    Carl Lewis Newbie

      Hi,

       

      Is it possible to @Inject an HttpSession? I'm trying to stick with CDI Events as much as possible in my application and would like to access the session in a similar manner. I've seen that it's possible using a Errai Message like below, but so far I've avoided using this api in preference to firing and observing events. My assumption is that CDI Events can be used in place of Errai Messaging.

       

      Thanks,

       

      Carl

       

      @Service
      public class ClientHelloService implements MessageCallback {
       
      @Override
       
      public void callback(final Message message) {
        
      HttpSession session = message.getResource(
        
      HttpServletRequest.class, HttpServletRequest.class.getName()).getSession();
        
      System.out.println("Client said hello. Session ID: " + session.getId());
       
      }
      }

        • 1. Re: Inject HttpSession
          Max Barkley Expert

          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
            Carl Lewis Newbie

            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
              Max Barkley Expert

              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.