Skip to content
JakeHoward edited this page Jul 16, 2015 · 1 revision

Utterlyidle makes it really easy to handle cookies.

public class CookiesResource {
    @GET
    @Path("foo")
    public Response getAndSet(@CookieParam("name") String name) {
        return ResponseBuilder.response().cookie("anotherName", cookie("anotherValue")).entity(name).build();
    }
}

In the example above, we read a cookie (name) from the Cookie HTTP request header by using @CookieParam annotation. We set another cookie (anotherName) in the Set-Cookie HTTP response header by using cookie() method on the ResponseBuilder.

Note: Utterlyidle can handle both quoted and unquoted cookie values in HTTP requests:

Cookie: name=value
Cookie: name="value" 

Cookie attributes

So far we've seen a cookie with only a name and a value. We can also specify extra cookie attributes like domain, max age, comment, path, secure and expires by using static methods defined in com.googlecode.utterlyidle.cookies.CookieAttribute:

import static com.googlecode.utterlyidle.cookies.Cookie.cookie;
import static com.googlecode.utterlyidle.cookies.CookieAttribute.*;
...
Cookie cookie = Cookie.cookie("1", comment("some comment"), domain(".acme.com"), maxAge(123), path("/products"), secure(), expires(calendar.getTime()));

If you put this cookie in the response object the following HTTP header will be included:

Set-Cookie: a="1"; Comment="some comment"; Domain=".acme.com"; Max-Age="123"; Path="/products"; Secure=""; Expires="Sun, 04-Sep-2011 06:15:36 GMT"
Clone this wiki locally