3

I have set an attribute in the session that would be true or false. If that attribute is true I need vaadin to intercept all requests and redirect it to a new custom Vaadin page. I know there is a interface called VaadinServiceInitListener however I am having trouble redirecting the user to the custom page since UI.getcurrent() returns null.

Here is my code:

@Override
public void serviceInit(ServiceInitEvent serviceEvent) {
    serviceEvent.getSource().addSessionInitListener(initEvent -> {
        initEvent.getSession().addRequestHandler(globalRequestHandler());
    });

}

public RequestHandler globalRequestHandler() {
    return (session, vaadinRequest, response) -> {
        session.access(() -> {
            if (isSessionlocked()) {
                UI.getCurrent().navigate(MyCustomView.class);
            }
        });
        return false;
    };
 }

1 Answers1

4

Try to redirect to the desired path

response.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, path);
Hawk
  • 2,042
  • 8
  • 16
  • This is working however it seems vaadin has other request handlers that comes before mine. If I add a spring filter and log all requests, i can see that there are several requests going though but only a few that is picked up by my request handler. I want to be able to catch all requests and deal with them appropriately (smilar to what Vaadinsession.lock() does). – Aigeth Magendran Oct 13 '21 at 14:01
  • 2
    You could create your own Filter with a high priority instead of calling addRequestHandler, see https://stackoverflow.com/a/69486045/9274948 – Hawk Oct 13 '21 at 21:24
  • Thanks, that seems to work. – Aigeth Magendran Oct 14 '21 at 10:44