HttpServletRequestListener conversion example for Vaadin 7

In Vaadin 6, we used HttpServletRequestListener to perform the following functions:

  1. onRequestStart() used the HttpServletRequest object to reconstruct the full external URL and context path of the application – it’s in non-Vaadin code that uses standard HttpServletRequest methods to do this.

  2. onRequestStart() was used to redirect a user to the login page (an external JSP page) if they were not already logged in.

  3. onRequestStart() was able to set the “last communications timestamp” to track the last access from the browser.

  4. onRequestEnd() was used to remove our ThreadLocal instances.

Is there an example of how to do this under Vaadin 7.0.5? Thanks!

As far as I can see, none of those functions actually needs anything to do Vaadin => I would be inclined to implement a plain old J2EE
javax.servlet.Filter
that works an interceptor, wrapping around the request before it hits the Vaadin Servlet.

HTH,

Cheers,

Charles.

Sure, that will work for simple intercepts. I think I could also extend VaadinServlet and deal with the service method, too.

I guess the follow-up question, then, is how do I get the UI instance that represents my Vaadin app from a regular session? Is there a standard session variable name we can count on since I presume it’s stored in the session? I need the UI instance because I also show the last communications time in our UI.

Okay, maybe your other answer regarding com.vaadin.terminal.gwt.server.WebApplicationContext.getApplicationContext(sess).getApplications() will do the trick to find the UIs to update:

VaadinSession.getCurrent().getUIs()

Spoke too soon. It seems that my code needs to run after the VaadinServlet has initialized all of the threadlocals, etc., or I’ll have to code to a more general way using HttpSession (not sure how for Vaadin). It seems that VaadinServlet.service() is called, but it’s all cleaned up by the time it exits, so if I hook before or after it, I don’t have access to VaadinSession.getCurrent() in that it always returns null.

We really need some sort of hook between the initialization, but before it attempt to process the actual request. Any ideas? Thanks!

In 7.1 you have the poissibility to add RequestHandlers to VaadinService. The primary purpose of the request handlers is to actually handle requests but you should be able to use them as sort of filters also. They are called with VaadinSession as a parameter in addition to the request and response. From that you can find the UI using session.getService().findUI(request).

Based on this and Artur’s response, I suspect that for 7.0.x, the best approach here would be to create a custom VaadinServlet and override service(HttpServletRequest, HttpServletResponse)

It seems that to access the VaadinSession from the httpRequest on the Servlet you should do

getService().findVaadinSession(createVaadinRequest(request));

NB: I’ve not had to do this yet, I’m just reading the source code!

Cheers,

Charles.

That sounds like it may resolve it. When do you think 7.1 will be generally available? I know it must be coming soon since I noted it was in beta release.

By the way, how may UI instances can there be in a session if we have just one “application”? Will there only be one, or will there be more if we open views in new browser windows?

Thanks, Charles. I may just wait for 7.1. It’s enough work porting our application as it is and I can do a hack workaround for now in that I just store my UI in my own session variable so my VaadinServlet can find it without using any Vaadin-isms.