5 Replies Latest reply on Jan 31, 2008 6:37 AM by Frode Nilsen

    How can I use session sope in jbossws?

    Chico Chen Newbie

      I use flash lite client to access jbossws webservice, ServletContexts.instance().getRequest() return null, so I can't get the session scope.

      Maybe I need to use a token in soap header? If so, is there a good way to map the session scope to the token?

        • 1. Re: How can I use session sope in jbossws?
          Frode Nilsen Newbie

          I would be very interested in some pointers on this as well, as I have the exact same problem. Have you figured it out, chicochen?

          • 2. Re: How can I use session sope in jbossws?
            Michael J. Hammel Newbie

            I don't know if this helps, but see my thread on a similar subject:
            http://www.jboss.com/index.html?module=bb&op=viewtopic&t=128375

            I tried to get the session context via the MessageContext, but that doesn't appear to work. At least not the way I tried. There is a helpful explanation on how to get the request context here:

            https://jax-ws.dev.java.net/articles/MessageContext.html

            and that's what I've used to get as far as I have. But I'm still not able to retrieve the property I'm after (the PASSWORD_PROPERTY).

            • 3. Re: How can I use session sope in jbossws?
              Frode Nilsen Newbie

              Thanks for your reply, mjhammel, Using the approach described in your links, I managed to get the session object. I can now set and retrieve variables from session as long as the client has the JSESSIONID http header field set correctly.

              However, I still have some unresolved issues with this.

              In my application, I have a JSF page where users can log in. The page uses a seam component, in which I have the following code (somewhat simplified):

              @Stateless
              @Name("authenticator")
              public class AuthenticatorAction {
              
               @EJB
               ProfileDAO profileDAO;
              
               @RequestParameter
               String username;
              
               @RequestParameter
               String password;
              
               @In(required=false)
               @Out(required=false, scope=SESSION)
               private Profile authenticatedProfile;
              
               public String login() {
               Profile p = profileDAO.getUser(username, password);
               authenticatedProfile = profile;
               }
              }


              This works fine, and the Profile object is put into the session scope (ie can be fetched in other seam components over different page loads etc).

              The question is, how can I retreive the profile object in my web service? Doing a
              session.getAttribute("authenticatedProfile")

              does not work. In fact,
              System.out.println((session.getValueNames()).length);

              reveals that there are no variables stored in the session!

              Any ideas?

              • 4. Re: How can I use session sope in jbossws?
                Chico Chen Newbie

                To froden:

                Would mind parse you code here pls?

                • 5. Re: How can I use session sope in jbossws?
                  Frode Nilsen Newbie

                  You mean for the webservice? This is the interface:

                  package services;
                  
                  import javax.jws.WebMethod;
                  import javax.jws.WebParam;
                  import javax.jws.WebService;
                  import javax.jws.soap.SOAPBinding;
                  
                  @WebService(
                   name = "EndpointInterface",
                   targetNamespace = "http://mynamespace/xml"
                  )
                  @SOAPBinding(style = SOAPBinding.Style.RPC)
                  public interface UserService {
                   @WebMethod(operationName = "registerUser", action = "urn:registerUser")
                   public String registerUser(
                   @WebParam(name = "nickname") String nickname,
                   @WebParam(name = "password") String password,
                   @WebParam(name = "email") String email
                   );
                  }


                  And the implementation:

                  package services;
                  
                  import javax.annotation.Resource;
                  import javax.ejb.EJB;
                  import javax.ejb.EJBContext;
                  import javax.ejb.Stateless;
                  import javax.jws.WebService;
                  import javax.naming.InitialContext;
                  import javax.servlet.http.HttpSession;
                  import javax.xml.ws.WebServiceContext;
                  import javax.xml.ws.WebServiceException;
                  import javax.xml.ws.handler.MessageContext;
                  
                  import org.jboss.ws.annotation.WebContext;
                  import org.jboss.ws.core.CommonMessageContext;
                  import org.jboss.ws.core.jaxws.WebServiceContextEJB;
                  import org.jboss.ws.core.jaxws.handler.SOAPMessageContextJAXWS;
                  import org.jboss.ws.core.soap.MessageContextAssociation;
                  
                  @WebService(
                   endpointInterface = services.UserService",
                   serviceName = "UserService"
                  )
                  @WebContext(contextRoot="/ws", urlPattern="/UserService")
                  @Stateless
                  public class UserServiceImpl implements UserService {
                  
                   WebServiceContext wsContext;
                  
                   @Resource
                   EJBContext ejbCtx;
                   public String registerUser(String nickname, String password, String email) {
                  
                   CommonMessageContext msgContext = MessageContextAssociation.peekMessageContext();
                   wsContext = new WebServiceContextEJB((SOAPMessageContextJAXWS)msgContext, ejbCtx);
                   MessageContext mc = wsContext.getMessageContext();
                  
                   HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
                  
                   if (session == null)
                   throw new WebServiceException("No session in WebServiceContext");
                  
                   String[] names = session.getValueNames();
                   System.out.println("There are " + names.length + " variables in the session.");
                   for(int i = 0; i < names.length; i++) {
                   System.out.println("We have a session variable called " + names[ i ] + " holding the value " + session.getAttribute(names[ i ]));
                   }
                  
                   // Get a session property "counter" from context
                   Integer counter = (Integer)session.getAttribute("counter");
                   if (counter == null) {
                   counter = new Integer(0);
                   System.out.println("Starting the Session");
                   }
                  
                   System.out.println("Session id is: " + session.getId());
                   counter = new Integer(counter.intValue() + 1);
                   session.setAttribute("counter", counter);
                  
                   System.out.println("Counter is: " + counter);
                   return "blabla";
                   }
                  
                  }


                  Sorry about the mess hehe