KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > Cookies


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * Provides access to browser cookies stored on the client. Because of browser
25  * restrictions, you will only be able to access cookies associated with the
26  * current page's domain.
27  */

28 public class Cookies {
29
30   /**
31    * Cached copy of cookies.
32    */

33   static HashMap JavaDoc cachedCookies = null;
34
35   /**
36    * Raw cookie string stored to allow cached cookies to be invalidated on
37    * write.
38    */

39   // Used only in JSNI.
40
static String JavaDoc rawCookies;
41
42   /**
43    * Gets the cookie associated with the given name.
44    *
45    * @param name the name of the cookie to be retrieved
46    * @return the cookie's value, or <code>null</code> if the cookie doesn't exist
47    */

48   public static String JavaDoc getCookie(String JavaDoc name) {
49     Map JavaDoc cookiesMap = ensureCookies();
50     return (String JavaDoc) cookiesMap.get(name);
51   }
52
53   /**
54    * Gets the names of all cookies in this page's domain.
55    *
56    * @return the names of all cookies
57    */

58   public static Collection JavaDoc getCookieNames() {
59     return ensureCookies().keySet();
60   }
61
62   /**
63    * Removes the cookie associated with the given name.
64    *
65    * @param name the name of the cookie to be removed
66    */

67   public static native void removeCookie(String JavaDoc name) /*-{
68     $doc.cookie = name + "='';expires='Fri, 02-Jan-1970 00:00:00 GMT'";
69   }-*/
;
70
71   /**
72    * Sets a cookie. The cookie will expire when the current browser session is
73    * ended.
74    *
75    * @param name the cookie's name
76    * @param value the cookie's value
77    */

78   public static void setCookie(String JavaDoc name, String JavaDoc value) {
79     setCookieImpl(name, value, "", null, null, false);
80   }
81
82   /**
83    * Sets a cookie.
84    *
85    * @param name the cookie's name
86    * @param value the cookie's value
87    * @param expires when the cookie expires
88    */

89   public static void setCookie(String JavaDoc name, String JavaDoc value, Date JavaDoc expires) {
90     setCookie(name, value, expires, null, null, false);
91   }
92
93   /**
94    * Sets a cookie.
95    *
96    * @param name the cookie's name
97    * @param value the cookie's value
98    * @param expires when the cookie expires
99    * @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 JavaDoc name, String JavaDoc value, Date JavaDoc expires,
104       String JavaDoc domain, String JavaDoc path, boolean secure) {
105     setCookieImpl(name, value, expires.toGMTString(), domain, path, secure);
106   }
107
108   static native void loadCookies(HashMap JavaDoc m) /*-{
109     var docCookie = $doc.cookie;
110     if (docCookie && docCookie != '') {
111       var crumbs = docCookie.split('; ');
112       for (var i = 0; i < crumbs.length; ++i) {
113         var name, value;
114         var eqIdx = crumbs[i].indexOf('=');
115         if (eqIdx == -1) {
116           name = crumbs[i];
117           value = '';
118         } else {
119           name = crumbs[i].substring(0, eqIdx);
120           value = crumbs[i].substring(eqIdx + 1);
121         }
122         name = decodeURIComponent(name);
123         value = decodeURIComponent(value);
124         m.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)(name,value);
125       }
126     }
127   }-*/
;
128
129   private static HashMap JavaDoc ensureCookies() {
130     if (cachedCookies == null || needsRefresh()) {
131       cachedCookies = new HashMap JavaDoc();
132       loadCookies(cachedCookies);
133     }
134     return cachedCookies;
135   }
136
137   private static native boolean needsRefresh() /*-{
138     var docCookie = $doc.cookie;
139         
140     // Check to see if cached cookies need to be invalidated.
141     if (docCookie != '' && docCookie != @com.google.gwt.user.client.Cookies::rawCookies) {
142       @com.google.gwt.user.client.Cookies::rawCookies = docCookie;
143       return true;
144     } else {
145       return false;
146     }
147   }-*/
;
148
149   private static native void setCookieImpl(String JavaDoc name, String JavaDoc value,
150       String JavaDoc expires, String JavaDoc domain, String JavaDoc path, boolean secure) /*-{
151     var c = encodeURIComponent(name) + '=' + encodeURIComponent(value);
152     c += ';expires=' + expires;
153     if (domain)
154       c += ';domain=' + domain;
155     if (path)
156       c += ';path=' + path;
157     if (secure)
158       c += ';secure';
159
160     $doc.cookie = c;
161   }-*/
;
162
163   private Cookies() {
164   }
165 }
166
Popular Tags