Bhavesh
Bhavesh

Reputation: 519

GWT-Platform : onReveal() method is not invoked on browser refresh when application is deployed on tomcat/jboss

In one of my GWT-Platform application I am facing a weird issue, when I run application in jetty configured in eclipse GWT plugin and hit the browser refresh current page is loaded (placeManager.getCurrentPlaceRequest()) succesfully but when application war is deployed in tomcat/jboss execution stops after onBind() method of current place request's presenter and doesn't reveal page.

In the process of handling refresh, first time canReveal() method of GateKeeper for that Presenter returns false and after a server call I reveal current place again which causes canReveal() method to return true but the presenter is still unrevealed. So, here I suspect something is tricky!!

Any hint on such behavior?

Following is the code snippet for implemented LoggedInGatekeeper which playing a key role:

public class LoggedInGatekeeper implements Gatekeeper {

private final EventBus eventBus;

private final DispatchAsync dispatcher;

private final PlaceManager placeManager;

private CurrentUser currentUser;

private boolean isUserLoggedOut;

private String lastPageAccessed;

@Inject
public LoggedInGatekeeper(final EventBus eventBus, final DispatchAsync dispatcher, final PlaceManager placeManager)
{
    this.eventBus = eventBus;

    this.dispatcher = dispatcher;

    this.placeManager = placeManager;

    this.eventBus.addHandler(LoginAuthenticatedEvent.getType(), new LoginAuthenticatedEventHandler()
    {
        @Override
        public void onLogin(LoginAuthenticatedEvent event)
        {
            currentUser = event.getCurrentUser();
            isUserLoggedOut = false;
        }
    });

    this.eventBus.addHandler(LogoutUserEvent.getType(), new LogoutUserEventHandler()
    {
        @Override
        public void onLogoutUser(LogoutUserEvent event)
        {
            SessionFactory.removeCookie(Constants.LAST_PAGE_ACCESSED);
            currentUser = null;
            isUserLoggedOut = true;
        }
    });
}

@Override
public boolean canReveal()
{
    Log.info("Browser fetched session cookie : " + Cookies.getCookie(Constants.JSESSION_COOKIE_KEY));

    if (Cookies.getCookie(Constants.JSESSION_COOKIE_KEY) == null)
    {
        SC.say("Your session is expired. Please login again");
        NavigateToLoginEvent.fire(eventBus);
        return false;
    }

    if (currentUser != null && !isUserLoggedOut)
    {

        lastPageAccessed = placeManager.getCurrentPlaceRequest().getNameToken();
        Log.info("canReveal() 1 : " + lastPageAccessed);
        SessionFactory.addCookie(Constants.LAST_PAGE_ACCESSED, lastPageAccessed);
        return currentUser.isLoggedIn();
    }
    else if (isUserLoggedOut)
    {
        Log.info("canReveal() 2 : User is logged out");
        NavigateToLoginEvent.fire(eventBus);
        return false;
    }
    else
    {
        Log.info("canReveal() 3 : Check on server for logged in user");
        dispatcher.execute(new FetchLoggedInUserAction(), new FetchLoggedInUserAsyncCallback());
        return true;
    }
}

class FetchLoggedInUserAsyncCallback extends MessageAsyncCallback<FetchLoggedInUserResult>
{
    /**
     * 
     */
    public FetchLoggedInUserAsyncCallback()
    {
        super("Loading...");
    }

    @Override
    public void doOnFailure(Throwable caught)
    {
        NavigateToLoginEvent.fire(eventBus);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void doOnSuccess(FetchLoggedInUserResult result)
    {
        if (result == null)
        {
            Log.info("doOnSuccess() 1 : LoggedInUser not found on server");
            NavigateToLoginEvent.fire(eventBus);
        }
        else
        {
            Log.info("doOnSuccess() 2 : LoggedInUser found on server");
            if (result.getLoggedInUser() != null)
            {

                currentUser = new CurrentUser();
                currentUser.setMerchantConsoleUser(result.getLoggedInUser());

                SessionFactory.getClientSessionInstance().put(SessionKeys.LOGGED_IN_USER, result.getLoggedInUser());

                PlaceRequest currentPlaceRequest = placeManager.getCurrentPlaceRequest();

                Log.info("doOnSuccess() 3 : " + currentPlaceRequest.getNameToken());

                if (currentPlaceRequest != null)
                {   
                    placeManager.revealPlace(currentPlaceRequest);
                }
            }
            else
            {
                NavigateToLoginEvent.fire(eventBus);
            }
        }
    }
}

}

Thanks in Advance.

Upvotes: 4

Views: 1394

Answers (1)

Bhavesh
Bhavesh

Reputation: 519

Mystery revealed, Added following code in onBind() method and browser refresh started working in all environment as intended. It seems like a fuzzy behavior of the framework where GateKeeper tries to fetch data from server at that time somehow presenter's visibility is set to false and this same thought caused me to write following code and it worked, Hope someone from GWT-P can have look at this post to think of proper way to handle such scenario:

/**
     * This piece of code takes care of revealing page on browser refresh event.
     * On browser refresh somehow visibility of page is set to false, so, we need to manually fire the reveal content event.
     */
    if(this.getProxy().canReveal() == true && !this.isVisible()){
                revealInParent();
    }

Upvotes: 1

Related Questions