How to handle invalid GWT token in places

101 Views Asked by At

In Contacts project, if a user entered an invalid token for contact editing, how to should handle it, for example, instead of #contactId:12 , entered #contactId:abc. Or in another app, maybe for security reason (such as authorization), the user does not have required permission for editing an object, how to inform user. I tried to navigate to a place such as InvalidUrlPlace but can not. I can not navigate from inside of Activity.start() method , using placeController.goTo()

update1:

@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus)
{
    GWT.log("inside start() method");
    if (assertValidPlace())
    {
        containerWidget.setWidget(view);
        initialize();
    }
}

protected boolean assertValidPlace()
{
    if (!place.isValid())
    {
                GWT.log("going to InvalidUrlPlace");
                placeController.goTo(new InvalidUrlPlace());
                return false;
    }

    return true;
}

update2:

<code>
public class InvalidUrlPlace extends Place
{
    private Messages messages = GWT.create(Messages.class);
    private InvalidToken invalidToken;

    public InvalidUrlPlace()
    {
    }

    public InvalidUrlPlace(InvalidToken invalidToken)
    {
        this.invalidToken = invalidToken;
    }

    public InvalidToken getInvalidToken()
    {
        return invalidToken;
    }

    public String getMessage()
    {
        if (invalidToken != null)
            return invalidToken.getMessage();
        return messages.pageNotFound();
    }

    @Prefix("invalidUrl")
    public static class Tokenizer implements PlaceTokenizer<InvalidUrlPlace>
    {
        @Override
        public InvalidUrlPlace getPlace(String token)
        {
            return new InvalidUrlPlace();
        }

        @Override
        public String getToken(InvalidUrlPlace place)
        {
            return "";
        }
    }
}

1

There are 1 best solutions below

8
Andrei Volgin On

Try this:

@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus)
{
    if (place.isValid())
    {
        containerWidget.setWidget(view);
        initialize();
    }
    else
    {
        placeController.goTo(new InvalidUrlPlace());
    }
}

You are trying to execute code (return false) after you already told the activity to leave to a new place - i.e. finish this activity and start a new one.