I had a difficult time too, but then it turned out that I was testing using
"localhost", which doesn't work for cookies, at least not on Mac OSX. I
tried 127.0.0.1, and everything worked just fine.
Here are some of my cookie methods (in a superclass). Note: in Scala, but I
can get the idea.
def getCookie(name: String): Option[String] = {
val cookies = getRequest().getCookies()
cookies.getFirstValue(COOKIE_NAME) match {
case value: String => Some(value)
case _ => None
}
}
def getDomain(): String =
getRequest().getHostRef().getHostDomain()
def setCookie(value: String): Unit = {
val hash = value
val cookie = new CookieSetting(0, COOKIE_NAME, hash, "/", getDomain())
getResponse().getCookieSettings().add(cookie)
}
def unsetCookie(): Unit = {
val cookie = new CookieSetting(0, COOKIE_NAME, null, "/", getDomain())
cookie.setMaxAge(0)
getResponse().getCookieSettings().add(cookie)
}
I spend many hours trying to get this to work before I discovered that
"localhost" was the issue. You might be able to fix it with a change to
/etc/hosts:
127.0.0.1 localhost.localdomain localhost
But I changed it by adding a filter that redirects all requests from
localhost to 127.0.0.1.
class LocalhostRedirect extends Filter {
// Redirect incoming requests from "localhost" to
// "127.0.0.1" so that cookies will work properly.
override
def beforeHandle(request: Request, response:Response): Int = {
def url = request.getOriginalRef().toString
if (! url.contains("localhost"))
return Filter.CONTINUE
def location = url.replace("localhost", "127.0.0.1")
response.redirectTemporary(location)
Filter.STOP
}
}
Again with the Scala.
Hope at least some of this helps!
Keith
On Mon, Oct 18, 2010 at 10:00 AM, <[email protected]> wrote:
> I'm having a heck of a time trying to set a cookie. This is the code I
> have so far:
>
> In the first part attempts to read any cookies that I put there before,
> this always comes up blank.
>
> The second part tries to add a new cookie to the response. Theres a few
> places that I could have gone wrong:
> 1) I didn't know how to instantiate a new Series<CookieSetting>, so I
> grabbed the existing one and modified it by by calling
> 'this.getCookieSettings()'.
> 2) I added the cookie by calling 'this.setCookieSettings()'
> 3) I'm doing this through 'POST'
>
> When I check the response headers 'Set-Cookie' doesn't appear to be there
> at all.
>
> public class TestCookieResource extends ServerResource {
>
> @Post
> public Representation post(Representation representation){
>
> // READ COOKIES FROM CLIENT
> Series<Cookie> cookies = this.getRequest().getCookies();
> int cookieNum = cookies.size();
> String values = "";
> for( Cookie cookie : cookies){
> values += cookie.getValue() + "\n";
> }
>
> // SET NEW COOKIE
> CookieSetting cS = new CookieSetting(0, "cookieName",
> "cookieValue");
> Series<CookieSetting> cookieSettings =
> this.getCookieSettings();
> cookieSettings.clear();
> cookieSettings.add(cS);
> this.setCookieSettings(cookieSettings);
>
> // SEND RESPONSE
> this.setStatus(Status.SUCCESS_OK);
> //StringRepresentation sR = new
> StringRepresentation(HELP.getJSON(HELP.getMessageMap("Test
> response")).toString());
> StringRepresentation sR = new StringRepresentation("test
> response");
> return sR;
> }
> }
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2673132
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2673158