duminică, 12 aprilie 2015

[OmniFaces utilities (2.0)] Add a cookie with given name, value, path, domain and maxage to the HTTP response


Because Google Chrome doesn't like domain: "localhost" on cookies, OmniFaces adapted this method in version 2.1 to skip setting cookie domain when it equals "localhost".

[OmniFaces utilities] The addResponseCookie() method add a cookie with given name, value, path and maxage to the HTTP response. The cookie value will implicitly be URL-encoded with UTF-8 so that any special characters can be stored in the cookie. The cookie will implicitly be set to secure when the current request is secure (i.e. when the current request is a HTTPS request). The cookie will implicitly be set in the domain and path of the current request URL.

Note I: The maxAge argument represents the maximum age of the cookie, in seconds. If this is 0, then the cookie will be removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is -1 then the cookie will become a session cookie and thus live as long as the established HTTP session.
Note II: The path argument represents the cookie path. If this is /, then the cookie is available in all pages of the webapp. If this is /somespecificpath, then the cookie is only available in pages under the specified path.
Note III: The domain argument represents cookie domain. You can use .example.com (with a leading period) if you'd like the cookie to be available to all subdomains of the domain. Note that you cannot set it to a different domain.

Method Faces#addResponseCookie() - add a cookie with given name, value and maxage to the HTTP response


Method Faces#addResponseCookie() - add a cookie with given name, value path and maxage to the HTTP response


Method Faces#addResponseCookie() - add a cookie with given name, value, path, domain and maxage to the HTTP response

Usage:

Add a cookie with given name, value and maxage to the HTTP response

import org.omnifaces.util.Faces;
...
// cookie will be removed
Faces.addResponseCookie("favorite_game", "Time Gap", 0);

// cookie will expire after 15 seconds
Faces.addResponseCookie("favorite_game", "Time Gap", 15);

// session cookie
Faces.addResponseCookie("favorite_game", "Time Gap", -1);


Add a cookie with given name, value, path and maxage to the HTTP response.

import org.omnifaces.util.Faces;
...
// session cookie with explicit path
Faces.addResponseCookie("favorite_game", "Time Gap", "/mypath", -1);


Add a cookie with given name, value, domain, path and maxage to the HTTP response.

import org.omnifaces.util.Faces;
...
// session cookie with explicit path and domain
Faces.addResponseCookie("favorite_game", "Time Gap", ".mydomain.com","/mypath", -1);


// setting 'localhost' as domain is taken into account in OmniFaces 2.0, but not in 2.1
Faces.addResponseCookie("favorite_game", "Time Gap", "localhost","/mypath", -1);

The cookie will implicitly be set to secure when the current request is secure (i.e. when the current request is a HTTPS request):

Niciun comentariu:

Trimiteți un comentariu