users@glassfish.java.net

Re: session persistence on gf3

From: <glassfish_at_javadesktop.org>
Date: Wed, 30 Jun 2010 09:18:07 PDT

> Are all the servlet in the same war file? How do gwt
> application calls the gwt servlet? Can you provide
> more details?

> Are all the servlet in the same war file? How do gwt
> application calls the gwt servlet? Can you provide
> more details?

hello,
>Are all the servlet in the same war file? How do gwt application calls the gwt servlet? Can you provide more details?

Yes.
User can login with loginUser(String userName, String passwd).
result of ProgrammaticLogin is true, but it doesn' t put user info to session.
I try to put user object into the session named ATTR_USER .
And then the client calls getRoles() method. In this I try to get ATTR_USER, but I get a null.

Here is my gwt-ext client code:

private final static UserServiceAsync userService = GWT.create(UserService.class);
...
 private void onLogin() {
        
        userService.loginUser(this.dialog.getUSerName(), this.dialog.getPassword(), new AsyncCallback<User>() {

            public void onFailure(Throwable caught) {
                loginFail();
            }

            public void onSuccess(User result) {
                if (result != null)
                    loginSuccess(result);
                else
                    loginFail();
            }
        });
    }

UserService:
@RemoteServiceRelativePath("userservice")
    public interface UserService extends RemoteService {
        User getUser();
        User loginUser(String userName, String passwd);
        Boolean logoutUser();
        List<String> getRoles();

    }
UserServiceAsync:
public interface UserServiceAsync {
    void getUser(AsyncCallback<User> asyncCallback);
    void loginUser(String userName, String password, AsyncCallback<User> asyncCallback);
    void logoutUser(AsyncCallback<Boolean> asyncCallback);
    void getRoles(AsyncCallback<List<String>> asyncCallback);
}

and the servlet:
UserServiceImpl:

public class UserServiceImpl extends ServiceImpl<User, UserRemote> implements UserService {

    private final static Logger log = Logger.getLogger(UserServiceImpl.class.getName());
    private final static String ATTR_USER = "actUser";

    @EJB(name = UserRemote.JNDI_NAME)
    private UserRemote userBean;


    @Override
    protected UserRemote getBean() {
        return this.userBean;
    }

    public User getUser() {
        try {
            Principal principal = this.getThreadLocalRequest().getUserPrincipal();
            User user = null;
            if (principal != null) {
                String userName = principal.getName();
                Response<List<User>> response = this.getBean().find(principal.getName()); // Find User by j_username.
                if (!response.isError()) {
                    if (response.get().size() == 1) {
                        user = response.get().get(0);
                    }
                    return user;
                } else {
                    log.log(Level.ALL, "getUser error");
                    return null;
                }
            }
            return user;
        } catch (Exception e) {
            log.log(Level.ALL, "getUser error", e);
            return null;
        }
    }

    public User loginUser(String userName, String passwd) {
        try {
            ProgrammaticLogin pl = new ProgrammaticLogin();
            if (pl.login(userName, passwd, BelasConsts.REALM_NAME, this.getThreadLocalRequest(), this.getThreadLocalResponse(), true)) {
                User usr = this.getUser();
                HttpSession session = this.perThreadRequest.get().getSession(true);
                session.setAttribute(ATTR_USER, usr);
                return usr;
            }
            return null;
        } catch (Exception e) {
            log.log(Level.ALL, "loginUser error:", e);
            return null;
        }
    }

    public Boolean logoutUser() {
        try {
            HttpServletRequest req = this.getThreadLocalRequest();
            Principal principal = req.getUserPrincipal();
            HttpSession session = req.getSession();
            if (session != null) {
                session.invalidate();
            }
            return true;
        } catch (Exception e) {
            log.log(Level.ALL, "logoutUser error", e);
            return false;
        }
    }

    public List<String> getRoles() {
        this.tryToRestoreUserFromSession();
        HttpServletRequest req = this.getThreadLocalRequest();

        Principal principal = req.getUserPrincipal();
        System.out.println("principal on userserviceimpl"+ (principal != null ? principal.getName() : "unknown!!!!!!!!!"));
        Response<List<String>> result = this.userBean.getRoles();
        return result.isError() ? Collections.EMPTY_LIST : result.get();
    }

    private void tryToRestoreUserFromSession() {
        HttpSession session = this.perThreadRequest.get().getSession(true);
        User usr = (User) session.getAttribute(ATTR_USER);
        
    }
}

and web.xml:
<servlet>
        <servlet-name>UserService</servlet-name>
        <servlet-class>com.zamek.belas.web.server.UserServiceImpl</servlet-class>
    </servlet>
...
  <servlet-mapping>
        <servlet-name>UserService</servlet-name>
        <url-pattern>/com.zamek.belas.web.Belas/userservice</url-pattern>
    </servlet-mapping>

...
<session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

thanx in advance

Zamek
[Message sent by forum member 'zamek']

http://forums.java.net/jive/thread.jspa?messageID=476429