yogev levi
yogev levi

Reputation: 377

How to bind the session to the User object using jersey

I am trying to create login function and I want to save in the session specific data to use in future requests of the user is it possible?

In the loginUser, first if is always false even if the user already logged and same in the updatePassword .

I need to save the attribute from the function loginUserToSession. Any idea why it doesn't work ?

here is my code

Resource

    @Path("/logIn")
    @Singleton
    public class UserResource extends baseResource<UserDao, UserEntity>
    {
        @Path("/authenticateUser")
        @GET
        @UnitOfWork
        public String loginUser(@Context HttpServletRequest req @QueryParam("callback") String callback, @QueryParam("loginInfo") LoginInfo loginInfo) throws JsonProcessingException
        {
            if(SessionManager.isUserConnected(req))
            {
                return ResourceResponse.getResourceJsonString("null", callback, "true", ErrorMessageEnum.SUCCESS);
            }
            String userName = loginInfo.username;
            String plainTextPassword = loginInfo.password;
            UserEntity user = objectDao.logIn(userName, plainTextPassword);
            if(user != null)
            {
                SessionManager.loginUserToSession(req, user.getUserId(), userName);
                return ResourceResponse.getResourceJsonString(user.getUserStatus(), callback, "true", ErrorMessageEnum.SUCCESS);
            }
            return ResourceResponse.getResourceJsonString("null", callback, "false", ErrorMessageEnum.LOGIN_FAILED);
        }

    @Path("/updatePassword")
    @GET
    @UnitOfWork
    public String updatePassword(@Context HttpServletRequest req, @QueryParam("callback") String callback, @QueryParam("oldPwd") String oldPwd, @QueryParam("newPwd") String newPwd) throws JsonProcessingException
    {
        if(SessionManager.isUserConnected(req))
        {
            short userId = SessionManager.getUserId(req);
            ObjectDaoResponse res = objectDao.updatePassword(userId, oldPwd, newPwd);
            return ResourceResponse.getResourceJsonString(res.getObjectJsonString(), callback, res.getSuccess(), res.getCode());
        }
        else
        {
            return ResourceResponse.getResourceFailResponseString(callback, ErrorMessageEnum.USER_NOT_CONNECTED); 
        }
    }
}

SessionManager.java

    public static void loginUserToSession(HttpServletRequest req, short userId, String userName)
    {
        if(req == null)
        {
            return;
        }
        HttpSession session = req.getSession(true);
        session.setAttribute(ATTRIBUTE_USER_NAME, userName);
        session.setAttribute(ATTRIBUTE_USER_ID, userId);
        session.setAttribute(ATTRIBUTE_USER_CONNECTED, true);
    }

    public static boolean isUserConnected(HttpServletRequest req)
    {

        if(req == null)
        {
            return false;
        }
        HttpSession session = req.getSession(false);
        if(session != null)
        {
            boolean userConnected = (boolean) session.getAttribute(ATTRIBUTE_USER_CONNECTED);
            if(userConnected)
            {
                 return userConnected;
            }
            System.out.Println("session.getAttribute(ATTRIBUTE_USER_CONNECTED)== null");
        }
        return false;
    }

Upvotes: 1

Views: 873

Answers (2)

yogev levi
yogev levi

Reputation: 377

It turns out that for some reason google postman don't send the HttpServletRequest as it should be. so jersey translate it like new user and create an empty new HttpServletRequest. Conclusion do not test your server side with Google's postman when i try to send the request from my client it work fine.

Upvotes: 0

Jay Prakash
Jay Prakash

Reputation: 805

Please change into Resource like this:

public String loginUser(@Context HttpServletRequest req @QueryParam("callback") String callback, @QueryParam("loginInfo") LoginInfo loginInfo) throws JsonProcessingException
        {
            if(SessionManager.isUserConnected(req))
            {
                return ResourceResponse.getResourceJsonString("null", callback, "true", ErrorMessageEnum.SUCCESS);
            }else{
            String userName = loginInfo.username;
            String plainTextPassword = loginInfo.password;
            UserEntity user = objectDao.logIn(userName, plainTextPassword);
            if(user != null)
            {
                SessionManager.loginUserToSession(req, user.getUserId(), userName);
                return ResourceResponse.getResourceJsonString(user.getUserStatus(), callback, "true", ErrorMessageEnum.SUCCESS);
            }
          }
        }

Above was the flow error , whatever i got, Now you have to setattribute into session scope then use this:

HttpSession session = request.getSession();
session.setAttribute("UserName", "Usename_Value");

Or for request Scope use this:

request.setAttribute("attributeName",yourStringVAlue);

Upvotes: 1

Related Questions