001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client;
017:
018: import java.util.Collection;
019: import java.util.Date;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * Provides access to browser cookies stored on the client. Because of browser
025: * restrictions, you will only be able to access cookies associated with the
026: * current page's domain.
027: */
028: public class Cookies {
029:
030: /**
031: * Cached copy of cookies.
032: */
033: static HashMap<String, String> cachedCookies = null;
034:
035: /**
036: * Raw cookie string stored to allow cached cookies to be invalidated on
037: * write.
038: */
039: // Used only in JSNI.
040: static String rawCookies;
041:
042: /**
043: * Gets the cookie associated with the given name.
044: *
045: * @param name the name of the cookie to be retrieved
046: * @return the cookie's value, or <code>null</code> if the cookie doesn't exist
047: */
048: public static String getCookie(String name) {
049: Map<String, String> cookiesMap = ensureCookies();
050: return cookiesMap.get(name);
051: }
052:
053: /**
054: * Gets the names of all cookies in this page's domain.
055: *
056: * @return the names of all cookies
057: */
058: public static Collection<String> getCookieNames() {
059: return ensureCookies().keySet();
060: }
061:
062: /**
063: * Removes the cookie associated with the given name.
064: *
065: * @param name the name of the cookie to be removed
066: */
067: public static native void removeCookie(String name) /*-{
068: $doc.cookie = name + "=;expires=Fri, 02-Jan-1970 00:00:00 GMT";
069: }-*/;
070:
071: /**
072: * Sets a cookie. The cookie will expire when the current browser session is
073: * ended.
074: *
075: * @param name the cookie's name
076: * @param value the cookie's value
077: */
078: public static void setCookie(String name, String value) {
079: setCookieImpl(name, value, 0, null, null, false);
080: }
081:
082: /**
083: * Sets a cookie.
084: *
085: * @param name the cookie's name
086: * @param value the cookie's value
087: * @param expires when the cookie expires
088: */
089: public static void setCookie(String name, String value, Date expires) {
090: setCookie(name, value, expires, null, null, false);
091: }
092:
093: /**
094: * Sets a cookie.
095: *
096: * @param name the cookie's name
097: * @param value the cookie's value
098: * @param expires when the cookie expires
099: * @param domain the domain to be associated with this cookie
100: * @param path the path to be associated with this cookie
101: * @param secure <code>true</code> to make this a secure cookie
102: */
103: public static void setCookie(String name, String value,
104: Date expires, String domain, String path, boolean secure) {
105: setCookieImpl(name, value, (expires == null) ? 0 : expires
106: .getTime(), domain, path, secure);
107: }
108:
109: static native void loadCookies(HashMap<String, String> m) /*-{
110: var docCookie = $doc.cookie;
111: if (docCookie && docCookie != '') {
112: var crumbs = docCookie.split('; ');
113: for (var i = 0; i < crumbs.length; ++i) {
114: var name, value;
115: var eqIdx = crumbs[i].indexOf('=');
116: if (eqIdx == -1) {
117: name = crumbs[i];
118: value = '';
119: } else {
120: name = crumbs[i].substring(0, eqIdx);
121: value = crumbs[i].substring(eqIdx + 1);
122: }
123: name = decodeURIComponent(name);
124: value = decodeURIComponent(value);
125: m.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)(name,value);
126: }
127: }
128: }-*/;
129:
130: private static HashMap<String, String> ensureCookies() {
131: if (cachedCookies == null || needsRefresh()) {
132: cachedCookies = new HashMap<String, String>();
133: loadCookies(cachedCookies);
134: }
135: return cachedCookies;
136: }
137:
138: private static native boolean needsRefresh() /*-{
139: var docCookie = $doc.cookie;
140:
141: // Check to see if cached cookies need to be invalidated.
142: if (docCookie != @com.google.gwt.user.client.Cookies::rawCookies) {
143: @com.google.gwt.user.client.Cookies::rawCookies = docCookie;
144: return true;
145: } else {
146: return false;
147: }
148: }-*/;
149:
150: private static native void setCookieImpl(String name, String value,
151: long expires, String domain, String path, boolean secure) /*-{
152: var c = encodeURIComponent(name) + '=' + encodeURIComponent(value);
153: if ( expires )
154: c += ';expires=' + (new Date(expires)).toGMTString();
155: if (domain)
156: c += ';domain=' + domain;
157: if (path)
158: c += ';path=' + path;
159: if (secure)
160: c += ';secure';
161:
162: $doc.cookie = c;
163: }-*/;
164:
165: private Cookies() {
166: }
167: }
|