View Javadoc

1   /*
2    * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.gwtwidgets.client.util;
17  
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  /***
24   * Class that is responsible for reading and writing cookies.
25   * 
26   * @author Brian Glick
27   * @author rhanson
28   */
29  public class CookieUtils {
30  
31      /***
32       * Creates, sets, and returns the Cookie object
33       * @param name
34       * @param value
35       * @param expires
36       * @param path
37       * @return the Cookie created or reset
38       */
39      public static Cookie write (String name, String value, Date expires, String path)
40      {
41          boolean useExp = false;
42          int year = -1, month = -1, day = -1;
43          if (expires != null) {
44              useExp = true;
45              year = expires.getYear() + 1900;
46              month = expires.getMonth();
47              day = expires.getDate();
48          }
49          writeNative(name, value, useExp, year, month, day, path);
50          return new Cookie(name, value);
51      }
52  
53      public static Cookie write (String name, String value, Date expires)
54      {
55          return write(name, value, expires, "/");
56      }
57  
58      
59      private static void writeNative(String name, String value, boolean useExpires, int year, int month, int day)
60      {
61          writeNative(name, value, useExpires, year, month, day, "/");
62      }
63  
64      private static native void writeNative(String name, String value, boolean useExpires, int year, int month, int day, String path) /*-{
65          var expStr = (useExpires) ? "; expires=" + new Date(year, month, day).toGMTString() : "";
66          $doc.cookie = name + "=" + escape(value) + expStr + "; path=" + path;
67      }-*/;
68      
69      /***
70       * Erases cookie with given name.
71       * @param name
72       */
73      public static void erase (String name)
74      {
75          writeNative(name, "", true, 1970, 1, 1);
76      }
77  
78      /***
79       * Gets the value of the cookie with the given name.
80       * @param name Name of cookie whose value should be retrieved
81       * @return Cookie value or null if cookie is not found
82       */
83      public static String readValue (String name)
84      {
85          Cookie c = read(name);
86          return c == null ? null : c.getValue();
87      }
88      
89      /***
90       * Gets the cookie with the given name.
91       * @param name Name of cookie to get.
92       * @return Cookie or null if the cookie is not found.
93       */
94      public static Cookie read (String name)
95      {
96          String val = getValue(name);
97          return (val == null) ? null : new Cookie(name, val);
98      }
99  
100     /***
101      * Tries to load the cookie, but only sets fields if they are found
102      * @param name
103      * @param c
104      */
105     private static native String getValue (String name) /*-{
106    	    var start = $doc.cookie.indexOf(name + '=');
107         var len = start + name.length + 1;
108         if ((!start) && (name != $doc.cookie.substring(0,name.length))) {
109             return null;
110         }
111         if (start == -1) {
112             return null;
113         }
114         var end = document.cookie.indexOf(';',len);
115         if (end == -1) {
116             end = $doc.cookie.length;
117         }
118         return unescape($doc.cookie.substring(len,end));
119     }-*/;
120     
121     /***
122      * Erase the given cookie
123      * @param c
124      */
125     public static void erase (Cookie c)
126     {
127         erase(c.getName());
128     }
129     
130     /***
131      * Write the given cookie
132      * @param c
133      */
134     public static void write (Cookie c, Date expires)
135     {
136         write(c.getName(), c.getValue(), expires);
137     }
138 
139     public static Cookie[] getAll ()
140     {
141         Map cookies = getCookieMap();
142         Cookie[] rVal = new Cookie[cookies.size()];
143         int counter = 0;
144         for (Iterator i = cookies.keySet().iterator(); i.hasNext(); counter++) {
145             String name = (String) i.next();
146             rVal[counter] = new Cookie(name, (String) cookies.get(name));
147         }
148         return rVal;
149     }
150 
151     private static Map getCookieMap ()
152     {
153         Map jar = new HashMap();
154         fillCookieJar(jar);
155         return jar;
156     }
157     
158     private static native void fillCookieJar (Map jar) /*-{
159         var cookies = $doc.cookie;
160         if (cookies && cookies != '') {
161             var cl = cookies.split('; ');
162             for (var i = 0; i < cl.length; ++i) {
163                 var parts = cl[i].split('=');
164                 jar.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)(parts[0], unescape(parts[1]));
165             }
166         }
167     }-*/;
168 
169 }