3 Replies Latest reply on Nov 20, 2012 3:55 AM by Martin Kouba

    Seam 2.2.1 and stateless web service

    Ricardo Memoria Novice

      Hi all,

       

      I have a web app in SEAM 2.2.1 using JBOSS 4.2.3 in a WAR file.

       

      I've tried SEAM documentation that doens't cover much about that.

       

      I'm trying to implement web services in this app. I notice that when I call the authenticator bean (using the same authenticator I use in web login page), the web service creates a session context and keeps it after the execution of the web service.

       

      This is the authenticator I call

       

      @WebService(name="authenticatorService", serviceName="authenticatorService")
      @SOAPBinding(style=Style.RPC)
      public class AuthenticatorService {
      
      
                @WebMethod
                public String login(String username, String password) {
                          try {
                                    Identity.instance().setUsername(username);
                                    Identity.instance().setPassword(password);
                                    Identity.instance().login();
        
                                      // testing the caller
                                    HttpServletRequest req = (HttpServletRequest)ServletContexts.instance().getRequest();
                                    System.out.println(req.getRemoteAddr());
                                    System.out.println(req.getHeader("User-Agent"));
      
      
                                    if (Identity.instance().isLoggedIn()) {
                                              Identity.instance().logout();     // useless
                                               Session.instance().invalidate();     // useless
                                                        AuthenticatorBean authenticator = (AuthenticatorBean)Component.getInstance("authenticator");
                                              return authenticator.getMySessionId();
                                    }
                                    else return "fail";
      
                          } catch (Exception e) {
                                    return e.getMessage();
                          }
                }
      

       

      As you may see, I'm trying to finish the session immediatelly after the execution of my Web Service, but the session remains.

       

      I see it keeps the session active because I have a

       

          @Observer("org.jboss.seam.preDestroyContext.SESSION")

       

      That is just called after the session timeout (45 minutes).

       

      Does anyone know how to implement pure stateless web services using SEAM?

       

      or maybe, How to terminate the session after execution of the web service?

       

      Thanks,

      Ricardo

        • 1. Re: Seam 2.2.1 and stateless web service
          Martin Kouba Master

          Indeed the SOAPRequestHandler does not perform standard context cleanup. It only calls ServletLifecycle.beginRequest() for inbound message, but no ServletLifecycle.endRequest() for outbound message. The question is whether it's intentional or a bug. I think it might be related to the conversation stuff (Conversational Web Services). Note that Session.instance().invalidate() only schedules HttpSession invalidation at the end of the request (few Seam internal classes check this flag and do the real invalidation).

           

          In any case you can try to extend SOAPRequestHandler and override handleOutbound() method like this...

          public boolean handleOutbound(MessageContext messageContext) {
            try {
                  boolean continue = super.handleOutbound(messageContext);
                  if(continue) {
                    HttpServletRequest request = (HttpServletRequest) messageContext.get(MessageContext.SERVLET_REQUEST);
                    ServletLifecycle.endRequest(request);
                  }
                  return continue;
                }
                catch (SOAPException ex)  {
                   log.error("Error handling inbound SOAP request", ex);
                   return false;
                }
          }

          And of course replace the Seam SOAPRequestHandler in jaxws-endpoint-config.xml with your implementation.

           

          Though I'm not sure this will work and also about the consequences (the conversation stuff and so on).

          • 2. Re: Seam 2.2.1 and stateless web service
            Ricardo Memoria Novice

            Hi Martin,

             

            Thanks a lot, this has just worked for me.

             

            It really seems to be a bug, since it doesn't make sense to me to leave a session open after calling a Web Service.

             

            Anyway... Thanks again,

            Ricardo

            • 3. Re: Seam 2.2.1 and stateless web service
              Martin Kouba Master

              Well, it does make sense for certain scenarios (although it's not a neat WS solution :-).

               

              BTW I filed an issue to clarify this: JBSEAM-5060.