setting and reading cookies in vaadin 7

Hi All,

How do I set and read cookies in vaadin 7 with the new APIs? Trying

public class Foo extends Root implements HttpServletRequestListener {…} doesn’t actually work since the onRequestStart methods is not hit.

I am just looking for a vaadin 7 equivalent of https://vaadin.com/book/-/page/advanced.httpservletrequestlistener.html where it talks about setting and reading cookies.

Thanks

If you want to, you can still use the HttpServletRequestListener interface. While it doesn’t have any effect if implemented by a Root, it still works as previously with Application. See e.g.
https://vaadin.com/forum/-/message_boards/view_message/1032255
for more info.

Another option is that you use the WrappedRequest that is passed e.g. to Root.init and to RequestHandler.handleRequest. The WrappedRequest API does not yet give any direct access to cookies, but you could use WrappedHttpServletRequest.cast(WrappedRequest) to get a WrappedHttpServletRequest that gives full access to the HttpServletRequest API.

Hey Leif,

Thanks that definitely answers how to read cookies but how about setting them using the wrappedhttpservletresponse api? I prefer to use that than implement HttpServletRequestListener. Root.init only has access to WrappedRequest.

Also I noticed that in WrappedResponse interface there is no setCookie so I wonder even if I could get access to wrappedhttpservletresponse if I can set the cookie via the setCookie api. The HttpServletResponse object is private as well.

Thanks

It is true that no response object is provided to Root.init, and I’m not sure whether there should be one.

You can however get a response object through using HttpServletRequestListener or RequestHandler. There is for some reason no method for getting a HttpServletResponse from a WrappedResponse, but by casting the WrappedResponse to a WrappedHttpServletResponse, you get access to all the methods from the HttpServletResponse API.

We clearly need to put some effort into the API for using cookies before Vaadin 7 is ready for prime time.

thank you. Yep definitely making it easier and hopefully mirror the reqest APIs would be great when vaadin 7 ships. More intuitive that way.

Any news about this subject?

Fabiano

Hi, i’m not sure is this a right solution, but my way of doin this is to cast the VaadinRequest to VaadinServletRequest at the init when ur application start to get the cookies, otherwise you can also use VaadinService ( a static class ) to get the VaadinRequest and cast it to VaadinServletRequest and get cookies. To set cookie you can get the VaadinResponse by VaadinService and cast it to VaadinServletResponse then addcookie. By the way i’m using vaadin 7 beta3 ).


Cookie[] cookies = ((VaadinServletRequest)VaadinService.getCurrentRequest()).getCookies();

((VaadinServletResponse)VaadinService.getCurrentResponse()).addCookie(ursCookie);       

This code just work inside “init()” method.

Regards,

Fabiano

I need to add a cookie after a user has logged in. How can I add a cookie if init has already been called?

I also tried to set the cookie the way it’s described in this thread but it didn’t work - neither in the init() method nor outside of it.

I ended up setting it via JavaScript:


Page.getCurrent().getJavaScript().execute(String.format("document.cookie = '%s=%s; expires=%s;';", loginCookieName, userName, getCookieLifeTime()));