01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.state;
09:
10: import java.util.Date;
11:
12: import com.google.gwt.user.client.Cookies;
13:
14: /**
15: * The default <code>Provider</code> implementation which saves state via
16: * cookies.
17: */
18: public class CookieProvider extends Provider {
19:
20: private Date expires;
21: private String path, domain;
22: private boolean secure;
23:
24: /**
25: * Creates a new cookie provider
26: *
27: * @param path The path for which the cookie is active (defaults to root '/'
28: * which makes it active for all pages in the site)
29: * @param expires the cookie expiration date (defaults to 7 days from now)
30: * @param domain The domain to save the cookie for. Note that you cannot
31: * specify a different domain than your page is on, but you can
32: * specify a sub-domain.
33: * @param secure <code>true</code> if the site is using SSL
34: */
35: public CookieProvider(String path, Date expires, String domain,
36: boolean secure) {
37: this .path = path == null ? "/" : path;
38: this .secure = secure;
39: this .domain = domain;
40: if (expires == null) {
41: expires = new Date(new Date().getTime()
42: + (1000 * 60 * 60 * 24 * 7)); // 7-days
43: }
44: this .expires = expires;
45: }
46:
47: protected void clearKey(String name) {
48: Cookies.removeCookie(name);
49: }
50:
51: protected String getValue(String name) {
52: return Cookies.getCookie(name);
53: }
54:
55: protected void setValue(String name, String value) {
56: Cookies.setCookie(name, value, expires, domain, path, secure);
57: }
58:
59: public void clear(String name) {
60: Cookies.removeCookie(name);
61: }
62:
63: public void set(String name, String value, Date expires) {
64: Cookies.setCookie(name, value, expires, domain, path, secure);
65: }
66:
67: }
|