Search code examples
gwtgxt

How to recognize a a http error code from within GWT?


When my users try to do an action our website after their session has expired (for instance, if they left their browser open), the server responds with a HTTP Status 405 because the user is no longer logged in.

When this happens, I want to redirect the user to the login screen.

How can I recognize when a 405 error code is returned within GWT so I can then redirect the users?

Thanks

Edit: Here's the filter I'm using:

public class AuthenticationFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        if (req instanceof HttpServletRequest) {
            boolean isLoggedIn = CustomSecurity.login((HttpServletRequest)req);
            if (isLoggedIn) {
                // TODO: How to redirect the user here???
                }
        }
        chain.doFilter(req, res);
    }

    public void init(FilterConfig arg0) throws ServletException {
    }
}

web.xml content:

<filter>
    <filter-name>Authentication Filter</filter-name>
    <filter-class>com.company.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Authentication Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

How can I make that redirect the user? Also, is there a way to force the whole browser to redirect? Since this goes into a widget, I think a Window.Location.assign('url') will only redirect the widget's HTML content.


Solution

  • I currently work on a GWT application that does something very similar to what you are asking. However, we handle these redirects in a standard javax.servlet.Filter subclass that we define in our web.xml as per usual and it is mapped to /* so it catches all requests (we use this filter for many other reasons as well).

    I don't know if you are also using a Filter or perhaps even just some sort of catch-all Servlet in your GWT app, but if you are then the solution to your issue is quite easy. You will have a handle to the HttpServletRequest and you can see if the getSession(false) method returns null then you know that the user who sent the request no longer has a session. Then you can just do a standard response.sendRedirect(...) to your login page.