Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and retrieve cookies in Grails?

Tags:

cookies

grails

I'm trying to use a cookie in a Grails application with no success. In particular, in the show() method of a test suite controller, I want to set a cookie to be the most recent test suite shown; that is, the value of params.id as made available to the show() method. Then, in the list() method, I want to provide a shortcut to showing the most recent test suite (the value I'm trying to set with a cookie).

However, the cookie I set (named "tcCookie") is visible only in the show() method - not in the list() method. Here's the code:

in show():

request.cookies.each { println "show: cookie ${it.name} <${it.value}>" }

in list():

request.cookies.each { println "list: cookie ${it.name} <${it.value}>" }

I set the cookie in the show() method by:

Cookie cookie = new Cookie( "tcCookie", params.id )
cookie.maxAge = 315360000000L
response.addCookie( cookie )

When I go back and forth between the list and show views (after show() with params.id = 277) I get:

show: cookie tcCookie <277>
show: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
list: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
show: cookie tcCookie <277>
show: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
list: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>

which appears to me to indicate that the cookie "tcCookie" is visible only in the show() method, which is useless to me. I need to set it so that it's visible in other methods.

Additionally the cookie tcCookie survives across multiple sessions - as I would expect - but still is visible only in the show() method.

Maybe my understanding of cookies is wrong, but I'm still stuck. Any help would be appreciated. Additionally, there must be hundreds of other cookies set in the browser, none of which are visible using the code I've used, so is there some way to get to the other cookies?

like image 506
hbrednek Avatar asked Sep 22 '14 21:09

hbrednek


2 Answers

The problem is a side effect of setting the cookie without explicitly setting the path. Assuming the Grails application's name is "app" it would be accessed by

http://hostname.com/app

This will cause the cookie JSESSIONID to be set as /app/JSESSIONID which will be visible anywhere in the application. Since the application's name is available within the application as grailsApplication.metadata['app.name'] determining the actual value of app can be made automatic.

However, in an individual view, for example the list view in the xxx_domain object, accessed as

http://hostname.com/app/xxx_domain/list

setting a cookie with the code

def newCookie = new Cookie( "myCookie", "cookieValue" )
response.addCookie newCookie

will set the path of newCookie to /app/xxx_domain/list/ where it will be visible only in the list() method and view. In order for that cookie to be visible anywhere in the application, the path must be set as shown in the above answer, but not to /, rather to /app as in

def newCookie = new Cookie( "myCookie", "cookieValue" )
newCookie.path = "/${grailsApplication.metadata['app.name']}/"
response.addCookie newCookie
like image 136
hbrednek Avatar answered Oct 04 '22 03:10

hbrednek


or you can read the basic Java doc and use plain response and request objects to do that:

Cookie homeCookie = new Cookie( 'home', 'name' )
homeCookie.path = '/'
homeCookie.maxAge = 0
response.addCookie homeCookie

and

String home = request.cookies.find{ 'home' == it.name }?.value
like image 43
injecteer Avatar answered Oct 04 '22 02:10

injecteer